// 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 deleteChannelFromDatabase(CellBroadcastChannel oldChannel) { String where = NUMBER + "=" + oldChannel.getChannelId(); try { getContentResolver().delete(mUri, where, null); } catch (IllegalArgumentException e) { return false; } return true; }
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; }
@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 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; }
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); }