OSDN Git Service

Add utility for toggling settings from the cmd line.
authorBrett Chabot <brettchabot@android.com>
Mon, 11 Oct 2010 23:21:20 +0000 (16:21 -0700)
committerBrett Chabot <brettchabot@android.com>
Mon, 11 Oct 2010 23:25:03 +0000 (16:25 -0700)
Change-Id: I4dc7052c22fac3edf66e2d7db0c8d7039be4f196

apps/SettingsCmd/Android.mk [new file with mode: 0644]
apps/SettingsCmd/AndroidManifest.xml [new file with mode: 0644]
apps/SettingsCmd/src/com/android/settingscmd/SettingsInstrument.java [new file with mode: 0644]

diff --git a/apps/SettingsCmd/Android.mk b/apps/SettingsCmd/Android.mk
new file mode 100644 (file)
index 0000000..282b89b
--- /dev/null
@@ -0,0 +1,29 @@
+# Copyright (C) 2010 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_PACKAGE_NAME := SettingsCmd
+LOCAL_CERTIFICATE := platform
+
+LOCAL_SDK_VERSION := 4
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+include $(BUILD_PACKAGE)
diff --git a/apps/SettingsCmd/AndroidManifest.xml b/apps/SettingsCmd/AndroidManifest.xml
new file mode 100644 (file)
index 0000000..63553fa
--- /dev/null
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (C) 2010 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.
+ -->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="com.android.settingscmd">
+
+    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
+    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+
+    <application>
+    </application>
+
+    <instrumentation android:name=".SettingsInstrument"
+                     android:targetPackage="com.android.settingscmd"
+                     android:label="instrumentation utility to modify system settings"/>
+
+    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="4" />
+
+</manifest>
diff --git a/apps/SettingsCmd/src/com/android/settingscmd/SettingsInstrument.java b/apps/SettingsCmd/src/com/android/settingscmd/SettingsInstrument.java
new file mode 100644 (file)
index 0000000..1f8d9ec
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2010, 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.settingscmd;
+
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.os.Bundle;
+import android.provider.Settings;
+
+/**
+ * Utility for modifying system settings.
+ * <p/>
+ * Usage:
+ * <p/>
+ * adb shell am instrument -w [-e secure true/false] -e name <setting name>
+ * -e value <setting value> -w com.android.settingscmd/.SettingsInstrument
+ * <p/>
+ * This apk must be signed with the platform certificate to modify secure settings.
+ */
+public class SettingsInstrument extends Instrumentation {
+
+    private boolean mSecure;
+    private String mName;
+    private String mValue;
+
+    public SettingsInstrument() {
+        super();
+    }
+
+    @Override
+    public void onCreate(Bundle arguments) {
+        mSecure = getBooleanArgument(arguments, "secure");
+        mName = arguments.getString("name");
+        mValue = arguments.getString("value");
+        start();
+    }
+
+    private boolean getBooleanArgument(Bundle arguments, String tag) {
+        String tagString = arguments.getString(tag);
+        return tagString != null && Boolean.parseBoolean(tagString);
+    }
+
+    @Override
+    public void onStart() {
+        if (mName == null || mValue == null) {
+            reportError("Missing arguments. Usage:\n [-e secure true] "
+                    + "-e name <setting name> -e value <setting value>");
+            return;
+        }
+        boolean status = false;
+        if (mSecure) {
+            status = Settings.Secure.putString(getTargetContext().getContentResolver(), mName,
+                    mValue);
+        } else {
+            status = Settings.System.putString(getTargetContext().getContentResolver(), mName,
+                    mValue);
+        }
+        Bundle bundleResponse = new Bundle();
+        bundleResponse.putBoolean("result", status);
+        finish(Activity.RESULT_OK, bundleResponse);
+    }
+
+    private void reportError(String msg) {
+        Bundle bundleResponse = new Bundle();
+        bundleResponse.putString("error", msg);
+        finish(Activity.RESULT_CANCELED, bundleResponse);
+    }
+}