Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.model; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 17 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 18 | import android.accounts.Account; |
| 19 | import android.accounts.AccountManager; |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 21 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 22 | import com.android.contacts.Experiments; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 23 | import com.android.contacts.model.account.AccountWithDataSet; |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 24 | import com.android.contactsbind.ObjectFactory; |
| 25 | import com.android.contactsbind.experiments.Flags; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 26 | |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 27 | import java.util.Collections; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 28 | import java.util.HashSet; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 29 | import java.util.List; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 30 | import java.util.Set; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 33 | * Attempts to detect accounts for device contacts |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 34 | */ |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 35 | public abstract class DeviceLocalAccountLocator { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 36 | |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 37 | /** |
| 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 Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 51 | public static DeviceLocalAccountLocator create(Context context, |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 52 | Set<String> knownAccountTypes) { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 53 | if (Flags.getInstance().getBoolean(Experiments.OEM_CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) { |
| 54 | return new Cp2DeviceLocalAccountLocator(context.getContentResolver(), |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 55 | 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 Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 70 | } |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 71 | return NULL_ONLY; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 72 | } |
| 73 | } |