找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2414|回复: 1

豌豆荚基础库

[复制链接]

307

主题

228

回帖

7343

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5593 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
发表于 2016-3-20 17:21:57 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. package base.dexs;

  2. import android.content.Context;
  3. import android.os.Build;
  4. import base.reflect.JavaCalls;
  5. import base.utils.ArrayUtil;
  6. import dalvik.system.DexFile;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Set;
  10. import java.util.zip.ZipFile;

  11. public class ContextClassLoaderUtility {
  12.     private static final String LOG_TAG = "ContextClassLoaderUtility";
  13.     private ContextClassLoaderUtility() {
  14.         super();
  15.     }

  16.     static class Element {
  17.         public final File file;
  18.         public final ZipFile zipFile;
  19.         public final DexFile dexFile;

  20.         public Element(File file, ZipFile zipFile, DexFile dexFile) {
  21.             this.file = file;
  22.             this.zipFile = zipFile;
  23.             this.dexFile = dexFile;
  24.         }
  25.     }

  26.     private static DexFile createDexFile(File file, File optimizedDirectory) {
  27.         if(Build.VERSION.SDK_INT >= 14) {
  28.             return (DexFile) JavaCalls.callStaticMethod("dalvik.system.DexPathList", "loadDexFile",
  29.                     new Object[]{file, optimizedDirectory});
  30.         }
  31.         else{
  32.             String outputName = (String) JavaCalls.callStaticMethod("dalvik.system.DexClassLoader", "generateOutputName",
  33.                     new Object[]{file.getAbsolutePath(), optimizedDirectory.getAbsolutePath()});
  34.             try {
  35.                 return DexFile.loadDex(file.getAbsolutePath(), outputName, 0);
  36.             }
  37.             catch(IOException e) {
  38.             }
  39.         }
  40.         return null;
  41.     }

  42.     private static Element createElement(File file, File optimizedDirectory) {
  43.         String fileName = file.getName();
  44.         ZipFile zipfile = null;
  45.         DexFile dexfile = null;
  46.         if(!fileName.endsWith(".dex")) {
  47.             if(!fileName.endsWith(".apk") && !fileName.endsWith(".jar") && !fileName.endsWith(".zip") && !fileName.endsWith(".so")) {
  48.                 throw new IllegalArgumentException("Unknown file type for: " + file);
  49.             }
  50.             try {
  51.                 zipfile = new ZipFile(file);
  52.             }
  53.             catch(IOException e) {

  54.             }
  55.         }
  56.         dexfile = ContextClassLoaderUtility.createDexFile(file, optimizedDirectory);
  57.         return new Element(file, zipfile, dexfile);
  58.     }

  59.     private static Object[] createEmptyDexElements(int num) {
  60.         Class cls = null;
  61.         try {
  62.             cls = Class.forName("dalvik.system.DexPathList$Element");
  63.         }
  64.         catch(ClassNotFoundException v0) {
  65.             throw new IllegalArgumentException("Can't find class: dalvik.system.DexPathList$Element");
  66.         }
  67.         Object[] obj = new Object[num];
  68.         for(int i = 0; i < num; ++i) {
  69.             obj[i] = JavaCalls.newEmptyInstance(cls);
  70.         }
  71.         return obj;
  72.     }

  73.     private static Object[] getDexElements(ClassLoader clsloader) {
  74.         return (Object[]) JavaCalls.getField(JavaCalls.getField(clsloader, "pathList"), "dexElements");
  75.     }

  76.     private static File getOptDir(Context context) {
  77.         File dir = new File(context.getFilesDir(), "opt");
  78.         if(!dir.exists()) {
  79.             if(!dir.mkdirs()) {
  80.                 return null;//create opt dir meet ex.
  81.             }
  82.             return dir;
  83.         }
  84.         else if(dir.isFile()) {
  85.             if((dir.delete()) && (dir.mkdirs())) {
  86.                 return dir;
  87.             }
  88.         }
  89.         else if(dir.isDirectory()) {
  90.             return dir;
  91.         }
  92.         return null;//create opt dir meet ex.
  93.     }

  94.     public static void inject(Context context, Set<String> pathset) {
  95.         if(pathset != null && !pathset.isEmpty()) {
  96.             String[] patharr = pathset.toArray(new String[pathset.size()]);
  97.             if(Build.VERSION.SDK_INT >= 14) {
  98.                 ContextClassLoaderUtility.injectV14(context, patharr);
  99.             }
  100.             else {
  101.                 ContextClassLoaderUtility.injectBelowV14(context, patharr);
  102.             }
  103.         }
  104.     }

  105.     private static void injectBelowV14(Context context, String[] patharr) {
  106.         File dir = ContextClassLoaderUtility.getOptDir(context);
  107.         File[] files = new File[patharr.length];
  108.         ZipFile[] zfiles = new ZipFile[patharr.length];
  109.         DexFile[] dfiles = new DexFile[patharr.length];
  110.         for(int i = 0; i < patharr.length; ++i) {
  111.             ContextClassLoaderUtility.resetValueBelowV14(new File(patharr[i]), dir, files, zfiles, dfiles, i);
  112.         }
  113.         ClassLoader loader = context.getClassLoader();
  114.         Object mFiles = JavaCalls.getField(loader, "mFiles");
  115.         Object mPaths = JavaCalls.getField(loader, "mPaths");
  116.         Object mZips = JavaCalls.getField(loader, "mZips");
  117.         Object mDexs = JavaCalls.getField(loader, "mDexs");
  118.         JavaCalls.setField(loader, "mPaths", ArrayUtil.combineArray((Object[])mPaths, patharr));
  119.         JavaCalls.setField(loader, "mFiles", ArrayUtil.combineArray((Object[])mFiles, files));
  120.         JavaCalls.setField(loader, "mZips", ArrayUtil.combineArray((Object[])mZips, zfiles));
  121.         JavaCalls.setField(loader, "mDexs", ArrayUtil.combineArray((Object[])mDexs, dfiles));
  122.     }

  123.     private static void injectV14(Context context, String[] file) {
  124.         File dir = ContextClassLoaderUtility.getOptDir(context);
  125.         Object[] objarr = ContextClassLoaderUtility.createEmptyDexElements(file.length);
  126.         for(int i = 0; i < file.length; ++i) {
  127.             ContextClassLoaderUtility.resetValueV14(new File(file[i]), dir, objarr[i]);
  128.         }
  129.         ClassLoader loader = context.getClassLoader();
  130.         ContextClassLoaderUtility.setDexElements(loader, ArrayUtil.combineArray(ContextClassLoaderUtility.getDexElements(loader), objarr));
  131.     }

  132.     private static void resetValueBelowV14(File file, File optimizedDirectory, Object[] files, Object[] zfiles, Object[] dfiles, int index) {
  133.         Element ele = createElement(file, optimizedDirectory);
  134.         if(ele != null) {
  135.             if(index < files.length) {
  136.                 files[index] = ele.file;
  137.             }
  138.             if(index < zfiles.length) {
  139.                 zfiles[index] = ele.zipFile;
  140.             }
  141.             if(index < dfiles.length) {
  142.                 dfiles[index] = ele.dexFile;
  143.             }
  144.         }
  145.     }

  146.     private static void resetValueV14(File file, File optimizedDirectory, Object clsobj) {
  147.         Element ele = createElement(file, optimizedDirectory);
  148.         if(ele != null) {
  149.             JavaCalls.setField(clsobj, "file", ele.file);
  150.             JavaCalls.setField(clsobj, "zipFile", ele.zipFile);
  151.             JavaCalls.setField(clsobj, "dexFile", ele.dexFile);
  152.         }
  153.     }

  154.     private static void setDexElements(ClassLoader loader, Object[] elements) {
  155.         JavaCalls.setField(JavaCalls.getField(loader, "pathList"), "dexElements", elements);
  156.     }
  157. }
  158. package base.dexs;

  159. import android.content.Context;
  160. import android.os.Build;

  161. import java.io.File;
  162. import java.util.List;

  163. import base.reflect.JavaCalls;
  164. import base.utils.ArrayUtil;

  165. public class ContextLibraryUtility {
  166.     private ContextLibraryUtility() {
  167.         super();
  168.     }

  169.     public static void inject(Context context, File file) {
  170.         if(Build.VERSION.SDK_INT >= 14) {
  171.             ContextLibraryUtility.injectV14(context, file);
  172.         }
  173.         else if(Build.VERSION.SDK_INT >= 9) {
  174.             injectV9(context, file);
  175.         }
  176.         else {
  177.            injectBelowV9(context, file);
  178.         }
  179.     }

  180.     private static void injectBelowV9(Context context, File libdir) {
  181.         String[] v0_2;
  182.         ClassLoader loader = context.getClassLoader();
  183.         Object[] mLibPathsOld = (Object[]) JavaCalls.getField(loader, "mLibPaths");
  184.         Object[] mLibPathsNew = null;
  185.         String libdirslash = libdir.getAbsolutePath() + "/";
  186.         if(mLibPathsOld.length > 0) {
  187.             mLibPathsNew = ArrayUtil.insert(mLibPathsOld, 1, libdirslash);
  188.         }
  189.         else {
  190.             mLibPathsNew = new String[]{libdirslash};
  191.         }

  192.         JavaCalls.setField(loader, "mLibPaths", mLibPathsNew);
  193.     }

  194.     private static void injectV14(Context context, File libdir) {
  195.         Object pathList = JavaCalls.getField(context.getClassLoader(), "pathList");
  196.         Object[] nativeLibraryDirectoriesOld = (Object[]) JavaCalls.getField(pathList, "nativeLibraryDirectories");
  197.         Object[] nativeLibraryDirectoriesNew = null;
  198.         if(nativeLibraryDirectoriesOld.length > 0) {
  199.             nativeLibraryDirectoriesNew = ArrayUtil.insert(nativeLibraryDirectoriesOld, 1, libdir);
  200.         }
  201.         else {
  202.             nativeLibraryDirectoriesNew = new File[]{libdir};
  203.         }
  204.         JavaCalls.setField(pathList, "nativeLibraryDirectories", nativeLibraryDirectoriesNew);
  205.     }

  206.     private static void injectV9(Context context, File libdir) {
  207.         List libraryPathElements = (List) JavaCalls.getField(context.getClassLoader(), "libraryPathElements");
  208.         String libdirslash = libdir.getAbsolutePath() + "/";
  209.         if((libraryPathElements).size() > 0) {
  210.             libraryPathElements.add(1, libdirslash);
  211.         }
  212.         else {
  213.             libraryPathElements.add(libdirslash);
  214.         }
  215.     }
  216. }
