Android Open Source - helsinki-testbed2-android Animation Activity






From Project

Back to project page helsinki-testbed2-android.

License

The source code is released under:

GNU General Public License

If you think the Android project helsinki-testbed2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.testbed2.android.activity;
//  w  w  w. j a  v a  2  s . co  m
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import com.google.inject.Inject;
import com.googlecode.androidannotations.annotations.*;
import fi.testbed2.R;
import fi.testbed2.android.app.Logger;
import fi.testbed2.android.app.MainApplication;
import fi.testbed2.android.task.DownloadImagesTask;
import fi.testbed2.android.ui.view.AnimationView;
import fi.testbed2.android.ui.view.MapScaleInfo;
import fi.testbed2.domain.TestbedParsedPage;
import fi.testbed2.service.BitmapService;
import fi.testbed2.service.LocationService;
import fi.testbed2.service.PageService;
import fi.testbed2.util.SeekBarUtil;

import java.lang.reflect.Method;

/**
 * Activity handling the main map animation view.
 */
@EActivity(R.layout.animation)
@OptionsMenu(R.menu.animation_menu)
@RoboGuice
public class AnimationActivity extends AbstractActivity {

    @Inject
    LocationService locationService;

    @Inject
    BitmapService bitmapService;

    @Inject
    PageService pageService;

    @ViewById(R.id.animation_view)
    AnimationView animationView;

    @ViewById(R.id.playpause_button)
    ImageButton playPauseButton;

    @ViewById(R.id.timestamp_view)
    TextView timestampView;

    @ViewById(R.id.seek)
    SeekBar seekBar;

    boolean isPlaying = true;
    private int orientation;
    private boolean allImagesDownloaded;

