@Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Log.w(THIS_FILE, "Click at index " + position + " id " + id);
    super.onListItemClick(l, v, position, id);

    PrefGroup pref_gp = adapter.getItem(position);
    startActivity(pref_gp.intent);
  }
 @Override
 public Bitmap getContactPhoto(Context ctxt, Uri uri, Integer defaultResource) {
   Bitmap img = null;
   try {
     img = People.loadContactPhoto(ctxt, uri, defaultResource, null);
   } catch (IllegalArgumentException e) {
     Log.w("Contact3", "Failed to find contact photo");
   }
   return img;
 }
  /**
   * Hide a preference from the screen so that user can't see and modify it
   *
   * @param parent the parent group preference if any, leave null if preference is a root pref
   * @param fieldName the preference key name to hide
   */
  public void hidePreference(String parent, String fieldName) {
    PreferenceScreen pfs = getPreferenceScreen();
    PreferenceGroup parentPref = pfs;
    if (parent != null) {
      parentPref = (PreferenceGroup) pfs.findPreference(parent);
    }

    Preference toRemovePref = pfs.findPreference(fieldName);

    if (toRemovePref != null && parentPref != null) {
      parentPref.removePreference(toRemovePref);
    } else {
      Log.w("Generic prefs", "Not able to find" + parent + " " + fieldName);
    }
  }
 @Override
 public void onTrigger(int keyCode, int dialTone) {
   if (dialPadTextView != null) {
     // Update text view
     KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
     char nbr = event.getNumber();
     StringBuilder sb = new StringBuilder(dialPadTextView.getText());
     sb.append(nbr);
     dialPadTextView.setText(sb.toString());
   }
   if (getSherlockActivity() instanceof OnDtmfListener) {
     Integer callId = getArguments().getInt(EXTRA_CALL_ID);
     if (callId != null) {
       ((OnDtmfListener) getSherlockActivity()).OnDtmf(callId, keyCode, dialTone);
     } else {
       Log.w(THIS_FILE, "Impossible to find the call associated to this view");
     }
   }
 }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(THIS_FILE, "Upgrading database from version " + oldVersion + " to " + newVersion);
      if (oldVersion < 1) {
        db.execSQL("DROP TABLE IF EXISTS " + SipProfile.ACCOUNTS_TABLE_NAME);
      }
      if (oldVersion < 5) {
        try {
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_KA_INTERVAL
                  + " INTEGER");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 6) {
        db.execSQL("DROP TABLE IF EXISTS " + SipManager.FILTERS_TABLE_NAME);
      }
      if (oldVersion < 10) {
        try {
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_ALLOW_CONTACT_REWRITE
                  + " INTEGER");
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_CONTACT_REWRITE_METHOD
                  + " INTEGER");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 13) {
        try {
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_TRANSPORT
                  + " INTEGER");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRANSPORT
                  + "="
                  + SipProfile.TRANSPORT_UDP
                  + " WHERE prevent_tcp=1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRANSPORT
                  + "="
                  + SipProfile.TRANSPORT_TCP
                  + " WHERE use_tcp=1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRANSPORT
                  + "="
                  + SipProfile.TRANSPORT_AUTO
                  + " WHERE use_tcp=0 AND prevent_tcp=0");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 17) {
        try {
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_KA_INTERVAL
                  + "=0");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 18) {
        try {
          // As many users are crying... remove auto transport and force udp
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRANSPORT
                  + "="
                  + SipProfile.TRANSPORT_UDP
                  + " WHERE "
                  + SipProfile.FIELD_TRANSPORT
                  + "="
                  + SipProfile.TRANSPORT_AUTO);
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 22) {
        try {
          // Add use proxy row
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_REG_USE_PROXY
                  + " INTEGER");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_REG_USE_PROXY
                  + "=3");
          // Add stack field
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_SIP_STACK
                  + " INTEGER");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_SIP_STACK
                  + "=0");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 23) {
        try {
          // Add use zrtp row
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_USE_ZRTP
                  + " INTEGER");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_USE_ZRTP
                  + "=-1");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 24) {
        try {
          // Add voice mail row
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_VOICE_MAIL_NBR
                  + " TEXT");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_VOICE_MAIL_NBR
                  + "=''");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 25) {
        try {
          // Add voice mail row
          db.execSQL(
              "ALTER TABLE "
                  + SipMessage.MESSAGES_TABLE_NAME
                  + " ADD "
                  + SipMessage.FIELD_FROM_FULL
                  + " TEXT");
          db.execSQL(
              "UPDATE "
                  + SipMessage.MESSAGES_TABLE_NAME
                  + " SET "
                  + SipMessage.FIELD_FROM_FULL
                  + "="
                  + SipMessage.FIELD_FROM);
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 26) {
        try {
          // Add reg delay before refresh row
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_REG_DELAY_BEFORE_REFRESH,
              "INTEGER DEFAULT -1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_REG_DELAY_BEFORE_REFRESH
                  + "=-1");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 27) {
        try {
          // Add reg delay before refresh row
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_TRY_CLEAN_REGISTERS,
              "INTEGER DEFAULT 0");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRY_CLEAN_REGISTERS
                  + "=0");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 28) {
        try {
          // Add call log profile id
          addColumn(
              db, SipManager.CALLLOGS_TABLE_NAME, SipManager.CALLLOG_PROFILE_ID_FIELD, "INTEGER");
          // Add call log status code
          addColumn(
              db, SipManager.CALLLOGS_TABLE_NAME, SipManager.CALLLOG_STATUS_CODE_FIELD, "INTEGER");
          db.execSQL(
              "UPDATE "
                  + SipManager.CALLLOGS_TABLE_NAME
                  + " SET "
                  + SipManager.CALLLOG_STATUS_CODE_FIELD
                  + "=200");
          // Add call log status text
          addColumn(
              db, SipManager.CALLLOGS_TABLE_NAME, SipManager.CALLLOG_STATUS_TEXT_FIELD, "TEXT");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 30) {
        try {
          // Add reg delay before refresh row
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_USE_RFC5626,
              "INTEGER DEFAULT 1");
          addColumn(
              db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_RFC5626_INSTANCE_ID, "TEXT");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_RFC5626_REG_ID, "TEXT");

          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_VID_IN_AUTO_SHOW,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_VID_OUT_AUTO_TRANSMIT,
              "INTEGER DEFAULT -1");
          addColumn(
              db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_RTP_PORT, "INTEGER DEFAULT -1");

          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_RTP_ENABLE_QOS,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_RTP_QOS_DSCP,
              "INTEGER DEFAULT -1");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_RTP_PUBLIC_ADDR, "TEXT");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_RTP_BOUND_ADDR, "TEXT");

          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      // Nightly build bug -- restore mime type field to mime_type
      if (oldVersion == 30) {
        try {
          addColumn(db, SipMessage.MESSAGES_TABLE_NAME, SipMessage.FIELD_MIME_TYPE, "TEXT");
          db.execSQL(
              "UPDATE "
                  + SipMessage.MESSAGES_TABLE_NAME
                  + " SET "
                  + SipMessage.FIELD_MIME_TYPE
                  + "='text/plain'");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }

      if (oldVersion < 32) {
        try {
          // Add android group for buddy list
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_ANDROID_GROUP, "TEXT");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 33) {
        try {
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_ALLOW_VIA_REWRITE,
              "INTEGER DEFAULT 0");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_ALLOW_VIA_REWRITE
                  + "=0");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 34) {
        try {
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_SIP_STUN_USE,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_MEDIA_STUN_USE,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_ICE_CFG_USE,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_ICE_CFG_ENABLE,
              "INTEGER DEFAULT 0");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_TURN_CFG_USE,
              "INTEGER DEFAULT -1");
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_TURN_CFG_ENABLE,
              "INTEGER DEFAULT 0");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_TURN_CFG_SERVER, "TEXT");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_TURN_CFG_USER, "TEXT");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_TURN_CFG_PASSWORD, "TEXT");

          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_SIP_STUN_USE
                  + "=-1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_MEDIA_STUN_USE
                  + "=-1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_ICE_CFG_USE
                  + "=-1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_ICE_CFG_ENABLE
                  + "=0");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TURN_CFG_USE
                  + "=-1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TURN_CFG_ENABLE
                  + "=0");

          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 35) {
        try {
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_IPV6_MEDIA_USE,
              "INTEGER DEFAULT 0");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_IPV6_MEDIA_USE
                  + "=0");
          Log.d(THIS_FILE, "Upgrade done");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 36) {
        try {
          // Enable try to clean register for all but ones that doesn't support contact rewrite
          // normal (legacy)
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRY_CLEAN_REGISTERS
                  + "=1 WHERE 1");
          db.execSQL(
              "UPDATE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " SET "
                  + SipProfile.FIELD_TRY_CLEAN_REGISTERS
                  + "=0 WHERE "
                  + SipProfile.FIELD_CONTACT_REWRITE_METHOD
                  + "=1");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 37) {
        try {
          addColumn(
              db,
              SipProfile.ACCOUNTS_TABLE_NAME,
              SipProfile.FIELD_AUTH_INITIAL_AUTH,
              "INTEGER DEFAULT 0");
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_AUTH_ALGO, "TEXT");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 38) {
        try {
          addColumn(db, SipProfile.ACCOUNTS_TABLE_NAME, SipProfile.FIELD_WIZARD_DATA, "TEXT");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      if (oldVersion < 39) {
        try {
          db.execSQL(
              "ALTER TABLE "
                  + SipProfile.ACCOUNTS_TABLE_NAME
                  + " ADD "
                  + SipProfile.FIELD_DEFAULT_URI_SCHEME
                  + " TEXT");
        } catch (SQLiteException e) {
          Log.e(THIS_FILE, "Upgrade fail... maybe a crappy rom...", e);
        }
      }
      onCreate(db);
    }
    @Override
    public void run() {
      Log.d(THIS_FILE, "InCallTonePlayer.run(toneId = " + mToneId + ")...");

      int toneType; // passed to ToneGenerator.startTone()
      int toneVolume; // passed to the ToneGenerator constructor
      int toneLengthMillis;
      switch (mToneId) {
        case TONE_CALL_WAITING:
          toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
          toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
          toneLengthMillis = 5000;
          break;
        case TONE_BUSY:
          toneType = ToneGenerator.TONE_SUP_BUSY;
          toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
          toneLengthMillis = 4000;
          break;
        case TONE_CONGESTION:
          toneType = ToneGenerator.TONE_SUP_CONGESTION;
          toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
          toneLengthMillis = 4000;
          break;
        case TONE_BATTERY_LOW:
          // For now, use ToneGenerator.TONE_PROP_ACK (two quick
          // beeps). TODO: is there some other ToneGenerator
          // tone that would be more appropriate here? Or
          // should we consider adding a new custom tone?
          toneType = ToneGenerator.TONE_PROP_ACK;
          toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
          toneLengthMillis = 1000;
          break;
        case TONE_CALL_ENDED:
          toneType = ToneGenerator.TONE_PROP_PROMPT;
          toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
          toneLengthMillis = 2000;
          break;
        default:
          throw new IllegalArgumentException("Bad toneId: " + mToneId);
      }

      // If the mToneGenerator creation fails, just continue without it.
      // It is
      // a local audio signal, and is not as important.
      ToneGenerator toneGenerator;
      try {
        toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, toneVolume);
        // if (DBG) log("- created toneGenerator: " + toneGenerator);
      } catch (RuntimeException e) {
        Log.w(THIS_FILE, "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
        toneGenerator = null;
      }

      // Using the ToneGenerator (with the CALL_WAITING / BUSY /
      // CONGESTION tones at least), the ToneGenerator itself knows
      // the right pattern of tones to play; we do NOT need to
      // manually start/stop each individual tone, or manually
      // insert the correct delay between tones. (We just start it
      // and let it run for however long we want the tone pattern to
      // continue.)
      //
      // TODO: When we stop the ToneGenerator in the middle of a
      // "tone pattern", it sounds bad if we cut if off while the
      // tone is actually playing. Consider adding API to the
      // ToneGenerator to say "stop at the next silent part of the
      // pattern", or simply "play the pattern N times and then
      // stop."

      if (toneGenerator != null) {
        toneGenerator.startTone(toneType);
        SystemClock.sleep(toneLengthMillis);
        toneGenerator.stopTone();

        Log.v(THIS_FILE, "- InCallTonePlayer: done playing.");
        toneGenerator.release();
      }
    }