/*
**
** Copyright 2007, 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.tm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.helloworld.android.tvapi.common.TvManager;
import com.helloworld.android.tvapi.common.PictureManager;
import com.helloworld.android.tvapi.common.AudioManager;
import com.helloworld.android.tvapi.common.CecManager;
import com.helloworld.android.tvapi.common.ChannelManager;
import com.helloworld.android.tvapi.common.DatabaseManager;
import com.helloworld.android.tvapi.common.LogoManager;
import com.helloworld.android.tvapi.common.MhlManager;
import com.helloworld.android.tvapi.common.ParentalcontrolManager;
import com.helloworld.android.tvapi.common.PipManager;
import com.helloworld.android.tvapi.common.PvrManager;
import com.helloworld.android.tvapi.common.TimerManager;
import com.helloworld.android.tvapi.factory.FactoryManager;
public class Tm {
private static final String CLASSNAME = "--class=";
private static final String METHOD = "--method=";
private static final String STATIC_METHOD = "--static=";
private static final String ARGS_INT = "-i";
private static final String ARGS_CHAR = "-c";
private static final String ARGS_SHORT = "-s";
private static final String ARGS_DOUBLE = "-d";
private static final String ARGS_STRING = "-S";
private static final String ARGS_BOOLEAN = "-b";
private static final String ARGS_ENUM = "-e";
private static final String ARGS_LONG = "-l";
private static final String CLASS_NAME_TVMANAGER = "com.helloworld.android.tvapi.common.TvManager";
private static final String CLASS_NAME_PICTUREMANAGER = "com.helloworld.android.tvapi.common.PictureManager";
private static final String CLASS_NAME_AUDIOMANAGER = "com.helloworld.android.tvapi.common.AudioManager";
private static final String CLASS_NAME_CECMANAGER = "com.helloworld.android.tvapi.common.CecManager";
private static final String CLASS_NAME_ChannelManager = "com.helloworld.android.tvapi.common.ChannelManager";
private static final String CLASS_NAME_DatabaseManager = "com.helloworld.android.tvapi.common.DatabaseManager";
private static final String CLASS_NAME_MhlManager = "com.helloworld.android.tvapi.common.MhlManager";
private static final String CLASS_NAME_ParentalcontrolManager = "com.helloworld.android.tvapi.common.ParentalcontrolManager";
private static final String CLASS_NAME_PipManager = "com.helloworld.android.tvapi.common.PipManager";
private static final String CLASS_NAME_PvrManager = "com.helloworld.android.tvapi.common.PvrManager";
private static final String CLASS_NAME_TimerManager = "com.helloworld.android.tvapi.common.TimerManager";
private static final String CLASS_NAME_FactoryManager = "com.helloworld.android.tvapi.factory.FactoryManager";
private HashMap<String, Object> allObjs = new HashMap<String, Object>();
public Tm() {
allObjs.put(CLASS_NAME_TVMANAGER, TvManager.getInstance());
allObjs.put(CLASS_NAME_PICTUREMANAGER, PictureManager.getInstance());
allObjs.put(CLASS_NAME_AUDIOMANAGER, TvManager.getInstance()
.getAudioManager());
allObjs.put(CLASS_NAME_CECMANAGER, TvManager.getInstance()
.getCecManager());
allObjs.put(CLASS_NAME_ChannelManager, TvManager.getInstance()
.getChannelManager());
allObjs.put(CLASS_NAME_DatabaseManager, TvManager.getInstance()
.getDatabaseManager());
allObjs.put(CLASS_NAME_MhlManager, TvManager.getInstance()
.getMhlManager());
allObjs.put(CLASS_NAME_ParentalcontrolManager, TvManager.getInstance()
.getParentalcontrolManager());
allObjs.put(CLASS_NAME_PipManager, TvManager.getInstance()
.getPipManager());
allObjs.put(CLASS_NAME_PvrManager, TvManager.getInstance()
.getPvrManager());
allObjs.put(CLASS_NAME_TimerManager, TvManager.getInstance()
.getTimerManager());
allObjs.put(CLASS_NAME_FactoryManager,TvManager.getInstance().getFactoryManager());
}
public static void main(String[] args) {
try {
(new Tm()).run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
private void run(String[] args) throws Exception {
dumpArgs(args);
if (args.length < 2) {
showUsage();
return;
}
int index = 0;
String command = args[index];
String hasClassName = null;
String hasMethodName = null;
String hasStaticMethod = null;
boolean hasArgs = false;
String[] argsArray = null;
int argsCount = 0;
if (args.length > 2) {
hasArgs = true;
argsArray = new String[args.length - 2];
}
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith(CLASSNAME)) {
hasClassName = args[i].substring(CLASSNAME.length());
} else if (args[i].startsWith(METHOD)) {
hasMethodName = args[i].substring(METHOD.length());
} else if (args[i].startsWith(STATIC_METHOD)) {
hasStaticMethod = args[i].substring(STATIC_METHOD.length());
} else {
if (argsArray != null) {
argsArray[argsCount++] = args[i];
}
}
}
System.out.println("ClassName : " + hasClassName + "Method : "
+ hasMethodName + " StaticMethod : " + hasStaticMethod);
System.out.println("dump Args :");
dumpArgs(argsArray);
if ((hasClassName == null)
|| ((hasClassName != null) && (hasMethodName == null) && (hasStaticMethod == null))) {
System.out.println("Error ClassName : " + hasClassName
+ "Method : " + hasMethodName + " StaticMethod : "
+ hasStaticMethod);
}
Object[] argsObjArray = null;
Class[] argsClassArray = null;
if (hasArgs) {
argsObjArray = getObjectArrayForString(argsArray);
argsClassArray = getClassArrayForString(argsArray);
if (argsObjArray.length != argsClassArray.length) {
System.out.println("dismatched args");
}
}
dumpArgs(argsObjArray);
dumpArgs(argsClassArray);
Class cls = null;
try {
cls = Class.forName(hasClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
cls = null;
System.out.println(hasClassName + " not found");
}
if (cls == null) {
return;
}
Constructor[] constructors = cls.getDeclaredConstructors();
if (constructors == null || constructors.length == 0)
return;
Method[] allMethod = null;
try {
allMethod = cls.getDeclaredMethods();
} catch (Exception e) {
e.printStackTrace();
System.out.println("this object has no method");
}
setAllMethodAccessible(allMethod);
Field[] allField = null;
try {
allField = cls.getDeclaredFields();
} catch (Exception e) {
e.printStackTrace();
System.out.println("this object has no field");
}
setAllFieldAccessible(allField);
Object obj = getObjectForClassName(hasClassName);
Method method = cls.getDeclaredMethod(hasMethodName, argsClassArray);
Object result = method.invoke(obj, argsObjArray);
System.out.println(hasClassName + "." + hasMethodName + " return "
+ result);
}
private Object getObjectForClassName(String classname) {
return allObjs.get(classname);
}
private void setAllMethodAccessible(Method[] methods) {
if (methods == null || methods.length <= 0)
return;
for (Method method : methods) {
method.setAccessible(true);
}
}
private void setAllFieldAccessible(Field[] fields) {
if (fields == null || fields.length <= 0)
return;
for (Field field : fields) {
field.setAccessible(true);
}
}
private Object[] getObjectArrayForString(String args[]) throws Exception {
if (args == null || args.length < 1)
return (Object[]) null;
Object[] tmp = new Object[args.length];
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ((arg == null) || (arg.length() <= 2) || (!arg.startsWith("-"))
|| (!isObjectTypeLegal(arg.charAt(1)))) {
throw (new Exception("args nor right"));
} else {
Object obj = getObjectForString(arg);
if (obj == null)
throw (new Exception("args nor right"));
tmp[i] = obj;
}
}
return tmp;
}
private Class[] getClassArrayForString(String[] args) throws Exception {
if (args == null || args.length < 1)
return (Class[]) null;
Class[] tmp = new Class[args.length];
for (int i = 0; i < args.length; i++) {
tmp[i] = getClassForString(args[i]);
if(tmp[i] == null){
throw (new Exception("args nor right,can't find class"));
}
}
return tmp;
}
private boolean isObjectTypeLegal(char type) {
if (type == 'i' || type == 'c' || type == 's' || type == 'd'
|| type == 'S' || type == 'b' || type == 'e')
return true;
return false;
}
private Class getClassForString(String arg) {
if ((arg == null) || (arg.length() <= 2))
return null;
if (arg.startsWith(ARGS_BOOLEAN)) {
return boolean.class;
}
if (arg.startsWith(ARGS_CHAR)) {
return char.class;
}
if (arg.startsWith(ARGS_DOUBLE)) {
return double.class;
}
if (arg.startsWith(ARGS_INT)) {
return int.class;
}
if (arg.startsWith(ARGS_SHORT)) {
return short.class;
}
if (arg.startsWith(ARGS_STRING)) {
return String.class;
}
if (arg.startsWith(ARGS_LONG)) {
return long.class;
}
if(arg.startsWith(ARGS_ENUM)){
String tmp = convertString(arg.substring(2));
String enumClassName = tmp.substring(0, tmp.lastIndexOf("."));
Class cls =null;
try {
cls = Class.forName(enumClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
cls= null;
}
return cls;
}
return null;
}
private String convertString(String source){
if(source != null)
return source.replace("*", "$");
return null;
}
private Object getObjectForString(String arg) {
if ((arg == null) || (arg.length() <= 2))
return null;
if (arg.startsWith(ARGS_BOOLEAN)) {
String tmp = arg.substring(2);
if ("true".equals(tmp) || "1".equals(tmp)) {
return true;
}
if ("false".equals(tmp) || "0".equals(tmp)) {
return false;
}
return null;
}
if (arg.startsWith(ARGS_CHAR)) {
char tmp = arg.charAt(2);
return tmp;
}
if (arg.startsWith(ARGS_DOUBLE)) {
String tmp = arg.substring(2);
return new Double(tmp);
}
if (arg.startsWith(ARGS_INT)) {
String tmp = arg.substring(2);
return Integer.parseInt(tmp);
}
if (arg.startsWith(ARGS_SHORT)) {
String tmp = arg.substring(2);
return Short.parseShort(tmp);
}
if (arg.startsWith(ARGS_STRING)) {
String tmp = arg.substring(2);
return tmp;
}
if (arg.startsWith(ARGS_LONG)) {
String tmp = arg.substring(2);
return Long.parseLong(tmp);
}
if (arg.startsWith(ARGS_ENUM)) {
String tmp = convertString(arg.substring(2));
String enumClassName = tmp.substring(0, tmp.lastIndexOf("."));
String enumName = tmp.substring(tmp.lastIndexOf(".") + 1);
Class cls =null;
try {
cls = Class.forName(enumClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
cls= null;
}
if(cls == null) return null;
System.out.println("enumClassName="+enumClassName + " enumName="+enumName);
return Enum.valueOf(cls, enumName);
}
return null;
}
private void dumpArgs(Object[] args) {
if (args != null) {
for (Object tmp : args) {
System.out.print(tmp + "|");
}
}
System.out.println();
}
private void showUsage() {
//tm --class=com.helloworld.android.tvapi.common.PictureManager --method=setBacklight -i10
//tm --class=com.helloworld.android.tvapi.common.PictureManager --method=setMfc -ecom.helloworld.android.tvapi.common.vo.EnumMfcMode.E_OFF
System.out.println("tm --class=com.helloworld.xxx --method=xxx [-iXXX -cXXX -sXXX -dXXX -SXXX -bXXX -eXXX -lXXX]");
System.out.println("--method you can append your class argument after --class,such as --class=com.helloworld.android.tvapi.common.PictureManager");
System.out.println("--method you can append your method argument after --method,such as --method=setBacklight");
System.out.println("-iXXX you can append an int argument after -i,such as -i10");
System.out.println("-cXXX you can append an char argument after -c,such as -ic");
System.out.println("i represents integer, c represents char, s represents shot, S represents String, d represent double, b reprents bool , e reprements enum, l represents long");
System.out.println("-e is a little complex, for example public final void setMfc(EnumMfcMode mfcMode), you should use like follow");
System.out.println("tm --class=com.helloworld.android.tvapi.common.PictureManager --method=setMfc -ecom.helloworld.android.tvapi.common.vo.EnumMfcMode.E_OFF");
System.err.println();
}
}