Android Open Source - sthlmtraveling Data Migration Service






From Project

Back to project page sthlmtraveling.

License

The source code is released under:

Apache License

If you think the Android project sthlmtraveling 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 com.markupartist.sthlmtraveling.service;
/* w  ww  . j  a  v  a 2s. co  m*/
import java.util.ArrayList;

import org.json.JSONException;

import com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter;
import com.markupartist.sthlmtraveling.provider.TransportMode;
import com.markupartist.sthlmtraveling.provider.JourneysProvider.Journey.Journeys;
import com.markupartist.sthlmtraveling.provider.planner.JourneyQuery;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

public class DataMigrationService extends WakefulIntentService {

    private static final String TAG = "DataMigrationService";

    public DataMigrationService() {
        super("data_migration");
    }

    @Override
    void doWakefulWork(Intent intent) {
        Context context = getApplicationContext();

        maybeConvertFavorites(context);

        Log.d(TAG, "Update complete...");
        Intent updateUi = new Intent("sthlmtraveling.intent.action.UPDATE_UI");
        updateUi.putExtra("sthlmtraveling.intent.extra.FAVORITES_UPDATED", true);
        sendBroadcast(updateUi);
    }

    private void maybeConvertFavorites(Context context) {
        if (!isFavoritesMigrated(context)) {
            try {
                convertFavorites(context);
            } catch (Exception e) {
                Log.w(TAG, "Failed to convert favorites. Mark as converted and move on.");
            }
            SharedPreferences settings = context.getSharedPreferences("sthlmtraveling", MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("converted_favorites", true);
            editor.commit();
        }        
    }
    
    private void convertFavorites(Context context) {
        Log.d(TAG, "About to convert favorites...");

        FavoritesDbAdapter favoritesDbAdapter = new FavoritesDbAdapter(this);
        favoritesDbAdapter.open();

        Cursor cursor = favoritesDbAdapter.fetch();

        if (cursor.moveToFirst()) {
            ArrayList<String> transportModes = new ArrayList<String>();
            transportModes.add(TransportMode.BUS);
            transportModes.add(TransportMode.FLY);
            transportModes.add(TransportMode.METRO);
            transportModes.add(TransportMode.TRAIN);
            transportModes.add(TransportMode.TRAM);
            transportModes.add(TransportMode.WAX);
            do {
                JourneyQuery journeyQuery = new JourneyQuery.Builder()
                    .transportModes(transportModes)
                    .origin(
                            cursor.getString(FavoritesDbAdapter.INDEX_START_POINT),
                            cursor.getInt(FavoritesDbAdapter.INDEX_START_POINT_LATITUDE),
                            cursor.getInt(FavoritesDbAdapter.INDEX_START_POINT_LONGITUDE))
                    .destination(
                            cursor.getString(FavoritesDbAdapter.INDEX_END_POINT),
                            cursor.getInt(FavoritesDbAdapter.INDEX_END_POINT_LATITUDE),
                            cursor.getInt(FavoritesDbAdapter.INDEX_END_POINT_LONGITUDE))
                    .create();

                // Store new journey
                String json = null;
                try {
                    json = journeyQuery.toJson(false).toString();
                } catch (JSONException e) {
                    Log.e(TAG, "Failed to convert journey to a json document.");
                }

                // Malformed json, skip it.
                if (json != null) {
                    ContentValues values = new ContentValues();
                    values.put(Journeys.JOURNEY_DATA, json);
                    values.put(Journeys.STARRED, "1");
                    values.put(Journeys.CREATED_AT,
                            cursor.getString(FavoritesDbAdapter.INDEX_CREATED));
                    getContentResolver().insert(Journeys.CONTENT_URI, values);
    
                    Log.d(TAG, String.format("Converted favorite journey %s -> %s.",
                            journeyQuery.origin.name, journeyQuery.destination.name));
                }
            } while (cursor.moveToNext());
        }

        cursor.close();
        favoritesDbAdapter.close();
    }

    private static boolean isFavoritesMigrated(Context context) {
        SharedPreferences settings = context.getSharedPreferences("sthlmtraveling", MODE_PRIVATE);
        boolean migrated =
            settings.getBoolean("converted_favorites", false);
        if (migrated) {
            Log.d(TAG, "Favorites converted.");
            return true;
        }

        FavoritesDbAdapter favoritesDbAdapter = null;
        try {
            favoritesDbAdapter = new FavoritesDbAdapter(context);
            favoritesDbAdapter.open();
            Cursor favoritesCursor = favoritesDbAdapter.fetch();
            if (favoritesCursor.getCount() == 0) {
                // Also mark it as migrated to avoid sending an intent.
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("converted_favorites", true);
                editor.commit();
                migrated = true;
            }
        } finally {
            if (favoritesDbAdapter != null) {
                favoritesDbAdapter.close();
            }
        }
        if (migrated) {
            Log.d(TAG, "No previous favorites, treat as converted.");
            return true;
        }

        String[] projection = new String[] {
                Journeys._ID, // 0
            };
        ContentResolver resolver = context.getContentResolver();
        Cursor journeyCursor = resolver.query(Journeys.CONTENT_URI, projection, null, null, null);
        if (journeyCursor.getCount() > 0) {
            // Also mark it as migrated to avoid sending an intent.
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("converted_favorites", true);
            editor.commit();
            
            Log.d(TAG, "Existing journeys, treat as converted.");
            return true;
        }

        return false;
    }

    public static boolean hasMigrations(Context context) {
        boolean hasMigrations = false;
        try {
            hasMigrations = !isFavoritesMigrated(context);
        } catch (Exception e) {
            Log.w(TAG, "Failed to determine if we had any migrations. Skip and move on.");
        }
        return hasMigrations;
    }

    public static void startService(Context context) {
        if (hasMigrations(context)) {
            WakefulIntentService.acquireStaticLock(context);
            context.startService(new Intent(context, DataMigrationService.class));
        }
    }
}




Java Source Code List

com.markupartist.sthlmtraveling.AboutActivity.java
com.markupartist.sthlmtraveling.AllTests.java
com.markupartist.sthlmtraveling.AppConfig.java
com.markupartist.sthlmtraveling.AutoCompleteStopAdapter.java
com.markupartist.sthlmtraveling.BaseActivity.java
com.markupartist.sthlmtraveling.BaseFragmentActivity.java
com.markupartist.sthlmtraveling.BaseFragment.java
com.markupartist.sthlmtraveling.BaseListActivity.java
com.markupartist.sthlmtraveling.BaseListFragmentActivity.java
com.markupartist.sthlmtraveling.BaseListFragment.java
com.markupartist.sthlmtraveling.BasePreferenceActivity.java
com.markupartist.sthlmtraveling.ChangeRouteTimeActivity.java
com.markupartist.sthlmtraveling.DepartureAdapter.java
com.markupartist.sthlmtraveling.DeparturesActivity.java
com.markupartist.sthlmtraveling.DeviationDetailActivity.java
com.markupartist.sthlmtraveling.DeviationsActivity.java
com.markupartist.sthlmtraveling.DialogHelper.java
com.markupartist.sthlmtraveling.FavoritesFragment.java
com.markupartist.sthlmtraveling.MultipleListAdapter.java
com.markupartist.sthlmtraveling.MyApplication.java
com.markupartist.sthlmtraveling.MyLocationManager.java
com.markupartist.sthlmtraveling.NearbyActivity.java
com.markupartist.sthlmtraveling.PlannerFragmentActivity.java
com.markupartist.sthlmtraveling.PlannerFragment.java
com.markupartist.sthlmtraveling.PointOnMapActivity.java
com.markupartist.sthlmtraveling.RouteDetailActivity.java
com.markupartist.sthlmtraveling.RouteParserTest.java
com.markupartist.sthlmtraveling.RoutesActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragmentActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragment.java
com.markupartist.sthlmtraveling.SectionedAdapter.java
com.markupartist.sthlmtraveling.SettingsActivity.java
com.markupartist.sthlmtraveling.StartActivity.java
com.markupartist.sthlmtraveling.TrafficStatusFragment.java
com.markupartist.sthlmtraveling.ViewOnMapActivity.java
com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter.java
com.markupartist.sthlmtraveling.provider.HistoryDbAdapter.java
com.markupartist.sthlmtraveling.provider.JourneysProvider.java
com.markupartist.sthlmtraveling.provider.PlacesProvider.java
com.markupartist.sthlmtraveling.provider.TransportMode.java
com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationStore.java
com.markupartist.sthlmtraveling.provider.deviation.Deviation.java
com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java
com.markupartist.sthlmtraveling.provider.planner.Planner.java
com.markupartist.sthlmtraveling.provider.site.Site.java
com.markupartist.sthlmtraveling.provider.site.SitesStore.java
com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver.java
com.markupartist.sthlmtraveling.receivers.OnBootReceiver.java
com.markupartist.sthlmtraveling.service.DataMigrationService.java
com.markupartist.sthlmtraveling.service.DeviationService.java
com.markupartist.sthlmtraveling.service.WakefulIntentService.java
com.markupartist.sthlmtraveling.ui.view.DelayAutoCompleteTextView.java
com.markupartist.sthlmtraveling.ui.view.LineSegment.java
com.markupartist.sthlmtraveling.ui.view.SmsTicketDialog.java
com.markupartist.sthlmtraveling.ui.view.TripView.java
com.markupartist.sthlmtraveling.utils.Analytics.java
com.markupartist.sthlmtraveling.utils.BarcodeScannerIntegrator.java
com.markupartist.sthlmtraveling.utils.DateTimeUtil.java
com.markupartist.sthlmtraveling.utils.DisplayMetricsHelper.java
com.markupartist.sthlmtraveling.utils.ErrorReporter.java
com.markupartist.sthlmtraveling.utils.HttpHelper.java
com.markupartist.sthlmtraveling.utils.IntentUtil.java
com.markupartist.sthlmtraveling.utils.LocationUtils.java
com.markupartist.sthlmtraveling.utils.StreamUtils.java
com.markupartist.sthlmtraveling.utils.StringUtils.java
com.markupartist.sthlmtraveling.utils.ViewHelper.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java