技术宅的结界

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

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 1450|回复: 1
收起左侧

豌豆荚基础库

[复制链接]

272

主题

446

帖子

4795

积分

用户组: 真·技术宅

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

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

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

x
[Java] 纯文本查看 复制代码
package base.dexs;

import android.content.Context;
import android.os.Build;
import base.reflect.JavaCalls;
import base.utils.ArrayUtil;
import dalvik.system.DexFile;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.zip.ZipFile;

public class ContextClassLoaderUtility {
    private static final String LOG_TAG = "ContextClassLoaderUtility";
    private ContextClassLoaderUtility() {
        super();
    }

    static class Element {
        public final File file;
        public final ZipFile zipFile;
        public final DexFile dexFile;

        public Element(File file, ZipFile zipFile, DexFile dexFile) {
            this.file = file;
            this.zipFile = zipFile;
            this.dexFile = dexFile;
        }
    }

    private static DexFile createDexFile(File file, File optimizedDirectory) {
        if(Build.VERSION.SDK_INT >= 14) {
            return (DexFile) JavaCalls.callStaticMethod("dalvik.system.DexPathList", "loadDexFile",
                    new Object[]{file, optimizedDirectory});
        }
        else{
            String outputName = (String) JavaCalls.callStaticMethod("dalvik.system.DexClassLoader", "generateOutputName",
                    new Object[]{file.getAbsolutePath(), optimizedDirectory.getAbsolutePath()});
            try {
                return DexFile.loadDex(file.getAbsolutePath(), outputName, 0);
            }
            catch(IOException e) {
            }
        }
        return null;
    }

    private static Element createElement(File file, File optimizedDirectory) {
        String fileName = file.getName();
        ZipFile zipfile = null;
        DexFile dexfile = null;
        if(!fileName.endsWith(".dex")) {
            if(!fileName.endsWith(".apk") && !fileName.endsWith(".jar") && !fileName.endsWith(".zip") && !fileName.endsWith(".so")) {
                throw new IllegalArgumentException("Unknown file type for: " + file);
            }
            try {
                zipfile = new ZipFile(file);
            }
            catch(IOException e) {

            }
        }
        dexfile = ContextClassLoaderUtility.createDexFile(file, optimizedDirectory);
        return new Element(file, zipfile, dexfile);
    }

    private static Object[] createEmptyDexElements(int num) {
        Class cls = null;
        try {
            cls = Class.forName("dalvik.system.DexPathList$Element");
        }
        catch(ClassNotFoundException v0) {
            throw new IllegalArgumentException("Can't find class: dalvik.system.DexPathList$Element");
        }
        Object[] obj = new Object[num];
        for(int i = 0; i < num; ++i) {
            obj[i] = JavaCalls.newEmptyInstance(cls);
        }
        return obj;
    }

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

    private static File getOptDir(Context context) {
        File dir = new File(context.getFilesDir(), "opt");
        if(!dir.exists()) {
            if(!dir.mkdirs()) {
                return null;//create opt dir meet ex.
            }
            return dir;
        }
        else if(dir.isFile()) {
            if((dir.delete()) && (dir.mkdirs())) {
                return dir;
            }
        }
        else if(dir.isDirectory()) {
            return dir;
        }
        return null;//create opt dir meet ex.
    }

    public static void inject(Context context, Set<String> pathset) {
        if(pathset != null && !pathset.isEmpty()) {
            String[] patharr = pathset.toArray(new String[pathset.size()]);
            if(Build.VERSION.SDK_INT >= 14) {
                ContextClassLoaderUtility.injectV14(context, patharr);
            }
            else {
                ContextClassLoaderUtility.injectBelowV14(context, patharr);
            }
        }
    }

    private static void injectBelowV14(Context context, String[] patharr) {
        File dir = ContextClassLoaderUtility.getOptDir(context);
        File[] files = new File[patharr.length];
        ZipFile[] zfiles = new ZipFile[patharr.length];
        DexFile[] dfiles = new DexFile[patharr.length];
        for(int i = 0; i < patharr.length; ++i) {
            ContextClassLoaderUtility.resetValueBelowV14(new File(patharr[i]), dir, files, zfiles, dfiles, i);
        }
        ClassLoader loader = context.getClassLoader();
        Object mFiles = JavaCalls.getField(loader, "mFiles");
        Object mPaths = JavaCalls.getField(loader, "mPaths");
        Object mZips = JavaCalls.getField(loader, "mZips");
        Object mDexs = JavaCalls.getField(loader, "mDexs");
        JavaCalls.setField(loader, "mPaths", ArrayUtil.combineArray((Object[])mPaths, patharr));
        JavaCalls.setField(loader, "mFiles", ArrayUtil.combineArray((Object[])mFiles, files));
        JavaCalls.setField(loader, "mZips", ArrayUtil.combineArray((Object[])mZips, zfiles));
        JavaCalls.setField(loader, "mDexs", ArrayUtil.combineArray((Object[])mDexs, dfiles));
    }

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

