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