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.model; 17 18 import android.accounts.Account; 19 import android.accounts.AccountManager; 20 import android.content.Context; 21 import android.provider.ContactsContract; 22 23 import com.android.contacts.model.account.AccountWithDataSet; 24 import com.android.contacts.model.account.GoogleAccountType; 25 26 import java.util.Collections; 27 import java.util.List; 28 29 /** 30 * Attempts to detect accounts for device contacts 31 */ 32 public final class DeviceLocalAccountLocator { 33 34 private final Context mContext; 35 private final AccountManager mAccountManager; 36 private final List<AccountWithDataSet> mLocalAccount; 37 DeviceLocalAccountLocator(Context context, AccountManager accountManager)38 public DeviceLocalAccountLocator(Context context, AccountManager accountManager) { 39 mContext = context; 40 mAccountManager = accountManager; 41 mLocalAccount = Collections.singletonList(AccountWithDataSet.getLocalAccount(context)); 42 } 43 44 /** 45 * Returns a list of device local accounts 46 */ getDeviceLocalAccounts()47 public List<AccountWithDataSet> getDeviceLocalAccounts() { 48 @SuppressWarnings("MissingPermission") final Account[] accounts = mAccountManager 49 .getAccountsByType(GoogleAccountType.ACCOUNT_TYPE); 50 51 if (accounts.length > 0 && !mLocalAccount.get(0).hasData(mContext)) { 52 return Collections.emptyList(); 53 } else { 54 return mLocalAccount; 55 } 56 } 57 } 58