summaryrefslogtreecommitdiffstats
path: root/cmds/appwidget/src/com/android/commands/appwidget
diff options
context:
space:
mode:
authorSvet Ganov <svetoslavganov@google.com>2014-07-21 23:46:05 -0700
committerSvetoslav Ganov <svetoslavganov@google.com>2014-07-23 02:25:25 +0000
commitbe19c3a91a11508bd12e3355f3b1d9adf7f53194 (patch)
tree905c591f5289e2342a8290bbc6cc1d96e5acf0db /cmds/appwidget/src/com/android/commands/appwidget
parent52ff21d841ad5b39b6def0c286a737fd4b74b2e2 (diff)
downloadframeworks_base-be19c3a91a11508bd12e3355f3b1d9adf7f53194.zip
frameworks_base-be19c3a91a11508bd12e3355f3b1d9adf7f53194.tar.gz
frameworks_base-be19c3a91a11508bd12e3355f3b1d9adf7f53194.tar.bz2
Adding shell commands for grant/revoke of bind app widget permisison.
Testing the app widget APIs requires binding of widgets which is guarded by a system signature permission or white listing. Since CTS tests can not be signed with the platform certificate, they should be able to temporariliy white list their package. This command adds the ability to grant and revoke the provilege to bind app widgets to a given package. Change-Id: I6de1bbf27ac684ec47e61157f19ec6d29e2db979
Diffstat (limited to 'cmds/appwidget/src/com/android/commands/appwidget')
-rw-r--r--cmds/appwidget/src/com/android/commands/appwidget/AppWidget.java167
1 files changed, 167 insertions, 0 deletions
diff --git a/cmds/appwidget/src/com/android/commands/appwidget/AppWidget.java b/cmds/appwidget/src/com/android/commands/appwidget/AppWidget.java
new file mode 100644
index 0000000..dd45d39
--- /dev/null
+++ b/cmds/appwidget/src/com/android/commands/appwidget/AppWidget.java
@@ -0,0 +1,167 @@
+/*
+** Copyright 2014, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+package com.android.commands.appwidget;
+
+import android.content.Context;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+import android.text.TextUtils;
+
+import com.android.internal.appwidget.IAppWidgetService;
+
+/**
+ * This class is a command line utility for manipulating app widgets. A client
+ * can grant or revoke the permission for a given package to bind app widgets.
+ */
+public class AppWidget {
+
+ private static final String USAGE =
+ "usage: adb shell appwidget [subcommand] [options]\n"
+ + "\n"
+ + "usage: adb shell appwidget grantbind --package <PACKAGE> "
+ + " [--user <USER_ID> | current]\n"
+ + " <PACKAGE> an Android package name.\n"
+ + " <USER_ID> The user id under which the package is installed.\n"
+ + " Example:\n"
+ + " # Grant the \"foo.bar.baz\" package to bind app widgets for the current user.\n"
+ + " adb shell grantbind --package foo.bar.baz --user current\n"
+ + "\n"
+ + "usage: adb shell appwidget revokebind --package <PACKAGE> "
+ + "[--user <USER_ID> | current]\n"
+ + " <PACKAGE> an Android package name.\n"
+ + " <USER_ID> The user id under which the package is installed.\n"
+ + " Example:\n"
+ + " # Revoke the permisison to bind app widgets from the \"foo.bar.baz\" package.\n"
+ + " adb shell revokebind --package foo.bar.baz --user current\n"
+ + "\n";
+
+ private static class Parser {
+ private static final String ARGUMENT_GRANT_BIND = "grantbind";
+ private static final String ARGUMENT_REVOKE_BIND = "revokebind";
+ private static final String ARGUMENT_PACKAGE = "--package";
+ private static final String ARGUMENT_USER = "--user";
+ private static final String ARGUMENT_PREFIX = "--";
+ private static final String VALUE_USER_CURRENT = "current";
+
+ private final Tokenizer mTokenizer;
+
+ public Parser(String[] args) {
+ mTokenizer = new Tokenizer(args);
+ }
+
+ 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;
+ }
+ }
+
+ 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);
+ }
+
+ private String argumentValueRequired(String argument) {
+ String value = mTokenizer.nextArg();
+ if (TextUtils.isEmpty(value) || value.startsWith(ARGUMENT_PREFIX)) {
+ throw new IllegalArgumentException("No value for argument: " + argument);
+ }
+ return value;
+ }
+ }
+
+ private static class Tokenizer {
+ private final String[] mArgs;
+ private int mNextArg;
+
+ public Tokenizer(String[] args) {
+ mArgs = args;
+ }
+
+ private String nextArg() {
+ if (mNextArg < mArgs.length) {
+ return mArgs[mNextArg++];
+ } else {
+ return null;
+ }
+ }
+ }
+
+ private static class SetBindAppWidgetPermissionCommand implements Runnable {
+ final String mPackageName;
+ final boolean mGranted;
+ final int mUserId;
+
+ public SetBindAppWidgetPermissionCommand(String packageName, boolean granted,
+ int userId) {
+ mPackageName = packageName;
+ mGranted = granted;
+ mUserId = userId;
+ }
+
+ @Override
+ public void run() {
+ IBinder binder = ServiceManager.getService(Context.APPWIDGET_SERVICE);
+ IAppWidgetService appWidgetService = IAppWidgetService.Stub.asInterface(binder);
+ try {
+ appWidgetService.setBindAppWidgetPermission(mPackageName, mGranted, mUserId);
+ } catch (RemoteException re) {
+ re.printStackTrace();
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Parser parser = new Parser(args);
+ Runnable command = parser.parseCommand();
+ if (command != null) {
+ command.run();
+ }
+ }
+}