Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android GB2/GB3版本預置聯系人且不可編輯或刪除

android GB2/GB3版本預置聯系人且不可編輯或刪除

編輯:關於Android編程

一、針對GB2、GB3等版本,預置聯系人可以 中Part one這部分即可。
例如:在AbstractStartSIMService類的開始增加的定義如下:
public static final int INSERT_NUMBER_COUNT = 4; //定義要預置的聯系人的個數
public static final String defaultNumbers[] = {"xxxx", "xxxx", "xxxx" , "xxxx"}; //定義要預置的聯系人的號碼
public static final String defaultNames[] = {"xxxxx", "xxxxx", "xxxxx", "xxxxx"}; //定義要預置的聯系人的姓名

二、預置後不希望對這些聯系人進行刪除或編輯操作。
1、請參考如下方式修改ContactsListActivity.java文件:
(1) onCreateContextMenu()方法中
if(addEditAndDelete && simHasImported){
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact);
menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact);
}
這部分之前添加如下部分,設置編輯和刪除選項不可見:
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
addEditAndDelete = false;
}
}

(2) onListItemClick()方法中
if((isHeader && id == 0) || (null != PhoneOwner.getInstance() && id == PhoneOwner.getInstance().getOwnerID())) {
......................
}else{
if(mSearchMode && mAdapter.isSearchAllContactsItemPosition(position)){
doSearch();
}else if(mMode == MODE_INSERT_OR_EDIT_CONTACT || mMode == MODE_QUERY_PICK_TO_EDIT){
................
}else if((mMode & MODE_MASK_CREATE_NEW) == MODE_MASK_CREATE_NEW && position == 0 ){
................
}else if(mMode == MODE_JOIN_CONTACT && id == JOIN_MODE_SHOW_ALL_CONTACTS_ID){
................
}else if(id > 0){
................
if(mMode == MODE_DEFAULT && mDeleteState){
try{
...............
CheckBox cb = rootLayout.getCheckBox();
contactId = cursor.getInt(cursor.getColumnIndexOrThrow(Contacts.ID));

//(2)步驟的第一處修改開始處
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
break;
}
}
}
//(2)步驟的第一處修改結束處
...................
if(cbPrevState){
cb.setChecked(false);

//(2)步驟的第二處修改開始處
if(isDefaultContact){
cb.setVisibility(View.INVISIBLE);
}
//(2)步驟的第二處修改結束處
...............
}else{
//(2)步驟的第三處修改開始處
//cb.setChecked(true);
//indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
//mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
//long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
if(!isDefaultContact){
cb.setChecked(true);
cb.setVisibility(View.VISIBLE);
indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
}else{
cb.setChecked(false);
cb.setVisibility(View.INVISIBLE);
}
//(2)步驟的第三處修改結束處
........................
if(mSelected == mAllContacts.size()){
mSelectAllToDel = true;

//(2)步驟的第四處修改開始處
//mSelectAllBox.setChecked(mSelectAllToDel);
if(!isDefaultContact){
mSelectAllBox.setChecked(mSelectAllToDel);
}else{
mSelectAllBox.setChecked(false);
mSelectAllBox.setVisibility(View.INVISIBLE);
}
//(2)步驟的第四處修改結束處
mSelectAllButton.setText(mUnSelectText);
}
} //end else
.........................
} //end try
..................
}
...........
}
............
}
........................

(3) onQueryComplete()方法中
//mtk80909 start
if(activity.mDeleteState /*&& ! activity.mAllOrNone*/){
................
if(tmpCursor.moveToFirst()){
do{
..................
long indexInSim = tmpCursor.getInt(tmpCursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//(3)步驟的修改開始處
//mAllContacts.put(currentID, indicate);
//mAllContactsIndexInSim.put(currentID, indexInSim); // index
// in
// sim
String displayName = tmpCursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
}
}
}
if( ! isDefaultContact ){
mAllContacts.put(currentID, indicate);
mAllContactsIndexInSim.put(currentID, indexInSim); // index in sim
}
//(3)步驟的修改結束處
}while(tmpCursor.moveToNext());
................
}
}

(4) bindView()方法中
if(mMode == MODE_DEFAULT && mDeleteState){ // [mtk80909]
long currentID = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
//(4)步驟第一處修改的開始處
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
Log.d(TAG, "displayName: " + displayName);
//(4)步驟第一處修改的結束處

CheckBox cb = view.getCheckBox();
if(cb != null){
cb.setVisibility(View.VISIBLE);
//(4)步驟第二處修改的開始處
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
cb.setVisibility(View.INVISIBLE);
break;
}
}
//(4)步驟第二處修改的結束處
.................

}
}

2、按以下方式修改ViewContactActivity.java文件
(1) 在onCreateContextMenu()方法中,去掉這幾個號碼的刪除選項
將 menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number); 語句修改為:
int count = 0;
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(entry.data.equals(AbstractStartSIMService.defaultNumbers[i])){
count++;
}
}
if(count == 0){
menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number);
}

