List of usage examples for android.provider ContactsContract AUTHORITY
String AUTHORITY
To view the source code for android.provider ContactsContract AUTHORITY.
Click Source Link
From source file:org.skt.runtime.html5apis.contacts.ContactAccessorSdk5.java
/** * Creates a new contact and stores it in the database * //from ww w . j a v a2 s . c o m * @param contact the contact to be saved * @param account the account to be saved under */ private String createNewContact(JSONObject contact, String accountType, String accountName) { // Create a list of attributes to add to the contact database ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); //Add contact type ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build()); // Add name try { JSONObject name = contact.optJSONObject("name"); String displayName = contact.getString("displayName"); if (displayName != null || name != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix")) .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix")) .build()); } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get name object"); } //Add phone numbers JSONArray phones = null; try { phones = contact.getJSONArray("phoneNumbers"); if (phones != null) { for (int i = 0; i < phones.length(); i++) { JSONObject phone = (JSONObject) phones.get(i); insertPhone(ops, phone); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get phone numbers"); } // Add emails JSONArray emails = null; try { emails = contact.getJSONArray("emails"); if (emails != null) { for (int i = 0; i < emails.length(); i++) { JSONObject email = (JSONObject) emails.get(i); insertEmail(ops, email); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add addresses JSONArray addresses = null; try { addresses = contact.getJSONArray("addresses"); if (addresses != null) { for (int i = 0; i < addresses.length(); i++) { JSONObject address = (JSONObject) addresses.get(i); insertAddress(ops, address); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get addresses"); } // Add organizations JSONArray organizations = null; try { organizations = contact.getJSONArray("organizations"); if (organizations != null) { for (int i = 0; i < organizations.length(); i++) { JSONObject org = (JSONObject) organizations.get(i); insertOrganization(ops, org); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get organizations"); } // Add IMs JSONArray ims = null; try { ims = contact.getJSONArray("ims"); if (ims != null) { for (int i = 0; i < ims.length(); i++) { JSONObject im = (JSONObject) ims.get(i); insertIm(ops, im); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add note String note = getJsonString(contact, "note"); if (note != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build()); } // Add nickname String nickname = getJsonString(contact, "nickname"); if (nickname != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build()); } // Add urls JSONArray websites = null; try { websites = contact.getJSONArray("websites"); if (websites != null) { for (int i = 0; i < websites.length(); i++) { JSONObject website = (JSONObject) websites.get(i); insertWebsite(ops, website); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get websites"); } // Add birthday String birthday = getJsonString(contact, "birthday"); if (birthday != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build()); } // Add photos JSONArray photos = null; try { photos = contact.getJSONArray("photos"); if (photos != null) { for (int i = 0; i < photos.length(); i++) { JSONObject photo = (JSONObject) photos.get(i); insertPhoto(ops, photo); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get photos"); } String newId = null; //Add contact try { ContentProviderResult[] cpResults = mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); if (cpResults.length >= 0) { newId = cpResults[0].uri.getLastPathSegment(); } } catch (RemoteException e) { Log.e(LOG_TAG, e.getMessage(), e); } catch (OperationApplicationException e) { Log.e(LOG_TAG, e.getMessage(), e); } return newId; }
From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java
/** * Creates a new contact and stores it in the database * //from w ww . j av a 2 s .c o m * @param contact * the contact to be saved * @param account * the account to be saved under */ private String createNewContact(JSONObject contact, String accountType, String accountName) { // Create a list of attributes to add to the contact database ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); // Add contact type ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build()); // Add name try { JSONObject name = contact.optJSONObject("name"); String displayName = contact.getString("displayName"); if (displayName != null || name != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix")) .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix")) .build()); } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get name object"); } // Add phone numbers JSONArray phones = null; try { phones = contact.getJSONArray("phoneNumbers"); if (phones != null) { for (int i = 0; i < phones.length(); i++) { JSONObject phone = (JSONObject) phones.get(i); insertPhone(ops, phone); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get phone numbers"); } // Add emails JSONArray emails = null; try { emails = contact.getJSONArray("emails"); if (emails != null) { for (int i = 0; i < emails.length(); i++) { JSONObject email = (JSONObject) emails.get(i); insertEmail(ops, email); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add addresses JSONArray addresses = null; try { addresses = contact.getJSONArray("addresses"); if (addresses != null) { for (int i = 0; i < addresses.length(); i++) { JSONObject address = (JSONObject) addresses.get(i); insertAddress(ops, address); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get addresses"); } // Add organizations JSONArray organizations = null; try { organizations = contact.getJSONArray("organizations"); if (organizations != null) { for (int i = 0; i < organizations.length(); i++) { JSONObject org = (JSONObject) organizations.get(i); insertOrganization(ops, org); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get organizations"); } // Add IMs JSONArray ims = null; try { ims = contact.getJSONArray("ims"); if (ims != null) { for (int i = 0; i < ims.length(); i++) { JSONObject im = (JSONObject) ims.get(i); insertIm(ops, im); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add note String note = getJsonString(contact, "note"); if (note != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build()); } // Add nickname String nickname = getJsonString(contact, "nickname"); if (nickname != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build()); } // Add urls JSONArray websites = null; try { websites = contact.getJSONArray("urls"); if (websites != null) { for (int i = 0; i < websites.length(); i++) { JSONObject website = (JSONObject) websites.get(i); insertWebsite(ops, website); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get websites"); } // Add birthday String birthday = getJsonString(contact, "birthday"); if (birthday != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build()); } // Add photos JSONArray photos = null; try { photos = contact.getJSONArray("photos"); if (photos != null) { for (int i = 0; i < photos.length(); i++) { JSONObject photo = (JSONObject) photos.get(i); insertPhoto(ops, photo); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get photos"); } String newId = null; // Add contact try { ContentProviderResult[] cpResults = getContext().getContentResolver() .applyBatch(ContactsContract.AUTHORITY, ops); if (cpResults.length >= 0) { newId = cpResults[0].uri.getLastPathSegment(); } } catch (RemoteException e) { Log.e(LOG_TAG, e.getMessage(), e); } catch (OperationApplicationException e) { Log.e(LOG_TAG, e.getMessage(), e); } return newId; }
From source file:com.mwebster.exchange.SyncManager.java
private long checkMailboxes() { // First, see if any running mailboxes have been deleted ArrayList<Long> deletedMailboxes = new ArrayList<Long>(); synchronized (sSyncLock) { for (long mailboxId : mServiceMap.keySet()) { Mailbox m = Mailbox.restoreMailboxWithId(this, mailboxId); if (m == null) { deletedMailboxes.add(mailboxId); }// w w w . java2 s .c om } // If so, stop them or remove them from the map for (Long mailboxId : deletedMailboxes) { AbstractSyncService svc = mServiceMap.get(mailboxId); if (svc == null || svc.mThread == null) { releaseMailbox(mailboxId); continue; } else { boolean alive = svc.mThread.isAlive(); log("Deleted mailbox: " + svc.mMailboxName); if (alive) { stopManualSync(mailboxId); } else { log("Removing from serviceMap"); releaseMailbox(mailboxId); } } } } long nextWait = SYNC_MANAGER_HEARTBEAT_TIME; long now = System.currentTimeMillis(); // Start up threads that need it; use a query which finds eas mailboxes where the // the sync interval is not "never". This is the set of mailboxes that we control if (mAccountObserver == null) { log("mAccountObserver null; service died??"); return nextWait; } Cursor c = getContentResolver().query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION, mAccountObserver.getSyncableEasMailboxWhere(), null, null); // Contacts/Calendar obey this setting from ContentResolver // Mail is on its own schedule boolean masterAutoSync = ContentResolver.getMasterSyncAutomatically(); try { while (c.moveToNext()) { long mid = c.getLong(Mailbox.CONTENT_ID_COLUMN); AbstractSyncService service = null; synchronized (sSyncLock) { service = mServiceMap.get(mid); } if (service == null) { // We handle a few types of mailboxes specially int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN); // If background data is off, we only sync Outbox // Manual syncs are initiated elsewhere, so they will continue to be respected if (!mBackgroundData && type != Mailbox.TYPE_OUTBOX) { continue; } if (type == Mailbox.TYPE_CONTACTS || type == Mailbox.TYPE_CALENDAR) { // We don't sync these automatically if master auto sync is off if (!masterAutoSync) { continue; } // Get the right authority for the mailbox String authority; Account account = getAccountById(c.getInt(Mailbox.CONTENT_ACCOUNT_KEY_COLUMN)); if (account != null) { if (type == Mailbox.TYPE_CONTACTS) { authority = ContactsContract.AUTHORITY; } else { authority = Calendar.AUTHORITY; if (!mCalendarObservers.containsKey(account.mId)) { // Make sure we have an observer for this Calendar, as // we need to be able to detect sync state changes, sigh registerCalendarObserver(account); } } android.accounts.Account a = new android.accounts.Account(account.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE); // See if "sync automatically" is set; if not, punt if (!ContentResolver.getSyncAutomatically(a, authority)) { continue; // See if the calendar is enabled; if not, punt } else if ((type == Mailbox.TYPE_CALENDAR) && !isCalendarEnabled(account.mId)) { continue; } } } else if (type == Mailbox.TYPE_TRASH) { continue; } // Check whether we're in a hold (temporary or permanent) SyncError syncError = mSyncErrorMap.get(mid); if (syncError != null) { // Nothing we can do about fatal errors if (syncError.fatal) continue; if (now < syncError.holdEndTime) { // If release time is earlier than next wait time, // move next wait time up to the release time if (syncError.holdEndTime < now + nextWait) { nextWait = syncError.holdEndTime - now; mNextWaitReason = "Release hold"; } continue; } else { // Keep the error around, but clear the end time syncError.holdEndTime = 0; } } // Otherwise, we use the sync interval long interval = c.getInt(Mailbox.CONTENT_SYNC_INTERVAL_COLUMN); if (interval == Mailbox.CHECK_INTERVAL_PUSH) { Mailbox m = EmailContent.getContent(c, Mailbox.class); requestSync(m, SYNC_PUSH, null); } else if (type == Mailbox.TYPE_OUTBOX) { int cnt = EmailContent.count(this, Message.CONTENT_URI, EasOutboxService.MAILBOX_KEY_AND_NOT_SEND_FAILED, new String[] { Long.toString(mid) }); if (cnt > 0) { Mailbox m = EmailContent.getContent(c, Mailbox.class); startServiceThread(new EasOutboxService(this, m), m); } } else if (interval > 0 && interval <= ONE_DAY_MINUTES) { long lastSync = c.getLong(Mailbox.CONTENT_SYNC_TIME_COLUMN); long sinceLastSync = now - lastSync; if (sinceLastSync < 0) { log("WHOA! lastSync in the future for mailbox: " + mid); sinceLastSync = interval * MINUTES; } long toNextSync = interval * MINUTES - sinceLastSync; String name = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN); if (toNextSync <= 0) { Mailbox m = EmailContent.getContent(c, Mailbox.class); requestSync(m, SYNC_SCHEDULED, null); } else if (toNextSync < nextWait) { nextWait = toNextSync; if (Eas.USER_LOG) { log("Next sync for " + name + " in " + nextWait / 1000 + "s"); } mNextWaitReason = "Scheduled sync, " + name; } else if (Eas.USER_LOG) { log("Next sync for " + name + " in " + toNextSync / 1000 + "s"); } } } else { Thread thread = service.mThread; // Look for threads that have died and remove them from the map if (thread != null && !thread.isAlive()) { if (Eas.USER_LOG) { log("Dead thread, mailbox released: " + c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN)); } releaseMailbox(mid); // Restart this if necessary if (nextWait > 3 * SECONDS) { nextWait = 3 * SECONDS; mNextWaitReason = "Clean up dead thread(s)"; } } else { long requestTime = service.mRequestTime; if (requestTime > 0) { long timeToRequest = requestTime - now; if (service instanceof AbstractSyncService && timeToRequest <= 0) { service.mRequestTime = 0; service.alarm(); } else if (requestTime > 0 && timeToRequest < nextWait) { if (timeToRequest < 11 * MINUTES) { nextWait = timeToRequest < 250 ? 250 : timeToRequest; mNextWaitReason = "Sync data change"; } else { log("Illegal timeToRequest: " + timeToRequest); } } } } } } } finally { c.close(); } return nextWait; }
From source file:com.android.exchange.SyncManager.java
private long checkMailboxes() { // First, see if any running mailboxes have been deleted ArrayList<Long> deletedMailboxes = new ArrayList<Long>(); synchronized (sSyncLock) { for (long mailboxId : mServiceMap.keySet()) { Mailbox m = Mailbox.restoreMailboxWithId(this, mailboxId); if (m == null) { deletedMailboxes.add(mailboxId); }//from w w w . j a v a2 s. c om } // If so, stop them or remove them from the map for (Long mailboxId : deletedMailboxes) { AbstractSyncService svc = mServiceMap.get(mailboxId); if (svc == null || svc.mThread == null) { releaseMailbox(mailboxId); continue; } else { boolean alive = svc.mThread.isAlive(); log("Deleted mailbox: " + svc.mMailboxName); if (alive) { stopManualSync(mailboxId); } else { log("Removing from serviceMap"); releaseMailbox(mailboxId); } } } } long nextWait = SYNC_MANAGER_HEARTBEAT_TIME; long now = System.currentTimeMillis(); // Start up threads that need it; use a query which finds eas mailboxes where the // the sync interval is not "never". This is the set of mailboxes that we control if (mAccountObserver == null) { log("mAccountObserver null; service died??"); return nextWait; } Cursor c = getContentResolver().query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION, mAccountObserver.getSyncableEasMailboxWhere(), null, null); // Contacts/Calendar obey this setting from ContentResolver // Mail is on its own schedule boolean masterAutoSync = ContentResolver.getMasterSyncAutomatically(); try { while (c.moveToNext()) { long mid = c.getLong(Mailbox.CONTENT_ID_COLUMN); AbstractSyncService service = null; synchronized (sSyncLock) { service = mServiceMap.get(mid); } if (service == null) { // We handle a few types of mailboxes specially int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN); // If background data is off, we only sync Outbox // Manual syncs are initiated elsewhere, so they will continue to be respected if (!mBackgroundData && type != Mailbox.TYPE_OUTBOX) { continue; } if (type == Mailbox.TYPE_CONTACTS || type == Mailbox.TYPE_CALENDAR) { // We don't sync these automatically if master auto sync is off if (!masterAutoSync) { continue; } // Get the right authority for the mailbox String authority; Account account = getAccountById(c.getInt(Mailbox.CONTENT_ACCOUNT_KEY_COLUMN)); if (account != null) { if (type == Mailbox.TYPE_CONTACTS) { authority = ContactsContract.AUTHORITY; } else { authority = Calendar.AUTHORITY; if (!mCalendarObservers.containsKey(account.mId)) { // Make sure we have an observer for this Calendar, as // we need to be able to detect sync state changes, sigh registerCalendarObserver(account); } } android.accounts.Account a = new android.accounts.Account(account.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE); // See if "sync automatically" is set; if not, punt if (!ContentResolver.getSyncAutomatically(a, authority)) { continue; // See if the calendar is enabled; if not, punt } else if ((type == Mailbox.TYPE_CALENDAR) && !isCalendarEnabled(account.mId)) { continue; } } } else if (type == Mailbox.TYPE_TRASH) { continue; } // Check whether we're in a hold (temporary or permanent) SyncError syncError = mSyncErrorMap.get(mid); if (syncError != null) { // Nothing we can do about fatal errors if (syncError.fatal) continue; if (now < syncError.holdEndTime) { // If release time is earlier than next wait time, // move next wait time up to the release time if (syncError.holdEndTime < now + nextWait) { nextWait = syncError.holdEndTime - now; mNextWaitReason = "Release hold"; } continue; } else { // Keep the error around, but clear the end time syncError.holdEndTime = 0; } } // Otherwise, we use the sync interval long interval = c.getInt(Mailbox.CONTENT_SYNC_INTERVAL_COLUMN); if (interval == Mailbox.CHECK_INTERVAL_PUSH) { Mailbox m = EmailContent.getContent(c, Mailbox.class); requestSync(m, SYNC_PUSH, null); } else if (type == Mailbox.TYPE_OUTBOX) { int cnt = EmailContent.count(this, Message.CONTENT_URI, EasOutboxService.MAILBOX_KEY_AND_NOT_SEND_FAILED, new String[] { Long.toString(mid) }); if (cnt > 0) { Mailbox m = EmailContent.getContent(c, Mailbox.class); startServiceThread(new EasOutboxService(this, m), m); } } else if (interval > 0 && interval <= ONE_DAY_MINUTES) { long lastSync = c.getLong(Mailbox.CONTENT_SYNC_TIME_COLUMN); long sinceLastSync = now - lastSync; if (sinceLastSync < 0) { log("WHOA! lastSync in the future for mailbox: " + mid); sinceLastSync = interval * MINUTES; } long toNextSync = interval * MINUTES - sinceLastSync; String name = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN); if (toNextSync <= 0) { Mailbox m = EmailContent.getContent(c, Mailbox.class); requestSync(m, SYNC_SCHEDULED, null); } else if (toNextSync < nextWait) { nextWait = toNextSync; if (Eas.USER_LOG) { log("Next sync for " + name + " in " + nextWait / 1000 + "s"); } mNextWaitReason = "Scheduled sync, " + name; } else if (Eas.USER_LOG) { log("Next sync for " + name + " in " + toNextSync / 1000 + "s"); } } } else { Thread thread = service.mThread; // Look for threads that have died and remove them from the map if (thread != null && !thread.isAlive()) { if (Eas.USER_LOG) { log("Dead thread, mailbox released: " + c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN)); } releaseMailbox(mid); // Restart this if necessary if (nextWait > 3 * SECONDS) { nextWait = 3 * SECONDS; mNextWaitReason = "Clean up dead thread(s)"; } } else { long requestTime = service.mRequestTime; if (requestTime > 0) { long timeToRequest = requestTime - now; if (timeToRequest <= 0) { service.mRequestTime = 0; service.alarm(); } else if (requestTime > 0 && timeToRequest < nextWait) { if (timeToRequest < 11 * MINUTES) { nextWait = timeToRequest < 250 ? 250 : timeToRequest; mNextWaitReason = "Sync data change"; } else { log("Illegal timeToRequest: " + timeToRequest); } } } } } } } finally { c.close(); } return nextWait; }
From source file:com.remobile.contacts.ContactAccessorSdk5.java
/** * Creates a new contact and stores it in the database * * @param contact the contact to be saved * @param account the account to be saved under *///from w w w . j av a2s . co m private String createNewContact(JSONObject contact, String accountType, String accountName) { // Create a list of attributes to add to the contact database ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); //Add contact type ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build()); // Add name try { JSONObject name = contact.optJSONObject("name"); String displayName = contact.getString("displayName"); if (displayName != null || name != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName")) .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix")) .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix")) .build()); } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get name object"); } //Add phone numbers JSONArray phones = null; try { phones = contact.getJSONArray("phoneNumbers"); if (phones != null) { for (int i = 0; i < phones.length(); i++) { JSONObject phone = (JSONObject) phones.get(i); insertPhone(ops, phone); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get phone numbers"); } // Add emails JSONArray emails = null; try { emails = contact.getJSONArray("emails"); if (emails != null) { for (int i = 0; i < emails.length(); i++) { JSONObject email = (JSONObject) emails.get(i); insertEmail(ops, email); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add addresses JSONArray addresses = null; try { addresses = contact.getJSONArray("addresses"); if (addresses != null) { for (int i = 0; i < addresses.length(); i++) { JSONObject address = (JSONObject) addresses.get(i); insertAddress(ops, address); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get addresses"); } // Add organizations JSONArray organizations = null; try { organizations = contact.getJSONArray("organizations"); if (organizations != null) { for (int i = 0; i < organizations.length(); i++) { JSONObject org = (JSONObject) organizations.get(i); insertOrganization(ops, org); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get organizations"); } // Add IMs JSONArray ims = null; try { ims = contact.getJSONArray("ims"); if (ims != null) { for (int i = 0; i < ims.length(); i++) { JSONObject im = (JSONObject) ims.get(i); insertIm(ops, im); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get emails"); } // Add note String note = getJsonString(contact, "note"); if (note != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build()); } // Add nickname String nickname = getJsonString(contact, "nickname"); if (nickname != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build()); } // Add urls JSONArray websites = null; try { websites = contact.getJSONArray("urls"); if (websites != null) { for (int i = 0; i < websites.length(); i++) { JSONObject website = (JSONObject) websites.get(i); insertWebsite(ops, website); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get websites"); } // Add birthday String birthday = getJsonString(contact, "birthday"); if (birthday != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build()); } // Add photos JSONArray photos = null; try { photos = contact.getJSONArray("photos"); if (photos != null) { for (int i = 0; i < photos.length(); i++) { JSONObject photo = (JSONObject) photos.get(i); insertPhoto(ops, photo); } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not get photos"); } String newId = null; //Add contact try { ContentProviderResult[] cpResults = mApp.getActivity().getContentResolver() .applyBatch(ContactsContract.AUTHORITY, ops); if (cpResults.length >= 0) { newId = cpResults[0].uri.getLastPathSegment(); } } catch (RemoteException e) { Log.e(LOG_TAG, e.getMessage(), e); } catch (OperationApplicationException e) { Log.e(LOG_TAG, e.getMessage(), e); } return newId; }
From source file:com.android.exchange.ExchangeService.java
/** * Determine whether a mailbox of a given type in a given account can be synced automatically * by ExchangeService. This is an increasingly complex determination, taking into account * security policies and user settings (both within the Email application and in the Settings * application)// w w w. j a v a 2 s. c o m * * @param account the Account that the mailbox is in * @param type the type of the Mailbox * @return whether or not to start a sync */ private boolean isMailboxSyncable(Account account, int type) { // This 'if' statement performs checks to see whether or not a mailbox is a // candidate for syncing based on policies, user settings, & other restrictions if (type == Mailbox.TYPE_OUTBOX || type == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) { // Outbox and account mailbox are always syncable return true; } else if (type == Mailbox.TYPE_CONTACTS || type == Mailbox.TYPE_CALENDAR) { // Contacts/Calendar obey this setting from ContentResolver if (!ContentResolver.getMasterSyncAutomatically()) { return false; } // Get the right authority for the mailbox String authority; if (type == Mailbox.TYPE_CONTACTS) { authority = ContactsContract.AUTHORITY; } else { authority = CalendarContract.AUTHORITY; if (!mCalendarObservers.containsKey(account.mId)) { // Make sure we have an observer for this Calendar, as // we need to be able to detect sync state changes, sigh registerCalendarObserver(account); } } // See if "sync automatically" is set; if not, punt if (!ContentResolver.getSyncAutomatically(account.mAmAccount, authority)) { return false; // See if the calendar is enabled from the Calendar app UI; if not, punt } else if ((type == Mailbox.TYPE_CALENDAR) && !isCalendarEnabled(account.mId)) { return false; } // Never automatically sync trash } else if (type == Mailbox.TYPE_TRASH) { return false; // For non-outbox, non-account mail, we do three checks: // 1) are we restricted by policy (i.e. manual sync only), // 2) has the user checked the "Sync Email" box in Account Settings, and // 3) does the user have the master "background data" box checked in Settings } else if (!canAutoSync(account) || !canSyncEmail(account.mAmAccount) || !mBackgroundData) { return false; } return true; }