/**
  * 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.");
   }
 }
 /**
  * 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();
 }
 /**
  * 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 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");
 }