复制代码

  1. package base.reflect;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.HashMap;
  7. import java.util.Map;

  8. @SuppressWarnings("unchecked")
  9. public class JavaCalls {
  10.     private static final String LOG_TAG = "JavaCalls";
  11.     private static Map<Class<?>,Class<?>> PRIMITIVE_MAP = null;
  12.    
  13.     public class JavaParam{
  14.             Class clazz;
  15.             Object obj;
  16.             public JavaParam(Class cls,Object o){
  17.                     clazz = cls;
  18.                     obj = o;
  19.             }
  20.     }

  21.     static  {
  22.         JavaCalls.PRIMITIVE_MAP = new HashMap<Class<?>,Class<?>>();
  23.         JavaCalls.PRIMITIVE_MAP.put(Boolean.class, Boolean.TYPE);
  24.         JavaCalls.PRIMITIVE_MAP.put(Byte.class, Byte.TYPE);
  25.         JavaCalls.PRIMITIVE_MAP.put(Character.class, Character.TYPE);
  26.         JavaCalls.PRIMITIVE_MAP.put(Short.class, Short.TYPE);
  27.         JavaCalls.PRIMITIVE_MAP.put(Integer.class, Integer.TYPE);
  28.         JavaCalls.PRIMITIVE_MAP.put(Float.class, Float.TYPE);
  29.         JavaCalls.PRIMITIVE_MAP.put(Long.class, Long.TYPE);
  30.         JavaCalls.PRIMITIVE_MAP.put(Double.class, Double.TYPE);
  31.         JavaCalls.PRIMITIVE_MAP.put(Boolean.TYPE, Boolean.TYPE);
  32.         JavaCalls.PRIMITIVE_MAP.put(Byte.TYPE, Byte.TYPE);
  33.         JavaCalls.PRIMITIVE_MAP.put(Character.TYPE, Character.TYPE);
  34.         JavaCalls.PRIMITIVE_MAP.put(Short.TYPE, Short.TYPE);
  35.         JavaCalls.PRIMITIVE_MAP.put(Integer.TYPE, Integer.TYPE);
  36.         JavaCalls.PRIMITIVE_MAP.put(Float.TYPE, Float.TYPE);
  37.         JavaCalls.PRIMITIVE_MAP.put(Long.TYPE, Long.TYPE);
  38.         JavaCalls.PRIMITIVE_MAP.put(Double.TYPE, Double.TYPE);
  39.     }

  40.     public JavaCalls() {
  41.         super();
  42.     }

  43.     public static Object callMethod(Object clsObject, String methodName, Object[] params) {
  44.         Object result;
  45.         try {
  46.             result = JavaCalls.callMethodOrThrow(clsObject,methodName,params);
  47.         }
  48.         catch(Exception v0) {
  49.             result = null;
  50.         }

  51.         return result;
  52.     }

  53.     public static Object callMethodOrThrow(Object clsObject, String methodName, Object[] params)
  54.                     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  55.         return JavaCalls.getDeclaredMethod(clsObject.getClass(), methodName, JavaCalls.getParameterTypes(params))
  56.                 .invoke(clsObject, JavaCalls.getParameters(params));
  57.     }

  58.     public static Object callStaticMethod(String clsName, String method, Object[] params) {
  59.         Object obj = null;
  60.         try {
  61.             obj = JavaCalls.callStaticMethodOrThrow(Class.forName(clsName), method, params);
  62.         }
  63.         catch(Exception v0) {
  64.         }
  65.         return obj;
  66.     }

  67.     public static Object callStaticMethodOrThrow(Class cls, String method, Object[] params)
  68.                     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  69.         return JavaCalls.getDeclaredMethod(cls, method, JavaCalls.getParameterTypes(params)).invoke(null
  70.                 , JavaCalls.getParameters(params));
  71.     }

  72.     public static Object callStaticMethodOrThrow(String clsName, String method, Object[] params)
  73.                     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
  74.         return JavaCalls.getDeclaredMethod(Class.forName(clsName), method, JavaCalls.getParameterTypes(params
  75.                 )).invoke(null, JavaCalls.getParameters(params));
  76.     }

  77.     /*
  78.      * �Ƚ��෽����������
  79.      */
  80.     private static boolean compareClassLists(Class[] paramTypes1, Class[] paramTypes2) {
  81.         if(paramTypes1 == null || paramTypes1.length == 0) {
  82.             if(paramTypes2 == null || paramTypes2.length == 0) {
  83.                 return true;
  84.             }
  85.                         else{
  86.                                 return false;
  87.                         }
  88.         }
  89.         else {
  90.                 if(paramTypes2 == null || paramTypes2.length == 0){
  91.                         return false;
  92.                 }
  93.                 if(paramTypes1.length != paramTypes2.length){
  94.                         return false;
  95.                 }
  96.                 for(int i=0;i<paramTypes1.length;i++){
  97.                         if(!paramTypes1[i].isAssignableFrom(paramTypes2[i])) {
  98.                     if(JavaCalls.PRIMITIVE_MAP.containsKey(paramTypes1[i])) {
  99.                         if(!JavaCalls.PRIMITIVE_MAP.get(paramTypes1[i]).equals(JavaCalls.PRIMITIVE_MAP.get(
  100.                                 paramTypes2[i]))) {
  101.                                 return false;
  102.                         }
  103.                     }
  104.                 }
  105.                 }
  106.                 return true;
  107.         }
  108.     }

  109.     private static Method findMethodByName(Method[] methodArr, String methodName, Class[] methodParamCls) {
  110.         Method method = null;
  111.         if(methodName == null) {
  112.             throw new NullPointerException("Method name must not be null.");
  113.         }
  114.         for(int i = 0; i < methodArr.length; ++i) {
  115.             method = methodArr[i];
  116.             if((method.getName().equals(methodName)) && (JavaCalls.compareClassLists(method.getParameterTypes(), methodParamCls
  117.                     ))) {
  118.                 return method;
  119.             }
  120.         }
  121.         return null;
  122.     }

  123.     private static Method getDeclaredMethod(Class cls, String methodName, Class[] methodParamCls) {
  124.         Method method = null;
  125.         try{
  126.             while(method == null) {
  127.                 method = JavaCalls.findMethodByName(cls.getDeclaredMethods(), methodName, methodParamCls);
  128.                 if(method == null) {
  129.                     if(cls.getSuperclass() == null) {
  130.                             throw new NoSuchMethodException();
  131.                     }
  132.                     cls = cls.getSuperclass();
  133.                 }
  134.             }
  135.             method.setAccessible(true);       
  136.         }
  137.         catch(Exception e){
  138.                
  139.         }
  140.         return method;
  141.     }

  142.     private static Object getDefaultValue(Class cls) {
  143.         if((Integer.TYPE.equals(cls)) || (Integer.class.equals(cls)) || (Byte.TYPE.equals(cls)) ||
  144.                 (Byte.class.equals(cls)) || (Short.TYPE.equals(cls)) || (Short.class.equals(cls))
  145.                  || (Long.TYPE.equals(cls)) || (Long.class.equals(cls)) || (Double.TYPE.equals(cls
  146.                 )) || (Double.class.equals(cls)) || (Float.TYPE.equals(cls)) || (Float.class.equals
  147.                 (cls))) {
  148.             return Integer.valueOf(0);
  149.         }
  150.         else {
  151.             if(!Boolean.TYPE.equals(cls) && !Boolean.class.equals(cls)) {
  152.                 if(!Character.TYPE.equals(cls) && !Character.class.equals(cls)) {
  153.                     return null;
  154.                 }
  155.                 return Character.valueOf('\u0000');
  156.             }
  157.             return Boolean.valueOf(false);
  158.         }
  159.     }

  160.     public static Object getField(Object clsObject, String fieldName) {
  161.         try {
  162.                         return JavaCalls.getFieldOrThrow(clsObject, fieldName);
  163.                 }
  164.                 catch (IllegalArgumentException e) {
  165.                
  166.                 }
  167.                 catch (IllegalAccessException e) {
  168.                
  169.                 }
  170.                 catch (NoSuchFieldException e) {
  171.                
  172.                 }
  173.                 return null;
  174.     }

  175.     public static Object getFieldOrThrow(Object clsObj, String fieldName)
  176.     throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
  177.         Class cls = clsObj.getClass();
  178.         Field field = null;
  179.         do {
  180.             if(field != null) {
  181.                     field.setAccessible(true);
  182.                         return field.get(clsObj);
  183.             }

  184.             try {
  185.                 field = cls.getDeclaredField(fieldName);
  186.                 field.setAccessible(true);
  187.             }
  188.             catch(NoSuchFieldException v2) {
  189.                 cls = cls.getSuperclass();
  190.             }
  191.         }
  192.         while(cls != null);

  193.         throw new NoSuchFieldException();
  194.     }

  195.     private static Class[] getParameterTypes(Object[] clsObject) {
  196.         Class[] cls = null;
  197.         if(clsObject != null && clsObject.length > 0) {
  198.             cls = new Class[clsObject.length];
  199.             for(int i = 0; i < clsObject.length; ++i) {
  200.                 Object cur = clsObject[i];
  201.                 if(cur != null && ((cur instanceof JavaParam))) {
  202.                     cls[i] = ((JavaParam)cur).clazz;
  203.                 }
  204.                 else if(cur == null) {
  205.                     cls[i] = null;
  206.                 }
  207.                 else {
  208.                         cls[i] = cur.getClass();
  209.                 }
  210.             }
  211.         }
  212.         return cls;
  213.     }

  214.     private static Object[] getParameters(Object[] params) {
  215.         Object[] objs = null;
  216.         if(params != null && params.length > 0) {
  217.             objs = new Object[params.length];
  218.             for(int i = 0; i < params.length; ++i) {
  219.                 Object cur = params[i];
  220.                 if(cur == null || !(cur instanceof JavaParam)) {
  221.                     objs[i] = cur;
  222.                 }
  223.                 else {
  224.                     objs[i] = ((JavaParam)cur).obj;
  225.                 }
  226.             }
  227.         }
  228.         return objs;
  229.     }

  230.     public static Object newEmptyInstance(Class cls) {
  231.         Object o = null;
  232.         try {
  233.             o = JavaCalls.newEmptyInstanceOrThrow(cls);
  234.         }
  235.         catch(Exception v0) {
  236.         }
  237.         return o;
  238.     }

  239.     public static Object newEmptyInstanceOrThrow(Class cls)
  240.             throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
  241.         Object o;
  242.         Constructor[] cons = cls.getDeclaredConstructors();
  243.         if(cons != null && cons.length != 0) {
  244.             Constructor cur = cons[0];
  245.             cur.setAccessible(true);
  246.             Class[] paramcls = cur.getParameterTypes();
  247.             if(paramcls == null || paramcls.length == 0) {
  248.                 o = cur.newInstance();
  249.             }
  250.             else {
  251.                 Object[] tmp = new Object[paramcls.length];
  252.                 for(int i=0;i<paramcls.length;i++){
  253.                         tmp[i] = JavaCalls.getDefaultValue(paramcls[i]);
  254.                 }
  255.                 o = cur.newInstance(tmp);
  256.             }
  257.             return o;
  258.         }

  259.         throw new IllegalArgumentException("Can\'t get even one available constructor for " + cls);
  260.     }

  261.     public static Object newInstance(Class cls, Object[] params) {
  262.         Object o = null;
  263.         try {
  264.             o = JavaCalls.newInstanceOrThrow(cls, params);
  265.         }
  266.         catch(Exception v0) {
  267.         }
  268.         return o;
  269.     }

  270.     public static Object newInstance(String clsName, Object[] params) {
  271.         Object o = null;
  272.         try {
  273.             o = JavaCalls.newInstanceOrThrow(clsName, params);
  274.         }
  275.         catch(Exception v0) {

  276.         }
  277.         return o;
  278.     }

  279.     public static Object newInstanceOrThrow(Class cls, Object[] params)
  280.     throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  281.         return cls.getConstructor(JavaCalls.getParameterTypes(params)).newInstance(JavaCalls.getParameters
  282.                 (params));
  283.     }

  284.     public static Object newInstanceOrThrow(String clsName, Object[] params)
  285.     throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
  286.         return JavaCalls.newInstanceOrThrow(Class.forName(clsName), JavaCalls.getParameters(params));
  287.     }

  288.     public static void setField(Object clsObj, String fieldName, Object dataToset) {
  289.         try {
  290.             JavaCalls.setFieldOrThrow(clsObj, fieldName, dataToset);
  291.         }
  292.         catch(IllegalAccessException v0) {
  293.         }
  294.         catch(NoSuchFieldException v0_1) {
  295.         }
  296.     }

  297.     public static void setFieldOrThrow(Object clsObj, String fieldName, Object dataToset) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
  298.         Class cls = clsObj.getClass();
  299.         Field field = null;
  300.         do {
  301.             if(field != null) {
  302.                 field.setAccessible(true);
  303.                 field.set(clsObj, dataToset);
  304.                 return;
  305.             }
  306.             try {
  307.                 field = cls.getDeclaredField(fieldName);
  308.             }
  309.             catch(NoSuchFieldException v2) {
  310.                 cls = cls.getSuperclass();
  311.             }
  312.         }
  313.         while(cls != null);

  314.         throw new NoSuchFieldException();
  315.     }
  316. }
