@Override
  protected void onPause() {
    super.onPause();
    if (Log.DEBUG) Log.v("SMSPopupActivity: onPause()");

    // Hide the soft keyboard in case it was shown via quick reply
    hideSoftKeyboard();

    // Shutdown eyes-free TTS
    if (eyesFreeTts != null) {
      eyesFreeTts.shutdown();
    }

    // Shutdown Android TTS
    if (androidTextToSpeechAvailable) {
      if (androidTts != null) {
        androidTts.shutdown();
      }
    }

    // Dismiss loading dialog
    if (mProgressDialog != null) {
      mProgressDialog.dismiss();
    }

    if (wasVisible) {
      // Cancel the receiver that will clear our locks
      ClearAllReceiver.removeCancel(getApplicationContext());
      ClearAllReceiver.clearAll(!exitingKeyguardSecurely);
    }

    mDbAdapter.close();
  }
  public ManagePreferences(Context _context, String _contactId) {
    contactId = _contactId;
    context = _context;
    useDatabase = false;

    if (Log.DEBUG) Log.v("contactId = " + contactId);
    long contactIdLong;
    try {
      contactIdLong = Long.parseLong(contactId);
    } catch (NumberFormatException e) {
      contactIdLong = 0;
    }

    if (contactIdLong > 0) {
      mDbAdapter = new SmsPopupDbAdapter(context);
      try {
        mDbAdapter.open(true); // Open database read-only
        contactCursor = mDbAdapter.fetchContactSettings(contactIdLong);
        if (contactCursor != null) {
          if (Log.DEBUG) Log.v("Contact found - using database");
          useDatabase = true;
        }
        mDbAdapter.close();
      } catch (SQLException e) {
        if (Log.DEBUG) Log.v("Error opening or creating database");
        useDatabase = false;
      }
    } else {
      if (Log.DEBUG) Log.v("Contact NOT found - using prefs");
    }

    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
  }
 public void close() {
   if (contactCursor != null) {
     contactCursor.close();
   }
   if (mDbAdapter != null) {
     mDbAdapter.close();
   }
 }
 public void putString(int resPrefId, String newVal, String dbColumnNum) {
   if (useDatabase) {
     mDbAdapter.open(); // Open write
     mDbAdapter.updateContact(Long.valueOf(contactId), dbColumnNum, newVal);
     mDbAdapter.close();
   } else {
     SharedPreferences.Editor settings = mPrefs.edit();
     settings.putString(context.getString(resPrefId), newVal);
     settings.commit();
   }
 }