Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.contacts.util; |
| 17 | |
| 18 | import android.accounts.Account; |
| 19 | import android.content.ContentResolver; |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 20 | import android.content.Context; |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 21 | import android.net.ConnectivityManager; |
| 22 | import android.net.NetworkInfo; |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract; |
| 24 | |
Marcus Hagerott | 396aab7 | 2016-12-12 12:00:21 -0800 | [diff] [blame] | 25 | import com.android.contacts.model.account.AccountWithDataSet; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 26 | import com.android.contacts.model.account.GoogleAccountType; |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 27 | |
Marcus Hagerott | 396aab7 | 2016-12-12 12:00:21 -0800 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Utilities related to sync. |
| 32 | */ |
| 33 | public final class SyncUtil { |
| 34 | private static final String TAG = "SyncUtil"; |
| 35 | |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 36 | public static final int SYNC_SETTING_SYNC_ON = 0; |
| 37 | public static final int SYNC_SETTING_GLOBAL_SYNC_OFF = 1; |
| 38 | public static final int SYNC_SETTING_ACCOUNT_SYNC_OFF = 2; |
| 39 | |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 40 | private SyncUtil() { |
| 41 | } |
| 42 | |
| 43 | public static final boolean isSyncStatusPendingOrActive(Account account) { |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 44 | if (account == null) { |
| 45 | return false; |
| 46 | } |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 47 | return ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY) |
| 48 | || ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY); |
| 49 | } |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Marcus Hagerott | 396aab7 | 2016-12-12 12:00:21 -0800 | [diff] [blame] | 52 | * Returns true {@link ContentResolver#isSyncPending(Account, String)} or |
| 53 | * {@link ContentResolver#isSyncActive(Account, String)} is true for any account in accounts |
| 54 | */ |
| 55 | public static final boolean isAnySyncing(List<AccountWithDataSet> accounts) { |
| 56 | for (AccountWithDataSet accountWithDataSet : accounts) { |
| 57 | if (isSyncStatusPendingOrActive(accountWithDataSet.getAccountOrNull())) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /** |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 65 | * Returns true if the given Google account is not syncable. |
| 66 | */ |
| 67 | public static final boolean isUnsyncableGoogleAccount(Account account) { |
| 68 | if (account == null || !GoogleAccountType.ACCOUNT_TYPE.equals(account.type)) { |
| 69 | return false; |
| 70 | } |
| 71 | return ContentResolver.getIsSyncable(account, ContactsContract.AUTHORITY) <= 0; |
| 72 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 73 | |
| 74 | public static boolean isAlertVisible(Context context, Account account, int reason) { |
| 75 | if (reason == SYNC_SETTING_GLOBAL_SYNC_OFF) { |
| 76 | return (SharedPreferenceUtil.getNumOfDismissesForAutoSyncOff(context) == 0); |
| 77 | } else if (reason == SYNC_SETTING_ACCOUNT_SYNC_OFF && account != null) { |
| 78 | return (SharedPreferenceUtil.getNumOfDismissesforAccountSyncOff( |
| 79 | context, account.name) == 0); |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | public static int calculateReasonSyncOff(Context context, Account account) { |
| 85 | // Global sync is turned off |
| 86 | if (!ContentResolver.getMasterSyncAutomatically()) { |
| 87 | if (account != null) { |
| 88 | SharedPreferenceUtil.resetNumOfDismissesForAccountSyncOff( |
| 89 | context, account.name); |
| 90 | } |
| 91 | return SYNC_SETTING_GLOBAL_SYNC_OFF; |
| 92 | } |
| 93 | |
| 94 | // Global sync is on, clear the number of times users has dismissed this |
| 95 | // alert so that next time global sync is off, alert gets displayed again. |
| 96 | SharedPreferenceUtil.resetNumOfDismissesForAutoSyncOff(context); |
| 97 | if (account != null) { |
| 98 | // Account level sync is off |
| 99 | if (!ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) { |
| 100 | return SYNC_SETTING_ACCOUNT_SYNC_OFF; |
| 101 | } |
| 102 | // Account sync is on, clear the number of times users has dismissed this |
| 103 | // alert so that next time sync is off, alert gets displayed again. |
| 104 | SharedPreferenceUtil.resetNumOfDismissesForAccountSyncOff( |
| 105 | context, account.name); |
| 106 | } |
| 107 | return SYNC_SETTING_SYNC_ON; |
| 108 | } |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 109 | |
| 110 | public static boolean isNetworkConnected(Context context) { |
| 111 | ConnectivityManager cm = |
| 112 | (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 113 | NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); |
| 114 | return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); |
| 115 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 116 | } |