[Contacts Data][Part One]在手机中预置联系人

来源:互联网 发布:法国达索软件 编辑:程序博客网 时间:2024/04/28 16:04

[DESCRIPTION]

预置联系人/Service Number

此方法比较适合预置联系人的数目不是特别多的情况

[SOLUTION]

本方案实现预置联系人(包含姓名、号码信息)至手机中;           

代码分为两部分:

Part One 预置的联系人插入到数据库中;

(在三个地方屏蔽对预置联系人进行编辑处理:联系人详情界面、联系人多选界面、新建联系人选择合并联系人时


Part One

1.新建PresetContactsImportProcessor.java

Path: alps\packages\apps\Contacts\src\com\mediatek\contacts\simservice

package com.mediatek.contacts.simservice;import com.mediatek.contacts.simservice.SIMProcessorManager.ProcessorCompleteListener;import android.content.Context;import android.content.Intent;//import android.util.Log;import android.content.ContentProviderOperation;import android.content.ContentValues;import android.content.OperationApplicationException;import android.database.Cursor;import android.net.Uri;import android.provider.ContactsContract;import android.provider.ContactsContract.CommonDataKinds.Email; //for usimimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;import android.provider.ContactsContract.CommonDataKinds.Phone;import android.provider.ContactsContract.CommonDataKinds.StructuredName;import android.provider.ContactsContract.Data;import android.provider.ContactsContract.Groups;import android.provider.ContactsContract.RawContacts;import com.android.contacts.common.model.account.AccountType;import android.os.RemoteException;import java.util.ArrayList;import com.mediatek.contacts.simservice.SIMProcessorManager.ProcessorCompleteListener;import com.mediatek.contacts.simservice.SIMServiceUtils;import com.mediatek.contacts.simservice.SIMServiceUtils.ServiceWorkData;import com.mediatek.contacts.simcontact.SimCardUtils;import com.mediatek.contacts.util.Log;import android.provider.ContactsContract.PhoneLookup;  public class PresetContactsImportProcessor extends SIMProcessorBase { private static final String TAG = "PresetContactsImportProcessor";    private static boolean sIsRunningNumberCheck = false;    private static final int INSERT_PRESET_NUMBER_COUNT = 5;         //预置联系人的个数    private static final String INSERT_PRESET_NAME[]   = {"Policia","Hospital","Buzón de voz","Ambulancia","Bomberos"}; //各预置联系人的姓名     private static final String INSERT_PRESET_NUMBER[] = {"105","117","154","115","116"};   //各预置联系人的号码    private int mSlotId;    private Context mContext;       public PresetContactsImportProcessor(Context context, int slotId, Intent intent, ProcessorCompleteListener listener) {super(intent, listener);mContext = context;mSlotId = slotId;   }    @Override    public int getType() {       return SIMServiceUtils.SERVICE_WORK_IMPORT_PRESET_CONTACTS;   }    @Override    public void doWork() {       if (isCancelled()) {           Log.d(TAG,"[doWork]cancel import preset contacts work. Thread id=" + Thread.currentThread().getId());           return;       }       importDefaultReadonlyContact();   }     private void importDefaultReadonlyContact(){          Log.i(TAG, "isRunningNumberCheck before: " + sIsRunningNumberCheck);         if (sIsRunningNumberCheck) {            return;         }         sIsRunningNumberCheck = true;         for(int i = 0;i < INSERT_PRESET_NUMBER_COUNT; i++) {             Log.i(TAG, "isRunningNumbrCheck after: " + sIsRunningNumberCheck);             Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(INSERT_PRESET_NUMBER[i]));             Log.i(TAG, "getContactInfoByPhoneNumbers(), uri = " + uri);             Cursor contactCursor = mContext.getContentResolver().query(uri,                                 new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID}, null, null, null);                 try {if (contactCursor != null && contactCursor.getCount() > 0) {return;} else {final ArrayList operationList = new ArrayList();ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);ContentValues contactvalues = new ContentValues();contactvalues.put(RawContacts.ACCOUNT_NAME, AccountType.ACCOUNT_NAME_LOCAL_PHONE);contactvalues.put(RawContacts.ACCOUNT_TYPE, AccountType.ACCOUNT_TYPE_LOCAL_PHONE);contactvalues.put(RawContacts.INDICATE_PHONE_SIM, ContactsContract.RawContacts.INDICATE_PHONE);builder.withValues(contactvalues);builder.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED);operationList.add(builder.build());builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);builder.withValue(Phone.TYPE, Phone.TYPE_MOBILE);builder.withValue(Phone.NUMBER, INSERT_PRESET_NUMBER[i]);builder.withValue(Data.IS_PRIMARY, 1);operationList.add(builder.build()); builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);builder.withValue(StructuredName.DISPLAY_NAME, INSERT_PRESET_NAME[i]);operationList.add(builder.build()); try {mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);} catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));}}} finally {             // when this service start,but the contactsprovider has not been started yet              // the contactCursor perhaps null, but not always.(first load will weekup the provider)             // so add null block to avoid nullpointerexceptionif (contactCursor != null) {contactCursor.close();}} //END forLog.i(TAG, "isRunningNumberCheck insert: " + sIsRunningNumberCheck);sIsRunningNumberCheck = false;}     } }

2. 修改SIMServiceUtils.java

Pathalps\packages\apps\ContactsCommon\src\com\mediatek\contacts\simservice 

添加

public static final int SERVICE_WORK_IMPORT_PRESET_CONTACTS = 5;

3. 修改SIMProcessorManager.java

Pathalps\packages\apps\Contacts\src\com\mediatek\contacts\simservice

在SIMProcessorManager.java中createProcessor函数里添加

private SIMProcessorBase createProcessor(Context context, int subId, int workType,            Intent intent, ProcessorCompleteListener listener) {        Log.d(TAG, "[createProcessor] create new processor for subId: " + subId + ", workType: "                + workType);        SIMProcessorBase processor = null;        if (workType == SIMServiceUtils.SERVICE_WORK_IMPORT) {            processor = new SIMImportProcessor(context, subId, intent, listener);        } else if (workType == SIMServiceUtils.SERVICE_WORK_REMOVE) {            processor = new SIMRemoveProcessor(context, subId, intent, listener);        } else if (workType == SIMServiceUtils.SERVICE_WORK_EDIT) {            processor = new SIMEditProcessor(context, subId, intent, listener);        } else if (workType == SIMServiceUtils.SERVICE_WORK_DELETE) {            processor = new SIMDeleteProcessor(context, subId, intent, listener);//added Preset contacts lds 20161215        }else if (workType == SIMServiceUtils.SERVICE_WORK_IMPORT_PRESET_CONTACTS) {processor = new PresetContactsImportProcessor(context, subId, intent, listener);}//end        return processor;    }

4. 修改BootCmpReceiver.java

Pathalps\packages\apps\Contacts\src\com\mediatek\contacts\simcontact

在BootCmpReceiver.java中添加如下方法:

/** * when boot complete,preset the service number directly. */ private void presetServiceNumber(Context context) {     LogUtils.d(TAG, "presetServiceNumber");     startSimService(context, -1, SIMServiceUtils.SERVICE_WORK_IMPORT_PRESET_CONTACTS); }

5. 修改BootCmpReceiver.java

Pathalps\packages\apps\Contacts\src\com\mediatek\contacts\simcontact

在BootCmpReceiver.java中onReceive()方法

public void onReceive(Context context, Intent intent) {     ... ...     } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {         // fix ALPS01003520,when boot complete,remove the contacts if the         // card of a slot has been removed         if (!isPhbReady()) {             processBootComplete(context);         }     }     ... ... }
修改为:
public void onReceive(Context context, Intent intent) {     ... ...     } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {         // fix ALPS01003520,when boot complete,remove the contacts if the         // card of a slot has been removed         if (!isPhbReady()) {             processBootComplete(context);         }             // [START] add for Preset service number          presetServiceNumber(context);          // [END]      }      ... ... } 


0 0