2012年11月5日月曜日

ContentProviderOperation によるアドレス帳へのアクセス

AndroidManifest に

 <uses-permission android:name="android.permission.READ_CONTACTS" />
 <uses-permission android:name="android.permission.WRITE_CONTACTS" />

を追加しておくこと。



文字数が長くなると折り畳まれるので import とインデントで対応してるっす。

必要な import はこんな感じ。



import java.util.ArrayList;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.util.Log;



それでは挿入部分から、ここは特に問題無し。
一応だが getContentResolver は Context のメソッド。



public ContentProviderResult[] Insert() {

 ArrayList<ContentProviderOperation> wk_cpos =
  new ArrayList<ContentProviderOperation>() ;

 int wk_rCtId = wk_cpos.size() ;

 wk_cpos.add(
  ContentProviderOperation.newInsert( RawContacts.CONTENT_URI )
  .withValue( RawContacts.ACCOUNT_TYPE, null )
  .withValue( RawContacts.ACCOUNT_NAME, null )
  .build() ) ;

 /*
  * 氏名
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE )
  .withValue( StructuredName.DISPLAY_NAME, "氷室 鰆" )
  .withValue( StructuredName.FAMILY_NAME, "氷室" )
  .withValue( StructuredName.GIVEN_NAME, "鰆" )
  .withValue( StructuredName.PHONETIC_FAMILY_NAME, "ヒムロ" )
  .withValue( StructuredName.PHONETIC_GIVEN_NAME, "サワラ" )
  .build() ) ;

 /*
  * 会社
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE )
  .withValue( Organization.COMPANY, "株式会社サワラン" )
  .withValue( Organization.TYPE, Organization.TYPE_WORK )
  .build() ) ;


 /*
  * 自宅電話番号
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE )
  .withValue( Phone.NUMBER, "08011112222" )
  .withValue( Phone.TYPE, Phone.TYPE_HOME )
  .build() ) ;

 /*
  * 会社電話番号
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE )
  .withValue( Phone.NUMBER, "08022223333" )
  .withValue( Phone.TYPE, Phone.TYPE_WORK )
  .build() ) ;

 /*
  * 携帯番号
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE )
  .withValue( Phone.NUMBER, "08033334444" )
  .withValue( Phone.TYPE, Phone.TYPE_MOBILE )
  .build() ) ;

 /*
  * Fax 番号
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE )
  .withValue( Phone.NUMBER, "08044445555" )
  .withValue( Phone.TYPE, Phone.TYPE_FAX_HOME )
  .build() ) ;

 /*
  * Email
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE )
  .withValue( Email.DATA, "sawaran@gmail.com" )
  .withValue( Email.TYPE, Email.TYPE_WORK )
  .build() ) ;

 /*
  * 住所
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE )
  .withValue( StructuredPostal.POSTCODE, "123-4567" )
  .withValue( StructuredPostal.STREET, "猫通り1-23-45" )
  .withValue( StructuredPostal.CITY, "猫阪市" )
  .withValue( StructuredPostal.REGION, "猫阪府" )
  .withValue( StructuredPostal.COUNTRY,"日本" )
  .withValue( StructuredPostal.TYPE, StructuredPostal.TYPE_HOME )
  .build() ) ;

 /*
  * メモ
  */
 wk_cpos.add(
  ContentProviderOperation.newInsert( Data.CONTENT_URI )
  .withValueBackReference( Data.RAW_CONTACT_ID, wk_rCtId )
  .withValue( Data.MIMETYPE, Note.CONTENT_ITEM_TYPE )
  .withValue( Note.NOTE, "俺、猫" )
  .build() ) ;


 try {
  ContentProviderResult[] res =
   getContentResolver().applyBatch( ContactsContract.AUTHORITY, wk_cpos ) ;
  return res ;
 } catch (RemoteException _e) {
  Log.e( "ERR", _e.getMessage() ) ;
 } catch (OperationApplicationException _e ) {
  Log.e( "ERR", "+_e.getMessage() ) ;
 }
 return null ;
}



次は更新部分。
withSelection は where句 と同じ扱いなんで検索条件を増やすも減らすも状況によるが
条件に一致した全てのデータを変更してしまうので減らしすぎは注意っす。
今回は CONTACT_ID を指定しているので問題無いが DISPLAY_NAME などで指定した場合は注意が必要っす。



public ContentProviderResult[] Update( long _id ) {

 ArrayList<ContentProviderOperation> wk_cpos =
  new ArrayList<ContentProviderOperation>();

 /*
  * 氏名
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    StructuredName.CONTENT_ITEM_TYPE } )
  .withValue( StructuredName.DISPLAY_NAME, "氷室 白子" )
  .withValue( StructuredName.FAMILY_NAME, "氷室" )
  .withValue( StructuredName.GIVEN_NAME, "白子" )
  .withValue( StructuredName.PHONETIC_FAMILY_NAME, "ヒムロ" )
  .withValue( StructuredName.PHONETIC_GIVEN_NAME, "シラス" )
  .build() ) ;

 /*
  * 会社
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   Organization.TYPE + "=?",
   new String[]{
    String.valueOf( _id ) ,
    Organization.CONTENT_ITEM_TYPE,
    String.valueOf( Organization.TYPE_WORK ) } )
  .withValue( Organization.COMPANY, "シラス商店" )
  .build() ) ;

 /*
  * 自宅電話番号
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   Phone.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Phone.CONTENT_ITEM_TYPE,
    String.valueOf( Phone.TYPE_HOME ) } )
  .withValue( Phone.NUMBER, "08055556666" )
  .build() ) ;

 /*
  * 会社電話番号
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   Phone.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Phone.CONTENT_ITEM_TYPE,
    String.valueOf( Phone.TYPE_WORK ) } )
  .withValue( Phone.NUMBER,"08066667777" )
  .build() ) ;

 /*
  * 携帯番号
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?" + " AND " +
   Phone.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Phone.CONTENT_ITEM_TYPE,
    String.valueOf( Phone.TYPE_MOBILE ) } )
  .withValue( Phone.NUMBER,"08077778888" )
  .build() ) ;

 /*
  * Fax 番号
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   Phone.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Phone.CONTENT_ITEM_TYPE,
    String.valueOf( Phone.TYPE_FAX_HOME ) } )
  .withValue( Phone.NUMBER,"08088889999" )
  .build() ) ;

 /*
  * Email
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   Email.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Email.CONTENT_ITEM_TYPE,
    String.valueOf( Email.TYPE_WORK ) } )
  .withValue( Email.DATA,"sirasu@gmail.com" )
  .build() ) ;

 /*
  * 住所
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?"+ " AND " +
   StructuredPostal.TYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    StructuredPostal.CONTENT_ITEM_TYPE,
    String.valueOf( StructuredPostal.TYPE_HOME ) } )
  .withValue( StructuredPostal.POSTCODE, "987-6543" )
  .withValue( StructuredPostal.STREET, "犬通り987-6" )
  .withValue( StructuredPostal.CITY, "犬阪市" )
  .withValue( StructuredPostal.REGION, "犬阪府" )
  .withValue( StructuredPostal.COUNTRY, "日本" )
  .build() ) ;


 /*
  * メモ
  */
 wk_cpos.add(
  ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?" + " AND " +
   Data.MIMETYPE + "=?",
   new String[]{
    String.valueOf( _id ),
    Note.CONTENT_ITEM_TYPE } )
  .withValue( Note.NOTE, "我が輩は、犬" )
  .build() ) ;


 try {
  ContentProviderResult[] res =
   getContentResolver().applyBatch( ContactsContract.AUTHORITY, wk_cpos ) ;
  return res ;
 } catch ( RemoteException _e ) {
  Log.e( "ERR", _e.getMessage() ) ;
 } catch ( OperationApplicationException _e ) {
  Log.e( "ERR", _e.getMessage() ) ;
 }
 return null ;
}


最後は削除部分。


public ContentProviderResult[] Delete( long _id ) {

 ArrayList<ContentProviderOperation> wk_cpos =
  new ArrayList<ContentProviderOperation>() ;

 wk_cpos.add(
  ContentProviderOperation.newDelete( RawContacts.CONTENT_URI )
  .withSelection(
   Data.CONTACT_ID + "=?",
   new String[]{
    String.valueOf( _id ) } )
  .build() ) ;

 try {
  ContentProviderResult[] res =
   getContentResolver().applyBatch( ContactsContract.AUTHORITY, wk_cpos ) ;
  return res ;
 } catch ( RemoteException _e ) {
  Log.e( "ERR", _e.getMessage() ) ;
 } catch ( OperationApplicationException _e ) {
  Log.e( "ERR", _e.getMessage() ) ;
 }
 return null ;
}



おわりっす。

0 件のコメント:

コメントを投稿