    private static void resetValueBelowV14(File file, File optimizedDirectory, Object[] files, Object[] zfiles, Object[] dfiles, int index) {
        Element ele = createElement(file, optimizedDirectory);
        if(ele != null) {
            if(index < files.length) {
                files[index] = ele.file;
            }
            if(index < zfiles.length) {
                zfiles[index] = ele.zipFile;
            }
            if(index < dfiles.length) {
                dfiles[index] = ele.dexFile;
            }
        }
    }

    private static void resetValueV14(File file, File optimizedDirectory, Object clsobj) {
        Element ele = createElement(file, optimizedDirectory);
        if(ele != null) {
            JavaCalls.setField(clsobj, "file", ele.file);
            JavaCalls.setField(clsobj, "zipFile", ele.zipFile);
            JavaCalls.setField(clsobj, "dexFile", ele.dexFile);
        }
    }

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

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

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

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

public class ContextLibraryUtility {
    private ContextLibraryUtility() {
        super();
    }

    public static void inject(Context context, File file) {
        if(Build.VERSION.SDK_INT >= 14) {
            ContextLibraryUtility.injectV14(context, file);
        }
        else if(Build.VERSION.SDK_INT >= 9) {
            injectV9(context, file);
        }
        else {
           injectBelowV9(context, file);
        }
    }

