Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Conflicts:
	library/build.gradle
  • Loading branch information
scottyab committed Mar 5, 2015
2 parents 7e5edde + e1c4ccf commit f4fc698
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Much of the original code is from Daniel Abraham article on [codeproject](http:/


##Release Notes:
0.0.4
* Gralde support thanks @yelinaung
* Fix for OnPreferenceChanged listener @richardleggett

0.0.3

* Added test Project
Expand Down
1 change: 0 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ android {
manifest.srcFile 'AndroidManifest.xml'
}
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
}
65 changes: 57 additions & 8 deletions library/src/com/securepreferences/SecurePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class SecurePreferences implements SharedPreferences {
private static SharedPreferences sFile;
private static byte[] sKey;
private static boolean sLoggingEnabled = false;
// links user's OnSharedPreferenceChangeListener to secure OnSharedPreferenceChangeListener
private static HashMap<OnSharedPreferenceChangeListener, OnSharedPreferenceChangeListener>
sOnSharedPreferenceChangeListeners;
private static final String TAG = SecurePreferences.class.getName();

/**
Expand Down Expand Up @@ -108,6 +111,9 @@ public SecurePreferences(Context context) {
}
throw new IllegalStateException(e);
}
// initialize OnSecurePreferencesChangeListener HashMap
sOnSharedPreferenceChangeListeners =
new HashMap<OnSharedPreferenceChangeListener, OnSharedPreferenceChangeListener>(10);
}

private static String encode(byte[] input) {
Expand Down Expand Up @@ -495,17 +501,60 @@ public static void setLoggingEnabled(boolean loggingEnabled) {
sLoggingEnabled = loggingEnabled;
}

@Override
public void registerOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener listener) {
SecurePreferences.sFile
.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void registerOnSharedPreferenceChangeListener(
final OnSharedPreferenceChangeListener listener) {
SecurePreferences.sFile
.registerOnSharedPreferenceChangeListener(listener);
}

/**
* @param listener OnSharedPreferenceChangeListener
* @param decryptKeys Callbacks receive the "key" parameter decrypted
*/
public void registerOnSharedPreferenceChangeListener(
final OnSharedPreferenceChangeListener listener, boolean decryptKeys) {

if(!decryptKeys) {
registerOnSharedPreferenceChangeListener(listener);
return;
}

// wrap user's OnSharedPreferenceChangeListener with another that decrypts key before
// calling the onSharedPreferenceChanged callback
OnSharedPreferenceChangeListener secureListener =
new OnSharedPreferenceChangeListener() {
private OnSharedPreferenceChangeListener mInsecureListener = listener;
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
try {
String decryptedKey = decrypt(key);
if(decryptedKey != null) {
mInsecureListener.onSharedPreferenceChanged(sharedPreferences,
decryptedKey);
}
} catch (Exception e) {
Log.w(TAG, "Unable to decrypt key: " + key);
}
}
};
sOnSharedPreferenceChangeListeners.put(listener, secureListener);
SecurePreferences.sFile
.registerOnSharedPreferenceChangeListener(secureListener);
}

@Override
public void unregisterOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener listener) {
SecurePreferences.sFile
.unregisterOnSharedPreferenceChangeListener(listener);
if(sOnSharedPreferenceChangeListeners.containsKey(listener)) {
OnSharedPreferenceChangeListener secureListener =
sOnSharedPreferenceChangeListeners.remove(listener);
SecurePreferences.sFile
.unregisterOnSharedPreferenceChangeListener(secureListener);
} else {
SecurePreferences.sFile
.unregisterOnSharedPreferenceChangeListener(listener);
}
}
}
17 changes: 15 additions & 2 deletions sample/src/com/securepreferences/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.securepreferences.sample;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
Expand All @@ -31,15 +32,15 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.securepreferences.SecurePreferences;

public class MainActivity extends Activity {

private SecurePreferences mSecurePrefs;
private SecurePreferences mSecurePrefs;
private SharedPreferences mInSecurePrefs;

private TextView encValuesTextView;
Expand All @@ -65,6 +66,18 @@ public void onSharedPreferenceChanged(
}
});

// listen for specific keys and receive unencrypted key name on change
mSecurePrefs
.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Toast.makeText(MainActivity.this,
"SecurePreference changed with key: " + key,
Toast.LENGTH_SHORT).show();
}
},
true);
}

private void initViews() {
Expand Down

0 comments on commit f4fc698

Please sign in to comment.