Statistics
| Branch: | Tag: | Revision:

notecipher / app / src / info / guardianproject / notepadbot / Settings.java @ 72b0db4c

History | View | Annotate | Download (5.61 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.navigateUpTo(this, new Intent(this, NoteCipher.class));
73
                        return true;
74
                }
75
                return super.onOptionsItemSelected(item);
76
        }
77

    
78
        @Override
79
        public void onDestroy() {
80
                super.onDestroy();
81
        }
82
        
83
        private Preference.OnPreferenceClickListener changeLockTimeoutListener = 
84
                        new Preference.OnPreferenceClickListener() {
85
                                        @Override
86
                                        public boolean onPreferenceClick(Preference pref) {
87
                                                changeTimeoutPrompt();
88
                                                return true;
89
                                        }
90
        };
91
        
92
        private Preference.OnPreferenceChangeListener vibrateChangeListener = 
93
                        new OnPreferenceChangeListener(){
94
                @Override
95
                public boolean onPreferenceChange(Preference pref, Object newValue) {
96
                        // save option internally in cacheword as well
97
                        mCacheWord.setVibrateSetting((Boolean) newValue);
98
                        return true;
99
                }
100
        };
101
        
102
        public static final boolean getNoteLinesOption(Context context) {
103
                boolean defValue = context.getResources().getBoolean(R.bool.notecipher_uselines_default);
104
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(NConstants.SHARED_PREFS_NOTELINES, defValue);
105
        }
106
        
107
        private void changeTimeoutPrompt() {
108
                if (mCacheWord.isLocked())
109
            return;
110
                
111
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
112
        builder.setTitle(R.string.change_timeout_prompt_title);
113
        builder.setMessage(R.string.change_timeout_prompt);
114
        final NumberPicker input = new NumberPicker(this);
115
        input.setMinValue(1);
116
        input.setMaxValue(60);
117
        input.setValue( mCacheWord.getTimeoutMinutes() );
118
        builder.setView(input);
119

    
120
        builder.setPositiveButton("OK",
121
                new DialogInterface.OnClickListener() {
122
                    @Override
123
                    public void onClick(DialogInterface dialog, int which) {
124
                        int timeout = input.getValue();
125
                        mCacheWord.setTimeoutMinutes(timeout);
126
                        dialog.dismiss();
127
                    }
128
                });
129
        builder.setNegativeButton("Cancel",
130
                new DialogInterface.OnClickListener() {
131
                    @Override
132
                    public void onClick(DialogInterface dialog, int which) {
133
                        dialog.cancel();
134
                    }
135
                });
136

    
137
        builder.show();
138
    }
139

    
140
        @Override
141
        public void onCacheWordUninitialized() {
142
                Log.d(NConstants.TAG, "onCacheWordUninitialized");
143
                System.gc();
144
                showLockScreen();
145
        }
146

    
147
        @Override
148
        public void onCacheWordLocked() {
149
                Log.d(NConstants.TAG, "onCacheWordLocked");
150
                System.gc();
151
                showLockScreen();
152
        }
153

    
154
        @Override
155
        public void onCacheWordOpened() { 
156
                Log.d(NConstants.TAG, "onCacheWordOpened");
157
        }
158
        
159
        @Override
160
    protected void onPause() {
161
        super.onPause();
162
        mCacheWord.onPause();
163
    }
164

    
165
    @Override
166
    protected void onResume() {
167
        super.onResume();
168
        mCacheWord.onResume();
169
    }
170
    
171
    void showLockScreen() {
172
        Intent intent = new Intent(this, LockScreenActivity.class);
173
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
174
        intent.putExtra("originalIntent", getIntent());
175
        startActivity(intent);
176
        finish();
177
    }
178
}