-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCacheUtilConfig.java
165 lines (138 loc) · 5 KB
/
CacheUtilConfig.java
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
package com.haohaohu.cachemanage;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import com.haohaohu.cachemanage.strategy.IEncryptStrategy;
import com.haohaohu.cachemanage.strategy.KeyStoreEncryptStrategy;
import java.io.File;
/**
* 配置项
*
* @author haohao on 2017/8/24 10:37
* @version v1.0
*/
public class CacheUtilConfig {
private Context context;
private boolean isKeyEncrypt;
private boolean isEncrypt;
private boolean memoryCache;
private ACache aCache;
private IEncryptStrategy mIEncryptStrategy;
private CacheUtilConfig(Builder builder) {
context = builder.context;
isEncrypt = builder.isEncrypt;
isKeyEncrypt = builder.isKeyEncrypt;
memoryCache = builder.memoryCache;
aCache = builder.aCache;
mIEncryptStrategy = builder.iEncryptStrategy;
}
public static Builder builder(Context context) {
return new Builder(context.getApplicationContext());
}
public Context getContext() {
return context;
}
public boolean isEncrypt() {
return isEncrypt;
}
public boolean isKeyEncrypt() {
return isKeyEncrypt;
}
public boolean isMemoryCache() {
return memoryCache;
}
public ACache getACache() {
return aCache;
}
public void setACache(ACache aCache) {
this.aCache = aCache;
}
public IEncryptStrategy getIEncryptStrategy() {
return mIEncryptStrategy;
}
public void setIEncryptStrategy(IEncryptStrategy mIEncryptStrategy) {
this.mIEncryptStrategy = mIEncryptStrategy;
}
public static class Builder {
public IEncryptStrategy iEncryptStrategy;
private Context context;
private boolean isEncrypt = true;//默认加密
private boolean isKeyEncrypt = true;//key默认加密
private boolean memoryCache = true;//默认保存到内存
private boolean isPreventPowerDelete = false;//防止被删除
private String alias;//私钥
private ACache aCache;//ACache示例
public Builder(Context context) {
this.context = context;
}
public Builder allowMemoryCache(boolean memoryCache) {
this.memoryCache = memoryCache;
return this;
}
public Builder setACache(ACache aCache) {
this.aCache = aCache;
return this;
}
public Builder setIEncryptStrategy(IEncryptStrategy iEncryptStrategy) {
this.iEncryptStrategy = iEncryptStrategy;
return this;
}
public Builder allowEncrypt(boolean isEncrypt) {
this.isEncrypt = isEncrypt;
return this;
}
public Builder allowKeyEncrypt(boolean isKeyEncrypt) {
this.isKeyEncrypt = isKeyEncrypt;
return this;
}
public Builder preventPowerDelete(boolean isPreventPowerDelete) {
this.isPreventPowerDelete = isPreventPowerDelete;
return this;
}
public Builder setAlias(String alise) {
this.alias = alise;
return this;
}
public CacheUtilConfig build() {
if (this.iEncryptStrategy == null) {
if (TextUtils.isEmpty(alias)) {
iEncryptStrategy = new KeyStoreEncryptStrategy(context);
} else {
iEncryptStrategy = new KeyStoreEncryptStrategy(context, alias);
}
}
if (this.aCache == null) {
if (isPreventPowerDelete) {
File file = context.getDatabasePath("cachetest");
if (file == null || !file.exists()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
context.openOrCreateDatabase("cachetest",
Context.MODE_ENABLE_WRITE_AHEAD_LOGGING, null);
}
file = context.getDatabasePath("cachetest");
}
File cacheFile = new File(file.getParent(), "cachemanage/");
if (cacheFile.exists() && cacheFile.isFile()) {
cacheFile.delete();
}
if (!cacheFile.exists()) {
cacheFile.mkdirs();
}
//删除临时创建的数据库文件
if (file.exists()) {
file.delete();
}
this.aCache = ACache.get(cacheFile);
} else {
File file = new File(ACache.getDiskCacheDir(context));
File cacheFile = new File(file.getParent(), "cachemanage/");
if (!cacheFile.exists()) {
cacheFile.mkdirs();
}
this.aCache = ACache.get(cacheFile);
}
}
return new CacheUtilConfig(this);
}
}
}