/**
  * onWakefulHandleIntent called by the WakefullIntent service any time service is called
  *
  * @param intext - the intent we need to handle
  * @return boolean
  */
 @Override
 protected boolean onWakefulHandleIntent(Intent intent) {
   LogUtils.i(
       LogUtils.TAG, "CPMaintenanceService - onWakefulHandleIntent for %s", intent.getAction());
   boolean success = false;
   int action = getAction(intent.getAction());
   switch (action) {
     case DB_MAINT_ACTION:
       break;
     case DB_UPGRADE_ACTION:
       success = doDbUpgrade(intent);
       break;
     case DB_LOCALE_ACTION:
       break;
     case START_ACTION:
       success = doStartupAction(readActionState());
       break;
   }
   if (success) {
     // Overwrite the preference object to indicate we are not in the middle of a task
     writeActionState(UNKNOWN_ACTION, null);
   }
   LogUtils.i(LogUtils.TAG, "Current Maintenance state value is %d", readActionState());
   return success;
 }
  /**
   * doDbUpgrade is triggered by someone calling the static method "processPackageReplacedBroadcast"
   *
   * @param cps - a String array containing all the ContentProvider authorities to upgrade
   * @return
   */
  protected boolean doDbUpgrade(final Intent intent) {
    boolean success = false;
    try {
      // First set the global lock on the Base ContentProvider
      LogUtils.i(LogUtils.TAG, "Package has been replaced, perform database upgrades...");
      // PIMContentProviderBase.setMaintenanceLock(cpLock, true);

      // Get the list of content authorities from the Intent extras
      Bundle extras = intent.getExtras();
      String[] cps = extras.getStringArray(CP_LIST);
      if (cps == null) {
        // Nothing to upgrade?
        return success;
      }
      // Write out our current task information to a preference file
      writeActionState(DB_UPGRADE_ACTION, cps);

      // Lock all providers, then upgrade, then unlock
      lockProviders(cps);
      success = upgradeProviders(cps);
      unlockProviders(cps);

    } catch (Exception e) {
      LogUtils.e(LogUtils.TAG, "Database upgrade exception: %s", e.getMessage());
    } finally {
      // Unlock the Base Content Provider
      // PIMContentProviderBase.setMaintenanceLock(cpLock, false);
    }
    LogUtils.i(LogUtils.TAG, "Package has been replaced, perform database upgrades...done");
    return success;
  }
 /**
  * doStartupAction - during startup, check if a previous task was not finished
  *
  * @param action - last action performed
  * @return always returns true
  */
 protected boolean doStartupAction(int lastAction) {
   LogUtils.i(LogUtils.TAG, "Last Maintenance state value is %d", lastAction);
   if (lastAction != UNKNOWN_ACTION) {
     queueUnfinishedTask(lastAction);
   }
   return true;
 }
 /**
  * processPackageReplacedBroadcast should be called when the MY_PACKAGE_REPLACED broadcast is
  * received by this application
  *
  * @param context
  * @param broadcastIntext - the raw broadcast
  * @param cps - a list of ContentProvider Authorities to upgrade
  * @return
  */
 public static void processPackageReplacedBroadcast(
     Context context, Intent broadcastIntent, String[] cps) {
   LogUtils.i(LogUtils.TAG, "CPMaintenanceService - package replaced");
   Intent i = new Intent(context, CPMaintenanceService.class);
   i.setAction(ACTION_PACKAGE_REPLACED_BROADCAST);
   i.putExtra(Intent.EXTRA_INTENT, broadcastIntent);
   i.putExtra(CP_LIST, cps);
   context.startService(i);
 }
 /**
  * queueUnfinishedTask - If the last action did not complete the queue up again
  *
  * @param lastAction - the last action saved to the prefs file
  * @return N/A
  */
 protected void queueUnfinishedTask(int lastAction) {
   switch (lastAction) {
     case DB_UPGRADE_ACTION:
       // Read in the list of authorities
       String[] cps = getCPListFromSharedPrefs();
       // Schedule the upgrade task
       if (cps != null) {
         LogUtils.w(LogUtils.TAG, "Previous Database upgrade task failed:");
         for (String cp : cps) {
           LogUtils.i(LogUtils.TAG, "   upgrading CP: %s", cp);
         }
         processPackageReplacedBroadcast(getApplicationContext(), null, cps);
       } else {
         LogUtils.i(LogUtils.TAG, "Last task was a db upgrade but could not find CP list");
       }
       break;
   }
 }
 private ConversationMessage getMessageFromCursor(MessageCursor cursor) {
   // ignore cursors that are still loading results
   if (cursor == null || !cursor.isLoaded()) {
     LogUtils.i(LOG_TAG, "CONV RENDER: existing cursor is null, rendering from scratch");
     return null;
   }
   if (mActivity == null || mActivity.isFinishing()) {
     // Activity is finishing, just bail.
     return null;
   }
   if (!cursor.moveToFirst()) {
     LogUtils.e(LOG_TAG, "unable to open message cursor");
     return null;
   }
   return cursor.getMessage();
 }
 /**
  * processStartupTask should be called when the very first ContentProvider is created is received
  * by this application
  *
  * @param context
  * @param broadcastIntext - the raw broadcast
  * @param cps - a list of ContentProvider Authorities to upgrade
  * @return
  */
 public static void processStartupTask(Context context) {
   LogUtils.i(LogUtils.TAG, "CPMaintenanceService - startup of first ContentProvider");
   Intent i = new Intent(context, CPMaintenanceService.class);
   i.setAction(ACTION_NORMAL_START);
   context.startService(i);
 }