    private static void injectBelowV9(Context context, File libdir) {
        String[] v0_2;
        ClassLoader loader = context.getClassLoader();
        Object[] mLibPathsOld = (Object[]) JavaCalls.getField(loader, "mLibPaths");
        Object[] mLibPathsNew = null;
        String libdirslash = libdir.getAbsolutePath() + "/";
        if(mLibPathsOld.length > 0) {
            mLibPathsNew = ArrayUtil.insert(mLibPathsOld, 1, libdirslash);
        }
        else {
            mLibPathsNew = new String[]{libdirslash};
        }

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

    private static void injectV14(Context context, File libdir) {
        Object pathList = JavaCalls.getField(context.getClassLoader(), "pathList");
        Object[] nativeLibraryDirectoriesOld = (Object[]) JavaCalls.getField(pathList, "nativeLibraryDirectories");
        Object[] nativeLibraryDirectoriesNew = null;
        if(nativeLibraryDirectoriesOld.length > 0) {
            nativeLibraryDirectoriesNew = ArrayUtil.insert(nativeLibraryDirectoriesOld, 1, libdir);
        }
        else {
            nativeLibraryDirectoriesNew = new File[]{libdir};
        }
        JavaCalls.setField(pathList, "nativeLibraryDirectories", nativeLibraryDirectoriesNew);
    }

    private static void injectV9(Context context, File libdir) {
        List libraryPathElements = (List) JavaCalls.getField(context.getClassLoader(), "libraryPathElements");
        String libdirslash = libdir.getAbsolutePath() + "/";
        if((libraryPathElements).size() > 0) {
            libraryPathElements.add(1, libdirslash);
        }
        else {
            libraryPathElements.add(libdirslash);
        }
    }
}


[Java] 纯文本查看 复制代码
package base.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class JavaCalls {
    private static final String LOG_TAG = "JavaCalls";
    private static Map<Class<?>,Class<?>> PRIMITIVE_MAP = null;
    
    public class JavaParam{
    	Class clazz;
    	Object obj;
    	public JavaParam(Class cls,Object o){
    		clazz = cls;
    		obj = o;
    	}
    }

    static  {
        JavaCalls.PRIMITIVE_MAP = new HashMap<Class<?>,Class<?>>();
        JavaCalls.PRIMITIVE_MAP.put(Boolean.class, Boolean.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Byte.class, Byte.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Character.class, Character.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Short.class, Short.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Integer.class, Integer.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Float.class, Float.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Long.class, Long.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Double.class, Double.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Boolean.TYPE, Boolean.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Byte.TYPE, Byte.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Character.TYPE, Character.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Short.TYPE, Short.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Integer.TYPE, Integer.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Float.TYPE, Float.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Long.TYPE, Long.TYPE);
        JavaCalls.PRIMITIVE_MAP.put(Double.TYPE, Double.TYPE);
    }

    public JavaCalls() {
        super();
    }

    public static Object callMethod(Object clsObject, String methodName, Object[] params) {
        Object result;
        try {
            result = JavaCalls.callMethodOrThrow(clsObject,methodName,params);
        }
        catch(Exception v0) {
            result = null;
        }

        return result;
    }

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

    public static Object callStaticMethod(String clsName, String method, Object[] params) {
        Object obj = null;
        try {
            obj = JavaCalls.callStaticMethodOrThrow(Class.forName(clsName), method, params);
        }
        catch(Exception v0) {
        }
        return obj;
    }

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

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

    /*
     * �Ƚ��෽����������
     */
    private static boolean compareClassLists(Class[] paramTypes1, Class[] paramTypes2) {
        if(paramTypes1 == null || paramTypes1.length == 0) {
            if(paramTypes2 == null || paramTypes2.length == 0) {
                return true;
            }
			else{
				return false;
			}
        }
        else {
        	if(paramTypes2 == null || paramTypes2.length == 0){
        		return false;
        	}
        	if(paramTypes1.length != paramTypes2.length){
        		return false;
        	}
        	for(int i=0;i<paramTypes1.length;i++){
        		if(!paramTypes1[i].isAssignableFrom(paramTypes2[i])) {
                    if(JavaCalls.PRIMITIVE_MAP.containsKey(paramTypes1[i])) {
                        if(!JavaCalls.PRIMITIVE_MAP.get(paramTypes1[i]).equals(JavaCalls.PRIMITIVE_MAP.get(
                                paramTypes2[i]))) {
                        	return false;
                        }
                    }
                }
        	}
        	return true;
        }
    }

    private static Method findMethodByName(Method[] methodArr, String methodName, Class[] methodParamCls) {
        Method method = null;
        if(methodName == null) {
            throw new NullPointerException("Method name must not be null.");
        }
        for(int i = 0; i < methodArr.length; ++i) {
            method = methodArr[i];
            if((method.getName().equals(methodName)) && (JavaCalls.compareClassLists(method.getParameterTypes(), methodParamCls
                    ))) {
                return method;
            }
        }
        return null;
    }

    private static Method getDeclaredMethod(Class cls, String methodName, Class[] methodParamCls) {
        Method method = null;
        try{
            while(method == null) {
                method = JavaCalls.findMethodByName(cls.getDeclaredMethods(), methodName, methodParamCls);
                if(method == null) {
                    if(cls.getSuperclass() == null) {
                    	throw new NoSuchMethodException();
                    }
                    cls = cls.getSuperclass();
                }
            }
            method.setAccessible(true);	
        }
        catch(Exception e){
        	
        }
        return method;
    }

    private static Object getDefaultValue(Class cls) {
        if((Integer.TYPE.equals(cls)) || (Integer.class.equals(cls)) || (Byte.TYPE.equals(cls)) || 
                (Byte.class.equals(cls)) || (Short.TYPE.equals(cls)) || (Short.class.equals(cls))
                 || (Long.TYPE.equals(cls)) || (Long.class.equals(cls)) || (Double.TYPE.equals(cls
                )) || (Double.class.equals(cls)) || (Float.TYPE.equals(cls)) || (Float.class.equals
                (cls))) {
            return Integer.valueOf(0);
        }
        else {
            if(!Boolean.TYPE.equals(cls) && !Boolean.class.equals(cls)) {
                if(!Character.TYPE.equals(cls) && !Character.class.equals(cls)) {
                    return null;
                }
                return Character.valueOf('\u0000');
            }
            return Boolean.valueOf(false);
        }
    }

    public static Object getField(Object clsObject, String fieldName) {
        try {
			return JavaCalls.getFieldOrThrow(clsObject, fieldName);
		} 
		catch (IllegalArgumentException e) {
		
		} 
		catch (IllegalAccessException e) {
		
		} 
		catch (NoSuchFieldException e) {
		
		}
		return null;
    }

    public static Object getFieldOrThrow(Object clsObj, String fieldName) 
    throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        Class cls = clsObj.getClass();
        Field field = null;
        do {
            if(field != null) {
            	field.setAccessible(true);
        		return field.get(clsObj);
            }

            try {
                field = cls.getDeclaredField(fieldName);
                field.setAccessible(true);
            }
            catch(NoSuchFieldException v2) {
                cls = cls.getSuperclass();
            }
        }
        while(cls != null);

        throw new NoSuchFieldException();
    }

