List of usage examples for android.content ContentResolver getSyncAutomatically
public static boolean getSyncAutomatically(Account account, String authority)
This method requires the caller to hold the permission android.Manifest.permission#READ_SYNC_SETTINGS .
From source file:com.chen.emailsync.SyncManager.java
/** * Convenience method to determine whether Email sync is enabled for a given account * @param account the Account in question * @return whether Email sync is enabled *///from w w w . j ava2 s . c om private static boolean canSyncEmail(android.accounts.Account account) { return ContentResolver.getSyncAutomatically(account, EmailContent.AUTHORITY); }
From source file:com.chen.emailsync.SyncManager.java
/** * Determine whether a mailbox of a given type in a given account can be synced automatically * by SyncServiceManager. 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 2s . com*/ * * @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) { // Outbox is always syncable return true; } else if (type == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) { // Always sync EAS mailbox unless master sync is off return ContentResolver.getMasterSyncAutomatically(); } 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(mAccountList.getAmAccount(account), 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 two 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 } else if (!canAutoSync(account) || !canSyncEmail(mAccountList.getAmAccount(account))) { return false; } return true; }
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); }//from w w w . j a v a 2 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); }/* w w w. j av a 2 s . c o m*/ } // 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.android.exchange.ExchangeService.java
/** * Convenience method to determine whether Email sync is enabled for a given account * @param account the Account in question * @return whether Email sync is enabled *//*from w ww . j av a 2 s . co m*/ private boolean canSyncEmail(android.accounts.Account account) { return ContentResolver.getSyncAutomatically(account, EmailContent.AUTHORITY); }
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)//from www . ja v a2 s . co 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; }