- moved timeout to PINCODE_TIMEOUT
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
20 import android.accounts.Account;
21 import android.accounts.AccountManager;
22 import android.accounts.AccountManagerCallback;
23 import android.accounts.AccountManagerFuture;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.pm.PackageInfo;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.preference.CheckBoxPreference;
32 import android.preference.Preference;
33 import android.preference.Preference.OnPreferenceChangeListener;
34 import android.preference.Preference.OnPreferenceClickListener;
35 import android.preference.PreferenceCategory;
36 import android.preference.PreferenceManager;
37 import android.view.ContextMenu;
38 import android.view.ContextMenu.ContextMenuInfo;
39 import android.view.View;
40 import android.widget.AdapterView;
41 import android.widget.AdapterView.OnItemLongClickListener;
42 import android.widget.ListAdapter;
43 import android.widget.ListView;
44
45 import com.actionbarsherlock.app.ActionBar;
46 import com.actionbarsherlock.app.SherlockPreferenceActivity;
47 import com.actionbarsherlock.view.Menu;
48 import com.actionbarsherlock.view.MenuItem;
49 import com.owncloud.android.MainApp;
50 import com.owncloud.android.R;
51 import com.owncloud.android.authentication.AccountUtils;
52 import com.owncloud.android.authentication.AuthenticatorActivity;
53 import com.owncloud.android.authentication.PinCheck;
54 import com.owncloud.android.datamodel.OCFile;
55 import com.owncloud.android.db.DbHandler;
56 import com.owncloud.android.lib.common.utils.Log_OC;
57 import com.owncloud.android.ui.RadioButtonPreference;
58 import com.owncloud.android.utils.DisplayUtils;
59
60
61 /**
62 * An Activity that allows the user to change the application's settings.
63 *
64 * @author Bartek Przybylski
65 * @author David A. Velasco
66 */
67 public class Preferences extends SherlockPreferenceActivity implements AccountManagerCallback<Boolean> {
68
69 private static final String TAG = "OwnCloudPreferences";
70
71 private static final int ACTION_SELECT_UPLOAD_PATH = 1;
72 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
73
74 private DbHandler mDbHandler;
75 private CheckBoxPreference pCode;
76 private Preference pAboutApp;
77
78 private PreferenceCategory mAccountsPrefCategory = null;
79 private final Handler mHandler = new Handler();
80 private String mAccountName;
81 private boolean mShowContextMenu = false;
82 private String mUploadPath;
83 private Preference mPrefInstantUploadPath;
84 private Preference mPrefInstantVideoUploadPath;
85 private String mUploadVideoPath;
86
87
88 @SuppressWarnings("deprecation")
89 @Override
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 mDbHandler = new DbHandler(getBaseContext());
93 addPreferencesFromResource(R.xml.preferences);
94
95 ActionBar actionBar = getSherlock().getActionBar();
96 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
97 actionBar.setDisplayHomeAsUpEnabled(true);
98 actionBar.setTitle(R.string.actionbar_settings);
99
100 // Load the accounts category for adding the list of accounts
101 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
102
103 ListView listView = getListView();
104 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
105 @Override
106 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
107 ListView listView = (ListView) parent;
108 ListAdapter listAdapter = listView.getAdapter();
109 Object obj = listAdapter.getItem(position);
110
111 if (obj != null && obj instanceof RadioButtonPreference) {
112 mShowContextMenu = true;
113 mAccountName = ((RadioButtonPreference) obj).getKey();
114
115 Preferences.this.openContextMenu(listView);
116
117 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
118 return longListener.onLongClick(view);
119 }
120 return false;
121 }
122 });
123
124 // Load package info
125 String temp;
126 try {
127 PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
128 temp = pkg.versionName;
129 } catch (NameNotFoundException e) {
130 temp = "";
131 Log_OC.e(TAG, "Error while showing about dialog", e);
132 }
133 final String appVersion = temp;
134
135 // Register context menu for list of preferences.
136 registerForContextMenu(getListView());
137
138 pCode = (CheckBoxPreference) findPreference("set_pincode");
139 if (pCode != null){
140 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
141 @Override
142 public boolean onPreferenceChange(Preference preference, Object newValue) {
143 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
144 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
145 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
146 startActivity(i);
147
148 return true;
149 }
150 });
151
152 }
153
154 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
155
156 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
157 Preference pHelp = findPreference("help");
158 if (pHelp != null ){
159 if (helpEnabled) {
160 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
161 @Override
162 public boolean onPreferenceClick(Preference preference) {
163 String helpWeb =(String) getText(R.string.url_help);
164 if (helpWeb != null && helpWeb.length() > 0) {
165 Uri uriUrl = Uri.parse(helpWeb);
166 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
167 startActivity(intent);
168 }
169 return true;
170 }
171 });
172 } else {
173 preferenceCategory.removePreference(pHelp);
174 }
175
176 }
177
178
179 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
180 Preference pRecommend = findPreference("recommend");
181 if (pRecommend != null){
182 if (recommendEnabled) {
183 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
184 @Override
185 public boolean onPreferenceClick(Preference preference) {
186
187 Intent intent = new Intent(Intent.ACTION_SENDTO);
188 intent.setType("text/plain");
189 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
190 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
191
192 String appName = getString(R.string.app_name);
193 String downloadUrl = getString(R.string.url_app_download);
194 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
195 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
196
197 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
198 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
199
200 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
201 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
202 startActivity(intent);
203
204
205 return(true);
206
207 }
208 });
209 } else {
210 preferenceCategory.removePreference(pRecommend);
211 }
212
213 }
214
215 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
216 Preference pFeedback = findPreference("feedback");
217 if (pFeedback != null){
218 if (feedbackEnabled) {
219 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
220 @Override
221 public boolean onPreferenceClick(Preference preference) {
222 String feedbackMail =(String) getText(R.string.mail_feedback);
223 String feedback =(String) getText(R.string.prefs_feedback) + " - android v" + appVersion;
224 Intent intent = new Intent(Intent.ACTION_SENDTO);
225 intent.setType("text/plain");
226 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
227
228 intent.setData(Uri.parse(feedbackMail));
229 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
230 startActivity(intent);
231
232 return true;
233 }
234 });
235 } else {
236 preferenceCategory.removePreference(pFeedback);
237 }
238
239 }
240
241 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
242 Preference pImprint = findPreference("imprint");
243 if (pImprint != null) {
244 if (imprintEnabled) {
245 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
246 @Override
247 public boolean onPreferenceClick(Preference preference) {
248 String imprintWeb = (String) getText(R.string.url_imprint);
249 if (imprintWeb != null && imprintWeb.length() > 0) {
250 Uri uriUrl = Uri.parse(imprintWeb);
251 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
252 startActivity(intent);
253 }
254 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
255 return true;
256 }
257 });
258 } else {
259 preferenceCategory.removePreference(pImprint);
260 }
261 }
262
263 mPrefInstantUploadPath = findPreference("instant_upload_path");
264 if (mPrefInstantUploadPath != null){
265
266 mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
267 @Override
268 public boolean onPreferenceClick(Preference preference) {
269 if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
270 mUploadPath += OCFile.PATH_SEPARATOR;
271 }
272 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
273 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
274 startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
275 return true;
276 }
277 });
278 }
279
280 mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
281 if (mPrefInstantVideoUploadPath != null){
282
283 mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
284 @Override
285 public boolean onPreferenceClick(Preference preference) {
286 if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
287 mUploadVideoPath += OCFile.PATH_SEPARATOR;
288 }
289 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
290 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadVideoPath);
291 startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
292 return true;
293 }
294 });
295 }
296
297 /* About App */
298 pAboutApp = (Preference) findPreference("about_app");
299 if (pAboutApp != null) {
300 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
301 pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
302 }
303
304 loadInstantUploadPath();
305 loadInstantUploadVideoPath();
306
307 }
308
309 @Override
310 protected void onPause() {
311 super.onPause();
312
313 PinCheck.setUnlockTimestamp();
314 }
315
316 @Override
317 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
318
319 // Filter for only showing contextual menu when long press on the
320 // accounts
321 if (mShowContextMenu) {
322 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
323 mShowContextMenu = false;
324 }
325 super.onCreateContextMenu(menu, v, menuInfo);
326 }
327
328 /**
329 * Called when the user clicked on an item into the context menu created at
330 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
331 * every ownCloud {@link Account} , containing 'secondary actions' for them.
332 *
333 * {@inheritDoc}
334 */
335 @Override
336 public boolean onContextItemSelected(android.view.MenuItem item) {
337 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
338 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
339 for (Account a : accounts) {
340 if (a.name.equals(mAccountName)) {
341 if (item.getItemId() == R.id.change_password) {
342
343 // Change account password
344 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
345 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
346 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
347 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
348 startActivity(updateAccountCredentials);
349
350 } else if (item.getItemId() == R.id.delete_account) {
351
352 // Remove account
353 am.removeAccount(a, this, mHandler);
354 }
355 }
356 }
357
358 return true;
359 }
360
361 @Override
362 public void run(AccountManagerFuture<Boolean> future) {
363 if (future.isDone()) {
364 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
365 String accountName = "";
366 if (a == null) {
367 Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
368 if (accounts.length != 0)
369 accountName = accounts[0].name;
370 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
371 }
372 addAccountsCheckboxPreferences();
373 }
374 }
375
376 @Override
377 protected void onResume() {
378 super.onResume();
379 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
380 boolean state = appPrefs.getBoolean("set_pincode", false);
381 pCode.setChecked(state);
382
383 // Populate the accounts category with the list of accounts
384 addAccountsCheckboxPreferences();
385 }
386
387 @Override
388 public boolean onCreateOptionsMenu(Menu menu) {
389 super.onCreateOptionsMenu(menu);
390 return true;
391 }
392
393 @Override
394 public boolean onMenuItemSelected(int featureId, MenuItem item) {
395 super.onMenuItemSelected(featureId, item);
396 Intent intent;
397
398 switch (item.getItemId()) {
399 case android.R.id.home:
400 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
401 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
402 startActivity(intent);
403 break;
404 default:
405 Log_OC.w(TAG, "Unknown menu item triggered");
406 return false;
407 }
408 return true;
409 }
410
411 @Override
412 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
413 super.onActivityResult(requestCode, resultCode, data);
414
415 if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
416
417 OCFile folderToUpload = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
418
419 mUploadPath = folderToUpload.getRemotePath();
420
421 mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
422
423 // Show the path on summary preference
424 mPrefInstantUploadPath.setSummary(mUploadPath);
425
426 saveInstantUploadPathOnPreferences();
427
428 } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
429
430 OCFile folderToUploadVideo = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
431
432 mUploadVideoPath = folderToUploadVideo.getRemotePath();
433
434 mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
435
436 // Show the video path on summary preference
437 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
438
439 saveInstantUploadVideoPathOnPreferences();
440 }
441 }
442
443 @Override
444 protected void onDestroy() {
445 mDbHandler.close();
446 super.onDestroy();
447 }
448
449 /**
450 * Create the list of accounts that has been added into the app
451 */
452 @SuppressWarnings("deprecation")
453 private void addAccountsCheckboxPreferences() {
454
455 // Remove accounts in case list is refreshing for avoiding to have
456 // duplicate items
457 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
458 mAccountsPrefCategory.removeAll();
459 }
460
461 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
462 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
463 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
464
465 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
466 // Show create account screen if there isn't any account
467 am.addAccount(MainApp.getAccountType(), null, null, null, this,
468 null,
469 null);
470 }
471 else {
472
473 for (Account a : accounts) {
474 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
475 accountPreference.setKey(a.name);
476 // Handle internationalized domain names
477 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
478 mAccountsPrefCategory.addPreference(accountPreference);
479
480 // Check the current account that is being used
481 if (a.name.equals(currentAccount.name)) {
482 accountPreference.setChecked(true);
483 } else {
484 accountPreference.setChecked(false);
485 }
486
487 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
488 @Override
489 public boolean onPreferenceChange(Preference preference, Object newValue) {
490 String key = preference.getKey();
491 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
492 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
493 for (Account a : accounts) {
494 RadioButtonPreference p = (RadioButtonPreference) findPreference(a.name);
495 if (key.equals(a.name)) {
496 boolean accountChanged = !p.isChecked();
497 p.setChecked(true);
498 AccountUtils.setCurrentOwnCloudAccount(
499 getApplicationContext(),
500 a.name
501 );
502 if (accountChanged) {
503 // restart the main activity
504 Intent i = new Intent(
505 Preferences.this,
506 FileDisplayActivity.class
507 );
508 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
509 startActivity(i);
510 } else {
511 finish();
512 }
513 } else {
514 p.setChecked(false);
515 }
516 }
517 return (Boolean) newValue;
518 }
519 });
520
521 }
522
523 // Add Create Account preference at the end of account list if
524 // Multiaccount is enabled
525 if (getResources().getBoolean(R.bool.multiaccount_support)) {
526 createAddAccountPreference();
527 }
528
529 }
530 }
531
532 /**
533 * Create the preference for allow adding new accounts
534 */
535 private void createAddAccountPreference() {
536 Preference addAccountPref = new Preference(this);
537 addAccountPref.setKey("add_account");
538 addAccountPref.setTitle(getString(R.string.prefs_add_account));
539 mAccountsPrefCategory.addPreference(addAccountPref);
540
541 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
542 @Override
543 public boolean onPreferenceClick(Preference preference) {
544 AccountManager am = AccountManager.get(getApplicationContext());
545 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
546 return true;
547 }
548 });
549
550 }
551
552 /**
553 * Load upload path set on preferences
554 */
555 private void loadInstantUploadPath() {
556 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
557 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
558 mPrefInstantUploadPath.setSummary(mUploadPath);
559 }
560
561 /**
562 * Save the "Instant Upload Path" on preferences
563 */
564 private void saveInstantUploadPathOnPreferences() {
565 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
566 SharedPreferences.Editor editor = appPrefs.edit();
567 editor.putString("instant_upload_path", mUploadPath);
568 editor.commit();
569 }
570
571 /**
572 * Load upload video path set on preferences
573 */
574 private void loadInstantUploadVideoPath() {
575 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
576 mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
577 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
578 }
579
580 /**
581 * Save the "Instant Video Upload Path" on preferences
582 */
583 private void saveInstantUploadVideoPathOnPreferences() {
584 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
585 SharedPreferences.Editor editor = appPrefs.edit();
586 editor.putString("instant_video_upload_path", mUploadVideoPath);
587 editor.commit();
588 }
589 }