    private static Class[] getParameterTypes(Object[] clsObject) {
        Class[] cls = null;
        if(clsObject != null && clsObject.length > 0) {
            cls = new Class[clsObject.length];
            for(int i = 0; i < clsObject.length; ++i) {
                Object cur = clsObject[i];
                if(cur != null && ((cur instanceof JavaParam))) {
                    cls[i] = ((JavaParam)cur).clazz;
                }
                else if(cur == null) {
                    cls[i] = null;
                }
                else {
                	cls[i] = cur.getClass();
                }
            }
        }
        return cls;
    }

    private static Object[] getParameters(Object[] params) {
        Object[] objs = null;
        if(params != null && params.length > 0) {
            objs = new Object[params.length];
            for(int i = 0; i < params.length; ++i) {
                Object cur = params[i];
                if(cur == null || !(cur instanceof JavaParam)) {
                    objs[i] = cur;
                }
                else {
                    objs[i] = ((JavaParam)cur).obj;
                }
            }
        }
        return objs;
    }

    public static Object newEmptyInstance(Class cls) {
        Object o = null;
        try {
            o = JavaCalls.newEmptyInstanceOrThrow(cls);
        }
        catch(Exception v0) {
        }
        return o;
    }

    public static Object newEmptyInstanceOrThrow(Class cls) 
    	throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Object o;
        Constructor[] cons = cls.getDeclaredConstructors();
        if(cons != null && cons.length != 0) {
            Constructor cur = cons[0];
            cur.setAccessible(true);
            Class[] paramcls = cur.getParameterTypes();
            if(paramcls == null || paramcls.length == 0) {
                o = cur.newInstance();
            }
            else {
                Object[] tmp = new Object[paramcls.length];
                for(int i=0;i<paramcls.length;i++){
                	tmp[i] = JavaCalls.getDefaultValue(paramcls[i]);
                }
                o = cur.newInstance(tmp);
            }
            return o;
        }

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

    public static Object newInstance(Class cls, Object[] params) {
        Object o = null;
        try {
            o = JavaCalls.newInstanceOrThrow(cls, params);
        }
        catch(Exception v0) {
        }
        return o;
    }

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

        }
        return o;
    }

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

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

    public static void setField(Object clsObj, String fieldName, Object dataToset) {
        try {
            JavaCalls.setFieldOrThrow(clsObj, fieldName, dataToset);
        }
        catch(IllegalAccessException v0) {
        }
        catch(NoSuchFieldException v0_1) {
        }
    }

    public static void setFieldOrThrow(Object clsObj, String fieldName, Object dataToset) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Class cls = clsObj.getClass();
        Field field = null;
        do {
            if(field != null) {
                field.setAccessible(true);
                field.set(clsObj, dataToset);
                return;
            }
            try {
                field = cls.getDeclaredField(fieldName);
            }
            catch(NoSuchFieldException v2) {
                cls = cls.getSuperclass();
            }
        }
        while(cls != null);

        throw new NoSuchFieldException();
    }
}


[Java] 纯文本查看 复制代码
package base.storage;

