1/*
2 * Copyright (C) 2009 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
17package com.android.contacts.common.model.account;
18
19import android.content.ContentValues;
20import android.content.Context;
21import android.provider.ContactsContract.CommonDataKinds.Email;
22import android.provider.ContactsContract.CommonDataKinds.Event;
23import android.provider.ContactsContract.CommonDataKinds.Phone;
24import android.provider.ContactsContract.CommonDataKinds.Relation;
25import android.util.Log;
26
27import com.android.contacts.common.R;
28import com.android.contacts.common.model.dataitem.DataKind;
29import com.android.contacts.common.util.CommonDateUtils;
30import com.google.common.collect.Lists;
31
32import java.util.List;
33
34public class GoogleAccountType extends BaseAccountType {
35    private static final String TAG = "GoogleAccountType";
36
37    /**
38     * The package name that we should load contacts.xml from and rely on to handle
39     * G+ account actions. Even though this points to gms, in some cases gms will still hand
40     * off responsibility to the G+ app.
41     */
42    public static final String PLUS_EXTENSION_PACKAGE_NAME = "com.google.android.gms";
43
44    public static final String ACCOUNT_TYPE = "com.google";
45
46    private static final List<String> mExtensionPackages =
47            Lists.newArrayList(PLUS_EXTENSION_PACKAGE_NAME);
48
49    public GoogleAccountType(Context context, String authenticatorPackageName) {
50        this.accountType = ACCOUNT_TYPE;
51        this.resourcePackageName = null;
52        this.syncAdapterPackageName = authenticatorPackageName;
53
54        try {
55            addDataKindStructuredName(context);
56            addDataKindDisplayName(context);
57            addDataKindPhoneticName(context);
58            addDataKindNickname(context);
59            addDataKindPhone(context);
60            addDataKindEmail(context);
61            addDataKindStructuredPostal(context);
62            addDataKindIm(context);
63            addDataKindOrganization(context);
64            addDataKindPhoto(context);
65            addDataKindNote(context);
66            addDataKindWebsite(context);
67            addDataKindSipAddress(context);
68            addDataKindGroupMembership(context);
69            addDataKindRelation(context);
70            addDataKindEvent(context);
71
72            mIsInitialized = true;
73        } catch (DefinitionException e) {
74            Log.e(TAG, "Problem building account type", e);
75        }
76    }
77
78    @Override
79    public List<String> getExtensionPackageNames() {
80        return mExtensionPackages;
81    }
82
83    @Override
84    protected DataKind addDataKindPhone(Context context) throws DefinitionException {
85        final DataKind kind = super.addDataKindPhone(context);
86
87        kind.typeColumn = Phone.TYPE;
88        kind.typeList = Lists.newArrayList();
89        kind.typeList.add(buildPhoneType(Phone.TYPE_MOBILE));
90        kind.typeList.add(buildPhoneType(Phone.TYPE_WORK));
91        kind.typeList.add(buildPhoneType(Phone.TYPE_HOME));
92        kind.typeList.add(buildPhoneType(Phone.TYPE_MAIN));
93        kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_WORK).setSecondary(true));
94        kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_HOME).setSecondary(true));
95        kind.typeList.add(buildPhoneType(Phone.TYPE_PAGER).setSecondary(true));
96        kind.typeList.add(buildPhoneType(Phone.TYPE_OTHER));
97        kind.typeList.add(buildPhoneType(Phone.TYPE_CUSTOM).setSecondary(true)
98                .setCustomColumn(Phone.LABEL));
99
100        kind.fieldList = Lists.newArrayList();
101        kind.fieldList.add(new EditField(Phone.NUMBER, R.string.phoneLabelsGroup, FLAGS_PHONE));
102
103        return kind;
104    }
105
106    @Override
107    protected DataKind addDataKindEmail(Context context) throws DefinitionException {
108        final DataKind kind = super.addDataKindEmail(context);
109
110        kind.typeColumn = Email.TYPE;
111        kind.typeList = Lists.newArrayList();
112        kind.typeList.add(buildEmailType(Email.TYPE_HOME));
113        kind.typeList.add(buildEmailType(Email.TYPE_WORK));
114        kind.typeList.add(buildEmailType(Email.TYPE_OTHER));
115        kind.typeList.add(buildEmailType(Email.TYPE_CUSTOM).setSecondary(true).setCustomColumn(
116                Email.LABEL));
117
118        kind.fieldList = Lists.newArrayList();
119        kind.fieldList.add(new EditField(Email.DATA, R.string.emailLabelsGroup, FLAGS_EMAIL));
120
121        return kind;
122    }
123
124    private DataKind addDataKindRelation(Context context) throws DefinitionException {
125        DataKind kind = addKind(new DataKind(Relation.CONTENT_ITEM_TYPE,
126                R.string.relationLabelsGroup, Weight.RELATIONSHIP, true));
127        kind.actionHeader = new RelationActionInflater();
128        kind.actionBody = new SimpleInflater(Relation.NAME);
129
130        kind.typeColumn = Relation.TYPE;
131        kind.typeList = Lists.newArrayList();
132        kind.typeList.add(buildRelationType(Relation.TYPE_ASSISTANT));
133        kind.typeList.add(buildRelationType(Relation.TYPE_BROTHER));
134        kind.typeList.add(buildRelationType(Relation.TYPE_CHILD));
135        kind.typeList.add(buildRelationType(Relation.TYPE_DOMESTIC_PARTNER));
136        kind.typeList.add(buildRelationType(Relation.TYPE_FATHER));
137        kind.typeList.add(buildRelationType(Relation.TYPE_FRIEND));
138        kind.typeList.add(buildRelationType(Relation.TYPE_MANAGER));
139        kind.typeList.add(buildRelationType(Relation.TYPE_MOTHER));
140        kind.typeList.add(buildRelationType(Relation.TYPE_PARENT));
141        kind.typeList.add(buildRelationType(Relation.TYPE_PARTNER));
142        kind.typeList.add(buildRelationType(Relation.TYPE_REFERRED_BY));
143        kind.typeList.add(buildRelationType(Relation.TYPE_RELATIVE));
144        kind.typeList.add(buildRelationType(Relation.TYPE_SISTER));
145        kind.typeList.add(buildRelationType(Relation.TYPE_SPOUSE));
146        kind.typeList.add(buildRelationType(Relation.TYPE_CUSTOM).setSecondary(true)
147                .setCustomColumn(Relation.LABEL));
148
149        kind.defaultValues = new ContentValues();
150        kind.defaultValues.put(Relation.TYPE, Relation.TYPE_SPOUSE);
151
152        kind.fieldList = Lists.newArrayList();
153        kind.fieldList.add(new EditField(Relation.DATA, R.string.relationLabelsGroup,
154                FLAGS_RELATION));
155
156        return kind;
157    }
158
159    private DataKind addDataKindEvent(Context context) throws DefinitionException {
160        DataKind kind = addKind(new DataKind(Event.CONTENT_ITEM_TYPE,
161                    R.string.eventLabelsGroup, Weight.EVENT, true));
162        kind.actionHeader = new EventActionInflater();
163        kind.actionBody = new SimpleInflater(Event.START_DATE);
164
165        kind.typeColumn = Event.TYPE;
166        kind.typeList = Lists.newArrayList();
167        kind.dateFormatWithoutYear = CommonDateUtils.NO_YEAR_DATE_FORMAT;
168        kind.dateFormatWithYear = CommonDateUtils.FULL_DATE_FORMAT;
169        kind.typeList.add(buildEventType(Event.TYPE_BIRTHDAY, true).setSpecificMax(1));
170        kind.typeList.add(buildEventType(Event.TYPE_ANNIVERSARY, false));
171        kind.typeList.add(buildEventType(Event.TYPE_OTHER, false));
172        kind.typeList.add(buildEventType(Event.TYPE_CUSTOM, false).setSecondary(true)
173                .setCustomColumn(Event.LABEL));
174
175        kind.defaultValues = new ContentValues();
176        kind.defaultValues.put(Event.TYPE, Event.TYPE_BIRTHDAY);
177
178        kind.fieldList = Lists.newArrayList();
179        kind.fieldList.add(new EditField(Event.DATA, R.string.eventLabelsGroup, FLAGS_EVENT));
180
181        return kind;
182    }
183
184    @Override
185    public boolean isGroupMembershipEditable() {
186        return true;
187    }
188
189    @Override
190    public boolean areContactsWritable() {
191        return true;
192    }
193
194    @Override
195    public String getViewContactNotifyServiceClassName() {
196        return "com.google.android.syncadapters.contacts." +
197                "SyncHighResPhotoIntentService";
198    }
199
200    @Override
201    public String getViewContactNotifyServicePackageName() {
202        return "com.google.android.syncadapters.contacts";
203    }
204}
205