diff options
author | Clark Scheff <clark@cyngn.com> | 2015-02-02 09:42:23 -0800 |
---|---|---|
committer | Clark Scheff <clark@cyngn.com> | 2015-10-27 10:39:53 -0700 |
commit | 0e365250c8c08021c7bd9aa63bd171bfe195faf9 (patch) | |
tree | 8c9e0838bfbb5d2f885c54c8ab849dfd517ed249 /cmds/tm/src | |
parent | 7c5912d0af898691d87b72314dd04c01cff01b63 (diff) | |
download | frameworks_base-0e365250c8c08021c7bd9aa63bd171bfe195faf9.zip frameworks_base-0e365250c8c08021c7bd9aa63bd171bfe195faf9.tar.gz frameworks_base-0e365250c8c08021c7bd9aa63bd171bfe195faf9.tar.bz2 |
Themes: Add tm command line tool
This will allow us to perform automated testing using the theme
manager as well as a quick way to perform the various functions
that the theme service provides via the command line.
Change-Id: Id0aa906bb3400225b93080f7a8c2284a8fcf3c44
Diffstat (limited to 'cmds/tm/src')
-rw-r--r-- | cmds/tm/src/com/android/commands/tm/Tm.java | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/cmds/tm/src/com/android/commands/tm/Tm.java b/cmds/tm/src/com/android/commands/tm/Tm.java new file mode 100644 index 0000000..14101f7 --- /dev/null +++ b/cmds/tm/src/com/android/commands/tm/Tm.java @@ -0,0 +1,194 @@ +/* +** +** Copyright 2015, The CyanogenMod 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.tm; + +import android.content.pm.IPackageManager; +import android.content.pm.PackageInfo; +import android.content.pm.ParceledListSlice; +import android.content.pm.ThemeUtils; +import android.content.res.IThemeService; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.os.UserHandle; +import android.util.AndroidException; +import com.android.internal.os.BaseCommand; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Tm extends BaseCommand { + private static final String SYSTEM_THEME = "system"; + + IThemeService mTs; + IPackageManager mPm; + + /** + * Command-line entry point. + * + * @param args The command-line arguments + */ + public static void main(String[] args) { + (new Tm()).run(args); + } + + public void onShowUsage(PrintStream out) { + List<String> components = ThemeUtils.getAllComponents(); + StringBuilder sb = new StringBuilder(); + sb.append("usage: tm [subcommand] [options]\n"); + sb.append(" tm list\n"); + sb.append(" tm apply <PACKAGE_NAME> [-c <COMPONENT> [-c <COMPONENT>] ...]\n"); + sb.append(" tm rebuild\n"); + sb.append(" tm process <PACKAGE_NAME>\n"); + sb.append("\n"); + sb.append("tm list: return a list of theme packages.\n"); + sb.append("\n"); + sb.append("tm apply: applies the components for the theme specified by PACKAGE_NAME.\n"); + sb.append(" [-c <COMPONENT> [-c <COMPONENT>] ...]\n"); + sb.append(" if no components are specified all components will be applied.\n"); + sb.append(" Valid components are:\n"); + for (String component : components) { + sb.append(" "); + sb.append(component); + sb.append("\n"); + } + sb.append("\n"); + sb.append("tm rebuild: rebuilds the resource cache.\n"); + sb.append("\n"); + sb.append("tm process: processes the theme resources for the theme specified by " + + "PACKAGE_NAME.\n"); + + out.println(sb.toString()); + } + + public void onRun() throws Exception { + mTs = IThemeService.Stub.asInterface(ServiceManager.getService("themes")); + if (mTs == null) { + System.err.println(NO_SYSTEM_ERROR_CODE); + throw new AndroidException("Can't connect to theme service; is the system running?"); + } + + mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); + if (mPm == null) { + System.err.println(NO_SYSTEM_ERROR_CODE); + throw new AndroidException("Can't connect to package manager; is the system running?"); + } + + String op = nextArgRequired(); + + if (op.equals("list")) { + runListThemePackages(); + } else if (op.equals("apply")) { + runApplyTheme(); + } else if (op.equals("rebuild")) { + runRebuildResourceCache(); + } else if (op.equals("process")) { + runProcessTheme(); + } else { + showError("Error: unknown command '" + op + "'"); + return; + } + } + + private void runListThemePackages() throws Exception { + List<PackageInfo> packages = getInstalledPackages(mPm, 0, UserHandle.USER_OWNER); + + // there is always a "system" theme available + System.out.println("package:system [theme]"); + for (PackageInfo info : packages) { + if (info.isThemeApk || info.isLegacyIconPackApk) { + System.out.print("package:"); + System.out.print(info.packageName); + if (info.isThemeApk) { + System.out.println(" [theme]"); + } else { + System.out.println(" [icon pack]"); + } + } + } + } + + private void runApplyTheme() throws Exception { + String pkgName = nextArg(); + if (pkgName == null) { + System.err.println("Error: didn't specify theme package to apply"); + return; + } + if (!SYSTEM_THEME.equals(pkgName)) { + PackageInfo info = mPm.getPackageInfo(pkgName, 0, UserHandle.USER_OWNER); + if (info == null) { + System.err.println("Error: invalid package name"); + return; + } + if (!(info.isThemeApk || info.isLegacyIconPackApk)) { + System.err.println("Error: package is not a theme or icon pack"); + return; + } + } + + Map<String, String> componentMap = new HashMap<String, String>(); + String opt; + while ((opt=nextOption()) != null) { + if (opt.equals("-c")) { + componentMap.put(nextArgRequired(), pkgName); + } + } + + // No components specified so let's just try and apply EVERYTHING! + if (componentMap.size() == 0) { + List<String> components = ThemeUtils.getAllComponents(); + for (String component : components) { + componentMap.put(component, pkgName); + } + } + mTs.requestThemeChange(componentMap); + } + + private void runRebuildResourceCache() throws Exception { + mTs.rebuildResourceCache(); + } + + private void runProcessTheme() throws Exception { + String pkgName = nextArg(); + if (pkgName == null) { + System.err.println("Error: didn't specify theme package to apply"); + return; + } + PackageInfo info = mPm.getPackageInfo(pkgName, 0, UserHandle.USER_OWNER); + if (info == null) { + System.err.println("Error: invalid package name"); + return; + } + if (!info.isThemeApk) { + System.err.println("Error: package is not a theme"); + return; + } + + mTs.processThemeResources(pkgName); + } + + @SuppressWarnings("unchecked") + private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags, int userId) + throws RemoteException { + ParceledListSlice<PackageInfo> slice = pm.getInstalledPackages(flags, userId); + return slice.getList(); + } + +} |