import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences$Editor;
import android.os.Build;
import android.os.Build$VERSION;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.wandoujia.base.config.GlobalConfig;
import com.wandoujia.base.utils.FileUtil;
import com.wandoujia.base.utils.SharePrefSubmitor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class StorageManager {
    private static final int FORTH_LINE = 4;
    private static final String GENERIC_CONFIG_PREFERENCE_NAME = "com.wandoujia.phoenix2";
    private static final String KEY_LAST_USED_DIRECTORY = "key_last_used_directory";
    private static final long LIMIT_SIZE = 52428800;
    private static final int MIN_SDK = 14;
    private static final String ROOT_DIR = "/wandoujia/";
    private List availableStoragesPathList;
    private String defaultExternalStorageDirectory;
    private SharedPreferences genericSharedPrefs;
    private static StorageManager instance;
    private final List rdcListeners;

    private StorageManager() {
        super();
        this.genericSharedPrefs = GlobalConfig.getAppContext().getSharedPreferences("com.wandoujia.phoenix2"
                , 0);
        this.registerReceiver();
        this.rdcListeners = new ArrayList();
        this.availableStoragesPathList = this.getAvailableStorages();
        this.defaultExternalStorageDirectory = this.genericSharedPrefs.getString("key_last_used_directory"
                , null);
        this.checkDefaultPathAvailable();
    }

    public HashMap getAvailableDirectories(long arg10) {
        HashMap v1 = new HashMap();
        Iterator v2 = this.availableStoragesPathList.iterator();
        while(v2.hasNext()) {
            Object v0 = v2.next();
            long v4 = FileUtil.getAvailableBytes(((String)v0));
            if(v4 <= 52428800 + arg10) {
                continue;
            }

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

        return v1;
    }

    public String getAvailableDirectory(List arg13, long arg14) {
        Object v1_1;
        long v0_1;
        Object v2_1;
        String v1 = null;
        long v4 = -1;
        Iterator v6 = arg13.iterator();
        while(v6.hasNext()) {
            Object v0 = v6.next();
            long v2 = FileUtil.getAvailableBytes(((String)v0));
            if(v2 <= v4 || FileUtil.getAvailableBytes(((String)v0)) <= arg14) {
                v2_1 = v1;
                v0_1 = v4;
            }
            else {
                long v10 = v2;
                v2_1 = v0;
                v0_1 = v10;
            }

            v4 = v0_1;
            v1_1 = v2_1;
        }

        return ((String)v1_1);
    }



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

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

    public String getExternalStorageDirectory(long arg10) {
        String v0_1;
        if(FileUtil.getAvailableBytes(this.defaultExternalStorageDirectory) < 52428800 + arg10) {
            String v1 = this.defaultExternalStorageDirectory;
            ArrayList v2 = new ArrayList();
            ArrayList v3 = new ArrayList();
            Iterator v4 = this.availableStoragesPathList.iterator();
            while(v4.hasNext()) {
                Object v0 = v4.next();
                if(!TextUtils.isEmpty(this.defaultExternalStorageDirectory) && (this.defaultExternalStorageDirectory
                        .equals(v0))) {
                    continue;
                }

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

                ((List)v3).add(v0);
                continue;
            label_29:
                ((List)v2).add(v0);
            }

            v0_1 = this.getAvailableDirectory(((List)v3), arg10);
            if(TextUtils.isEmpty(((CharSequence)v0_1))) {
                v0_1 = this.getAvailableDirectory(((List)v2), arg10);
                if(TextUtils.isEmpty(((CharSequence)v0_1))) {
                    v0_1 = this.defaultExternalStorageDirectory;
                    goto label_38;
                }
                else {
                    this.defaultExternalStorageDirectory = v0_1;
                }
            }
            else {
                this.defaultExternalStorageDirectory = v0_1;
            }

            this.saveAndNotifyDefaultExternalStorageDiretory(v1, this.defaultExternalStorageDirectory
                    );
            goto label_42;
        }
        else {
        label_42:
            v0_1 = this.defaultExternalStorageDirectory;
        }

    label_38:
        return v0_1;
    }

    public static StorageManager getInstance() {
        if(StorageManager.instance == null) {
            Class v1 = StorageManager.class;
            __monitor_enter(v1);
            try {
                if(StorageManager.instance == null) {
                    StorageManager.instance = new StorageManager();
                }

                __monitor_exit(v1);
            }
            catch(Throwable v0) {
                try {
                label_12:
                    __monitor_exit(v1);
                }
                catch(Throwable v0) {
                    goto label_12;
                }

                throw v0;
            }
        }

        return StorageManager.instance;
    }

    public boolean isStorageMounted() {
        boolean v0_1;
        List v0 = this.getExternalStorageDirectories();
        if(v0 == null || v0.size() == 0) {
            v0_1 = false;
        }
        else {
            v0_1 = true;
        }

        return v0_1;
    }

    private void notifyPathChange(String arg5, String arg6) {
        ArrayList v1 = new ArrayList();
        List v2 = this.rdcListeners;
        __monitor_enter(v2);
        try {
            Iterator v3 = this.rdcListeners.iterator();
            while(v3.hasNext()) {
                Object v0_1 = v3.next().get();
                if(v0_1 == null) {
                    goto label_15;
                }

                ((List)v1).add(v0_1);
                continue;
            label_15:
                v3.remove();
            }

            __monitor_exit(v2);
        }
        catch(Throwable v0) {
            goto label_13;
        }

        new Handler(Looper.getMainLooper()).post(new a(((List) v1), arg5, arg6));
        return;
        try {
        label_13:
            __monitor_exit(v2);
        }
        catch(Throwable v0) {
            goto label_13;
        }

        throw v0;
    }

    private void registerReceiver() {
        IntentFilter v0 = new IntentFilter();
        v0.addAction("android.intent.action.MEDIA_MOUNTED");
        v0.addAction("android.intent.action.MEDIA_UNMOUNTED");
        v0.addDataScheme("file");
        GlobalConfig.getAppContext().registerReceiver(new StorageManager$MediaReceiver(this, 0), v0)
                ;
    }
}


[Java] 纯文本查看 复制代码
package base.utils;

import java.lang.reflect.Field;

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

/*
 * ��ֹ���뷨���о����app�޷��ر�
 */
public class ActivityLeakUtil {
    public ActivityLeakUtil() {
        super();
    }

    public static void fixInputMethodManagerLeak(Context context) {
        if(context == null) {
            return;
        }

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

        String[] viewName = {
        	"mCurRootView",
        	"mServedView",
        	"mNextServedView",
        };

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

                Object fieldData = field.get(clsObj);
                if(fieldData == null || !(fieldData instanceof View) || ((View)fieldData).getContext() != context) {
                    continue;
                }
                field.set(clsObj, null);
            }
            catch(Throwable t) {
            }
        }
    }
}

