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