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.util; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | import android.support.annotation.IntDef; |
| 20 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 21 | import com.android.contacts.model.account.AccountType; |
| 22 | import com.android.contacts.model.account.DeviceLocalAccountType; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 23 | |
| 24 | import java.lang.annotation.Retention; |
| 25 | |
Marcus Hagerott | b61e654 | 2016-12-12 12:44:11 -0800 | [diff] [blame] | 26 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 27 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Reports whether a value from RawContacts.ACCOUNT_TYPE should be considered a "Device" |
| 30 | * account |
| 31 | */ |
| 32 | public interface DeviceLocalAccountTypeFactory { |
| 33 | |
| 34 | @Retention(SOURCE) |
| 35 | @IntDef({TYPE_OTHER, TYPE_DEVICE, TYPE_SIM}) |
| 36 | @interface LocalAccountType {} |
| 37 | static final int TYPE_OTHER = 0; |
| 38 | static final int TYPE_DEVICE = 1; |
| 39 | static final int TYPE_SIM = 2; |
| 40 | |
| 41 | @DeviceLocalAccountTypeFactory.LocalAccountType int classifyAccount(String accountType); |
| 42 | |
| 43 | AccountType getAccountType(String accountType); |
| 44 | |
| 45 | class Util { |
| 46 | private Util() { } |
| 47 | |
| 48 | public static boolean isLocalAccountType(@LocalAccountType int type) { |
| 49 | return type == TYPE_SIM || type == TYPE_DEVICE; |
| 50 | } |
| 51 | |
| 52 | public static boolean isLocalAccountType(DeviceLocalAccountTypeFactory factory, |
| 53 | String type) { |
| 54 | |
| 55 | return isLocalAccountType(factory.classifyAccount(type)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | class Default implements DeviceLocalAccountTypeFactory { |
| 60 | private Context mContext; |
| 61 | |
| 62 | public Default(Context context) { |
| 63 | mContext = context; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int classifyAccount(String accountType) { |
| 68 | return accountType == null ? TYPE_DEVICE : TYPE_OTHER; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public AccountType getAccountType(String accountType) { |
| 73 | if (accountType != null) { |
| 74 | throw new IllegalArgumentException(accountType + " is not a device account type."); |
| 75 | } |
| 76 | return new DeviceLocalAccountType(mContext); |
| 77 | } |
| 78 | } |
| 79 | } |