Statistics
| Branch: | Tag: | Revision:

notecipher / app / src / info / guardianproject / notepadbot / Settings.java @ 61b66389

History | View | Annotate | Download (5.85 KB)

1
package info.guardianproject.notepadbot;
2

    
3

    
4
import net.simonvt.numberpicker.NumberPicker;
5
import info.guardianproject.cacheword.CacheWordActivityHandler;
6
import info.guardianproject.cacheword.Constants;
7
import info.guardianproject.cacheword.ICacheWordSubscriber;
8

    
9
import android.annotation.SuppressLint;
10
import android.app.AlertDialog;
11
import android.content.Context;
12
import android.content.DialogInterface;
13
import android.content.Intent;
14
import android.os.Build;
15
import android.os.Bundle;
16
import android.preference.Preference;
17
import android.preference.Preference.OnPreferenceChangeListener;
18
import android.preference.PreferenceFragment;
19
import android.preference.PreferenceManager;
20
import android.support.v4.app.NavUtils;
21
import android.util.Log;
22

    
23
import com.actionbarsherlock.app.SherlockPreferenceActivity;
24
import com.actionbarsherlock.view.MenuItem;
25

    
26
@SuppressLint("NewApi")
27
@SuppressWarnings("deprecation")
28
public class Settings extends SherlockPreferenceActivity implements ICacheWordSubscriber {
29
        
30
        public static final String LANG_SEL_KEY = "langSelected";
31
        
32
        private CacheWordActivityHandler mCacheWord;
33
        
34
        @Override
35
        public void onCreate(Bundle savedInstanceState) {
36
                super.onCreate(savedInstanceState);
37
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
38
                
39
                mCacheWord = new CacheWordActivityHandler(this, ((App)getApplication()).getCWSettings());
40
                
41
                // If in android 3+ use a preference fragment which is the new recommended way
42
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
43
                        getFragmentManager().beginTransaction()
44
                                        .replace(android.R.id.content, new PreferenceFragment() {
45
                                                @Override
46
                                                public void onCreate(final Bundle savedInstanceState) {
47
                                                        super.onCreate(savedInstanceState);
48
                                                        addPreferencesFromResource(R.xml.settings);
49
                                                        findPreference(Constants.SHARED_PREFS_TIMEOUT)
50
                                                                .setOnPreferenceClickListener(changeLockTimeoutListener);
51
                                                        findPreference(Constants.SHARED_PREFS_VIBRATE)
52
                                                                .setOnPreferenceChangeListener(vibrateChangeListener);
53
                                                }
54
                                        })
55
                                        .commit();
56
                } else {
57
                        // Otherwise load the preferences.xml in the Activity like in previous android versions
58
                        addPreferencesFromResource(R.xml.settings);
59
                        findPreference(Constants.SHARED_PREFS_TIMEOUT)
60
                                .setOnPreferenceClickListener(changeLockTimeoutListener);
61
                        findPreference(Constants.SHARED_PREFS_VIBRATE)
62
                                .setOnPreferenceChangeListener(vibrateChangeListener);
63
                }
64
        }
65
        
66
        
67
        
68
        @Override
69
        public boolean onOptionsItemSelected(MenuItem item) {
70
                switch (item.getItemId()) {
71
                case android.R.id.home:
72
                        NavUtils.navigateUpFromSameTask(this);
73
                        return true;
74
                }
75
                return super.onOptionsItemSelected(item);
76
        }
77

    
78
        @Override
79
        public void onDestroy() {
80
                // reload preferences on exit from settings screen
81
                Context context = getApplicationContext();
82
                loadSettings(context);
83
                super.onDestroy();
84
        }
85

    
86
        /** Loads user settings to app. Called when settings change and users exits from 
87
         *  settings screen or when the app first starts. 
88
         *  */
89
        public static void loadSettings(Context context) {
90
                
91
        }
92
        
93
        private Preference.OnPreferenceClickListener changeLockTimeoutListener = 
94
                        new Preference.OnPreferenceClickListener() {
95
                                        @Override
96
                                        public boolean onPreferenceClick(Preference pref) {
97
                                                changeTimeoutPrompt();
98
                                                return true;
99
                                        }
100
        };
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
        
112
        public static final boolean getNoteLinesOption(Context context) {
113
                boolean defValue = context.getResources().getBoolean(R.bool.notecipher_uselines_default);
114
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(NConstants.SHARED_PREFS_NOTELINES, defValue);
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
    }
185
}