(2) 在onPrepareOptionsMenu()方法中,去掉icon_menu中的編輯和刪除聯系人選項
menu.findItem(R.id.menu_edit).setVisible(addEditAndDelete);
menu.findItem(R.id.menu_delete).setVisible(addEditAndDelete); 語句的 前面 添加如下語句:
if(hasRawContact){
for (Entity contactEntity : mEntities) {
for (NamedContentValues subValue : contactEntity.getSubValues()) {
final ContentValues entryValues = subValue.values;
final String mimeType = entryValues.getAsString(Data.MIMETYPE);
final String data = entryValues.getAsString(Data.DATA1);
int count = 0;
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(data.equals(AbstractStartSIMService.defaultNumbers[i]) || data.equals(AbstractStartSIMService.defaultNames[i])){
count++;
}
}
if(count != 0){
addEditAndDelete = false;
}
}
}
}

3、為了防止刪除或編輯時誤判,修改EditContactActivity.java和EditSimContactActivity.java文件,不讓用戶插入與預置號碼的name、number一致的聯系人
(1) 修改EditContactActivity.java文件
a. 在該文件的開頭,導入如下包
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;

b. 在該類的開始定義兩個全局變量
boolean sameNumberExist = false;
boolean sameNameExist = false;

c. 在onBackPressed()方法中
將 doSaveAction(SAVE_MODE_DEFAULT); 語句修改成:
doSaveWithCheckField();
if(sameNumberExist || sameNameExist){
finish();
}

d. 在 doSaveWithCheckField()方法中
ArrayList list = null;
//第一處修改的開始
ArrayList listName = null;
//第一處修改的結束
if(mState != null && mState.size() > 0){
.............
//第二處修改的開始
listName = mState.get(0).getMimeEntries(StructuredName.CONTENT_ITEM_TYPE);
//第二處修改的結束
}
String num;
//第三處修改的開始
/* if(list != null){
for(ValuesDelta delta : list){...............}
} */
String name1 = null;
String name2 = null;
String name = null;
sameNumberExist = false;
sameNameExist = false;
if(list != null){
for(ValuesDelta delta : list){
num = delta.getAsString(Data.DATA1);
if("".equals(num)){
sameNumberExist = false;
}
if(!TextUtils.isEmpty(num)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(num.equals(AbstractStartSIMService.defaultNumbers[i])){
sameNumberExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}

if(listName != null && !sameNumberExist ){
for(ValuesDelta delta : listName){
name1 = delta.getAsString(Data.DATA2);
name2 = delta.getAsString(Data.DATA3);
if(name2 == null && name1 != null){
name = name1;
}else if(name2 != null && name1 == null){
name = name2;
}else if(name2 != null && name1 != null){
name = name2 + " " + name1;
}
if("".equals(name)){
sameNameExist = false;
}
if(!TextUtils.isEmpty(name)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(name.equals(AbstractStartSIMService.defaultNames[i])){
sameNameExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}
//第三處修改的結束
bundle.putCharSequenceArrayList(KEY_PHONE_NUMBER, numberList);
boolean hasCheckDuplicatePlugin = .........................
//第四處修改的開始
/* if( !hasCheckDuplicatePlugin ){
doSaveAction(SAVE_MODE_DEFAULT);
} */
if(!hasCheckDuplicatePlugin && !sameNumberExist && !sameNameExist){
doSaveAction(SAVE_MODE_DEFAULT);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//第四處修改的結束

(2) 修改EditSimContactActivity.java文件
a. 在該文件的開頭導入如下包
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;

b. 在該類的開始定義兩個全局變量
boolean hasSameNumber = false;
boolean hasSameName = false;

c. 在該文件中添加方法hasSameNameOrNumberAsDefaultContacts()
private void hasSameNameOrNumberAsDefaultContacts(){
hasSameNumber = false;
hasSameName = false;
updateName = edit_contact_name.getText().toString();
updatephone = edit_phone_numbers.getText().toString();
if("".equals(updatephone)){
hasSameNumber = false;
}
if("".equals(updateName)){
hasSameName = false;
}
if(!TextUtils.isEmpty(updatephone)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updatephone.equals(AbstractStartSIMService.defaultNumbers[i])){
hasSameNumber = true;
break;
}
}
}
if(!TextUtils.isEmpty(updateName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updateName.equals(AbstractStartSIMService.defaultNames[i])){
hasSameName = true;
break;
}
}
}

}

d. 在onClick()方法中
case R.id.btn_sim1_done: {
btn1.setClickable(false);
//修改的地方開始處
// doSaveAction(mMode);
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
btn1.setClickable(true);
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//修改的地方結束處
break;
}

e. onBackPressed()方法中
doSaveAction(mMode); 語句 修改成
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
finish();
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved