1, 概述
AppWidget是窗口小部件, appwidget命令主要是设置apk是否有放在窗口的权限。
2, AppWidget命令
命令格式:
appwidget <command>
命令列表:
命令 | 功能 | 实现方法 |
grantbind | 允许 权限 | appWidgetService.setBindAppWidgetPermission |
revokebind | 拒绝 | appWidgetService.setBindAppWidgetPermission |
3, 详细流程
所有adb命令的appwidget方法最后都会通过AppWidget.java执行,然后通过跨进程
调用AppWidgetService的方法完成,至于android系统中的AppWidget原理在此就不论述了。
AppWidget的main方法如下,
public static void main(String[] args) {
Parser parser = new Parser(args);
Runnable command = parser.parseCommand();
if (command != null) {
command.run();
}
}
Parser是一个内部类, parseCommand方法如下,
public Runnable parseCommand() {
try {
String operation = mTokenizer.nextArg();
if (ARGUMENT_GRANT_BIND.equals(operation)) {
return parseSetGrantBindAppWidgetPermissionCommand(true);
} else if (ARGUMENT_REVOKE_BIND.equals(operation)) {
return parseSetGrantBindAppWidgetPermissionCommand(false);
} else {
throw new IllegalArgumentException("Unsupported operation: " + operation);
}
} catch (IllegalArgumentException iae) {
System.out.println(USAGE);
System.out.println("[ERROR] " + iae.getMessage());
return null;
}
}
2个指令分别对应同一个方法,都是parseSetGrantBindAppWidgetPermissionCommand,只是参数不一样而已。
private SetBindAppWidgetPermissionCommand parseSetGrantBindAppWidgetPermissionCommand(
boolean granted) {
String packageName = null;
int userId = UserHandle.USER_OWNER;
for (String argument; (argument = mTokenizer.nextArg()) != null;) {
if (ARGUMENT_PACKAGE.equals(argument)) {
packageName = argumentValueRequired(argument);
} else if (ARGUMENT_USER.equals(argument)) {
String user = argumentValueRequired(argument);
if (VALUE_USER_CURRENT.equals(user)) {
userId = UserHandle.USER_CURRENT;
} else {
userId = Integer.parseInt(user);
}
} else {
throw new IllegalArgumentException("Unsupported argument: " + argument);
}
}
if (packageName == null) {
throw new IllegalArgumentException("Package name not specified."
+ " Did you specify --package argument?");
}
return new SetBindAppWidgetPermissionCommand(packageName, granted, userId);
}
返回的是SetBindAppWidgetPermissionCommand对象,该对象的run方法如下,
@Override
public void run() {
IBinder binder = ServiceManager.getService(Context.APPWIDGET_SERVICE);
IAppWidgetService appWidgetService = IAppWidgetService.Stub.asInterface(binder);
try {
appWidgetService.setBindAppWidgetPermission(mPackageName, mUserId, mGranted);
} catch (RemoteException re) {
re.printStackTrace();
}
}
2个命令最后都是调用appWidgetService 的setBindAppWidgetPermission完成,只是mGranted参数不一样。