1// Copyright 2013 Google Inc. All Rights Reserved.
2
3package com.android.exchange;
4
5import android.accounts.Account;
6import android.accounts.AccountManager;
7import android.content.BroadcastReceiver;
8import android.content.ContentResolver;
9import android.content.Context;
10import android.content.Intent;
11import android.os.Bundle;
12import android.provider.CalendarContract;
13import android.provider.ContactsContract;
14
15import com.android.emailcommon.provider.EmailContent;
16import com.android.emailcommon.provider.Mailbox;
17import com.android.exchange.R.string;
18import com.android.mail.utils.LogUtils;
19
20public class ExchangeBroadcastReceiver extends BroadcastReceiver {
21
22    @Override
23    public void onReceive(final Context context, final Intent intent) {
24        final Account[] accounts = AccountManager.get(context)
25                .getAccountsByType(context.getString(string.account_manager_type_exchange));
26        LogUtils.i(Eas.LOG_TAG, "Accounts changed - requesting FolderSync for unsynced accounts");
27        for (final Account account : accounts) {
28            // Only do a sync for accounts that are not configured to sync any types, since the
29            // initial sync will do the right thing if at least one of those is enabled.
30            if (!ContentResolver.getSyncAutomatically(account, EmailContent.AUTHORITY) &&
31                    !ContentResolver.getSyncAutomatically(account, CalendarContract.AUTHORITY) &&
32                    !ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) {
33                final Bundle bundle = new Bundle(3);
34                bundle.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
35                bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
36                bundle.putBoolean(Mailbox.SYNC_EXTRA_ACCOUNT_ONLY, true);
37                ContentResolver.requestSync(account, EmailContent.AUTHORITY, bundle);
38            }
39        }
40    }
41}
42