private boolean queryChannelFromDatabase() {
   MmsLog.d(LOG_TAG, "queryChannelFromDatabase start");
   clearChannel();
   String[] projection = new String[] {KEYID, NAME, NUMBER, ENABLE};
   Cursor cursor = null;
   try {
     cursor = this.getContentResolver().query(mUri, projection, null, null, null);
     if (cursor != null) {
       while (cursor.moveToNext()) {
         CellBroadcastChannel channel = new CellBroadcastChannel();
         channel.setChannelId(cursor.getInt(2));
         channel.setKeyId(cursor.getInt(0));
         channel.setChannelName(cursor.getString(1));
         channel.setChannelState(cursor.getInt(3) == 1);
         mChannelArray.add(channel);
       }
     }
   } catch (IllegalArgumentException e) {
     return false;
   } finally {
     cursor.close();
   }
   MmsLog.d(LOG_TAG, "queryChannelFromDatabase end");
   return true;
 }
  private void updateChannelsWithSingleConfig(SmsBroadcastConfigInfo info) {
    int channelBeginIndex = info.getFromServiceId();
    int channelEndIndex = info.getToServiceId();
    boolean state = info.isSelected();
    MmsLog.d(LOG_TAG, "updateChannelsWithSingleConfig STATE = " + state);

    if (channelBeginIndex != -1) {
      for (int j = channelBeginIndex; j <= channelEndIndex; j++) {
        if (mCellBroadcastAsyncTask.isCancelled()) {
          break;
        }
        String jStr = String.valueOf(j);
        CellBroadcastChannel channel = getChannelObjectFromKey(jStr);
        if (channel != null) {
          channel.setChannelState(state);
        } else {
          // add a new channel to dataBase while the channel doesn't exists
          String tName = getString(R.string.cb_default_new_channel_name) + jStr;
          CellBroadcastChannel newChannel = new CellBroadcastChannel(j, tName, state);
          if (!insertChannelToDatabase(newChannel)) {
            showUpdateDBErrorInfoDialog();
          }
          mChannelArray.add(newChannel);
          mChannelMap.put(jStr, newChannel);
        }
      }
    }
  }
 // convert operation info to SmsBroadcastConfigInfo[]
 private SmsBroadcastConfigInfo[] makeChannelConfigArray(CellBroadcastChannel channel) {
   SmsBroadcastConfigInfo[] objectList = new SmsBroadcastConfigInfo[1];
   int tChannelId = channel.getChannelId();
   objectList[0] =
       new SmsBroadcastConfigInfo(tChannelId, tChannelId, -1, -1, channel.getChannelState());
   return objectList;
 }
 private boolean checkChannelIdExist(int newChannelId, int keyId) {
   int length = mChannelArray.size();
   for (int i = 0; i < length; i++) {
     CellBroadcastChannel tChannel = mChannelArray.get(i);
     int tempChannelId = tChannel.getChannelId();
     int tempKeyId = tChannel.getKeyId();
     if (tempChannelId == newChannelId && tempKeyId != keyId) {
       return true;
     }
   }
   return false;
 }
 private boolean insertChannelToDatabase(CellBroadcastChannel channel) {
   ContentValues values = new ContentValues();
   values.put(NAME, channel.getChannelName());
   values.put(NUMBER, channel.getChannelId());
   values.put(ENABLE, channel.getChannelState());
   try {
     Uri uri = getContentResolver().insert(mUri, values);
     int insertId = Integer.valueOf(uri.getLastPathSegment());
     channel.setKeyId(insertId);
     MmsLog.d(LOG_TAG, "insertChannelToDatabase(), insertId: " + insertId);
   } catch (IllegalArgumentException e) {
     return false;
   }
   return true;
 }
 private boolean deleteChannelFromDatabase(CellBroadcastChannel oldChannel) {
   String where = NUMBER + "=" + oldChannel.getChannelId();
   try {
     getContentResolver().delete(mUri, where, null);
   } catch (IllegalArgumentException e) {
     return false;
   }
   return true;
 }
 private boolean updateChannelToDatabase(
     CellBroadcastChannel oldChannel, CellBroadcastChannel newChannel) {
   MmsLog.d(LOG_TAG, "updateChannelToDatabase start oldChannel =" + oldChannel);
   String[] projection = new String[] {KEYID, NAME, NUMBER, ENABLE};
   final int id = newChannel.getKeyId();
   final String name = newChannel.getChannelName();
   final boolean enable = newChannel.getChannelState();
   final int number = newChannel.getChannelId();
   ContentValues values = new ContentValues();
   values.put(KEYID, id);
   values.put(NAME, name);
   values.put(NUMBER, number);
   values.put(ENABLE, Integer.valueOf(enable ? 1 : 0));
   String where = KEYID + "=" + oldChannel.getKeyId();
   try {
     int lines = this.getContentResolver().update(mUri, values, where, null);
   } catch (IllegalArgumentException e) {
     return false;
   }
   MmsLog.d(LOG_TAG, "updateChannelToDatabase end newChannel =" + newChannel);
   return true;
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
   if (info == null) {
     MmsLog.i(LOG_TAG, "onCreateContextMenu,menuInfo is null");
     return;
   }
   int position = info.position;
   if (position >= 3) {
     int index = position - 3;
     CellBroadcastChannel channel = mChannelArray.get(index);
     String channelName = channel.getChannelName();
     menu.setHeaderTitle(channelName);
     if (channel.getChannelState()) {
       menu.add(0, MENU_CHANNEL_ENABLE_DISABLE, 0, R.string.disable);
     } else {
       menu.add(0, MENU_CHANNEL_ENABLE_DISABLE, 0, R.string.enable);
     }
     menu.add(1, MENU_CHANNEL_EDIT, 0, R.string.cb_menu_edit);
     menu.add(2, MENU_CHANNEL_DELETE, 0, R.string.cb_menu_delete);
   }
 }
 @Override
 public boolean onContextItemSelected(MenuItem item) {
   AdapterView.AdapterContextMenuInfo info =
       (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
   int index = info.position - 3;
   CellBroadcastChannel oldChannel = mChannelArray.get(index);
   switch (item.getItemId()) {
     case MENU_CHANNEL_ENABLE_DISABLE:
       CellBroadcastChannel newChannel = new CellBroadcastChannel();
       newChannel = oldChannel;
       newChannel.setChannelState(!oldChannel.getChannelState());
       int tempOldChannelId = oldChannel.getChannelId();
       SmsBroadcastConfigInfo[] objectList = new SmsBroadcastConfigInfo[1];
       objectList[0] =
           new SmsBroadcastConfigInfo(
               tempOldChannelId, tempOldChannelId, -1, -1, newChannel.getChannelState());
       if (updateChannelToDatabase(oldChannel, newChannel)) {
         setCellBroadcastConfig(objectList);
       } else {
         showUpdateDBErrorInfoDialog();
       }
       break;
     case MENU_CHANNEL_EDIT:
       showEditChannelDialog(oldChannel);
       break;
     case MENU_CHANNEL_DELETE:
       oldChannel.setChannelState(false);
       SmsBroadcastConfigInfo[] objectList1 = makeChannelConfigArray(oldChannel);
       if (deleteChannelFromDatabase(oldChannel)) {
         setCellBroadcastConfig(objectList1);
       } else {
         showUpdateDBErrorInfoDialog();
       }
       break;
     default:
       break;
   }
   return super.onContextItemSelected(item);
 }
  private void showEditChannelDialog(final CellBroadcastChannel oldChannel) {
    int keyId = oldChannel.getKeyId();
    int cid = oldChannel.getChannelId();
    String cname = oldChannel.getChannelName();
    boolean checked = oldChannel.getChannelState();
    LayoutInflater inflater = LayoutInflater.from(this);
    final View setView = inflater.inflate(R.layout.pref_add_channel, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(setView);
    builder.setTitle(R.string.cb_channel_dialog_edit_channel);
    final EditText channelName = (EditText) setView.findViewById(R.id.edit_channel_name);
    final EditText channelNum = (EditText) setView.findViewById(R.id.edit_channel_number);
    final CheckBox channelState = (CheckBox) setView.findViewById(R.id.checkbox_channel_enable);
    channelName.setText(cname);
    channelNum.setText(String.valueOf(cid));
    channelState.setChecked(checked);
    final AlertDialog dialog;
    builder.setPositiveButton(
        android.R.string.ok,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            String name = channelName.getText().toString();
            String num = channelNum.getText().toString();
            boolean checked = channelState.isChecked();
            String errorInfo = "";
            if (!checkChannelName(name)) {
              errorInfo += getString(R.string.cb_error_channel_name);
            }
            if (!checkChannelNumber(num)) {
              errorInfo += "\n" + getString(R.string.cb_error_channel_num);
            }
            if (errorInfo.equals("")) {
              int newChannelId = Integer.valueOf(num).intValue();
              int tempOldChannelId = oldChannel.getChannelId();
              if (!checkChannelIdExist(newChannelId, oldChannel.getKeyId())) {
                dialog.dismiss();
                CellBroadcastChannel newChannel =
                    new CellBroadcastChannel(oldChannel.getKeyId(), newChannelId, name, checked);
                oldChannel.setChannelState(false);
                int tempNewChannelId = newChannel.getChannelId();
                SmsBroadcastConfigInfo[] objectList = new SmsBroadcastConfigInfo[2];
                objectList[0] =
                    new SmsBroadcastConfigInfo(tempOldChannelId, tempOldChannelId, -1, -1, false);
                objectList[1] =
                    new SmsBroadcastConfigInfo(
                        tempNewChannelId, tempNewChannelId, -1, -1, newChannel.getChannelState());
                if (updateChannelToDatabase(oldChannel, newChannel)) {
                  setCellBroadcastConfig(objectList);
                } else {
                  showUpdateDBErrorInfoDialog();
                }
              } else {
                displayMessage(getString(R.string.cb_error_channel_id_exist));
              }
            } else {
              displayMessage(errorInfo);
            }
          }
        });

    builder.setNegativeButton(android.R.string.cancel, null);
    dialog = builder.create();
    dialog.show();
    requestInputMethod(dialog);
  }