eu.pinnoo.garbagecalendar.ui.AbstractSherlockActivity.java Source code

Java tutorial

Introduction

Here is the source code for eu.pinnoo.garbagecalendar.ui.AbstractSherlockActivity.java

Source

/* 
 * Copyright 2014 Wouter Pinnoo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.pinnoo.garbagecalendar.ui;

import android.content.ComponentName;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import eu.pinnoo.garbagecalendar.data.LocalConstants;
import eu.pinnoo.garbagecalendar.data.caches.AddressCache;
import eu.pinnoo.garbagecalendar.data.caches.CollectionCache;
import eu.pinnoo.garbagecalendar.data.caches.UserAddressCache;

/**
 *
 * @author Wouter Pinnoo <pinnoo.wouter@gmail.com>
 */
public abstract class AbstractSherlockActivity extends SherlockActivity {

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

        if (LocalConstants.DEBUG) {
            GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(getApplicationContext());
            googleAnalytics.setAppOptOut(true);
        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        EasyTracker.getInstance().activityStart(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EasyTracker.getInstance().activityStop(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void clearCachedIfRequired() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int old = prefs.getInt(LocalConstants.CacheName.VERSION.toString(), 0);
        int current = getVersionCode();
        if (old < current) {
            clearCaches();
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt(LocalConstants.CacheName.VERSION.toString(), current);
            editor.commit();
        }
    }

    public void clearCaches() {
        try {
            AddressCache.getInstance().clear();
            CollectionCache.getInstance().clear();
            UserAddressCache.getInstance().clear();
        } catch (NullPointerException ex) {
        }
    }

    protected int getVersionCode() {
        try {
            ComponentName componentName = new ComponentName(this, AbstractSherlockActivity.class);
            PackageInfo info = getPackageManager().getPackageInfo(componentName.getPackageName(), 0);
            return info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            return 0;
        }
    }
}