blob: 0d34057f98ffd3759408f6f6a4d3b2cea103fdff [file] [log] [blame]
Marcus Hagerottfac695a2016-08-24 17:02:40 -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 */
Gary Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.model;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070017
Marcus Hagerottc2093f32016-12-12 10:18:12 -080018import android.accounts.Account;
19import android.accounts.AccountManager;
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070020import android.content.Context;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070021
Gary Mai0a49afa2016-12-05 15:53:58 -080022import com.android.contacts.Experiments;
Gary Mai69c182a2016-12-05 13:07:03 -080023import com.android.contacts.model.account.AccountWithDataSet;
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070024import com.android.contactsbind.ObjectFactory;
25import com.android.contactsbind.experiments.Flags;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070026
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070027import java.util.Collections;
Marcus Hagerottc2093f32016-12-12 10:18:12 -080028import java.util.HashSet;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070029import java.util.List;
Marcus Hagerottc2093f32016-12-12 10:18:12 -080030import java.util.Set;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070031
32/**
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070033 * Attempts to detect accounts for device contacts
Marcus Hagerottfac695a2016-08-24 17:02:40 -070034 */
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070035public abstract class DeviceLocalAccountLocator {
Marcus Hagerottfac695a2016-08-24 17:02:40 -070036
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070037 /**
38 * Returns a list of device local accounts
39 */
40 public abstract List<AccountWithDataSet> getDeviceLocalAccounts();
41
42 // This works on Nexus and AOSP because the local device account is the null account but most
43 // OEMs have a special account name and type for their device account.
44 public static final DeviceLocalAccountLocator NULL_ONLY = new DeviceLocalAccountLocator() {
45 @Override
46 public List<AccountWithDataSet> getDeviceLocalAccounts() {
47 return Collections.singletonList(AccountWithDataSet.getNullAccount());
48 }
Marcus Hagerottfac695a2016-08-24 17:02:40 -070049 };
50
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070051 public static DeviceLocalAccountLocator create(Context context,
Marcus Hagerottc2093f32016-12-12 10:18:12 -080052 Set<String> knownAccountTypes) {
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070053 if (Flags.getInstance().getBoolean(Experiments.OEM_CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) {
54 return new Cp2DeviceLocalAccountLocator(context.getContentResolver(),
Marcus Hagerottc2093f32016-12-12 10:18:12 -080055 ObjectFactory.getDeviceLocalAccountTypeFactory(context), knownAccountTypes);
56 }
57 return NULL_ONLY;
58 }
59
60 public static DeviceLocalAccountLocator create(Context context) {
61 final AccountManager accountManager =
62 (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
63 final Set<String> knownTypes = new HashSet<>();
64 for (Account account : accountManager.getAccounts()) {
65 knownTypes.add(account.type);
66 }
67 if (Flags.getInstance().getBoolean(Experiments.OEM_CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) {
68 return new Cp2DeviceLocalAccountLocator(context.getContentResolver(),
69 ObjectFactory.getDeviceLocalAccountTypeFactory(context), knownTypes);
Marcus Hagerottfac695a2016-08-24 17:02:40 -070070 }
Marcus Hagerott7a756ab2016-11-01 18:16:02 -070071 return NULL_ONLY;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070072 }
73}