/**
  * Extract the subway station IDs from the favorite subway station list
  *
  * @param subwayStationFavList the favorite subway station list
  * @return the subway station IDs string
  */
 public static String extractSubwayStationIDsFromFavList(List<Fav> subwayStationFavList) {
   StringBuilder sb = new StringBuilder();
   for (Fav subwayStationpFav : subwayStationFavList) {
     if (sb.length() > 0) {
       sb.append("+");
     }
     sb.append(subwayStationpFav.getFkId());
   }
   return sb.toString();
 }
 /**
  * Update favorites bus lines to match January 2012 bus lines number changes.
  *
  * @param contentResolver the content resolver
  */
 @SuppressWarnings("deprecation")
 public static void updateFavoritesJan2012(ContentResolver contentResolver) {
   MyLog.v(TAG, "updateFavoritesJan2012()");
   try {
     Map<String, String> update = new HashMap<String, String>();
     update.put("77", "444");
     update.put("120", "495");
     update.put("132", "136");
     update.put("143", "440");
     update.put("148", "448");
     update.put("159", "469");
     // update.put("167", "777"); no bus stops
     // update.put("167", "767"); no bus stops
     // update.put("169", "769"); no bus stops
     update.put("173", "496");
     update.put("182", "486");
     update.put("184", "487");
     update.put("190", "491");
     update.put("194", "449");
     update.put("199", "432");
     update.put("210", "419");
     update.put("214", "409");
     update.put("221", "411");
     update.put("251", "212");
     update.put("261", "401");
     update.put("265", "407");
     update.put("268", "468");
     update.put("480", "178");
     update.put("505", "439");
     update.put("506", "406");
     update.put("515", "715");
     update.put("535", "435");
     List<Fav> busStopFavs =
         DataManager.findFavsByTypeList(contentResolver, Fav.KEY_TYPE_VALUE_BUS_STOP);
     for (Fav busStopFav : busStopFavs) {
       String busStopLineNumber = busStopFav.getFkId2();
       // IF the bus stop line number need to be updated DO
       if (update.keySet().contains(busStopLineNumber)) {
         // delete the old favorite
         DataManager.deleteFav(contentResolver, busStopFav.getId());
         // update the bus line number
         busStopFav.setFkId2(update.get(busStopLineNumber));
         // add the new favorite
         DataManager.addFav(contentResolver, busStopFav);
       }
     }
   } catch (Exception e) {
     MyLog.w(TAG, e, "Unknow error while updating favorites.");
   }
 }
 /**
  * Extract bike stations terminal name from favorite list.
  *
  * @param bikeStationFavList the bike stations favorite list
  * @return the bike stations terminal name
  */
 public static String extractBikeStationTerminNamesFromFavList(List<Fav> bikeStationFavList) {
   StringBuilder sb = new StringBuilder();
   for (Fav stationFav : bikeStationFavList) {
     if (stationFav.getFkId()
         == null) { // need check because of previous issue corrupting favorites
       continue;
     }
     if (sb.length() > 0) {
       sb.append("+");
     }
     sb.append(stationFav.getFkId());
   }
   return sb.toString();
 }
 /**
  * Clean favorites linking to old non-existing bus stops or subway stations. Use after STM DB
  * updates.
  *
  * @param contentResolver the content resolver
  */
 @SuppressWarnings("deprecation")
 public static void cleanFavorites(Context context) {
   MyLog.v(TAG, "cleanFavorites()");
   final ContentResolver contentResolver = context.getContentResolver();
   try {
     // bus stops
     List<Fav> busStopFavs =
         DataManager.findFavsByTypeList(contentResolver, Fav.KEY_TYPE_VALUE_BUS_STOP);
     List<RouteTripStop> routeTripStops =
         StmBusManager.findRouteTripStops(context, busStopFavs, false);
     for (Fav busStopFav : busStopFavs) {
       boolean stillInTheDB = false;
       for (RouteTripStop routeTripStop : routeTripStops) {
         if (busStopFav.getFkId().equals(routeTripStop.stop.id)
             && busStopFav.getFkId2().equals(routeTripStop.route.id)) {
           stillInTheDB = true;
         }
       }
       if (!stillInTheDB) {
         DataManager.deleteFav(contentResolver, busStopFav.getId());
       }
     }
     // subway stations
     List<Fav> subwayFavs =
         DataManager.findFavsByTypeList(contentResolver, Fav.KEY_TYPE_VALUE_SUBWAY_STATION);
     for (Fav subwayFav : subwayFavs) {
       Stop stop = StmSubwayManager.findStopWithId(context, Integer.valueOf(subwayFav.getFkId()));
       if (stop == null) {
         DataManager.deleteFav(contentResolver, subwayFav.getId());
       }
     }
   } catch (Exception e) {
     MyLog.w(TAG, e, "Unknow error while cleaning favorite.");
   }
 }
 /**
  * Set the DEMO mode with favorites and other settings...
  *
  * @param context the context
  */
 public static void setDemoMode(Context context) {
   // set favorites
   Fav newFav = new Fav();
   newFav.setType(Fav.KEY_TYPE_VALUE_AUTHORITY_ROUTE_STOP);
   newFav.setFkId(RouteStop.getUID(StmBusManager.AUTHORITY, 54321, 10));
   DataManager.addFav(context.getContentResolver(), newFav);
   newFav.setFkId(RouteStop.getUID(StmBusManager.AUTHORITY, 52509, 24));
   DataManager.addFav(context.getContentResolver(), newFav);
   newFav.setFkId(RouteStop.getUID(StmBusManager.AUTHORITY, 55140, 48));
   DataManager.addFav(context.getContentResolver(), newFav);
   newFav.setFkId(RouteStop.getUID(StmBusManager.AUTHORITY, 11, 1)); // Berri-UQAM - Verte
   DataManager.addFav(context.getContentResolver(), newFav);
   newFav.setFkId(RouteStop.getUID(StmBusManager.AUTHORITY, 9, 2)); // Mont-Royal - Orange
   DataManager.addFav(context.getContentResolver(), newFav);
   newFav.setType(Fav.KEY_TYPE_VALUE_BIKE_STATIONS);
   newFav.setFkId("6415"); // Wilson / Sherbrooke
   DataManager.addFav(context.getContentResolver(), newFav);
   SupportFactory.get().backupManagerDataChanged(context);
 }
 /** Update favorites bus stops into route stops. */
 @SuppressWarnings("deprecation")
 public static void updateBusStopsToRouteStops(Context context) {
   MyLog.v(TAG, "updateBusStopsToRouteStops()");
   final ContentResolver contentResolver = context.getContentResolver();
   List<Fav> busStopFavs =
       DataManager.findFavsByTypeList(contentResolver, Fav.KEY_TYPE_VALUE_BUS_STOP);
   MyLog.d(
       TAG,
       "Favorite bus stops to upgrade: %s",
       (busStopFavs == null ? null : busStopFavs.size()));
   if (busStopFavs != null) {
     for (Fav busStopFav : busStopFavs) {
       try {
         final int stopId = Integer.valueOf(busStopFav.getFkId());
         final int routeId = Integer.valueOf(busStopFav.getFkId2());
         final String uid = RouteStop.getUID(StmBusManager.AUTHORITY, stopId, routeId);
         Fav newRouteStopFav = new Fav();
         newRouteStopFav.setType(Fav.KEY_TYPE_VALUE_AUTHORITY_ROUTE_STOP);
         newRouteStopFav.setFkId(uid);
         final boolean alreadyFavorite =
             DataManager.findFav(contentResolver, Fav.KEY_TYPE_VALUE_AUTHORITY_ROUTE_STOP, uid)
                 != null;
         if (alreadyFavorite) {
           MyLog.d(TAG, "Favorite bus stop %s already migrated.", busStopFav);
         } else {
           final boolean added = DataManager.addFav(contentResolver, newRouteStopFav) != null;
           if (!added) {
             MyLog.d(TAG, "Favorite bus stop %s not converted to route stop!", busStopFav);
             continue; // don't remove not migrated
           }
         }
         final boolean deleted = DataManager.deleteFav(contentResolver, busStopFav.getId());
         if (!deleted) {
           MyLog.d(TAG, "Old favorite bus stop %s migrated but not deleted!", busStopFav);
         }
       } catch (Throwable t) {
         MyLog.w(TAG, t, "Error while migrating favorite bus stop %s to route stop!", busStopFav);
       }
     }
   }
   MyLog.d(TAG, "updateBusStopsToRouteStops() > DONE");
 }
 /** Update favorites subway stations into route stops. */
 @SuppressWarnings("deprecation")
 public static void updateSubwayStationsToRouteStops(Context context) {
   MyLog.v(TAG, "updateSubwayStationsToRouteStops()");
   final ContentResolver contentResolver = context.getContentResolver();
   List<Fav> subwayStationFavs =
       DataManager.findFavsByTypeList(contentResolver, Fav.KEY_TYPE_VALUE_SUBWAY_STATION);
   MyLog.d(
       TAG,
       "Favorite subway stations to upgrade: %s",
       (subwayStationFavs == null ? null : subwayStationFavs.size()));
   if (subwayStationFavs != null) {
     for (Fav subwayStationFav : subwayStationFavs) {
       try {
         boolean allStopRoutesMigrated = true;
         final int stopId = Integer.valueOf(subwayStationFav.getFkId());
         List<Route> stopRoutes = StmSubwayManager.findRoutesWithStopIdList(context, stopId);
         if (stopRoutes == null || stopRoutes.size() == 0) {
           MyLog.d(TAG, "Favorite subway station %s route(s) not found!", subwayStationFav);
           allStopRoutesMigrated = false; // no stop routes!
         }
         if (stopRoutes != null) {
           for (Route stopRoute : stopRoutes) {
             final int routeId = stopRoute.id;
             final String uid = RouteStop.getUID(StmSubwayManager.AUTHORITY, stopId, routeId);
             Fav newRouteStopFav = new Fav();
             newRouteStopFav.setType(Fav.KEY_TYPE_VALUE_AUTHORITY_ROUTE_STOP);
             newRouteStopFav.setFkId(uid);
             final boolean alreadyFavorite =
                 DataManager.findFav(contentResolver, Fav.KEY_TYPE_VALUE_AUTHORITY_ROUTE_STOP, uid)
                     != null;
             if (alreadyFavorite) {
               MyLog.d(TAG, "Favorite subway station %s already migrated.", newRouteStopFav);
             } else {
               final boolean added = DataManager.addFav(contentResolver, newRouteStopFav) != null;
               if (!added) {
                 MyLog.d(
                     TAG,
                     "Favorite subway station %s not converted to route stop!",
                     subwayStationFav);
                 allStopRoutesMigrated = false;
               }
             }
           }
         }
         if (!allStopRoutesMigrated) {
           MyLog.d(
               TAG, "Favorite subway station %s not converted to route stop!", subwayStationFav);
           continue; // don't remove not migrated
         }
         final boolean deleted = DataManager.deleteFav(contentResolver, subwayStationFav.getId());
         if (!deleted) {
           MyLog.d(
               TAG, "Old favorite subway station %s migrated but not deleted!", subwayStationFav);
         }
       } catch (Throwable t) {
         MyLog.w(
             TAG,
             t,
             "Error while migrating favorite subway station %s to route stop!",
             subwayStationFav);
       }
     }
   }
   MyLog.d(TAG, "updateSubwayStationsToRouteStops() > DONE");
 }