创建联系人及通话记录

58    private static final int TIMEOUT = 5000;
59    private static final int INNER_LOOP = 5;
60    private static final int EXPECTED_FRAMES = 100;
61    private static final String PACKAGE_NAME = "com.google.android.dialer";
62    private static final String RES_PACKAGE_NAME = "com.android.dialer";
63    private static final String RES_PACKAGE_NAME2 = "com.android.contacts";
64    private static final String RES_PACKAGE_NAME3 = "android";
65    private static final String APP_NAME = "Phone";
66    private static final String CONTACT_NAME = "A AAA Test Account";
67    private static final String CONTACT_NUMBER = "2468";
69    static final int PICK_CONTACT_REQUEST = 1;

162    // Method to insert a new contact
163    public void insertNewContacts() throws OperationApplicationException, RemoteException {
164        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
165        int rawContactID = ops.size();
166        // to insert a new raw contact in the table ContactsContract.RawContacts
167        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
168                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Test")
169                .withValue(RawContacts.ACCOUNT_NAME, CONTACT_NAME)
170                .build());
171
172        // to insert display name in the table ContactsContract.Data
173        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());
178
179        // to insert Mobile Number in the table ContactsContract.Data
180        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());
186
187            // Executing all the insert operations as a single database transaction
188        getInstrumentation().getContext().getContentResolver()
189                .applyBatch(ContactsContract.AUTHORITY, ops);
190    }
191
192    // Checks whether certain contact exists or not
193    public boolean doesContactExist() {
194        Uri uri = Uri.withAppendedPath(
195                ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(CONTACT_NUMBER));
196        Cursor contactLookup = getInstrumentation().getContext().getContentResolver().query(
197                uri, new String[] {
198                        BaseColumns._ID,
199                        ContactsContract.PhoneLookup.DISPLAY_NAME },
200                        null,
201                        null,
202                        null);
203        boolean found = false;
204        try {
205            if (contactLookup != null && contactLookup.getCount() > 0) {
206                contactLookup.moveToNext();
207                if (contactLookup.getString(contactLookup.getColumnIndex(
208                            ContactsContract.Data.DISPLAY_NAME)).equals(CONTACT_NAME))
209                    found = true;
210            }
211        } finally {
212            if (contactLookup != null) {
213                contactLookup.close();
214            }
215        }
216
217        return found;
218    }
219
220    // Inserts a new entry in the call log
221    public void addNumToCalLog(String number){
222        ContentValues values = new ContentValues();
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, "");
231        getInstrumentation().getContext().getContentResolver()
232                .insert(CallLog.Calls.CONTENT_URI, values);
233    }
234
235    // Gets call log count
236    public int getCallLogCount() {
237       Cursor cursor = getInstrumentation().getContext().getContentResolver()
238               .query(CallLog.Calls.CONTENT_URI, null, null, null, null);
239       return cursor.getCount();
240    }
241
242    // Generates a random phone number
243    public String getRandomPhoneNumber() {
244        Random rand = new Random();
245        int num1 = (rand.nextInt(7) + 1) * 100 + (rand.nextInt(8) * 10) + rand.nextInt(8);
246        int num2 = rand.nextInt(743);
247        int num3 = rand.nextInt(10000);
248
249        return String.format("%03d-%03d-%04d", num1, num2, num3);
250    }

31        // Clear call log and ensure there are no outgoing calls
32        Context context = getInstrumentation().getContext();
33        ContentResolver resolver = context.getContentResolver();
34        resolver.delete(CallLog.Calls.CONTENT_URI, null, null);

       // Add a single call and verify it returns as last outgoing call
53        ContentValues values = new ContentValues();
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*/));
58
59        resolver.insert(CallLog.Calls.CONTENT_URI, values);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值