コード例 #1
0
  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.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.getName(), journeyQuery.destination.getName()));
        }
      } while (cursor.moveToNext());
    }

    cursor.close();
    favoritesDbAdapter.close();
  }
コード例 #2
0
  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.apply();
        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) {
      journeyCursor.close();
      // Also mark it as migrated to avoid sending an intent.
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("converted_favorites", true);
      editor.apply();

      Log.d(TAG, "Existing journeys, treat as converted.");
      return true;
    }
    journeyCursor.close();

    return false;
  }