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.util;
17 
18 import android.content.Context;
19 import androidx.annotation.IntDef;
20 
21 import com.android.contacts.model.account.AccountType;
22 import com.android.contacts.model.account.DeviceLocalAccountType;
23 
24 import java.lang.annotation.Retention;
25 
26 import static java.lang.annotation.RetentionPolicy.SOURCE;
27 
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 
classifyAccount(String accountType)41     @DeviceLocalAccountTypeFactory.LocalAccountType int classifyAccount(String accountType);
42 
getAccountType(String accountType)43     AccountType getAccountType(String accountType);
44 
45     class Util {
Util()46         private Util() { }
47 
isLocalAccountType(@ocalAccountType int type)48         public static boolean isLocalAccountType(@LocalAccountType int type) {
49             return type == TYPE_SIM || type == TYPE_DEVICE;
50         }
51 
isLocalAccountType(DeviceLocalAccountTypeFactory factory, String type)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 
Default(Context context)62         public Default(Context context) {
63             mContext = context;
64         }
65 
66         @Override
classifyAccount(String accountType)67         public int classifyAccount(String accountType) {
68             return accountType == null ? TYPE_DEVICE : TYPE_OTHER;
69         }
70 
71         @Override
getAccountType(String accountType)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 }
80