StorageHacker.java 6.7 KB
Newer Older
H
haojianglong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
package com.didichuxing.doraemonkit.weex.storage;

import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.taobao.weex.WXSDKEngine;
import com.taobao.weex.appfram.storage.DefaultWXStorage;
import com.taobao.weex.appfram.storage.IWXStorageAdapter;
import com.taobao.weex.appfram.storage.WXSQLiteOpenHelper;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author haojianglong
 * @date 2019-06-18
 */

public final class StorageHacker {

    private IWXStorageAdapter mStorageAdapter;
    private Context mContext;
    private final boolean isDebug;

    private Handler mHandler = new Handler(Looper.getMainLooper());
    private ExecutorService mExecutor = Executors.newCachedThreadPool(new ThreadFactory() {
        @Override
        public Thread newThread(@NonNull Runnable r) {
            return new Thread(r, "wx_analyzer_storage_dumper");
        }
    });

    public StorageHacker(@NonNull Context context, boolean isDebug) {
        mStorageAdapter = WXSDKEngine.getIWXStorageAdapter();
        if (!(context instanceof Application)) {
            context = context.getApplicationContext();
        }
        this.mContext = context;
        this.isDebug = isDebug;
    }

    public void destroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }

        if (mExecutor != null) {
            mExecutor.shutdown();
            mExecutor = null;
        }
    }

    public boolean isDestroy() {
        return mHandler == null || mExecutor == null || mExecutor.isShutdown();
    }


    @SuppressWarnings("unchecked")
    public void fetch(@Nullable final OnLoadListener listener) {
        if (listener == null) {
            return;
        }

        if (mStorageAdapter == null) {
            listener.onLoad(Collections.<StorageInfo>emptyList());
            return;
        }

        if (!(mStorageAdapter instanceof DefaultWXStorage)) {
            listener.onLoad(Collections.<StorageInfo>emptyList());
            return;
        }

        if (isDestroy()) {
            listener.onLoad(Collections.<StorageInfo>emptyList());
            return;
        }

        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final List<StorageInfo> resultList = new ArrayList<>();
                WXSQLiteOpenHelper helper = null;
                Cursor c = null;
                try {
                    Constructor<WXSQLiteOpenHelper> constructor = WXSQLiteOpenHelper.class.getDeclaredConstructor(Context.class);
                    constructor.setAccessible(true);
                    helper = constructor.newInstance(mContext);

                    Method method = WXSQLiteOpenHelper.class.getDeclaredMethod("getDatabase");
                    method.setAccessible(true);
                    SQLiteDatabase db = (SQLiteDatabase) method.invoke(helper);

                    c = db.query("default_wx_storage", new String[]{"key", "value", "timestamp"}, null, null, null, null, null);

                    if (isDebug) {
                        Log.d("weex-analyzer", "start dump weex storage");
                    }

                    while (c.moveToNext()) {
                        StorageInfo info = new StorageInfo();
                        info.key = c.getString(c.getColumnIndex("key"));
                        info.value = c.getString(c.getColumnIndex("value"));
                        info.timestamp = c.getString(c.getColumnIndex("timestamp"));
                        if (isDebug) {
                            Log.d("weex-analyzer", "weex storage[" + info.key + " | " + info.value + "]");
                        }
                        resultList.add(info);
                    }

                    if (isDebug) {
                        Log.d("weex-analyzer", "end dump weex storage");
                    }

                    if (mHandler != null) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                listener.onLoad(resultList);
                            }
                        });
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (c != null) {
                        c.close();
                    }
                    if (helper != null) {
                        helper.closeDatabase();
                    }
                }
            }
        });
    }

    public void remove(@Nullable final String key, @Nullable final OnRemoveListener listener) {
        if (listener == null || TextUtils.isEmpty(key)) {
            return;
        }

        if (mStorageAdapter == null) {
            listener.onRemoved(false);
            return;
        }

        if (!(mStorageAdapter instanceof DefaultWXStorage)) {
            listener.onRemoved(false);
            return;
        }

        if (isDestroy()) {
            listener.onRemoved(false);
            return;
        }

        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    DefaultWXStorage storage = (DefaultWXStorage) mStorageAdapter;
                    Method method = storage.getClass().getDeclaredMethod("performRemoveItem", String.class);
                    if (method != null) {
                        method.setAccessible(true);
                        final boolean result = (boolean) method.invoke(storage, key);
                        if (mHandler != null) {
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    listener.onRemoved(result);
                                }
                            });
                            method.setAccessible(false);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    interface OnLoadListener {
        void onLoad(List<StorageInfo> list);
    }

    interface OnRemoveListener {
        void onRemoved(boolean status);
    }

}