blob: ce10937f57ae695a7992efb4ef83e8a0ef46f013 [file] [log] [blame]
Xi Chen4a7a9ea2016-07-20 11:23:30 -07001/*
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 */
16package com.android.contacts.util;
17
18import android.accounts.Account;
19import android.content.ContentResolver;
yaolu491cca52016-09-30 14:04:40 -070020import android.content.Context;
yaolu395c3f82016-10-18 11:13:39 -070021import android.net.ConnectivityManager;
22import android.net.NetworkInfo;
Xi Chen4a7a9ea2016-07-20 11:23:30 -070023import android.provider.ContactsContract;
24
Marcus Hagerott396aab72016-12-12 12:00:21 -080025import com.android.contacts.model.account.AccountWithDataSet;
Gary Mai69c182a2016-12-05 13:07:03 -080026import com.android.contacts.model.account.GoogleAccountType;
Xi Chena455bd52016-07-25 17:00:22 -070027
Marcus Hagerott396aab72016-12-12 12:00:21 -080028import java.util.List;
29
Xi Chen4a7a9ea2016-07-20 11:23:30 -070030/**
31 * Utilities related to sync.
32 */
33public final class SyncUtil {
34 private static final String TAG = "SyncUtil";
35
yaolu491cca52016-09-30 14:04:40 -070036 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 Chen4a7a9ea2016-07-20 11:23:30 -070040 private SyncUtil() {
41 }
42
43 public static final boolean isSyncStatusPendingOrActive(Account account) {
Xi Chena455bd52016-07-25 17:00:22 -070044 if (account == null) {
45 return false;
46 }
Xi Chen4a7a9ea2016-07-20 11:23:30 -070047 return ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY)
48 || ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY);
49 }
Xi Chena455bd52016-07-25 17:00:22 -070050
51 /**
Marcus Hagerott396aab72016-12-12 12:00:21 -080052 * 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 Chena455bd52016-07-25 17:00:22 -070065 * 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 }
yaolu491cca52016-09-30 14:04:40 -070073
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 }
yaolu395c3f82016-10-18 11:13:39 -0700109
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 }
yaolu491cca52016-09-30 14:04:40 -0700116}