package epson.print.storage;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 存储类
 */
public class StorageSecureStore {
    static StorageSecureStore mInstance;
    final String COLUMN_KEY = "key";
    final String COLUMN_VALUE = BoxRequestsMetadata.UpdateItemMetadata.BoxMetadataUpdateTask.VALUE;
    final String CREATE_KEYVALUE = "CREATE TABLE IF NOT EXISTS keyvalue_tbl (key TEXT PRIMARY KEY, value BLOB);";
    final String DELETE_KEYVALUE = "key=?";
    final String DROP_KEYVALUE = "DROP TABLE IF EXISTS keyvalue_tbl;";
    final String ENCRYPT_ALGORITHM = "AES";
    final int ENCRYPT_KEY_BYTES = 16;
    final int INDEX_KEY = 0;
    final int INDEX_VALUE = 1;
    final String KEYVALUE_DBNAME = "storage.data";
    final String KEYVALUE_TBLNAME = "keyvalue_tbl";
    final int KEYVALUE_VERSION = 1;
    final String SELECT_KEYVALUE = "SELECT * FROM keyvalue_tbl;";
    Context mContext = null;
    Map<String, String> mKeyValueMap = null;

    public enum EXEC_MODE {
        NONE,
        OVERWRITE_IF_EXIST
    }

    public static StorageSecureStore getSharedSecureStore() {
        return mInstance;
    }

    public boolean put(String str, String str2) {
        KeyValueDatabase keyValueDatabase = new KeyValueDatabase(mContext);
        boolean insert = keyValueDatabase.insert(str, str2);
        keyValueDatabase.close();
        reloadCache();
        return insert;
    }

    public boolean put(String str, String str2, EXEC_MODE exec_mode) {
        if (exec_mode == EXEC_MODE.OVERWRITE_IF_EXIST && fetch(str) != null) {
            revoke(str);
        }
        return put(str, str2);
    }

    public String fetch(String str) {
        return mKeyValueMap.get(str);
    }

    public boolean revoke(String str) {
        KeyValueDatabase keyValueDatabase = new KeyValueDatabase(mContext);
        boolean delete = keyValueDatabase.delete(str);
        keyValueDatabase.close();
        reloadCache();
        return delete;
    }

    public static void initSharedSecureStore(Context context) {
        if (mInstance == null) {
            mInstance = new StorageSecureStore(context);
        }
    }

    private byte[] encodeAes(byte[] bArr, String str) {
        if (bArr == null || str == null) {
            return null;
        }
        try {
            byte[] makeKeyBytes = makeKeyBytes(16, str.getBytes());
            Cipher instance = Cipher.getInstance("AES");
            instance.init(1, new SecretKeySpec(makeKeyBytes, "AES"));
            return instance.doFinal(bArr);
        } catch (Exception unused) {
            return null;
        }
    }

    private byte[] decodeAes(byte[] bArr, String str) {
        if (bArr == null || str == null) {
            return null;
        }
        try {
            byte[] makeKeyBytes = makeKeyBytes(16, str.getBytes());
            Cipher instance = Cipher.getInstance("AES");
            instance.init(2, new SecretKeySpec(makeKeyBytes, "AES"));
            return instance.doFinal(bArr);
        } catch (Exception unused) {
            return null;
        }
    }

    private byte[] makeKeyBytes(int i, byte[] bArr) {
        byte[] bArr2 = new byte[i];
        for (int i2 = 0; i2 < bArr2.length; i2++) {
            if (i2 < bArr.length) {
                bArr2[i2] = bArr[i2];
            } else {
                bArr2[i2] = 0;
            }
        }
        return bArr2;
    }

    StorageSecureStore(Context context) {
        mContext = context;
        reloadCache();
    }

    StorageSecureStore() {
        reloadCache();
    }

    private void reloadCache() {
        KeyValueDatabase keyValueDatabase = new KeyValueDatabase(mContext);
        mKeyValueMap = keyValueDatabase.select();
        keyValueDatabase.close();
    }

    public static void invalidateCache() {
        mInstance = null;
    }

    class KeyValueDatabase {
        String mSecretText;
        SQLiteDatabase mSqlDB = null;
        DatabaseHelper mSqlHelper = null;

        KeyValueDatabase(Context context) {
            mSqlHelper = new DatabaseHelper(context, "storage.data", (SQLiteDatabase.CursorFactory) null, 1);
            mSqlDB = mSqlHelper.getWritableDatabase();
            mSecretText = getClass().getName();
        }

