blob: 59ee7e5d38519ba1f93cee8d1d81f5e601e0f1a9 [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.util;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070017
18import android.content.Context;
19import android.support.annotation.IntDef;
20
Gary Mai69c182a2016-12-05 13:07:03 -080021import com.android.contacts.model.account.AccountType;
22import com.android.contacts.model.account.DeviceLocalAccountType;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070023
24import java.lang.annotation.Retention;
25
Marcus Hagerottb61e6542016-12-12 12:44:11 -080026import static java.lang.annotation.RetentionPolicy.SOURCE;
27
Marcus Hagerottfac695a2016-08-24 17:02:40 -070028/**
29 * Reports whether a value from RawContacts.ACCOUNT_TYPE should be considered a "Device"
30 * account
31 */
32public 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}