1/*
2 * Copyright (C) 2011 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.model;
18
19import com.android.contacts.R;
20import com.android.contacts.model.AccountType.EditField;
21import com.android.contacts.model.AccountType.EditType;
22import com.android.contacts.model.AccountType.StringInflater;
23import com.google.common.collect.Iterators;
24
25import android.content.ContentValues;
26import android.provider.ContactsContract.Data;
27
28import java.text.SimpleDateFormat;
29import java.util.List;
30
31/**
32 * Description of a specific data type, usually marked by a unique
33 * {@link Data#MIMETYPE}. Includes details about how to view and edit
34 * {@link Data} rows of this kind, including the possible {@link EditType}
35 * labels and editable {@link EditField}.
36 */
37public final class DataKind {
38
39    public static final String PSEUDO_MIME_TYPE_DISPLAY_NAME = "#displayName";
40    public static final String PSEUDO_MIME_TYPE_PHONETIC_NAME = "#phoneticName";
41    public static final String PSEUDO_COLUMN_PHONETIC_NAME = "#phoneticName";
42
43    public String resPackageName;
44    public String mimeType;
45    public int titleRes;
46    public int iconAltRes;
47    public int iconAltDescriptionRes;
48    public int weight;
49    public boolean editable;
50
51    public StringInflater actionHeader;
52    public StringInflater actionAltHeader;
53    public StringInflater actionBody;
54
55    public boolean actionBodySocial = false;
56
57    public String typeColumn;
58
59    /**
60     * Maximum number of values allowed in the list. -1 represents infinity.
61     */
62    public int typeOverallMax;
63
64    public List<EditType> typeList;
65    public List<EditField> fieldList;
66
67    public ContentValues defaultValues;
68
69    /** Layout resource id for an editor view to edit this {@link DataKind}. */
70    public final int editorLayoutResourceId;
71
72    /**
73     * If this is a date field, this specifies the format of the date when saving. The
74     * date includes year, month and day. If this is not a date field or the date field is not
75     * editable, this value should be ignored.
76     */
77    public SimpleDateFormat dateFormatWithoutYear;
78
79    /**
80     * If this is a date field, this specifies the format of the date when saving. The
81     * date includes month and day. If this is not a date field, the field is not editable or
82     * dates without year are not supported, this value should be ignored.
83     */
84    public SimpleDateFormat dateFormatWithYear;
85
86    public DataKind() {
87        editorLayoutResourceId = R.layout.text_fields_editor_view;
88    }
89
90    public DataKind(String mimeType, int titleRes, int weight, boolean editable,
91            int editorLayoutResourceId) {
92        this.mimeType = mimeType;
93        this.titleRes = titleRes;
94        this.weight = weight;
95        this.editable = editable;
96        this.typeOverallMax = -1;
97        this.editorLayoutResourceId = editorLayoutResourceId;
98    }
99
100    @Override
101    public String toString() {
102        final StringBuilder sb = new StringBuilder();
103        sb.append("DataKind:");
104        sb.append(" resPackageName=").append(resPackageName);
105        sb.append(" mimeType=").append(mimeType);
106        sb.append(" titleRes=").append(titleRes);
107        sb.append(" iconAltRes=").append(iconAltRes);
108        sb.append(" iconAltDescriptionRes=").append(iconAltDescriptionRes);
109        sb.append(" weight=").append(weight);
110        sb.append(" editable=").append(editable);
111        sb.append(" actionHeader=").append(actionHeader);
112        sb.append(" actionAltHeader=").append(actionAltHeader);
113        sb.append(" actionBody=").append(actionBody);
114        sb.append(" actionBodySocial=").append(actionBodySocial);
115        sb.append(" typeColumn=").append(typeColumn);
116        sb.append(" typeOverallMax=").append(typeOverallMax);
117        sb.append(" typeList=").append(toString(typeList));
118        sb.append(" fieldList=").append(toString(fieldList));
119        sb.append(" defaultValues=").append(defaultValues);
120        sb.append(" editorLayoutResourceId=").append(editorLayoutResourceId);
121        sb.append(" dateFormatWithoutYear=").append(toString(dateFormatWithoutYear));
122        sb.append(" dateFormatWithYear=").append(toString(dateFormatWithYear));
123
124        return sb.toString();
125    }
126
127    public static String toString(SimpleDateFormat format) {
128        return format == null ? "(null)" : format.toPattern();
129    }
130
131    public static String toString(Iterable<?> list) {
132        if (list == null) {
133            return "(null)";
134        } else {
135            return Iterators.toString(list.iterator());
136        }
137    }
138}