    private DownloadImagesTask task;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!compulsoryDataIsAvailable()) {
            reloadAllAndReturnToMainActivity();
            return;
        }

        orientation = getResources().getConfiguration().orientation;

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (!compulsoryDataIsAvailable()) {
            reloadAllAndReturnToMainActivity();
            return;
        }

        if (settingsService.showUserLocation()) {
            locationService.startListeningLocationChanges();
        }

        updateSettingsToView();

        if (!allImagesDownloaded) {
            task = new DownloadImagesTask(this);
            task.execute();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (task!=null) {
            task.cancel();
        }
        pauseAnimation();
        saveMapBoundsAndScaleFactor();
        locationService.stopListeningLocationChanges();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindDrawables(findViewById(R.id.AnimationRootView));
    }

    @Override
    public void onRefreshFromMenuSelected() {
        pauseAnimation();
        allImagesDownloaded = false;
        Intent intent = new Intent();
        setResult(MainApplication.RESULT_REFRESH, intent);
        finish();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        saveMapBoundsAndScaleFactor();
        orientation = newConfig.orientation;
        updateSettingsToView();
    }

    /**
     * Updates the Downloading text in top left corner
     * @param text
     */
    @UiThread
    public void updateDownloadProgressInfo(final String text) {
        animationView.setDownloadProgressText(text);
        timestampView.invalidate();
    }

    public void onAllImagesDownloaded() {
        allImagesDownloaded = true;
        animationView.setAllImagesDownloaded(true);
        animationView.setDownloadProgressText(null);
        animationView.initView(getApplicationContext());
        animationView.getPlayer().previous();
        updatePlayingState(false);
        if (settingsService.isStartAnimationAutomatically()) {
            playAnimation();
        }
        showHardwareAccelerationWarningIfNeeded();
    }

    @AfterViews
    protected void initView() {

        if (!compulsoryDataIsAvailable()) {
            reloadAllAndReturnToMainActivity();
            return;
        }

        animationView.setAllImagesDownloaded(false);
        animationView.setMunicipalities(settingsService.getSavedMunicipalities());
        animationView.initView(getApplicationContext());
        initAnimation();
    }

    private void initAnimation() {
        animationView.post(new Runnable() {
            @Override
            public void run() {
                final Rect bounds = settingsService.getSavedMapBounds(orientation);
                final MapScaleInfo scaleInfo = settingsService.getSavedScaleInfo(orientation);
                updatePlayingState(true);
                animationView.startAnimation(timestampView, seekBar, bounds, scaleInfo);
            }
        });
    }

    private void saveMapBoundsAndScaleFactor() {
        settingsService.saveMapBoundsAndScaleFactor(animationView.getBoundsUtil().getBounds(),
                animationView.getScaleInfo(), orientation);
    }

    private void updateSettingsToView() {
        animationView.setScaleInfo(settingsService.getSavedScaleInfo(orientation));
        animationView.updateBounds(settingsService.getSavedMapBounds(orientation));
        animationView.setMunicipalities(settingsService.getSavedMunicipalities());
        animationView.getPlayer().setFrameDelay(settingsService.getSavedFrameDelay());
        animationView.getCanvasUtil().resetMarkerAndPointImageCache();
    }

    /**
     * Check that we have all compulsory data before starting the activity.
     * Android system might kill the app after onPause() and when resuming the
     * app after it has been killed, all static data is null.
     *
     * @return
     */
    private boolean compulsoryDataIsAvailable() {

        TestbedParsedPage page = pageService.getTestbedParsedPage();

        /*
        * Parsed page should always be non-null.
        */
        if (page==null) {
            return false;
        }

        boolean noImagesExist = page.getAllTestbedImages()==null || page.getAllTestbedImages().isEmpty();
        boolean allImagesDownloadedButSomeAreMissing = allImagesDownloaded && pageService.getNotDownloadedImagesCount()>0;

        if (noImagesExist || allImagesDownloadedButSomeAreMissing) {
            return false;
        }

        return true;

    }

    private void reloadAllAndReturnToMainActivity() {
        allImagesDownloaded = false;
        pageService.evictPage();
        bitmapService.evictAll();
        System.gc();
        Intent intent = new Intent();
        setResult(MainApplication.RESULT_OK, intent);
        finish();
    }

    private void showHardwareAccelerationWarningIfNeeded() {

        try {
            // isHardwareAccelerated exists in API level >=11
            Method m = View.class.getMethod("isHardwareAccelerated");
            boolean isHardwareAccelerated = (Boolean)m.invoke(this.findViewById(android.R.id.content));

            Logger.debug("Hardware acceleration status: " + isHardwareAccelerated);

            if(settingsService.isShowHardwareAccelerationDialog() &&
                    isHardwareAccelerated) {
                dialogBuilder.getHardwareAccelerationDialog().show();
            }

        } catch (Exception e) {
            // Ignore
            Logger.debug("OK. No hardware acceleration because API level < 11");
        }

    }

    private void playAnimation() {
        updatePlayingState(true);
        animationView.getPlayer().play();
    }

    private void pauseAnimation() {
        updatePlayingState(false);
        animationView.getPlayer().pause();
    }


    @Click(R.id.playpause_button)
  public void onPlayPauseButtonClick() {

        if (!allImagesDownloaded) {
            return;
        }

        animationView.getPlayer().playOrPause();
        updatePlayingState(!isPlaying);

  }

    private void updatePlayingState(boolean animationIsPlaying) {

        isPlaying = animationIsPlaying;

        if (isPlaying) {
            playPauseButton.setImageResource(R.drawable.ic_media_pause);
        } else {
            playPauseButton.setImageResource(R.drawable.ic_media_play);
        }

    }

    @OptionsItem(R.id.main_menu_reset_zoom)
    public void onResetZoomSelected() {
        animationView.setScaleInfo(new MapScaleInfo());
        animationView.updateBounds(null);
        animationView.invalidate();
    }

    @SeekBarProgressChange(R.id.seek)
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            pauseAnimation();
            int totalFrames = pageService.getTestbedParsedPage().getAllTestbedImages().size();
            animationView.getPlayer().goToFrame(SeekBarUtil.
                    getFrameIndexFromSeekBarValue(progress, totalFrames));
        }
    }

    @SeekBarTouchStart(R.id.seek)
    public void onStartTrackingTouch(SeekBar seekBar) {
        pauseAnimation();
    }

    @SeekBarTouchStop(R.id.seek)
    public void onStopTrackingTouch(SeekBar seekBar) {
        // Don't do anything
    }

}




