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