今天遇到这样一个客制化需求要把上图中的Contacts 翻译同步改成俄语。
“Contacts”并没有在资源文件中定义,导致切换语言的时候字段是不会改过来的。跟踪源码发现右下角的布局定义都是从Google账号的数据库中读出来的数据,所以需要进行特殊处理,修改此String。
【一】首先我们借助DDMS工具进行定位修改的地方:
如图我们可以发现Contacts字段所在的控件是TextView,而且ID是“calendar”,他所在布局的父布局是RelativeLayout、
根据这些信息我们可以开始查找。
【二】使用grep 或者find命令查找修改地方的布局文件。
在源码package/apps/Calendar路径下使用命令:
grep -rn "@+id/calendar" packages/apps/Calendar/
搜索结果id是“calendar”的有
在这里我们可以发现布局id控件中有“calendar”的布局有两个:
mini_calendar_item.xml和calendar_sync_item.xml
我们可以根据路径去查看这两个布局文件,结果发现父布局是RelativeLayout的只有mini_calendar_item.xml,所以我们可以确定要修改的地方引用的布局是mini_calendar_item.xml
【三】顺藤摸瓜继续过滤布局在哪个地方被调用
grep -rn "R.layout.calendar_sync_item" packages/apps/Calendar/
现在到这里就很清晰了,原来布局是在这个SelectVisibleCalendarsFragment.java 文件被调用,他的路径在
packages/apps/Calendar/src/com/android/calendar/selectcalendars/SelectVisibleCalendarsFragment.java:63
完整源码如下:
package com.android.calendar.selectcalendars;
import android.app.Activity;
import android.app.Fragment;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Calendars;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.android.calendar.AsyncQueryService;
import com.android.calendar.CalendarController;
import com.android.calendar.CalendarController.EventInfo;
import com.android.calendar.CalendarController.EventType;
import com.android.calendar.R;
import com.android.calendar.Utils;
import com.android.calendar.selectcalendars.CalendarColorCache.OnCalendarColorsLoadedListener;
public class SelectVisibleCalendarsFragment extends Fragment
implements AdapterView.OnItemClickListener, CalendarController.EventHandler,
OnCalendarColorsLoadedListener {
private static final String TAG = "Calendar";
private static final String IS_PRIMARY = "\"primary\"";
private static final String SELECTION = Calendars.SYNC_EVENTS + "=?";
private static final String[] SELECTION_ARGS = new String[] {"1"};
private static final String[] PROJECTION = new String[] {
Calendars._ID,
Calendars.ACCOUNT_NAME,
Calendars.ACCOUNT_TYPE,
Calendars.OWNER_ACCOUNT,
Calendars.CALENDAR_DISPLAY_NAME,
Calendars.CALENDAR_COLOR,
Calendars.VISIBLE,
Calendars.SYNC_EVENTS,
"(" + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT + ") AS " + IS_PRIMARY,
};
private static int mUpdateToken;
private static int mQueryToken;
private static int mCalendarItemLayout = R.layout.mini_calendar_item;
private View mView = null;
private CalendarController mController;
private ListView mList;
private SelectCalendarsSimpleAdapter mAdapter;
private Activity mContext;
private AsyncQueryService mService;
private Cursor mCursor;
public SelectVisibleCalendarsFragment() {
}
public SelectVisibleCalendarsFragment(int itemLayout) {
mCalendarItemLayout = itemLayout;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mContext = activity;
mController = CalendarController.getInstance(activity);
mController.registerEventHandler(R.layout.select_calendars_fragment, this);
mService = new AsyncQueryService(activity) {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
mAdapter.changeCursor(cursor);
mCursor = cursor;
}
};
}
@Override
public void onDetach() {
super.onDetach();
mController.deregisterEventHandler(R.layout.select_calendars_fragment);
if (mCursor != null) {
mAdapter.changeCursor(null);
mCursor.close();
mCursor = null;
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mView = inflater.inflate(R.layout.select_calendars_fragment, null);
mList = (ListView)mView.findViewById(R.id.list);
// Hide the Calendars to Sync button on tablets for now.
// Long terms stick it in the list of calendars
if (Utils.getConfigBool(getActivity(), R.bool.multiple_pane_config)) {
// Don't show dividers on tablets
mList.setDivider(null);
View v = mView.findViewById(R.id.manage_sync_set);
if (v != null) {
v.setVisibility(View.GONE);
}
}
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new SelectCalendarsSimpleAdapter(mContext, mCalendarItemLayout, null,
getFragmentManager());
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mAdapter == null || mAdapter.getCount() <= position) {
return;
}
toggleVisibility(position);
}
@Override
public void onResume() {
super.onResume();
mQueryToken = mService.getNextToken();
mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
SELECTION_ARGS, Calendars.ACCOUNT_NAME);
}
/*
* Write back the changes that have been made.
*/
public void toggleVisibility(int position) {
mUpdateToken = mService.getNextToken();
Uri uri = ContentUris.withAppendedId(Calendars.CONTENT_URI, mAdapter.getItemId(position));
ContentValues values = new ContentValues();
// Toggle the current setting
int visibility = mAdapter.getVisible(position)^1;
values.put(Calendars.VISIBLE, visibility);
mService.startUpdate(mUpdateToken, null, uri, values, null, null, 0);
mAdapter.setVisible(position, visibility);
}
@Override
public void eventsChanged() {
if (mService != null) {
mService.cancelOperation(mQueryToken);
mQueryToken = mService.getNextToken();
mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
SELECTION_ARGS, Calendars.ACCOUNT_NAME);
}
}
@Override
public long getSupportedEventTypes() {
return EventType.EVENTS_CHANGED;
}
@Override
public void handleEvent(EventInfo event) {
eventsChanged();
}
@Override
public void onCalendarColorsLoaded() {
if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
}
}
}
在这个Fragment中,
private static int mCalendarItemLayout = R.layout.mini_calendar_item;
public SelectVisibleCalendarsFragment(int itemLayout) {
mCalendarItemLayout = itemLayout;
}
构造函数在初始化时会传入一个布局。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new SelectCalendarsSimpleAdapter(mContext, mCalendarItemLayout, null,
getFragmentManager());
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(this);
}
在这里我们可以看到使用的布局适配器是SelectCalendarsSimpleAdapter,我们知道Adapter里面会加载view,我们可以在加载view的时候修改字段
【四】查找Adapter,并打log验证
tlh@ubuntu:~/Extend/zhsong/niuSiMan/si7067ka_TZ720_M/sc7731_m$ find packages/apps/Calendar/ -name "SelectCalendarsSimpleAdapter.java"
packages/apps/Calendar/src/com/android/calendar/selectcalendars/SelectCalendarsSimpleAdapter.java
在getView()里可以发现name和accout就是从服务器返回的数据,我们给他加上log验证一下,结果如下
在这里我们可以发现log生效了,说明我们找对了地方,只需要在这里该就行,接下来的修改就很简单了
if(name.equalsIgnoreCase("Contacts")){
name = mInflater.getContext().getResources().getString(R.string.contacts);
}
不要忘了在资源文件里定义String哦