SyncUtil.java revision 395c3f8bafa89578d4559cf108eb668040e5ac93
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 */ 16package com.android.contacts.util; 17 18import android.accounts.Account; 19import android.content.ContentResolver; 20import android.content.Context; 21import android.net.ConnectivityManager; 22import android.net.NetworkInfo; 23import android.provider.ContactsContract; 24 25import com.android.contacts.common.model.account.GoogleAccountType; 26 27import java.util.List; 28 29/** 30 * Utilities related to sync. 31 */ 32public final class SyncUtil { 33 private static final String TAG = "SyncUtil"; 34 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 39 private SyncUtil() { 40 } 41 42 public static final boolean isSyncStatusPendingOrActive(Account account) { 43 if (account == null) { 44 return false; 45 } 46 return ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY) 47 || ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY); 48 } 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 } 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 } 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 } 102}