• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.provider.ContactsContract;
20 
21 import androidx.annotation.IntDef;
22 
23 import com.android.contacts.model.account.AccountType;
24 import com.android.contacts.model.account.AccountWithDataSet;
25 import com.android.contacts.model.account.DeviceLocalAccountType;
26 import com.android.contacts.model.account.SimAccountType;
27 
28 import java.lang.annotation.Retention;
29 import java.util.Objects;
30 
31 import static java.lang.annotation.RetentionPolicy.SOURCE;
32 
33 /**
34  * Reports whether a value from RawContacts.ACCOUNT_TYPE should be considered a "Device"
35  * account
36  */
37 public interface DeviceLocalAccountTypeFactory {
38 
39     @Retention(SOURCE)
40     @IntDef({TYPE_OTHER, TYPE_DEVICE, TYPE_SIM})
41     @interface LocalAccountType {}
42     static final int TYPE_OTHER = 0;
43     static final int TYPE_DEVICE = 1;
44     static final int TYPE_SIM = 2;
45 
classifyAccount(String accountType)46     @DeviceLocalAccountTypeFactory.LocalAccountType int classifyAccount(String accountType);
47 
getAccountType(String accountType)48     AccountType getAccountType(String accountType);
49 
50     class Util {
Util()51         private Util() { }
52 
isLocalAccountType(@ocalAccountType int type)53         public static boolean isLocalAccountType(@LocalAccountType int type) {
54             return type == TYPE_SIM || type == TYPE_DEVICE;
55         }
56 
isLocalAccountType(DeviceLocalAccountTypeFactory factory, String type)57         public static boolean isLocalAccountType(DeviceLocalAccountTypeFactory factory,
58                 String type) {
59 
60             return isLocalAccountType(factory.classifyAccount(type));
61         }
62     }
63 
64     class Default implements DeviceLocalAccountTypeFactory {
65         private Context mContext;
66 
Default(Context context)67         public Default(Context context) {
68             mContext = context;
69         }
70 
71         @Override
classifyAccount(String accountType)72         public int classifyAccount(String accountType) {
73             for (ContactsContract.SimAccount simAccount :
74                     ContactsContract.SimContacts.getSimAccounts(
75                             mContext.getContentResolver())) {
76                 if (accountType != null && Objects.equals(accountType,
77                         simAccount.getAccountType())) {
78                     return TYPE_SIM;
79                 }
80             }
81             return accountType == null ||
82                     Objects.equals(AccountWithDataSet.getLocalAccount(mContext).type, accountType)
83                     ? TYPE_DEVICE : TYPE_OTHER;
84         }
85 
86         @Override
getAccountType(String accountType)87         public AccountType getAccountType(String accountType) {
88             if (classifyAccount(accountType) == TYPE_SIM) {
89                 return new SimAccountType(mContext);
90             }
91             if (accountType != null && !Objects.equals(
92                     AccountWithDataSet.getLocalAccount(mContext).type, accountType)) {
93                 throw new IllegalArgumentException(accountType + " is not a device account type.");
94             }
95             return new DeviceLocalAccountType(mContext, true);
96         }
97     }
98 }
99