1.RemoteViews 支持的view 類型,不支持他們的子類及其他類型,
- Layout : FrameLayout,LinearLayout,RelativeLayout,GridLayout
- View: AnalogClock,Button,Chronometer,ImageButton,Imageview,ProgressBar,TextView,ViewFlipper,ListView,GridView,StackView,AdapterViewFlipper,ViewStub,
- RemoteViews 主要用于通知欄和桌面小部件.分別由NotificationManager 和AppWidgetManager 管理,通過Binder分別和SystemServer進程中的NotificationManagerService和 AppWidgetManagerService進行跨進程通信.所以需要通過一些列set方法來給RemoteViews設置屬性,然后在SystemServer進程中更近view.
- 系統提供一個Action 類,實現了 Parcelable接口,用來表示一個對view的更新操作.Action對象通過Binder夸進程傳輸到SystemServer 遠程進程. 遠程進程通過調用RemoteViews 的appply方法更新view,apply方法內部會遍歷所有Action并調用Action內部的apply方法來具體更新view.具體的更新方法是在Action內部的apply內部實現.
- 進行代碼追蹤
//RemoteViews
/通過set...方法來更新view屬性
public void setTextViewText(int viewId, CharSequence text) {
setCharSequence(viewId, "setText", text); //view的id,設置的屬性,設置的內容
}
public void setCharSequence(int viewId, String methodName, CharSequence value) {
//添加了一個 RefelctionAction對象 這是一個Action 的子類,先接著看
addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value));
}
private void addAction(Action a) {
if (hasLandscapeAndPortraitLayouts()) {
throw new RuntimeException("RemoteViews specifying separate landscape and portrait" +
" layouts cannot be modified. Instead, fully configure the landscape and" +
" portrait layouts individually before constructing the combined layout.");
}
if (mActions == null) {
mActions = new ArrayList<Action>(); //用一個集合保存所有的對view的操作Action
}
mActions.add(a);
// update the memory usage stats
a.updateMemoryUsageEstimate(mMemoryUsageCounter);
}
//到此時,只是把一個對view的操作添加到 RemoteViews 的 mActions集合中,接下來在看ReflectionAction前,還需要看下 RemoteViews 的apply方法.
/** @hide */
public View apply(Context context, ViewGroup parent, OnClickHandler handler) {
RemoteViews rvToApply = getRemoteViewsToApply(context); //得到需要更新屬性的RemoteViews
View result = inflateView(context, rvToApply, parent);//重點1 .rvToApply 代表RemoteViews,這里是加載我們為RemoteViews設置的layout布局
loadTransitionOverride(context, handler);
rvToApply.performApply(result, parent, handler); //重點2
return result;
}
//重點1
private View inflateView(Context context, RemoteViews rv, ViewGroup parent) {
...
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Clone inflater so we load resources from correct context and
// we don't add a filter to the static version returned by getSystemService.
inflater = inflater.cloneInContext(inflationContext);
inflater.setFilter(this);
View v = inflater.inflate(rv.getLayoutId(), parent, false); //加載了RemoteViews的布局文件進來
v.setTagInternal(R.id.widget_frame, rv.getLayoutId());
return v;
}
//重點2
private void performApply(View v, ViewGroup parent, OnClickHandler handler) {
if (mActions != null) {
handler = handler == null ? DEFAULT_ON_CLICK_HANDLER : handler;
final int count = mActions.size();
for (int i = 0; i < count; i++) { //其實就是依次調用了 Action內部的apply方法
Action a = mActions.get(i);
a.apply(v, parent, handler);
}
}
}
- 之后.當我們調用RemoteViews.set 方法時,不會立即更新view.而必須通過NotificationManager.noitefy 或AppWidgetManager.upodateAppWidget方法,在內部通過SystemServer夸進程調用RemoteViews的apply和reapply方法.apply在初始化時調用,reapply方法在每次更新時調用,二者內部都是調用 performApply()進而調用Action的apply方法.
- 接下來看ReflectionAction 的實現
//ReflectionAction 繼承 Action
//參數: viewId, 設置的屬性名,操作類型,操作的值
ReflectionAction(int viewId, String methodName, int type, Object value) {
this.viewId = viewId;
this.methodName = methodName;
this.type = type;
this.value = value;
}
@Override root使我們自己設定的layout,rootParent是RemoteViews
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View view = root.findViewById(viewId); //找到要更新的view
if (view == null) return;
Class<?> param = getParameterType(); //返回參數類型 int .long.bitmap.intent 等等
if (param == null) {
throw new ActionException("bad type: " + this.type);
}
try {
getMethod(view, this.methodName, param).invoke(view, wrapArg(this.value));//反射找到方法并執行
} catch (ActionException e) {
throw e;
} catch (Exception ex) {
throw new ActionException(ex);
}
}
private Method getMethod(View view, String methodName, Class<?> paramType) {
Method method;
Class<? extends View> klass = view.getClass();
synchronized (sMethodsLock) {
ArrayMap<MutablePair<String, Class<?>>, Method> methods = sMethods.get(klass);
//這個sMethods 是一個key為 View類的class. value為 ArrayMap<MutablePair<String, Class<?>,.Method>的map,
他的value也是map,key是方法名和形參類型的map,值是方法
if (methods == null) {
methods = new ArrayMap<MutablePair<String, Class<?>>, Method>();
sMethods.put(klass, methods);
}
//MutablePair 類, 由方法名和參數的class類構成
mPair.first = methodName;
mPair.second = paramType;
method = methods.get(mPair);//找到最重要反射執行的方法.
if (method == null) {
try {
if (paramType == null) { // 反射得到對應方法.區別在于是否有參數
method = klass.getMethod(methodName);
} else {
method = klass.getMethod(methodName, paramType);
}
} catch (NoSuchMethodException ex) {
throw new ActionException("view: " + klass.getName() + " doesn't have method: "
+ methodName + getParameters(paramType));
}
if (!method.isAnnotationPresent(RemotableViewMethod.class)) {
throw new ActionException("view: " + klass.getName()
+ " can't use method with RemoteViews: "
+ methodName + getParameters(paramType));
}
//保存方法
methods.put(new MutablePair<String, Class<?>>(methodName, paramType), method);
}
}
//返回得到的方法.
return method;
}
- 到此就結束了.當然,對于RemoteViews調用不同的set方法,添加的Action也是不同的.