blob: 34e6e8c222c9fee5c5648e94af354fc000ac544e [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
Xi Chena455bd52016-07-25 17:00:22 -070025import com.android.contacts.common.model.account.GoogleAccountType;
26
27import java.util.List;
28
Xi Chen4a7a9ea2016-07-20 11:23:30 -070029/**
30 * Utilities related to sync.
31 */
32public final class SyncUtil {
33 private static final String TAG = "SyncUtil";
34
yaolu491cca52016-09-30 14:04:40 -070035 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 Chen4a7a9ea2016-07-20 11:23:30 -070039 private SyncUtil() {
40 }
41
42 public static final boolean isSyncStatusPendingOrActive(Account account) {
Xi Chena455bd52016-07-25 17:00:22 -070043 if (account == null) {
44 return false;
45 }
Xi Chen4a7a9ea2016-07-20 11:23:30 -070046 return ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY)
47 || ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY);
48 }
Xi Chena455bd52016-07-25 17:00:22 -070049
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 }
yaolu491cca52016-09-30 14:04:40 -070059
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 }
yaolu395c3f82016-10-18 11:13:39 -070095
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 }
yaolu491cca52016-09-30 14:04:40 -0700102}