58privatestaticfinalint TIMEOUT =5000;59privatestaticfinalint INNER_LOOP =5;60privatestaticfinalint EXPECTED_FRAMES =100;61privatestaticfinalString PACKAGE_NAME ="com.google.android.dialer";62privatestaticfinalString RES_PACKAGE_NAME ="com.android.dialer";63privatestaticfinalString RES_PACKAGE_NAME2 ="com.android.contacts";64privatestaticfinalString RES_PACKAGE_NAME3 ="android";65privatestaticfinalString APP_NAME ="Phone";66privatestaticfinalString CONTACT_NAME ="A AAA Test Account";67privatestaticfinalString CONTACT_NUMBER ="2468";69staticfinalint PICK_CONTACT_REQUEST =1;162// Method to insert a new contact163publicvoidinsertNewContacts()throwsOperationApplicationException,RemoteException{
164ArrayList<ContentProviderOperation> ops =newArrayList<ContentProviderOperation>();165int rawContactID = ops.size();166// to insert a new raw contact in the table ContactsContract.RawContacts167 ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)168.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,"Test")169.withValue(RawContacts.ACCOUNT_NAME, CONTACT_NAME)170.build());171172// to insert display name in the table ContactsContract.Data173 ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)174.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)175.withValue(ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)176.withValue(StructuredName.DISPLAY_NAME, CONTACT_NAME)177.build());178179// to insert Mobile Number in the table ContactsContract.Data180 ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)181.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)182.withValue(ContactsContract.Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)183.withValue(Phone.NUMBER, CONTACT_NUMBER)184.withValue(Phone.TYPE,CommonDataKinds.Phone.TYPE_MOBILE)185.build());186187// Executing all the insert operations as a single database transaction188getInstrumentation().getContext().getContentResolver()189.applyBatch(ContactsContract.AUTHORITY, ops);190}191192// Checks whether certain contact exists or not193publicbooleandoesContactExist(){
194Uri uri =Uri.withAppendedPath(195ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(CONTACT_NUMBER));196Cursor contactLookup =getInstrumentation().getContext().getContentResolver().query(197 uri,newString[]{
198BaseColumns._ID,199ContactsContract.PhoneLookup.DISPLAY_NAME },200null,201null,202null);203boolean found =false;204try{
205if(contactLookup !=null&& contactLookup.getCount()>0){
206 contactLookup.moveToNext();207if(contactLookup.getString(contactLookup.getColumnIndex(208ContactsContract.Data.DISPLAY_NAME)).equals(CONTACT_NAME))209 found =true;210}211}finally{
212if(contactLookup !=null){
213 contactLookup.close();214}215}216217return found;218}219220// Inserts a new entry in the call log221publicvoidaddNumToCalLog(String number){
222ContentValues values =newContentValues();223 values.put(CallLog.Calls.NUMBER, number);224 values.put(CallLog.Calls.DATE,System.currentTimeMillis());225 values.put(CallLog.Calls.DURATION,0);226 values.put(CallLog.Calls.TYPE,CallLog.Calls.OUTGOING_TYPE);227 values.put(CallLog.Calls.NEW,1);228 values.put(CallLog.Calls.CACHED_NAME,"");229 values.put(CallLog.Calls.CACHED_NUMBER_TYPE,0);230 values.put(CallLog.Calls.CACHED_NUMBER_LABEL,"");231getInstrumentation().getContext().getContentResolver()232.insert(CallLog.Calls.CONTENT_URI, values);233}234235// Gets call log count236publicintgetCallLogCount(){
237Cursor cursor =getInstrumentation().getContext().getContentResolver()238.query(CallLog.Calls.CONTENT_URI,null,null,null,null);239return cursor.getCount();240}241242// Generates a random phone number243publicStringgetRandomPhoneNumber(){
244Random rand =newRandom();245int num1 =(rand.nextInt(7)+1)*100+(rand.nextInt(8)*10)+ rand.nextInt(8);246int num2 = rand.nextInt(743);247int num3 = rand.nextInt(10000);248249returnString.format("%03d-%03d-%04d", num1, num2, num3);250}
31// Clear call log and ensure there are no outgoing calls32Context context =getInstrumentation().getContext();33ContentResolver resolver = context.getContentResolver();34 resolver.delete(CallLog.Calls.CONTENT_URI,null,null);// Add a single call and verify it returns as last outgoing call53ContentValues values =newContentValues();54 values.put(CallLog.Calls.NUMBER, TEST_NUMBER);55 values.put(CallLog.Calls.TYPE,Integer.valueOf(CallLog.Calls.OUTGOING_TYPE));56 values.put(CallLog.Calls.DATE,Long.valueOf(0/*start time*/));57 values.put(CallLog.Calls.DURATION,Long.valueOf(5/*call duration*/));5859 resolver.insert(CallLog.Calls.CONTENT_URI, values);