package base.utils;

import android.os.Build;
import java.util.ArrayList;
import java.util.List;

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

    static  {
        AppKillBlacklistUtil.appKillWhiteList = new ArrayList<String>();
        AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.phoenix2");
        AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.roshan");
        AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia.eyepetizer");
        AppKillBlacklistUtil.appKillWhiteList.add("com.wandoujia");
        AppKillBlacklistUtil.appKillWhiteList.add("android");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.phone");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.mms");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.systemui");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.settings");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.applications");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.contacts");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.userdictionary");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.telephony");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.drm");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads");
        AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.media");

        if(Build.MANUFACTURER.equalsIgnoreCase("HTC")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.htccontacts");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.htcdialer");
            AppKillBlacklistUtil.appKillWhiteList.add("com.htc.messagecs");
            AppKillBlacklistUtil.appKillWhiteList.add("com.htc.idlescreen.shortcut");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.htcCheckin");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("ZTE")) {
            AppKillBlacklistUtil.appKillWhiteList.add("zte.com.cn.alarmclock");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.utk");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("huawei")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.widget.localcityweatherclock");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("Sony Ericsson")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.useragent");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.secureclockservice");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.widget.digitalclock");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.digitalclockwidget");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.samsung.inputmethod");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.controlpanel");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("motorola")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.numberlocation");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.android.fota");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.atcmd");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.locationsensor");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.conversations");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.alarmclock");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.providers.contacts");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("LGE")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.lge.clock");
        }
        else if(Build.MANUFACTURER.equalsIgnoreCase("magnum2x")) {
            AppKillBlacklistUtil.appKillWhiteList.add("ty.com.android.TYProfileSetting");
        }

        if((Build.MODEL.equalsIgnoreCase("HTC Sensation Z710e")) || 
        		(Build.MODEL.equalsIgnoreCase("HTC Sensation G14")) || 
        		(Build.MODEL.equalsIgnoreCase("HTC Z710e"))) {
            AppKillBlacklistUtil.appKillWhiteList.add("android.process.acore");
        }
        else if(Build.MODEL.equalsIgnoreCase("LT18i")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.customization");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.provider.useragent");
        }
        else if(Build.MODEL.equalsIgnoreCase("U8500") ||
        		Build.MODEL.equalsIgnoreCase("U8500 HiQQ")){
            AppKillBlacklistUtil.appKillWhiteList.add("android.process.launcherdb");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.process.system");
            AppKillBlacklistUtil.appKillWhiteList.add("com.nd.assistance.ServerService");
        }
        else if(Build.MODEL.equalsIgnoreCase("MT15I")){
        	AppKillBlacklistUtil.appKillWhiteList.add("com.sonyericsson.eventstream.calllogplugin");
        }	
        else if(Build.MODEL.equalsIgnoreCase("GT-I9100") ||
        		Build.MODEL.equalsIgnoreCase("GT-I9100G")){
            AppKillBlacklistUtil.appKillWhiteList.add("com.samsung.inputmethod");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.controlpanel");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.app.FileTransferManager");
            AppKillBlacklistUtil.appKillWhiteList.add("com.sec.android.providers.downloads");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads.ui");
        }
        else if(Build.MODEL.equalsIgnoreCase("DROIDX")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts.data");
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts");
        }
        else if(Build.MODEL.equalsIgnoreCase("DROID2") ||
        		Build.MODEL.equalsIgnoreCase("DROID2 GLOBA")){
        	AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.blur.contacts");
        }
        else if(Build.MODEL.startsWith("U8800")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.android.gpms");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.hwdrm");
            AppKillBlacklistUtil.appKillWhiteList.add("com.huawei.omadownload");
        }
        else if(Build.MODEL.equalsIgnoreCase("LG-P503")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.lge.simcontacts");
        }
        else if(Build.MODEL.equalsIgnoreCase("XT702")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.motorola.usb");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.alarmclock");
        }
        else if(Build.MODEL.equalsIgnoreCase("e15i")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.sec.ccl.csp.app.secretwallpaper.themetwo");
        }
        else if(Build.MODEL.equalsIgnoreCase("zte-c n600")) {
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.wallpaper");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.musicvis");
            AppKillBlacklistUtil.appKillWhiteList.add("com.android.magicsmoke");
        }
        else if(Build.MODEL.startsWith("GT-5830") ||
        		Build.MODEL.startsWith("HTC Velocity 4G")){
        	AppKillBlacklistUtil.appKillWhiteList.add("com.android.providers.downloads.ui");
        }
    }

    public AppKillBlacklistUtil() {
        super();
    }

    public static boolean isAppInWhiteList(String packageName) {
        return AppKillBlacklistUtil.appKillWhiteList.contains(packageName);
    }
}
package base.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class AppUtils {
    private static final int BUFFER_SIZE = 0x20000;
    private static final int MAX_ICON_SIZE = 300;

    public AppUtils() {
        super();
    }

    private static byte[] bitmapToPNGBytes(Bitmap bitmap) {
        if(bitmap != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            return baos.toByteArray();
        }
        return null;
    }

    public static String convertFirstCharToPinyin(String str, Context context) {
        if(TextUtils.isEmpty(((CharSequence)str))) {
            return null;
        }
        else {
            String tmp = str.trim();
            if(TextUtils.isEmpty(((CharSequence)tmp))) {
            	return null;
            }
            else {
                if(TextUtil.isChinese(tmp.charAt(0))) {
                    tmp = TextUtil.convert2Pinyin(context, tmp.substring(0, 1)).trim().toUpperCase() + tmp.substring(1);
                }
                tmp = tmp.trim().replaceAll("^[\\s ]*|[\\s ]*$", "").toUpperCase();
            }
        }
        return null;
    }

    public static byte[] drawableToBytes(Drawable drawable) {
        byte[] data = null;
        Bitmap bitmap = ImageUtil.drawableToBitmap(drawable, new Bitmap.Config[0]);
        if(bitmap != null) {
            data = AppUtils.bitmapToPNGBytes(bitmap);
        }
        return data;
    }

    public static byte[] getIconBytesFromPkgInfo(ApplicationInfo info, PackageManager manager) {
        byte[] data = null;
        int threshold = 300;
        if(info == null) {
            return null;
        }
        try {
            Drawable drawable = info.loadIcon(manager);
            if(drawable == null) {
                return null;
            }
            if(drawable.getIntrinsicHeight() > threshold || drawable.getIntrinsicWidth() > threshold) {
                return null;
            }
            data = AppUtils.drawableToBytes(drawable);
        }
        catch(OutOfMemoryError v1) {
        }
        return data;
    }

    public static PackageInfo getPackageInfo(Context context, String info, int flag) {
        try {
            return context.getPackageManager().getPackageInfo(info, flag);
        }
        catch(RuntimeException v1_1) {
        }
        catch(PackageManager.NameNotFoundException v1_2) {
        }
        return null;
    }

    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            if(context.getPackageManager().getPackageInfo(packageName, 0) != null) {
                return true;
            }
        }
        catch(PackageManager.NameNotFoundException v1) {
        }
        return false;
    }
}
package base.utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class ArrayUtil {
    public ArrayUtil() {
        super();
    }

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

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

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

        throw new ArrayIndexOutOfBoundsException();
    }

    public static Object[] insert(Object[] objArr, int insertPos, Object newone) {
        Object[] dst = (Object[]) Array.newInstance(objArr.getClass().getComponentType(), objArr.length + 1);
        for(int i = 0; i < dst.length; ++i) {
            if(i < insertPos) {
                dst[i] = objArr[i];
            }
            else if(i == insertPos) {
                dst[i] = newone;
            }
            else {
                dst[i] = objArr[i - 1];
            }
        }
        return dst;
    }
}

