a
554325746@qq.com
2020-01-15 956063ff14bc75e3a2a97c7bcaa06b9edc84ad24
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
package com.basic.security.manager;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Pair;
 
import com.basic.security.utils.Constants;
import com.basic.security.utils.ExceptionUtil;
 
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class DatabaseManager {
    public static final Lock databaseLock = new ReentrantLock();
    public static final Lock databaseExecSqlLock = new ReentrantLock();
    public static Map<String, SQLiteDatabase> databaseMap = new HashMap<>();
    static boolean setCustomDatabaseFiles = false;
 
    public static SQLiteDatabase getDatabase() {
        return getDatabase(Constants.baseDatabasePath);
    }
 
    public static SQLiteDatabase getDatabase(String databaseName) {
        SQLiteDatabase database = databaseMap.get(databaseName);
        try {
            databaseLock.lock();
            if (database == null) {
                database = SQLiteDatabase.openOrCreateDatabase(databaseName, null);
                databaseMap.put(databaseName, database);
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        } finally {
            databaseLock.unlock();
        }
        return database;
    }
 
    public static void execSQL(String sql) {
        try {
            databaseExecSqlLock.lock();
            getDatabase().execSQL(sql);
        } finally {
            databaseExecSqlLock.unlock();
        }
    }
 
    public static void setCustomDatabaseFiles(Context context) {
        if (setCustomDatabaseFiles) {
            return;
        }
        setCustomDatabaseFiles = true;
        try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Class[] argTypes = new Class[]{HashMap.class};
            Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
            HashMap<String, Pair<File, String>> customDatabaseFiles = new HashMap<>();
            customDatabaseFiles.put("xxbase_database", new Pair<>(new File(Constants.baseDatabasePath), ""));
            setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
    }
}