复制代码

  1. package base.storage;

  2. import android.content.Context;
  3. import android.content.IntentFilter;
  4. import android.content.SharedPreferences;
  5. import android.content.SharedPreferences$Editor;
  6. import android.os.Build;
  7. import android.os.Build$VERSION;
  8. import android.os.Environment;
  9. import android.os.Handler;
  10. import android.os.Looper;
  11. import android.text.TextUtils;
  12. import com.wandoujia.base.config.GlobalConfig;
  13. import com.wandoujia.base.utils.FileUtil;
  14. import com.wandoujia.base.utils.SharePrefSubmitor;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.lang.reflect.InvocationTargetException;
  22. import java.lang.reflect.Method;
  23. import java.nio.Buffer;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.List;

  28. public class StorageManager {
  29.     private static final int FORTH_LINE = 4;
  30.     private static final String GENERIC_CONFIG_PREFERENCE_NAME = "com.wandoujia.phoenix2";
  31.     private static final String KEY_LAST_USED_DIRECTORY = "key_last_used_directory";
  32.     private static final long LIMIT_SIZE = 52428800;
  33.     private static final int MIN_SDK = 14;
  34.     private static final String ROOT_DIR = "/wandoujia/";
  35.     private List availableStoragesPathList;
  36.     private String defaultExternalStorageDirectory;
  37.     private SharedPreferences genericSharedPrefs;
  38.     private static StorageManager instance;
  39.     private final List rdcListeners;

  40.     private StorageManager() {
  41.         super();
  42.         this.genericSharedPrefs = GlobalConfig.getAppContext().getSharedPreferences("com.wandoujia.phoenix2"
  43.                 , 0);
  44.         this.registerReceiver();
  45.         this.rdcListeners = new ArrayList();
  46.         this.availableStoragesPathList = this.getAvailableStorages();
  47.         this.defaultExternalStorageDirectory = this.genericSharedPrefs.getString("key_last_used_directory"
  48.                 , null);
  49.         this.checkDefaultPathAvailable();
  50.     }

  51.     public HashMap getAvailableDirectories(long arg10) {
  52.         HashMap v1 = new HashMap();
  53.         Iterator v2 = this.availableStoragesPathList.iterator();
  54.         while(v2.hasNext()) {
  55.             Object v0 = v2.next();
  56.             long v4 = FileUtil.getAvailableBytes(((String)v0));
  57.             if(v4 <= 52428800 + arg10) {
  58.                 continue;
  59.             }

  60.             v1.put(v0, Long.valueOf(v4));
  61.         }

  62.         return v1;
  63.     }

  64.     public String getAvailableDirectory(List arg13, long arg14) {
  65.         Object v1_1;
  66.         long v0_1;
  67.         Object v2_1;
  68.         String v1 = null;
  69.         long v4 = -1;
  70.         Iterator v6 = arg13.iterator();
  71.         while(v6.hasNext()) {
  72.             Object v0 = v6.next();
  73.             long v2 = FileUtil.getAvailableBytes(((String)v0));
  74.             if(v2 <= v4 || FileUtil.getAvailableBytes(((String)v0)) <= arg14) {
  75.                 v2_1 = v1;
  76.                 v0_1 = v4;
  77.             }
  78.             else {
  79.                 long v10 = v2;
  80.                 v2_1 = v0;
  81.                 v0_1 = v10;
  82.             }

  83.             v4 = v0_1;
  84.             v1_1 = v2_1;
  85.         }

  86.         return ((String)v1_1);
  87.     }



  88.     public List getExternalStorageDirectories() {
  89.         return this.availableStoragesPathList;
  90.     }

  91.     public String getExternalStorageDirectory() {
  92.         return this.getExternalStorageDirectory(0);
  93.     }

  94.     public String getExternalStorageDirectory(long arg10) {
  95.         String v0_1;
  96.         if(FileUtil.getAvailableBytes(this.defaultExternalStorageDirectory) < 52428800 + arg10) {
  97.             String v1 = this.defaultExternalStorageDirectory;
  98.             ArrayList v2 = new ArrayList();
  99.             ArrayList v3 = new ArrayList();
  100.             Iterator v4 = this.availableStoragesPathList.iterator();
  101.             while(v4.hasNext()) {
  102.                 Object v0 = v4.next();
  103.                 if(!TextUtils.isEmpty(this.defaultExternalStorageDirectory) && (this.defaultExternalStorageDirectory
  104.                         .equals(v0))) {
  105.                     continue;
  106.                 }

  107.                 if(!new File((((String)v0)) + "/wandoujia/").exists()) {
  108.                     goto label_29;
  109.                 }

  110.                 ((List)v3).add(v0);
  111.                 continue;
  112.             label_29:
  113.                 ((List)v2).add(v0);
  114.             }

  115.             v0_1 = this.getAvailableDirectory(((List)v3), arg10);
  116.             if(TextUtils.isEmpty(((CharSequence)v0_1))) {
  117.                 v0_1 = this.getAvailableDirectory(((List)v2), arg10);
  118.                 if(TextUtils.isEmpty(((CharSequence)v0_1))) {
  119.                     v0_1 = this.defaultExternalStorageDirectory;
  120.                     goto label_38;
  121.                 }
  122.                 else {
  123.                     this.defaultExternalStorageDirectory = v0_1;
  124.                 }
  125.             }
  126.             else {
  127.                 this.defaultExternalStorageDirectory = v0_1;
  128.             }

  129.             this.saveAndNotifyDefaultExternalStorageDiretory(v1, this.defaultExternalStorageDirectory
  130.                     );
  131.             goto label_42;
  132.         }
  133.         else {
  134.         label_42:
  135.             v0_1 = this.defaultExternalStorageDirectory;
  136.         }

  137.     label_38:
  138.         return v0_1;
  139.     }

  140.     public static StorageManager getInstance() {
  141.         if(StorageManager.instance == null) {
  142.             Class v1 = StorageManager.class;
  143.             __monitor_enter(v1);
  144.             try {
  145.                 if(StorageManager.instance == null) {
  146.                     StorageManager.instance = new StorageManager();
  147.                 }

  148.                 __monitor_exit(v1);
  149.             }
  150.             catch(Throwable v0) {
  151.                 try {
  152.                 label_12:
  153.                     __monitor_exit(v1);
  154.                 }
  155.                 catch(Throwable v0) {
  156.                     goto label_12;
  157.                 }

  158.                 throw v0;
  159.             }
  160.         }

  161.         return StorageManager.instance;
  162.     }

  163.     public boolean isStorageMounted() {
  164.         boolean v0_1;
  165.         List v0 = this.getExternalStorageDirectories();
  166.         if(v0 == null || v0.size() == 0) {
  167.             v0_1 = false;
  168.         }
  169.         else {
  170.             v0_1 = true;
  171.         }

  172.         return v0_1;
  173.     }

  174.     private void notifyPathChange(String arg5, String arg6) {
  175.         ArrayList v1 = new ArrayList();
  176.         List v2 = this.rdcListeners;
  177.         __monitor_enter(v2);
  178.         try {
  179.             Iterator v3 = this.rdcListeners.iterator();
  180.             while(v3.hasNext()) {
  181.                 Object v0_1 = v3.next().get();
  182.                 if(v0_1 == null) {
  183.                     goto label_15;
  184.                 }

  185.                 ((List)v1).add(v0_1);
  186.                 continue;
  187.             label_15:
  188.                 v3.remove();
  189.             }

  190.             __monitor_exit(v2);
  191.         }
  192.         catch(Throwable v0) {
  193.             goto label_13;
  194.         }

  195.         new Handler(Looper.getMainLooper()).post(new a(((List) v1), arg5, arg6));
  196.         return;
  197.         try {
  198.         label_13:
  199.             __monitor_exit(v2);
  200.         }
  201.         catch(Throwable v0) {
  202.             goto label_13;
  203.         }

  204.         throw v0;
  205.     }

  206.     private void registerReceiver() {
  207.         IntentFilter v0 = new IntentFilter();
  208.         v0.addAction("android.intent.action.MEDIA_MOUNTED");
  209.         v0.addAction("android.intent.action.MEDIA_UNMOUNTED");
  210.         v0.addDataScheme("file");
  211.         GlobalConfig.getAppContext().registerReceiver(new StorageManager$MediaReceiver(this, 0), v0)
  212.                 ;
  213.     }
  214. }
