- SampleEclipseProject_20130327.zip
- SecurePreferences
- .classpath
- .project
- AndroidManifest.xml
- assets
- bin
- AndroidManifest.xml
- classes.dex
- classes
- com
- example
- securepreferences
- BuildConfig.class
- MainActivity.class
- NewPreferenceActivity.class
- NewPreferenceFragment.class
- OldPreferenceActivity.class
- R$array.class
- R$attr.class
- R$dimen.class
- R$drawable.class
- R$layout.class
- R$string.class
- R$style.class
- R$xml.class
- R.class
- SecurePreferences$Editor.class
- SecurePreferences.class
- dexedLibs
- res
- drawable-hdpi
- ic_launcher.png
- drawable-mdpi
- ic_launcher.png
- drawable-xhdpi
- ic_launcher.png
- resources.ap_
- SecurePreferences.apk
- gen
- com
- example
- securepreferences
- proguard-project.txt
- project.properties
- res
- drawable-hdpi
- ic_launcher.png
- drawable-mdpi
- ic_launcher.png
- drawable-xhdpi
- ic_launcher.png
- layout
- values
- values-sw600dp
- values-sw720dp-land
- values-v11
- values-v14
- xml
- src
- com
- example
- securepreferences
- SecurePreferences_20130327.zip
|
/*
* Copyright (C) 2013, Daniel Abraham
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.securepreferences;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import java.util.Map.Entry;
public class OldPreferenceActivity extends PreferenceActivity {
private SharedPreferences mInsecurePrefs;
private SharedPreferences mSecurePrefs;
private CheckBoxPreference mCheckBox;
private EditTextPreference mText;
private ListPreference mList;
@Override
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
mInsecurePrefs = PreferenceManager.getDefaultSharedPreferences(this);
mSecurePrefs = new SecurePreferences(this);
mCheckBox = (CheckBoxPreference) findPreference(getString(R.string.checkbox_key));
mText = (EditTextPreference) findPreference(getString(R.string.text_key));
mList = (ListPreference) findPreference(getString(R.string.list_key));
}
@Override
public void onStart() {
super.onStart();
// Decrypt relevant key/value pairs, if they exist
for (Entry<String, ?> entry : mSecurePrefs.getAll().entrySet()) {
final String key = entry.getKey();
if (key == null) {
continue;
} else if (key.equals(getString(R.string.checkbox_key))) {
mCheckBox.setChecked(mSecurePrefs.getBoolean(key, false));
} else if (key.equals(getString(R.string.text_key))) {
mText.setText(mSecurePrefs.getString(key, null));
} else if (key.equals(getString(R.string.list_key))) {
final int value = Integer.parseInt(mSecurePrefs.getString(key, null));
mList.setValueIndex(value - 1); // Zero based index = selection value - 1
}
}
}
@Override
public void onStop() {
super.onStop();
// Replace unencrypted key/value pairs with encrypted ones
final Editor insecureEditor = mInsecurePrefs.edit();
final Editor secureEditor = mSecurePrefs.edit();
String key = getString(R.string.checkbox_key);
if (mInsecurePrefs.contains(key)) {
secureEditor.putBoolean(key, mInsecurePrefs.getBoolean(key, false));
insecureEditor.remove(key);
}
key = getString(R.string.text_key);
if (mInsecurePrefs.contains(key)) {
secureEditor.putString(key, mInsecurePrefs.getString(key, null));
insecureEditor.remove(key);
}
key = getString(R.string.list_key);
if (mInsecurePrefs.contains(key)) {
secureEditor.putString(key, mInsecurePrefs.getString(key, null));
insecureEditor.remove(key);
}
insecureEditor.commit();
secureEditor.commit();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm a veteran software engineer. My professional focus is currently test automation for next-generation server network controllers. I also work a lot with Linux and Android, and dabble with wireless technologies in my spare time.