-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathMainActivity.java
More file actions
184 lines (157 loc) · 7.27 KB
/
Copy pathMainActivity.java
File metadata and controls
184 lines (157 loc) · 7.27 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package devliving.online.securedpreferencestoresample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.security.KeyStore;
import java.util.List;
import devliving.online.securedpreferencestore.DefaultRecoveryHandler;
import devliving.online.securedpreferencestore.SecuredPreferenceStore;
public class MainActivity extends AppCompatActivity {
EditText text1, number1, date1, text2, number2;
Button reloadButton, saveButton, multiThreadingButton, imageDemoBtn;
String TEXT_1 = "text_short", TEXT_2 = "text_long", NUMBER_1 = "number_int", NUMBER_2 = "number_float", DATE_1 = "date_text", DATE_2 = "date_long";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (EditText) findViewById(R.id.text_value_1);
number1 = (EditText) findViewById(R.id.number_1);
date1 = (EditText) findViewById(R.id.date_1);
text2 = (EditText) findViewById(R.id.text_value_2);
number2 = (EditText) findViewById(R.id.number_2);
reloadButton = (Button) findViewById(R.id.reload);
saveButton = (Button) findViewById(R.id.save);
multiThreadingButton = (Button) findViewById(R.id.multi_threading);
imageDemoBtn = findViewById(R.id.tryFile);
try {
//not mandatory, can be null too
String storeFileName = "securedStore";
//not mandatory, can be null too
String keyPrefix = "vss";
//it's better to provide one, and you need to provide the same key each time after the first time
byte[] seedKey = "seed".getBytes();
SecuredPreferenceStore.init(getApplicationContext(), storeFileName, keyPrefix, seedKey, new DefaultRecoveryHandler());
//SecuredPreferenceStore.init(getApplicationContext(), null);
setupStore();
} catch (Exception e) {
// Handle error.
e.printStackTrace();
}
reloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
reloadData();
} catch (Exception e) {
Log.e("SECURED-PREFERENCE", "", e);
Toast.makeText(MainActivity.this, "An exception occurred, see log for details", Toast.LENGTH_SHORT).show();
}
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
saveData();
} catch (Exception e) {
Log.e("SECURED-PREFERENCE", "", e);
Toast.makeText(MainActivity.this, "An exception occurred, see log for details", Toast.LENGTH_SHORT).show();
}
}
});
multiThreadingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runnable saveJob = new Runnable() {
@Override
public void run() {
Log.d("queen", Thread.currentThread().getName() + " gets started");
while(true) {
saveData();
reloadData();
}
}
};
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
new Thread(saveJob).start();
}
});
imageDemoBtn.setOnClickListener(v -> {
Intent intent = new Intent(this, FileDemoActivity.class);
startActivity(intent);
});
}
private void setupStore() {
SecuredPreferenceStore.setRecoveryHandler(new DefaultRecoveryHandler(){
@Override
protected boolean recover(Exception e, KeyStore keyStore, List<String> keyAliases, SharedPreferences preferences) {
Toast.makeText(MainActivity.this, "Encryption key got invalidated, will try to start over.", Toast.LENGTH_SHORT).show();
return super.recover(e, keyStore, keyAliases, preferences);
}
});
try {
reloadData();
} catch (Exception e) {
Log.e("SECURED-PREFERENCE", "", e);
Toast.makeText(this, "An exception occurred, see log for details", Toast.LENGTH_SHORT).show();
}
}
void reloadData() {
SecuredPreferenceStore prefStore = SecuredPreferenceStore.getSharedInstance();
final String textShort = prefStore.getString(TEXT_1, null);
final String textLong = prefStore.getString(TEXT_2, null);
final int numberInt = prefStore.getInt(NUMBER_1, 0);
final float numberFloat = prefStore.getFloat(NUMBER_2, 0);
final String dateText = prefStore.getString(DATE_1, null);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
text1.setText(textShort);
text2.setText(textLong);
number1.setText(String.valueOf(numberInt));
number2.setText(String.valueOf(numberFloat));
date1.setText(dateText);
}
});
}
void saveData() {
SecuredPreferenceStore prefStore = SecuredPreferenceStore.getSharedInstance();
prefStore.edit().putString(TEXT_1, text1.length() > 0 ? text1.getText().toString() : null).apply();
prefStore.edit().putString(TEXT_2, text2.length() > 0 ? text2.getText().toString() : null).apply();
prefStore.edit().putInt(NUMBER_1, number1.length() > 0 ? Integer.parseInt(number1.getText().toString().trim()) : 0).apply();
prefStore.edit().putFloat(NUMBER_2, number2.length() > 0 ? Float.parseFloat(number2.getText().toString().trim()) : 0).apply();
prefStore.edit().putString(DATE_1, date1.length() > 0 ? date1.getText().toString() : null).apply();
}
}