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