Java Source Code List

com.larvalabs.svgandroid.ParserHelper.java
com.larvalabs.svgandroid.SVGParseException.java
com.larvalabs.svgandroid.SVGParser.java
com.larvalabs.svgandroid.SVG.java
com.robobunny.SeekBarPreference.java
com.threefiftynice.android.preference.ListPreferenceMultiSelect.java
fi.testbed2.MainModule.java
fi.testbed2.android.activity.AbstractActivity.java
fi.testbed2.android.activity.AnimationActivity.java
fi.testbed2.android.activity.MainActivity.java
fi.testbed2.android.activity.ParsingActivity.java
fi.testbed2.android.activity.TestbedPreferenceActivity.java
fi.testbed2.android.app.Logger.java
fi.testbed2.android.app.MainApplication.java
fi.testbed2.android.task.AbstractTask.java
fi.testbed2.android.task.DownloadImagesTask.java
fi.testbed2.android.task.ParseAndInitTask.java
fi.testbed2.android.task.Task.java
fi.testbed2.android.task.exception.DownloadTaskException.java
fi.testbed2.android.task.exception.TaskCancelledException.java
fi.testbed2.android.ui.dialog.AlertDialogBuilder.java
fi.testbed2.android.ui.dialog.DialogBuilder.java
fi.testbed2.android.ui.svg.LocationMarkerSVG.java
fi.testbed2.android.ui.svg.MunicipalityMarkerSVG.java
fi.testbed2.android.ui.view.AnimationViewPlayer.java
fi.testbed2.android.ui.view.AnimationView.java
fi.testbed2.android.ui.view.MapScaleInfo.java
fi.testbed2.android.ui.view.util.AnimationViewBoundsUtil.java
fi.testbed2.android.ui.view.util.AnimationViewCanvasUtil.java
fi.testbed2.android.ui.view.util.AnimationViewScaleAndGestureUtil.java
fi.testbed2.domain.MapLocationGPS.java
fi.testbed2.domain.MapLocationXY.java
fi.testbed2.domain.Municipality.java
fi.testbed2.domain.TestbedMapImage.java
fi.testbed2.domain.TestbedParsedPage.java
fi.testbed2.robotium.MainActivityRobotiumTest.java
fi.testbed2.service.BitmapService.java
fi.testbed2.service.CoordinateService.java
fi.testbed2.service.HttpUrlService.java
fi.testbed2.service.LocationService.java
fi.testbed2.service.MunicipalityService.java
fi.testbed2.service.PageService.java
fi.testbed2.service.SettingsService.java
fi.testbed2.service.impl.ApacheHttpUrlService.java
fi.testbed2.service.impl.InlineMunicipalityService.java
fi.testbed2.service.impl.LruCacheBitmapService.java
fi.testbed2.service.impl.LruCachePageService.java
fi.testbed2.service.impl.MercatorCoordinateService.java
fi.testbed2.service.impl.PreferenceBasedLocationService.java
fi.testbed2.service.impl.SharedPreferenceSettingsService.java
fi.testbed2.util.ColorUtil.java
fi.testbed2.util.MathUtil.java
fi.testbed2.util.SeekBarUtil.java
fi.testbed2.util.TimeUtil.java
net.margaritov.preference.colorpicker.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.ColorPickerDialog.java
net.margaritov.preference.colorpicker.ColorPickerPanelView.java
net.margaritov.preference.colorpicker.ColorPickerPreference.java
net.margaritov.preference.colorpicker.ColorPickerView.java