public static void loadFavoritesFromDb() {
   FavoritesData.clear();
   String where = " launchTimes > 0 ";
   final Cursor c =
       mFavoritesDbOperation.query(
           FavoritesDatabaseOperation.TABLE_FAVORITE, null, where, null, null);
   try {
     final int idIndex = c.getColumnIndexOrThrow(FavoritesDatabaseOperation.Favorites._ID);
     final int packageIndex =
         c.getColumnIndexOrThrow(FavoritesDatabaseOperation.Favorites.PACKAGE_NAME);
     final int launchTimesIndex =
         c.getColumnIndexOrThrow(FavoritesDatabaseOperation.Favorites.LAUNCH_TIMES);
     FavoritesAppInfo appInfo;
     String packageDescription;
     long id;
     while (c.moveToNext()) {
       try {
         id = c.getLong(idIndex);
         packageDescription = c.getString(packageIndex);
         appInfo = FavoritesData.getAppInfo(packageDescription);
         if (appInfo != null) {
           appInfo.id = id;
           appInfo.launchTimes = c.getLong(launchTimesIndex);
           FavoritesData.add(appInfo);
         }
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   } finally {
     if (c != null) {
       c.close();
     }
   }
 }
 private FavoritesAppInfo removeApp(final String[] packages, int i) {
   FavoritesAppInfo appInfo = FavoritesData.getAppInfo(packages[i]);
   if (appInfo != null) {
     FavoritesData.mAppsAll.remove(appInfo);
   }
   appInfo = FavoritesData.getDatasApp(packages[i]);
   if (appInfo != null) {
     FavoritesData.datas.remove(appInfo);
   }
   deleteItemFromDatabase(mContext, appInfo);
   return appInfo;
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
       || Intent.ACTION_PACKAGE_REMOVED.equals(action)
       || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
     Log.i(TAG, "action: " + action);
     final String packageName = intent.getData().getSchemeSpecificPart();
     final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
     int op = PackageUpdatedTask.OP_NONE;
     if (packageName == null || packageName.length() == 0) {
       // they sent us a bad intent
       return;
     }
     if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
       op = PackageUpdatedTask.OP_UPDATE;
     } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
       if (!replacing) {
         op = PackageUpdatedTask.OP_REMOVE;
       }
     } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
       if (!replacing) {
         op = PackageUpdatedTask.OP_ADD;
       } else {
         op = PackageUpdatedTask.OP_UPDATE;
       }
     }
     mHandler.post(new PackageUpdatedTask(op, new String[] {packageName}));
   } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
     Log.i(TAG, "ACTION_EXTERNAL_APPLICATIONS_AVAILABLE");
     String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
     int op = PackageUpdatedTask.OP_ADD;
     mHandler.post(new PackageUpdatedTask(op, packages));
   } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
     Log.i(TAG, "ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE");
     String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
     int op = PackageUpdatedTask.OP_REMOVE;
     mHandler.post(new PackageUpdatedTask(op, packages));
   } else if (Intent.ACTION_TIME_TICK.equals(action)
       || Intent.ACTION_TIME_CHANGED.equals(action)
       || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
     timeChanged();
   } else if (FavoritesModel.ACTION_FAVOTITE_UPDATE.equals(action)) {
     Log.i(TAG, "ACTION_FAVOTITE_UPDATE");
     FavoritesData.saveFavoritesToDatabase(mContext);
     FavoritesData.sort();
   }
 }
 private void updateApp(final String[] packages, int i) {
   ApplicationInfo applicationInfo;
   try {
     applicationInfo = mPackageManager.getApplicationInfo(packages[i], 0);
     FavoritesAppInfo appInfo = FavoritesData.getAppInfo(packages[i]);
     if (appInfo != null) {
       appInfo.appName = applicationInfo.loadLabel(mPackageManager).toString();
       appInfo.appIntent =
           mPackageManager.getLaunchIntentForPackage(applicationInfo.packageName);
       appInfo.appIcon = applicationInfo.loadIcon(mPackageManager);
     }
   } catch (NameNotFoundException e) {
     e.printStackTrace();
   }
 }
 private void timeChanged() {
   boolean isChanged = false;
   Calendar calendar = Calendar.getInstance();
   int year = calendar.get(Calendar.YEAR);
   int month = calendar.get(Calendar.MONTH);
   int day = calendar.get(Calendar.DAY_OF_MONTH);
   if (mYear != year || mMonth != month || mDay != day) {
     mYear = year;
     mMonth = month;
     mDay = day;
     isChanged = true;
     FavoritesService.isDataChanged = true;
   }
   if (isChanged) {
     FavoritesData.dayDecrease();
   }
 }