package base.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BatteryUtils extends BroadcastReceiver {
    private int current;
    private static BatteryUtils instance;
    private boolean isCharging;
    private int total;

    static  {
        BatteryUtils.instance = null;
    }

    private BatteryUtils() {
        super();
        this.current = 100;
        this.total = 100;
        this.isCharging = false;
    }

    public static BatteryUtils getInstance() {
        if(BatteryUtils.instance == null) {
            BatteryUtils.instance = new BatteryUtils();
        }

        return BatteryUtils.instance;
    }

    public void onReceive(Context context, Intent intent) {
        int isplugged;
        if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
            this.current = intent.getExtras().getInt("level");
            this.total = intent.getExtras().getInt("scale");
            if(intent.getExtras().getInt("plugged", 0) != 0) {
                isplugged = 1;
            }
            else {
                isplugged = 0;
            }

            if((this.isCharging) && isplugged == 0) {
                this.isCharging = false;
                return;
            }

            if(this.isCharging) {
                return;
            }

            if(isplugged == 0) {
                return;
            }

            this.isCharging = true;
        }
    }
}

package base.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

public class ClipboardUtil {
    public ClipboardUtil() {
        super();
    }

    public static void copyText(String text, Context context) {
        if(SystemUtil.aboveApiLevel(11)) {
        	if(context == null)
        		return;
        	ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            if(manager != null)
                manager.setPrimaryClip(ClipData.newPlainText("phoenix", text));
        }
        else {
            ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            if(manager != null)
                manager.setText(text);
        }
    }
}