复制代码

  1. package base.utils;

  2. import java.lang.reflect.Field;

  3. import android.content.Context;
  4. import android.view.View;

  5. /*
  6. * ��ֹ���뷨���о����app�޷��ر�
  7. */
  8. public class ActivityLeakUtil {
  9.     public ActivityLeakUtil() {
  10.         super();
  11.     }

  12.     public static void fixInputMethodManagerLeak(Context context) {
  13.         if(context == null) {
  14.             return;
  15.         }

  16.         Object clsObj = context.getSystemService(Context.INPUT_METHOD_SERVICE);
  17.         if(clsObj == null) {
  18.             return;
  19.         }

  20.         String[] viewName = {
  21.                 "mCurRootView",
  22.                 "mServedView",
  23.                 "mNextServedView",
  24.         };

  25.         for(int i = 0; i < viewName.length; i++) {
  26.             try {
  27.                 Field field = clsObj.getClass().getDeclaredField(viewName[i]);
  28.                 if(!field.isAccessible()) {
  29.                     field.setAccessible(true);
  30.                 }

  31.                 Object fieldData = field.get(clsObj);
  32.                 if(fieldData == null || !(fieldData instanceof View) || ((View)fieldData).getContext() != context) {
  33.                     continue;
  34.                 }
  35.                 field.set(clsObj, null);
  36.             }
  37.             catch(Throwable t) {
  38.             }
  39.         }
  40.     }
  41. }

  42. package base.utils;

  43. import android.os.Build;
  44. import java.util.ArrayList;
  45. import java.util.List;

  46. public class AppKillBlacklistUtil {
  47.     private static List<String> appKillWhiteList = null;

  48.     static  {
  49.         AppKillBlacklistUtil.appKillWhiteList = new ArrayList<String>();
  50.         AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.phoenix2");
  51.         AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.roshan");
  52.         AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.eyepetizer");
  53.         AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia");
  54.         AppKillBlacklistUtil.appKillWhiteList.add("android");
  55.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.phone");
  56.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.mms");
  57.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.systemui");
  58.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.settings");
  59.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.applications");
  60.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.contacts");
  61.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.userdictionary");
  62.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.telephony");
  63.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.drm");
  64.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads");
  65.         AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.media");

  66.         if(Build.MANUFACTURER.equalsIgnoreCase("HTC")) {
  67.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.htccontacts");
  68.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.htcdialer");
  69.             AppKillBlacklistUtil.appKillWhiteList.add("com.htc.messagecs");
  70.             AppKillBlacklistUtil.appKillWhiteList.add("com.htc.idlescreen.shortcut");
  71.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.htcCheckin");
  72.         }
  73.         else if(Build.MANUFACTURER.equalsIgnoreCase("ZTE")) {
  74.             AppKillBlacklistUtil.appKillWhiteList.add("zte.com.cn.alarmclock");
  75.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.utk");
  76.         }
  77.         else if(Build.MANUFACTURER.equalsIgnoreCase("huawei")) {
  78.             AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.widget.localcityweatherclock");
  79.         }
  80.         else if(Build.MANUFACTURER.equalsIgnoreCase("Sony Ericsson")) {
  81.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.useragent");
  82.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
  83.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.secureclockservice");
  84.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.widget.digitalclock");
  85.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.digitalclockwidget");
  86.         }
  87.         else if(Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
  88.             AppKillBlacklistUtil.appKillWhiteList.add("com.samsung.inputmethod");
  89.             AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.controlpanel");
  90.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
  91.         }
  92.         else if(Build.MANUFACTURER.equalsIgnoreCase("motorola")) {
  93.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.numberlocation");
  94.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.android.fota");
  95.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.atcmd");
  96.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.locationsensor");
  97.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.conversations");
  98.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.alarmclock");
  99.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.providers.contacts");
  100.         }
  101.         else if(Build.MANUFACTURER.equalsIgnoreCase("LGE")) {
  102.             AppKillBlacklistUtil.appKillWhiteList.add("com.lge.clock");
  103.         }
  104.         else if(Build.MANUFACTURER.equalsIgnoreCase("magnum2x")) {
  105.             AppKillBlacklistUtil.appKillWhiteList.add("ty.com.android.TYProfileSetting");
  106.         }

  107.         if((Build.MODEL.equalsIgnoreCase("HTC Sensation Z710e")) ||
  108.                         (Build.MODEL.equalsIgnoreCase("HTC Sensation G14")) ||
  109.                         (Build.MODEL.equalsIgnoreCase("HTC Z710e"))) {
  110.             AppKillBlacklistUtil.appKillWhiteList.add("android.process.acore");
  111.         }
  112.         else if(Build.MODEL.equalsIgnoreCase("LT18i")) {
  113.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
  114.             AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.useragent");
  115.         }
  116.         else if(Build.MODEL.equalsIgnoreCase("U8500") ||
  117.                         Build.MODEL.equalsIgnoreCase("U8500 HiQQ")){
  118.             AppKillBlacklistUtil.appKillWhiteList.add("android.process.launcherdb");
  119.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.process.system");
  120.             AppKillBlacklistUtil.appKillWhiteList.add("com.nd.assistance.ServerService");
  121.         }
  122.         else if(Build.MODEL.equalsIgnoreCase("MT15I")){
  123.                 AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.eventstream.calllogplugin");
  124.         }       
  125.         else if(Build.MODEL.equalsIgnoreCase("GT-I9100") ||
  126.                         Build.MODEL.equalsIgnoreCase("GT-I9100G")){
  127.             AppKillBlacklistUtil.appKillWhiteList.add("com.samsung.inputmethod");
  128.             AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.controlpanel");
  129.             AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.FileTransferManager");
  130.             AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.providers.downloads");
  131.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads.ui");
  132.         }
  133.         else if(Build.MODEL.equalsIgnoreCase("DROIDX")) {
  134.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts.data");
  135.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts");
  136.         }
  137.         else if(Build.MODEL.equalsIgnoreCase("DROID2") ||
  138.                         Build.MODEL.equalsIgnoreCase("DROID2 GLOBA")){
  139.                 AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts");
  140.         }
  141.         else if(Build.MODEL.startsWith("U8800")) {
  142.             AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.android.gpms");
  143.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.hwdrm");
  144.             AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.omadownload");
  145.         }
  146.         else if(Build.MODEL.equalsIgnoreCase("LG-P503")) {
  147.             AppKillBlacklistUtil.appKillWhiteList.add("com.lge.simcontacts");
  148.         }
  149.         else if(Build.MODEL.equalsIgnoreCase("XT702")) {
  150.             AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.usb");
  151.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.alarmclock");
  152.         }
  153.         else if(Build.MODEL.equalsIgnoreCase("e15i")) {
  154.             AppKillBlacklistUtil.appKillWhiteList.add("com.sec.ccl.csp.app.secretwallpaper.themetwo");
  155.         }
  156.         else if(Build.MODEL.equalsIgnoreCase("zte-c n600")) {
  157.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.wallpaper");
  158.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.musicvis");
  159.             AppKillBlacklistUtil.appKillWhiteList.add("com.android.magicsmoke");
  160.         }
  161.         else if(Build.MODEL.startsWith("GT-5830") ||
  162.                         Build.MODEL.startsWith("HTC Velocity 4G")){
  163.                 AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads.ui");
  164.         }
  165.     }

  166.     public AppKillBlacklistUtil() {
  167.         super();
  168.     }

  169.     public static boolean isAppInWhiteList(String packageName) {
  170.         return AppKillBlacklistUtil.appKillWhiteList.contains(packageName);
  171.     }
  172. }
  173. package base.utils;

  174. import android.content.Context;
  175. import android.content.pm.ApplicationInfo;
  176. import android.content.pm.PackageInfo;
  177. import android.content.pm.PackageManager;
  178. import android.content.pm.PackageManager.NameNotFoundException;
  179. import android.graphics.Bitmap;
  180. import android.graphics.Bitmap.CompressFormat;
  181. import android.graphics.Bitmap.Config;
  182. import android.graphics.drawable.Drawable;
  183. import android.text.TextUtils;
  184. import java.io.BufferedInputStream;
  185. import java.io.ByteArrayOutputStream;
  186. import java.io.File;
  187. import java.io.FileInputStream;
  188. import java.io.FileNotFoundException;
  189. import java.io.IOException;
  190. import java.io.InputStream;
  191. import java.io.OutputStream;

  192. public class AppUtils {
  193.     private static final int BUFFER_SIZE = 0x20000;
  194.     private static final int MAX_ICON_SIZE = 300;

  195.     public AppUtils() {
  196.         super();
  197.     }

  198.     private static byte[] bitmapToPNGBytes(Bitmap bitmap) {
  199.         if(bitmap != null) {
  200.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  201.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  202.             return baos.toByteArray();
  203.         }
  204.         return null;
  205.     }

  206.     public static String convertFirstCharToPinyin(String str, Context context) {
  207.         if(TextUtils.isEmpty(((CharSequence)str))) {
  208.             return null;
  209.         }
  210.         else {
  211.             String tmp = str.trim();
  212.             if(TextUtils.isEmpty(((CharSequence)tmp))) {
  213.                     return null;
  214.             }
  215.             else {
  216.                 if(TextUtil.isChinese(tmp.charAt(0))) {
  217.                     tmp = TextUtil.convert2Pinyin(context, tmp.substring(0, 1)).trim().toUpperCase() + tmp.substring(1);
  218.                 }
  219.                 tmp = tmp.trim().replaceAll("^[\\s ]*|[\\s ]*$", "").toUpperCase();
  220.             }
  221.         }
  222.         return null;
  223.     }

  224.     public static byte[] drawableToBytes(Drawable drawable) {
  225.         byte[] data = null;
  226.         Bitmap bitmap = ImageUtil.drawableToBitmap(drawable, new Bitmap.Config[0]);
  227.         if(bitmap != null) {
  228.             data = AppUtils.bitmapToPNGBytes(bitmap);
  229.         }
  230.         return data;
  231.     }

  232.     public static byte[] getIconBytesFromPkgInfo(ApplicationInfo info, PackageManager manager) {
  233.         byte[] data = null;
  234.         int threshold = 300;
  235.         if(info == null) {
  236.             return null;
  237.         }
  238.         try {
  239.             Drawable drawable = info.loadIcon(manager);
  240.             if(drawable == null) {
  241.                 return null;
  242.             }
  243.             if(drawable.getIntrinsicHeight() > threshold || drawable.getIntrinsicWidth() > threshold) {
  244.                 return null;
  245.             }
  246.             data = AppUtils.drawableToBytes(drawable);
  247.         }
  248.         catch(OutOfMemoryError v1) {
  249.         }
  250.         return data;
  251.     }

  252.     public static PackageInfo getPackageInfo(Context context, String info, int flag) {
  253.         try {
  254.             return context.getPackageManager().getPackageInfo(info, flag);
  255.         }
  256.         catch(RuntimeException v1_1) {
  257.         }
  258.         catch(PackageManager.NameNotFoundException v1_2) {
  259.         }
  260.         return null;
  261.     }

  262.     public static boolean isAppInstalled(Context context, String packageName) {
  263.         try {
  264.             if(context.getPackageManager().getPackageInfo(packageName, 0) != null) {
  265.                 return true;
  266.             }
  267.         }
  268.         catch(PackageManager.NameNotFoundException v1) {
  269.         }
  270.         return false;
  271.     }
  272. }
  273. package base.utils;

  274. import java.lang.reflect.Array;
  275. import java.util.ArrayList;
  276. import java.util.List;

  277. public class ArrayUtil {
  278.     public ArrayUtil() {
  279.         super();
  280.     }

  281.     public static Object[] combineArray(Object[] arr1, Object[] arr2) {
  282.         Object[] newarr = copyOfRange(arr1, 0, arr1.length + arr2.length);
  283.         System.arraycopy(arr2, 0, newarr, arr1.length, arr2.length);
  284.         return newarr;
  285.     }

  286.     public static Object[] copyOfRange(Object[] src, int srcbegin, int dstlen) {
  287.         int srcend = src.length;
  288.         if(srcbegin > dstlen) {
  289.             throw new IllegalArgumentException();
  290.         }

  291.         if(srcbegin >= 0 && srcbegin <= srcend) {
  292.             int dstsize = dstlen - srcbegin;
  293.             int copylen = Math.min(dstsize, srcend - srcbegin);
  294.             Object[] dst = (Object[]) Array.newInstance(src.getClass().getComponentType(), dstsize);
  295.             System.arraycopy(src, srcbegin, dst, 0, copylen);
  296.             return ((Object[])dst);
  297.         }

  298.         throw new ArrayIndexOutOfBoundsException();
  299.     }

  300.     public static Object[] insert(Object[] objArr, int insertPos, Object newone) {
  301.         Object[] dst = (Object[]) Array.newInstance(objArr.getClass().getComponentType(), objArr.length + 1);
  302.         for(int i = 0; i < dst.length; ++i) {
  303.             if(i < insertPos) {
  304.                 dst[i] = objArr[i];
  305.             }
  306.             else if(i == insertPos) {
  307.                 dst[i] = newone;
  308.             }
  309.             else {
  310.                 dst[i] = objArr[i - 1];
  311.             }
  312.         }
  313.         return dst;
  314.     }
  315. }

  316. package base.utils;

  317. import android.content.BroadcastReceiver;
  318. import android.content.Context;
  319. import android.content.Intent;

  320. public class BatteryUtils extends BroadcastReceiver {
  321.     private int current;
  322.     private static BatteryUtils instance;
  323.     private boolean isCharging;
  324.     private int total;

  325.     static  {
  326.         BatteryUtils.instance = null;
  327.     }

  328.     private BatteryUtils() {
  329.         super();
  330.         this.current = 100;
  331.         this.total = 100;
  332.         this.isCharging = false;
  333.     }

  334.     public static BatteryUtils getInstance() {
  335.         if(BatteryUtils.instance == null) {
  336.             BatteryUtils.instance = new BatteryUtils();
  337.         }

  338.         return BatteryUtils.instance;
  339.     }

  340.     public void onReceive(Context context, Intent intent) {
  341.         int isplugged;
  342.         if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
  343.             this.current = intent.getExtras().getInt("level");
  344.             this.total = intent.getExtras().getInt("scale");
  345.             if(intent.getExtras().getInt("plugged", 0) != 0) {
  346.                 isplugged = 1;
  347.             }
  348.             else {
  349.                 isplugged = 0;
  350.             }

  351.             if((this.isCharging) && isplugged == 0) {
  352.                 this.isCharging = false;
  353.                 return;
  354.             }

  355.             if(this.isCharging) {
  356.                 return;
  357.             }

  358.             if(isplugged == 0) {
  359.                 return;
  360.             }

  361.             this.isCharging = true;
  362.         }
  363.     }
  364. }

  365. package base.utils;

  366. import android.content.ClipData;
  367. import android.content.ClipboardManager;
  368. import android.content.Context;

  369. public class ClipboardUtil {
  370.     public ClipboardUtil() {
  371.         super();
  372.     }

  373.     public static void copyText(String text, Context context) {
  374.         if(SystemUtil.aboveApiLevel(11)) {
  375.                 if(context == null)
  376.                         return;
  377.                 ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
  378.             if(manager != null)
  379.                 manager.setPrimaryClip(ClipData.newPlainText("phoenix", text));
  380.         }
  381.         else {
  382.             ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
  383.             if(manager != null)
  384.                 manager.setText(text);
  385.         }
  386.     }
  387. }

  388. package base.utils;

  389. import java.util.ArrayList;
  390. import java.util.Collection;
  391. import java.util.Collections;
  392. import java.util.List;

  393. public class CollectionUtils {
  394.     public CollectionUtils() {
  395.         super();
  396.     }

  397.     /*
  398.      * 将src2插入src1的index位置
  399.      */
  400.     public static List<Collection> appendFromPosition(List<Collection> src1, List<Collection> src2, int index) {
  401.         ArrayList<Collection> out = new ArrayList<Collection>();
  402.         if(src1 == null || src1.isEmpty()) {
  403.             if(src2 == null || src2.isEmpty())
  404.                 return out;
  405.             out.addAll(src2);
  406.         }
  407.         else if(src2 == null || src2.isEmpty()) {
  408.         }
  409.         else {
  410.             if(index > src1.size())
  411.                 index = src1.size();
  412.             out = new ArrayList<Collection>(src1);
  413.             out.addAll(index, src2);
  414.         }
  415.         return out;
  416.     }

  417.     public static ArrayList copyFrom(List<Collection> src) {
  418.         if(src == null) {
  419.             return null;
  420.         }
  421.         else {
  422.             return new ArrayList(src);
  423.         }
  424.     }

  425.     public static boolean isEmpty(Collection src) {
  426.         if(src == null || (src.isEmpty())) {
  427.             return true;
  428.         }
  429.         else {
  430.             return false;
  431.         }
  432.     }

  433.     public static List<Collection> notNull(List<Collection> src) {
  434.         if(src == null) {
  435.             src = Collections.emptyList();
  436.         }
  437.         return src;
  438.     }

  439.     public static List<Collection> replaceFromPosition(List<Collection> src1, List<Collection> src2, int index) {
  440.         ArrayList<Collection> out = new ArrayList<Collection>();
  441.         if(src1 == null || src1.isEmpty()) {
  442.             return out;
  443.         }
  444.         else if(src2 == null || src2.isEmpty()) {
  445.         }
  446.         else{
  447.             if(index > src1.size()) {
  448.                 index = src1.size();
  449.             }
  450.             out = new ArrayList<Collection>(src1);
  451.             out.addAll(index, src2);
  452.             while(out.size() > src2.size() + index) {
  453.                 out.remove(out.size() - 1);
  454.             }
  455.         }
  456.         return out;
  457.     }
  458. }



复制代码


回复

使用道具 举报

307

主题

228

回帖

7343

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5593 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
 楼主| 发表于 2016-3-20 18:16:36 | 显示全部楼层
不保证正确性,拿去耍吧 base.rar (50.14 KB, 下载次数: 0)
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-25 19:13 , Processed in 0.048539 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表