Revision 61b66389 app/src/info/guardianproject/notepadbot/Settings.java
app/src/info/guardianproject/notepadbot/Settings.java | ||
---|---|---|
1 | 1 |
package info.guardianproject.notepadbot; |
2 | 2 |
|
3 | 3 |
|
4 |
import net.simonvt.numberpicker.NumberPicker; |
|
5 |
import info.guardianproject.cacheword.CacheWordActivityHandler; |
|
4 | 6 |
import info.guardianproject.cacheword.Constants; |
7 |
import info.guardianproject.cacheword.ICacheWordSubscriber; |
|
5 | 8 |
|
6 | 9 |
import android.annotation.SuppressLint; |
10 |
import android.app.AlertDialog; |
|
7 | 11 |
import android.content.Context; |
12 |
import android.content.DialogInterface; |
|
13 |
import android.content.Intent; |
|
8 | 14 |
import android.os.Build; |
9 | 15 |
import android.os.Bundle; |
10 | 16 |
import android.preference.Preference; |
17 |
import android.preference.Preference.OnPreferenceChangeListener; |
|
11 | 18 |
import android.preference.PreferenceFragment; |
12 | 19 |
import android.preference.PreferenceManager; |
13 | 20 |
import android.support.v4.app.NavUtils; |
21 |
import android.util.Log; |
|
14 | 22 |
|
15 | 23 |
import com.actionbarsherlock.app.SherlockPreferenceActivity; |
16 | 24 |
import com.actionbarsherlock.view.MenuItem; |
17 | 25 |
|
18 | 26 |
@SuppressLint("NewApi") |
19 | 27 |
@SuppressWarnings("deprecation") |
20 |
public class Settings extends SherlockPreferenceActivity { |
|
28 |
public class Settings extends SherlockPreferenceActivity implements ICacheWordSubscriber {
|
|
21 | 29 |
|
22 | 30 |
public static final String LANG_SEL_KEY = "langSelected"; |
23 | 31 |
|
32 |
private CacheWordActivityHandler mCacheWord; |
|
33 |
|
|
24 | 34 |
@Override |
25 | 35 |
public void onCreate(Bundle savedInstanceState) { |
26 | 36 |
super.onCreate(savedInstanceState); |
27 | 37 |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
28 |
|
|
38 |
|
|
39 |
mCacheWord = new CacheWordActivityHandler(this, ((App)getApplication()).getCWSettings()); |
|
40 |
|
|
29 | 41 |
// If in android 3+ use a preference fragment which is the new recommended way |
30 | 42 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { |
31 | 43 |
getFragmentManager().beginTransaction() |
... | ... | |
36 | 48 |
addPreferencesFromResource(R.xml.settings); |
37 | 49 |
findPreference(Constants.SHARED_PREFS_TIMEOUT) |
38 | 50 |
.setOnPreferenceClickListener(changeLockTimeoutListener); |
51 |
findPreference(Constants.SHARED_PREFS_VIBRATE) |
|
52 |
.setOnPreferenceChangeListener(vibrateChangeListener); |
|
39 | 53 |
} |
40 | 54 |
}) |
41 | 55 |
.commit(); |
42 | 56 |
} else { |
43 | 57 |
// Otherwise load the preferences.xml in the Activity like in previous android versions |
44 | 58 |
addPreferencesFromResource(R.xml.settings); |
45 |
findPreference(Constants.SHARED_PREFS_TIMEOUT).setOnPreferenceClickListener(changeLockTimeoutListener); |
|
59 |
findPreference(Constants.SHARED_PREFS_TIMEOUT) |
|
60 |
.setOnPreferenceClickListener(changeLockTimeoutListener); |
|
61 |
findPreference(Constants.SHARED_PREFS_VIBRATE) |
|
62 |
.setOnPreferenceChangeListener(vibrateChangeListener); |
|
46 | 63 |
} |
47 | 64 |
} |
48 | 65 |
|
66 |
|
|
67 |
|
|
49 | 68 |
@Override |
50 | 69 |
public boolean onOptionsItemSelected(MenuItem item) { |
51 | 70 |
switch (item.getItemId()) { |
... | ... | |
75 | 94 |
new Preference.OnPreferenceClickListener() { |
76 | 95 |
@Override |
77 | 96 |
public boolean onPreferenceClick(Preference pref) { |
78 |
|
|
97 |
changeTimeoutPrompt(); |
|
79 | 98 |
return true; |
80 | 99 |
} |
81 | 100 |
}; |
82 | 101 |
|
102 |
private Preference.OnPreferenceChangeListener vibrateChangeListener = |
|
103 |
new OnPreferenceChangeListener(){ |
|
104 |
@Override |
|
105 |
public boolean onPreferenceChange(Preference pref, Object newValue) { |
|
106 |
// save option internally in cacheword as well |
|
107 |
mCacheWord.setVibrateSetting((Boolean) newValue); |
|
108 |
return true; |
|
109 |
} |
|
110 |
}; |
|
111 |
|
|
83 | 112 |
public static final boolean getNoteLinesOption(Context context) { |
84 | 113 |
boolean defValue = context.getResources().getBoolean(R.bool.notecipher_uselines_default); |
85 | 114 |
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(NConstants.SHARED_PREFS_NOTELINES, defValue); |
86 | 115 |
} |
116 |
|
|
117 |
private void changeTimeoutPrompt() { |
|
118 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
119 |
builder.setTitle(R.string.change_timeout_prompt_title); |
|
120 |
builder.setMessage(R.string.change_timeout_prompt); |
|
121 |
final NumberPicker input = new NumberPicker(this); |
|
122 |
input.setMinValue(1); |
|
123 |
input.setMaxValue(60); |
|
124 |
input.setValue( mCacheWord.getTimeoutMinutes() ); |
|
125 |
builder.setView(input); |
|
126 |
|
|
127 |
builder.setPositiveButton("OK", |
|
128 |
new DialogInterface.OnClickListener() { |
|
129 |
@Override |
|
130 |
public void onClick(DialogInterface dialog, int which) { |
|
131 |
int timeout = input.getValue(); |
|
132 |
mCacheWord.setTimeoutMinutes(timeout); |
|
133 |
dialog.dismiss(); |
|
134 |
} |
|
135 |
}); |
|
136 |
builder.setNegativeButton("Cancel", |
|
137 |
new DialogInterface.OnClickListener() { |
|
138 |
@Override |
|
139 |
public void onClick(DialogInterface dialog, int which) { |
|
140 |
dialog.cancel(); |
|
141 |
} |
|
142 |
}); |
|
143 |
|
|
144 |
builder.show(); |
|
145 |
} |
|
146 |
|
|
147 |
@Override |
|
148 |
public void onCacheWordUninitialized() { |
|
149 |
Log.d(NConstants.TAG, "onCacheWordUninitialized"); |
|
150 |
System.gc(); |
|
151 |
showLockScreen(); |
|
152 |
} |
|
153 |
|
|
154 |
@Override |
|
155 |
public void onCacheWordLocked() { |
|
156 |
Log.d(NConstants.TAG, "onCacheWordLocked"); |
|
157 |
System.gc(); |
|
158 |
showLockScreen(); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public void onCacheWordOpened() { |
|
163 |
Log.d(NConstants.TAG, "onCacheWordOpened"); |
|
164 |
} |
|
165 |
|
|
166 |
@Override |
|
167 |
protected void onPause() { |
|
168 |
super.onPause(); |
|
169 |
mCacheWord.onPause(); |
|
170 |
} |
|
171 |
|
|
172 |
@Override |
|
173 |
protected void onResume() { |
|
174 |
super.onResume(); |
|
175 |
mCacheWord.onResume(); |
|
176 |
} |
|
177 |
|
|
178 |
void showLockScreen() { |
|
179 |
Intent intent = new Intent(this, LockScreenActivity.class); |
|
180 |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
|
181 |
intent.putExtra("originalIntent", getIntent()); |
|
182 |
startActivity(intent); |
|
183 |
finish(); |
|
184 |
} |
|
87 | 185 |
} |
Also available in: Unified diff