-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedPreferencesActivity.java
181 lines (130 loc) · 5.52 KB
/
SharedPreferencesActivity.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//Author : Kenny
//Internal storage for ObjectOutputStream
package edu.wisc.ece.ece454minilab5;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class SharedPreferencesActivity extends Activity {
private static final String TAG = "Minilab5";
public static final String PREFS_MYRUNS = "MyPreferences";
String FILENAME = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
// Load user data to screen using the private helper function
// loadProfile
loadUserData();
}
public void onSaveClicked(View v) throws IOException {
// Save all information from the screen into a "shared preferences"
// using private helper function
saveUserData();
Toast.makeText(getApplicationContext(),
getString(R.string.save_message), Toast.LENGTH_SHORT).show();
Intent mIntent = new Intent(SharedPreferencesActivity.this,
MainLayoutActivity.class);
startActivity(mIntent);
}
public void onCancelClicked(View v) {
Toast.makeText(getApplicationContext(),
getString(R.string.cancel_message), Toast.LENGTH_SHORT).show();
Intent mIntent = new Intent(SharedPreferencesActivity.this,
MainLayoutActivity.class);
startActivity(mIntent);
}
// ****************** private helper functions ***************************//
// load the user data from shared preferences if there is no data make sure
// that we set it to something reasonable
private void loadUserData() {
// We can also use log.d to print to the LogCat
Log.d(TAG, "loadUserData()");
// Load and update all profile views
// Get the shared preferences - create or retrieve the activity
// preference object
String mKey = getString(R.string.preference_name);
SharedPreferences mPrefs = getSharedPreferences(mKey, MODE_PRIVATE);
// Load the user email
mKey = getString(R.string.preference_key_profile_email);
String mValue = mPrefs.getString(mKey, " ");
((EditText) findViewById(R.id.editEmail)).setText(mValue);
// Load gender info and set radio box
mKey = getString(R.string.preference_key_profile_gender);
int mIntValue = mPrefs.getInt(mKey, -1);
// In case there isn't one saved before:
if (mIntValue >= 0) {
// Find the radio button that should be checked.
RadioButton radioBtn = (RadioButton) ((RadioGroup) findViewById(R.id.radioGender))
.getChildAt(mIntValue);
// Check the button.
radioBtn.setChecked(true);
}
}
// load the user data from shared preferences if there is no data make sure
// that we set it to something reasonable
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void saveUserData() throws IOException {
Log.d(TAG, "saveUserData()");
// Getting the shared preferences editor
String mKey = getString(R.string.student_names);
SharedPreferences mPrefs = getSharedPreferences(mKey, MODE_PRIVATE);
// Load name using email field
mKey = getString(R.string.student_set);
Set<String> mValue = mPrefs.getStringSet(mKey, new HashSet<String>());
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.clear();
// Save name
String newStudent = (String) ((EditText) findViewById(R.id.editEmail))
.getText().toString();
HashMap<String, String> map = new HashMap<String, String>();
map.put("hello","kenny");
//String FILENAME = " ";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oout = new ObjectOutputStream(fos);
oout.writeObject(map);
oout.flush();
fos.close();
FileInputStream fin = openFileInput(FILENAME);
ObjectInputStream oin = new ObjectInputStream(fin);
HashMap<String, String> mapin = new HashMap<>();
try {
mapin = (HashMap<String, String>) oin.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Log.d("lol",mapin.get("hello"));
String res = convertStreamToString(fin);
Log.d("TEST",""+res);
// String studentName = new String(fin.read());
fin.close();
Log.d("!!!", newStudent);
//mValue.add(newStudent);
//mEditor.putStringSet(mKey, mValue);
mEditor.commit();
Toast.makeText(getApplicationContext(), "saved name: " + mValue,
Toast.LENGTH_SHORT).show();
}
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}