Skip to content

Commit 65a3f44

Browse files
committed
use weakrefrence
1 parent 67208b7 commit 65a3f44

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.1'
8+
classpath 'com.android.tools.build:gradle:2.3.3'
99
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1010

1111
// NOTE: Do not place your application dependencies here; they belong

library/src/main/java/com/zeroone/conceal/ConcealCrypto.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25+
import java.lang.ref.WeakReference;
2526
import java.security.MessageDigest;
2627
import java.security.NoSuchAlgorithmException;
2728

@@ -38,6 +39,7 @@
3839
public class ConcealCrypto {
3940

4041
private Crypto crypto;
42+
private Context mContext;
4143
private KeyChain keyChain;
4244
private Entity mEntityPassword = Entity.create(BuildConfig.APPLICATION_ID);
4345
private boolean enableCrypto=true;
@@ -46,6 +48,7 @@ public class ConcealCrypto {
4648

4749
private ConcealCrypto(CryptoBuilder builder){
4850
crypto = builder.crypto;
51+
mContext = builder.context.get();
4952
mEntityPassword = builder.mEntityPassword;
5053
enableCrypto = builder.mEnabledCrypto;
5154
enableHashKey = builder.mEnableHashKey;
@@ -320,7 +323,7 @@ public String deObscureWithIteration(String cipher,int iteration){
320323
}
321324

322325
public static class CryptoBuilder{
323-
Context context;
326+
private WeakReference<Context> context;
324327
private KeyChain makeKeyChain;
325328
private Crypto crypto;
326329
private CryptoConfig mKeyChain = CryptoConfig.KEY_256;
@@ -331,7 +334,7 @@ public static class CryptoBuilder{
331334
private String mFolderName;
332335

333336
public CryptoBuilder(Context context) {
334-
this.context = context;
337+
this.context = new WeakReference<Context>(context.getApplicationContext());
335338
}
336339

337340
public CryptoBuilder setKeyChain(CryptoConfig config){
@@ -365,8 +368,12 @@ public CryptoBuilder setStoredFolder(String folderName){
365368

366369
public ConcealCrypto create(){
367370

371+
if (this.context == null){
372+
throw new RuntimeException("Context cannot be null");
373+
}
374+
368375
mEntityPassword = Entity.create(CipherUtils.obscureEncodeSixFourString(mEntityPasswordRaw.getBytes()));
369-
makeKeyChain = new SharedPrefsBackedKeyChain(context,(mKeyChain==null)?CryptoConfig.KEY_256:mKeyChain);
376+
makeKeyChain = new SharedPrefsBackedKeyChain(this.context.get(),(mKeyChain==null)?CryptoConfig.KEY_256:mKeyChain);
370377

371378
if (mKeyChain == null) {
372379
crypto = AndroidConceal.get().createDefaultCrypto(makeKeyChain);

library/src/main/java/com/zeroone/conceal/ConcealPrefRepository.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.lang.ref.WeakReference;
2122
import java.util.ArrayList;
2223
import java.util.HashMap;
2324
import java.util.LinkedHashMap;
@@ -49,7 +50,7 @@ public class ConcealPrefRepository {
4950

5051
@SuppressLint("CommitPrefEdits")
5152
private ConcealPrefRepository(@NonNull PreferencesBuilder builder){
52-
mContext = builder.mContext;
53+
mContext = builder.mContext.get();
5354
mKeyChain = builder.mKeyChain;
5455
mEnabledCrypto = builder.mEnabledCrypto;
5556
mEnableCryptKey = builder.mEnableCryptKey;
@@ -851,7 +852,7 @@ public void apply() {
851852
****************************************************************************************/
852853
public static class PreferencesBuilder{
853854

854-
private Context mContext;
855+
private WeakReference<Context> mContext;
855856
private CryptoConfig mKeyChain = CryptoConfig.KEY_256;
856857
private String mPrefname = null;
857858
private String mFolderName = null;
@@ -862,7 +863,7 @@ public static class PreferencesBuilder{
862863
private OnDataChangeListener onDataChangeListener;
863864

864865
public PreferencesBuilder(Context context) {
865-
mContext = context;
866+
mContext = new WeakReference<>(context.getApplicationContext());
866867
}
867868

868869
public PreferencesBuilder useDefaultPrefStorage(){
@@ -932,6 +933,10 @@ public PreferencesBuilder setPrefListener(OnDataChangeListener listener){
932933
*/
933934
public ConcealPrefRepository create(){
934935

936+
if (this.mContext == null){
937+
throw new RuntimeException("Context cannot be null");
938+
}
939+
935940
if(mFolderName !=null){
936941
File file = new File(mFolderName);
937942
try {
@@ -947,10 +952,10 @@ public ConcealPrefRepository create(){
947952
}
948953

949954
if (mPrefname!=null){
950-
sharedPreferences = mContext.getSharedPreferences(CipherUtils.obscureEncodeSixFourString(mPrefname.getBytes()), MODE_PRIVATE);
955+
sharedPreferences = this.mContext.get().getSharedPreferences(CipherUtils.obscureEncodeSixFourString(mPrefname.getBytes()), MODE_PRIVATE);
951956
}
952957
else {
953-
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
958+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.mContext.get());
954959
}
955960

956961
return new ConcealPrefRepository(this);

0 commit comments

Comments
 (0)