package base.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CollectionUtils {
    public CollectionUtils() {
        super();
    }

    /*
     * 将src2插入src1的index位置
     */
    public static List<Collection> appendFromPosition(List<Collection> src1, List<Collection> src2, int index) {
        ArrayList<Collection> out = new ArrayList<Collection>();
        if(src1 == null || src1.isEmpty()) {
            if(src2 == null || src2.isEmpty())
                return out;
            out.addAll(src2);
        }
        else if(src2 == null || src2.isEmpty()) {
        }
        else {
            if(index > src1.size())
                index = src1.size();
            out = new ArrayList<Collection>(src1);
            out.addAll(index, src2);
        }
        return out;
    }

    public static ArrayList copyFrom(List<Collection> src) {
        if(src == null) {
            return null;
        }
        else {
            return new ArrayList(src);
        }
    }

    public static boolean isEmpty(Collection src) {
        if(src == null || (src.isEmpty())) {
            return true;
        }
        else {
            return false;
        }
    }

    public static List<Collection> notNull(List<Collection> src) {
        if(src == null) {
            src = Collections.emptyList();
        }
        return src;
    }

    public static List<Collection> replaceFromPosition(List<Collection> src1, List<Collection> src2, int index) {
        ArrayList<Collection> out = new ArrayList<Collection>();
        if(src1 == null || src1.isEmpty()) {
            return out;
        }
        else if(src2 == null || src2.isEmpty()) {
        }
        else{
            if(index > src1.size()) {
                index = src1.size();
            }
            out = new ArrayList<Collection>(src1);
            out.addAll(index, src2);
            while(out.size() > src2.size() + index) {
                out.remove(out.size() - 1);
            }
        }
        return out;
    }
}





272

主题

446

帖子

4795

积分

用户组: 真·技术宅

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

本版积分规则

QQ|申请友链||Archiver|手机版|小黑屋|技术宅的结界 ( 滇ICP备16008837号|网站地图

GMT+8, 2019-6-18 06:53 , Processed in 0.108650 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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