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