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 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); } }
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); }