        /* access modifiers changed from: package-private */
        /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public synchronized boolean insert(String r5, String r6) {
            /*
                r4 = this;
                monitor-enter(r4)
                r0 = -1
                com.epson.iprint.storage.StorageSecureStore r2 = com.epson.iprint.storage.StorageSecureStore.this     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                byte[] r6 = r6.getBytes()     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                java.lang.String r3 = r4.mSecretText     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                byte[] r6 = r2.encodeAes(r6, r3)     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                if (r6 == 0) goto L_0x0031
                android.content.ContentValues r2 = new android.content.ContentValues     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                r2.<init>()     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                java.lang.String r3 = "key"
                r2.put(r3, r5)     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                java.lang.String r5 = "value"
                r2.put(r5, r6)     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                android.database.sqlite.SQLiteDatabase r5 = r4.mSqlDB     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                java.lang.String r6 = "keyvalue_tbl"
                java.lang.String r3 = ""
                long r5 = r5.insert(r6, r3, r2)     // Catch:{ Exception -> 0x0031, all -> 0x002c }
                r0 = r5
                goto L_0x0031
            L_0x002c:
                r5 = move-exception
                throw r5     // Catch:{ all -> 0x002e }
            L_0x002e:
                r5 = move-exception
                monitor-exit(r4)
                throw r5
            L_0x0031:
                r5 = 0
                int r2 = (r0 > r5 ? 1 : (r0 == r5 ? 0 : -1))
                if (r2 < 0) goto L_0x0039
                r5 = 1
                goto L_0x003a
            L_0x0039:
                r5 = 0
            L_0x003a:
                monitor-exit(r4)
                return r5
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.StorageSecureStore.KeyValueDatabase.insert(java.lang.String, java.lang.String):boolean");
        }

        /* access modifiers changed from: package-private */
        /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public synchronized boolean delete(String r7) {
            /*
                r6 = this;
                monitor-enter(r6)
                r0 = 0
                android.database.sqlite.SQLiteDatabase r1 = r6.mSqlDB     // Catch:{ Exception -> 0x0018, all -> 0x0015 }
                java.lang.String r2 = "keyvalue_tbl"
                java.lang.String r3 = "key=?"
                r4 = 1
                java.lang.String[] r5 = new java.lang.String[r4]     // Catch:{ Exception -> 0x0018, all -> 0x0015 }
                r5[r0] = r7     // Catch:{ Exception -> 0x0018, all -> 0x0015 }
                int r7 = r1.delete(r2, r3, r5)     // Catch:{ Exception -> 0x0018, all -> 0x0015 }
                if (r7 <= 0) goto L_0x0018
                r0 = 1
                goto L_0x0018
            L_0x0015:
                r7 = move-exception
                monitor-exit(r6)
                throw r7
            L_0x0018:
                monitor-exit(r6)
                return r0
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.StorageSecureStore.KeyValueDatabase.delete(java.lang.String):boolean");
        }

        /* JADX WARNING: type inference failed for: r1v0, types: [java.lang.String[], android.database.Cursor] */
        private synchronized Map<String, String> select() {
            HashMap hashMap;
            hashMap = new HashMap();
            Cursor cursor = 0;
            try {
                cursor = mSqlDB.rawQuery("SELECT * FROM keyvalue_tbl;", cursor);
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToPosition(i);
                    String string = cursor.getString(0);
                    byte[] decodeAes = StorageSecureStore.this.decodeAes(cursor.getBlob(1), mSecretText);
                    if (decodeAes != null) {
                        hashMap.put(string, new String(decodeAes));
                    }
                }
            } finally {
                if (cursor != 0) {
                    cursor.close();
                }
            }
            return hashMap;
        }

        /* access modifiers changed from: package-private */
        /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public synchronized void close() {
            /*
                r1 = this;
                monitor-enter(r1)
                android.database.sqlite.SQLiteDatabase r0 = r1.mSqlDB     // Catch:{ Exception -> 0x000f, all -> 0x000c }
                r0.close()     // Catch:{ Exception -> 0x000f, all -> 0x000c }
                com.epson.iprint.storage.StorageSecureStore$KeyValueDatabase$DatabaseHelper r0 = r1.mSqlHelper     // Catch:{ Exception -> 0x000f, all -> 0x000c }
                r0.close()     // Catch:{ Exception -> 0x000f, all -> 0x000c }
                goto L_0x000f
            L_0x000c:
                r0 = move-exception
                monitor-exit(r1)
                throw r0
            L_0x000f:
                monitor-exit(r1)
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.StorageSecureStore.KeyValueDatabase.close():void");
        }

        class DatabaseHelper extends SQLiteOpenHelper {
            DatabaseHelper(Context context, String str, SQLiteDatabase.CursorFactory cursorFactory, int i) {
                super(context, str, cursorFactory, i);
            }

            public void onCreate(SQLiteDatabase sQLiteDatabase) {
                try {
                    sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS keyvalue_tbl (key TEXT PRIMARY KEY, value BLOB);");
                } catch (Exception unused) {
                }
            }

            public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
                try {
                    sQLiteDatabase.execSQL("DROP TABLE IF EXISTS keyvalue_tbl;");
                } catch (Exception unused) {
                }
                onCreate(sQLiteDatabase);
            }
        }
    }
}
