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 | |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 25 | import com.android.contacts.common.model.account.GoogleAccountType; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Utilities related to sync. |
| 31 | */ |
| 32 | public final class SyncUtil { |
| 33 | private static final String TAG = "SyncUtil"; |
| 34 | |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 35 | public static final int SYNC_SETTING_SYNC_ON = 0; |
| 36 | public static final int SYNC_SETTING_GLOBAL_SYNC_OFF = 1; |
| 37 | public static final int SYNC_SETTING_ACCOUNT_SYNC_OFF = 2; |
| 38 | |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 39 | private SyncUtil() { |
| 40 | } |
| 41 | |
| 42 | public static final boolean isSyncStatusPendingOrActive(Account account) { |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 43 | if (account == null) { |
| 44 | return false; |
| 45 | } |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 46 | return ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY) |
| 47 | || ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY); |
| 48 | } |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Returns true if the given Google account is not syncable. |
| 52 | */ |
| 53 | public static final boolean isUnsyncableGoogleAccount(Account account) { |
| 54 | if (account == null || !GoogleAccountType.ACCOUNT_TYPE.equals(account.type)) { |
| 55 | return false; |
| 56 | } |
| 57 | return ContentResolver.getIsSyncable(account, ContactsContract.AUTHORITY) <= 0; |
| 58 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 59 | |
| 60 | public static boolean isAlertVisible(Context context, Account account, int reason) { |
| 61 | if (reason == SYNC_SETTING_GLOBAL_SYNC_OFF) { |
| 62 | return (SharedPreferenceUtil.getNumOfDismissesForAutoSyncOff(context) == 0); |
| 63 | } else if (reason == SYNC_SETTING_ACCOUNT_SYNC_OFF && account != null) { |
| 64 | return (SharedPreferenceUtil.getNumOfDismissesforAccountSyncOff( |
| 65 | context, account.name) == 0); |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | public static int calculateReasonSyncOff(Context context, Account account) { |
| 71 | // Global sync is turned off |
| 72 | if (!ContentResolver.getMasterSyncAutomatically()) { |
| 73 | if (account != null) { |
| 74 | SharedPreferenceUtil.resetNumOfDismissesForAccountSyncOff( |
| 75 | context, account.name); |
| 76 | } |
| 77 | return SYNC_SETTING_GLOBAL_SYNC_OFF; |
| 78 | } |
| 79 | |
| 80 | // Global sync is on, clear the number of times users has dismissed this |
| 81 | // alert so that next time global sync is off, alert gets displayed again. |
| 82 | SharedPreferenceUtil.resetNumOfDismissesForAutoSyncOff(context); |
| 83 | if (account != null) { |
| 84 | // Account level sync is off |
| 85 | if (!ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) { |
| 86 | return SYNC_SETTING_ACCOUNT_SYNC_OFF; |
| 87 | } |
| 88 | // Account sync is on, clear the number of times users has dismissed this |
| 89 | // alert so that next time sync is off, alert gets displayed again. |
| 90 | SharedPreferenceUtil.resetNumOfDismissesForAccountSyncOff( |
| 91 | context, account.name); |
| 92 | } |
| 93 | return SYNC_SETTING_SYNC_ON; |
| 94 | } |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 95 | |
| 96 | public static boolean isNetworkConnected(Context context) { |
| 97 | ConnectivityManager cm = |
| 98 | (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 99 | NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); |
| 100 | return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); |
| 101 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 102 | } |