ContactsListActivity
在ContactsListActivity里创建了一个名片导入的菜单。
- // SIM import
- Intent importIntent = new Intent(Intent.ACTION_VIEW);
- importIntent.setType("vnd.android.cursor.item/sim-contact");
- importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
- menu.add(0, 0, 0, R.string.importFromSim)
- .setIcon(R.drawable.ic_menu_import_contact)
- .setIntent(importIntent);
复制代码
很明显可以看出名片应用程序只是启动了电话里面的SimContacts:
ADNList
SimContacts是从ADNList继承过来的,有必要先看下ADNList。它是一个ListActivity,用来显示SIM卡中的名片。
先查询SIM卡中的名片:
- protected Uri resolveIntent() {
- Intent intent = getIntent();
- if (intent.getData() == null) {
- intent.setData(Uri.parse("content://sim/adn"));
- }
-
- return intent.getData();
- }
-
- private void query() {
- Uri uri = resolveIntent();
- if (DBG) log("query: starting an async query");
- mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
- null, null, null);
- displayProgress(true);
- }
复制代码
mQueryHandler是AsyncQueryHandler的子类,当查询完成时,会调用onQueryComplete去更新SIM卡名片列表和进度状态:
- protected void onQueryComplete(int token, Object cookie, Cursor c) {
- if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
- mCursor = c;
- setAdapter();
- displayProgress(false);
- }
复制代码
SimContacts
再回到SimContacts,从SIM卡里查询名片是ADNList做的,SimContacts则主要是负责导入SIM卡名片到手机名片里:
选择某条名片导入,会给用户编辑的机会,由名片应用程序来负责:
- private void importOne(int position) {
- if (mCursor.moveToPosition(position)) {
- String name = mCursor.getString(NAME_COLUMN);
- String number = mCursor.getString(NUMBER_COLUMN);
- Object[] parsed = new Object[2];
- Uri personUrl = parseName(name, parsed);
-
- Intent intent;
- if (personUrl == null) {
- // Add a new contact
- intent = new Intent(Contacts.Intents.Insert.ACTION,
- Contacts.People.CONTENT_URI);
- intent.putExtra(Contacts.Intents.Insert.NAME, (String)parsed[0]);
- intent.putExtra(Contacts.Intents.Insert.PHONE, number);
- intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
- } else {
- // Add the number to an existing contact
- intent = new Intent(Intent.ACTION_EDIT, personUrl);
- }
- startActivity(intent);
- }
- }
复制代码
如果是全部导入,则直接通过ContentResolver插入进去,这里由一个独立的线程负责:
- public void run() {
- ContentValues map = new ContentValues();
- ContentResolver cr = getContentResolver();
- Object[] parsed = new Object[2];
-
- mCursor.moveToPosition(-1);
- while (!mCanceled && mCursor.moveToNext()) {
- String name = mCursor.getString(0);
- String number = mCursor.getString(1);
-
- Uri personUrl = parseName(name, parsed);
-
- if (personUrl == null) {
- map.clear();
- map.put(Contacts.People.NAME, (String) parsed[0]);
- personUrl = People.createPersonInMyContactsGroup(cr, map);
- if (personUrl == null) {
- Log.e(TAG, "Error inserting person " + map);
- continue;
- }
- }
-
- map.clear();
- map.put(Contacts.People.Phones.NUMBER, number);
- map.put(Contacts.People.Phones.TYPE, (Integer) parsed[1]);
- Uri numberUrl = cr.insert(
- Uri.withAppendedPath(personUrl, Contacts.People.Phones.CONTENT_DIRECTORY),
- map);
-
- mProgressDialog.incrementProgressBy(1);
- if (numberUrl == null) {
- Log.e(TAG, "Error inserting phone " + map + " for person " +
- personUrl + ", removing person");
- continue;
- }
- }
-
- mProgressDialog.dismiss();
-
- finish();
- }
|