2014-01-06 17:18:29
1. Phonebook中新建/编辑联系人的UI不是用xml文件写的,它是随着帐号类型的改变来加载不同的UI,比如SIM联系人,只有Name、Phone Number,如果是USIM,或许还有第二个号码、Email,但是本地联系人除了包含这些,还有Nickname,Website等,所以帐号如何定义以及UI如何加载就变得很复杂。
2. 帐号类型(以AdnAccountType SIM帐号为例)
继承关系:AdnAccountType.java --> BaseAccountType.java --> AccountType.java,在AccountType中有两个很重要的方法,如下:
public ArrayList<DataKind> getSortedDataKinds() {
Collections.sort(mKinds, sWeightComparator);
return mKinds;
} public DataKind addKind(DataKind kind) throws DefinitionException {
kind.resourcePackageName = this.resourcePackageName;
this.mKinds.add(kind);
this.mMimeKinds.put(kind.mimeType, kind);
return kind;
}
addKind()方法添加kind到mKinds,getSortedDataKinds()取出来所有的kind,这两个方法的使用将在后面介绍。关于BaseAccountType,这个类是所有帐号类的父类,下面看AdnAccountType:
public AdnAccountType(Context context, String resPackageName, String accountType) {
this.accountType = accountType;
//this.resPackageName = null;
this.syncAdapterPackageName = resPackageName; try {
addDataKindStructuredName(context);
addDataKindPhone(context); int simType = SimUtil.getIccType(context, AdnHelper.getSlotNumber(accountType));
if (simType == IccProviderConstant.USIM_TYPE
|| simType == IccProviderConstant.CSIM_TYPE) {
addDataKindEmail(context);
} mIsInitialized = true;
} catch (DefinitionException e) {
SpbLog.e(TAG, "Problem building account type", e);
}
} @Override
protected DataKind addDataKindStructuredName(Context context) throws DefinitionException {
DataKind kind = addKind(new DataKind(StructuredName.CONTENT_ITEM_TYPE,
R.string.nameLabelsGroup, -1, true, R.layout.structured_name_editor_view)); kind.fieldList = Lists.newArrayList();
kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.nameLabelsGroup,
FLAGS_PERSON_NAME).setNeedFocus(true)); return kind;
} @Override
protected DataKind addDataKindPhone(Context context) throws DefinitionException {
DataKind kind = super.addDataKindPhone(context); kind.iconAltRes = R.drawable.ic_message_icon;
kind.actionHeader = new PhoneActionInflater();
kind.actionAltHeader = new PhoneActionAltInflater();
kind.actionBody = new SimpleInflater(Phone.NUMBER); kind.typeColumn = Phone.TYPE;
kind.typeList = Lists.newArrayList();
kind.typeList.add(buildPhoneType(Phone.TYPE_MOBILE)); kind.fieldList = Lists.newArrayList();
kind.fieldList.add(new EditField(Phone.NUMBER, R.string.phoneLabelsGroup, FLAGS_PHONE)); return kind;
}
可以看到,SIM卡帐号添加了Name和Phone number两种数据的UI,这符合SIM联系人只能添加名称和号码的规则,如果SIM卡是USIM或者CSIM,那么还可以添加Email信息,看if判断语句就知道了。在addDataKindStructuredName()方法里面,调用了前面介绍的addKind()方法,将一个DataKind对象添加进去,同时为这个DataKind对象的fieldList添加了EditField,这个fieldList包含的是要显示的View,其实就是phone number或者Email条目包含的子条目,这些item会在后面具体介绍。下面看一下较复杂的LocalAccountType:
public LocalAccountType(Context context, String resPackageName) {
this.accountType = SpbIntents.ACCOUNT_TYPE;
this.resourcePackageName = null;
this.syncAdapterPackageName = resPackageName; try {
addDataKindStructuredName(context);
addDataKindNickname(context);
addDataKindPhone(context);
addDataKindEmail(context);
addDataKindStructuredPostal(context);
addDataKindIm(context);
addDataKindOrganization(context);
addDataKindPhoto(context);
addDataKindNote(context);
addDataKindWebsite(context);
addDataKindEvent(context);
addDataKindSipAddress(context);
addDataKindGroupMembership(context); mIsInitialized = true;
} catch (DefinitionException e) {
SpbLog.e(TAG, "Problem building account type", e);
}
} @Override
protected DataKind addDataKindPhone(Context context) throws DefinitionException {
final DataKind kind = super.addDataKindPhone(context); kind.typeColumn = Phone.TYPE;
kind.typeList = Lists.newArrayList();
kind.typeList.add(buildPhoneType(Phone.TYPE_HOME));
kind.typeList.add(buildPhoneType(Phone.TYPE_MOBILE));
kind.typeList.add(buildPhoneType(Phone.TYPE_WORK));
kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_WORK).setSecondary(true));
kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_HOME).setSecondary(true));
kind.typeList.add(buildPhoneType(Phone.TYPE_PAGER).setSecondary(true));
kind.typeList.add(buildPhoneType(Phone.TYPE_OTHER));
kind.typeList.add(buildPhoneType(Phone.TYPE_CUSTOM).setSecondary(true).setCustomColumn(
Phone.LABEL)); kind.fieldList = Lists.newArrayList();
kind.fieldList.add(new EditField(Phone.NUMBER, R.string.phoneLabelsGroup, FLAGS_PHONE)); return kind;
}
可以看到,本地帐号包含更多的信息,而且光Phone number就包含了好多类型,如HOME,MOBILE,FAX等。
addDataKindStructuredName添加的是姓名相关的UI,都包含在在kind.fieldList中,每一个EditField基本可以认为是一个EditTest,如图:
而addDataKindPhone添加的是PhoneNumber相关的UI,kind.fieldList只包含一个EditField,可以看见只有一个EditText,但是kind.typeList添加了很多东西,可以通过点击Type Spinner来看到,共八个,和代码一致,如下图:
3. 上面分析了一个帐号类型类包含的东西,包含了一些DataKind,而每一个DataKind又有kind.typeList,kind.fieldList以及其他的属性。所以一个DataKind代表一个类型的信息,如Name,Phone number,Email等,但是Name里面包含很多具体信息,如prefix,suffix,first name等,Phone number里面包含一个EditText,但是有很多种号码类型等等,这样构成了一个完整的帐号类,而在加载UI的时候,就是根据当前的帐号来添加帐号里面包含的UI。具体如何加载后面分析。
4. 上面的分析涉及到两个辅助类:DataKind和EditField,下面分别看一下这两个类:
public final class DataKind { public String resourcePackageName;
public String mimeType;
public int titleRes;
public int iconAltRes;
public int iconAltDescriptionRes;
public int weight;
public boolean secondary;
public boolean editable;
public StringInflater actionHeader;
public StringInflater actionAltHeader;
public StringInflater actionBody;
public boolean actionBodySocial = false;
public String typeColumn;
public int typeOverallMax;
public List<EditType> typeList;
public List<EditField> fieldList;
public ContentValues defaultValues;
public final int editorLayoutResourceId;
public SimpleDateFormat dateFormatWithoutYear;
public SimpleDateFormat dateFormatWithYear; public int maxLinesForDisplay; public DataKind() {
editorLayoutResourceId = R.layout.text_fields_editor_view;
maxLinesForDisplay = 1;
} public DataKind(String mimeType, int titleRes, int weight, boolean editable,
int editorLayoutResourceId) {
this.mimeType = mimeType;
this.titleRes = titleRes;
this.weight = weight;
this.editable = editable;
this.typeOverallMax = -1;
this.editorLayoutResourceId = editorLayoutResourceId;
maxLinesForDisplay = 1;
}
}
由上可见,DataKind就是一个封装信息的类,只起到封装信息的作用。
public static final class EditField {
public String column;
public int titleRes;
public int inputType;
public int minLines;
public boolean optional;
public boolean shortForm;
public boolean longForm;
public boolean needFocus = false;
public boolean editable = true; public InputFilter[] inputFilters; public EditField(String column, int titleRes) {
this.column = column;
this.titleRes = titleRes;
} public EditField(String column, int titleRes, int inputType) {
this(column, titleRes);
this.inputType = inputType;
} public EditField setOptional(boolean optional) {
this.optional = optional;
return this;
}
public EditField setShortForm(boolean shortForm) {
this.shortForm = shortForm;
return this;
} public EditField setLongForm(boolean longForm) {
this.longForm = longForm;
return this;
} public EditField setMinLines(int minLines) {
this.minLines = minLines;
return this;
} public boolean isMultiLine() {
return (inputType & EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) != 0;
} public EditField setNeedFocus(boolean needFocus){
this.needFocus = needFocus;
return this;
}
public EditField setEditable(boolean editable) {
this.editable = editable;
return this;
}
}
EditField与DataKind类似,封装了一些信息,至于这些信息是如何被解析的,在真正加载UI的时候再分析。