Don't allow to remove passcode without entering it. Don't allow to set incomplete...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 package com.owncloud.android.ui.activity;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.accounts.AccountManagerCallback;
27 import android.accounts.AccountManagerFuture;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.ServiceConnection;
32 import android.content.SharedPreferences;
33 import android.content.pm.PackageInfo;
34 import android.content.pm.PackageManager.NameNotFoundException;
35 import android.content.res.Configuration;
36 import android.net.Uri;
37 import android.os.Bundle;
38 import android.os.Handler;
39 import android.os.IBinder;
40 import android.preference.CheckBoxPreference;
41 import android.preference.Preference;
42 import android.preference.Preference.OnPreferenceChangeListener;
43 import android.preference.Preference.OnPreferenceClickListener;
44 import android.preference.PreferenceActivity;
45 import android.preference.PreferenceCategory;
46 import android.preference.PreferenceManager;
47 import android.support.annotation.LayoutRes;
48 import android.support.annotation.Nullable;
49 import android.support.v7.app.ActionBar;
50 import android.support.v7.app.AlertDialog;
51 import android.support.v7.app.AppCompatDelegate;
52 import android.support.v7.widget.Toolbar;
53 import android.view.ContextMenu;
54 import android.view.ContextMenu.ContextMenuInfo;
55 import android.view.Menu;
56 import android.view.MenuInflater;
57 import android.view.MenuItem;
58 import android.view.View;
59 import android.view.ViewGroup;
60 import android.widget.AdapterView;
61 import android.widget.AdapterView.OnItemLongClickListener;
62 import android.widget.ArrayAdapter;
63 import android.widget.ListAdapter;
64 import android.widget.ListView;
65 import android.widget.Toast;
66
67 import com.owncloud.android.BuildConfig;
68 import com.owncloud.android.MainApp;
69 import com.owncloud.android.R;
70 import com.owncloud.android.authentication.AccountUtils;
71 import com.owncloud.android.authentication.AuthenticatorActivity;
72 import com.owncloud.android.datamodel.FileDataStorageManager;
73 import com.owncloud.android.datamodel.OCFile;
74 import com.owncloud.android.db.DbHandler;
75 import com.owncloud.android.files.FileOperationsHelper;
76 import com.owncloud.android.files.services.FileDownloader;
77 import com.owncloud.android.files.services.FileUploader;
78 import com.owncloud.android.lib.common.utils.Log_OC;
79 import com.owncloud.android.services.OperationsService;
80 import com.owncloud.android.ui.RadioButtonPreference;
81 import com.owncloud.android.utils.DisplayUtils;
82
83
84 /**
85 * An Activity that allows the user to change the application's settings.
86 *
87 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
88 * with AppCompat.
89 */
90 public class Preferences extends PreferenceActivity
91 implements AccountManagerCallback<Boolean>, ComponentsGetter {
92
93 private static final String TAG = "OwnCloudPreferences";
94
95 private static final int ACTION_SELECT_UPLOAD_PATH = 1;
96 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
97 private static final int ACTION_REQUEST_PASSCODE = 5;
98 private static final int ACTION_CONFIRM_PASSCODE = 6;
99
100 private DbHandler mDbHandler;
101 private CheckBoxPreference pCode;
102 private Preference pAboutApp;
103 private AppCompatDelegate mDelegate;
104
105 private PreferenceCategory mAccountsPrefCategory = null;
106 private final Handler mHandler = new Handler();
107 private String mAccountName;
108 private boolean mShowContextMenu = false;
109 private String mUploadPath;
110 private PreferenceCategory mPrefInstantUploadCategory;
111 private Preference mPrefInstantUpload;
112 private Preference mPrefInstantUploadPath;
113 private Preference mPrefInstantUploadPathWiFi;
114 private Preference mPrefInstantVideoUpload;
115 private Preference mPrefInstantVideoUploadPath;
116 private Preference mPrefInstantVideoUploadPathWiFi;
117 private String mUploadVideoPath;
118
119 protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
120 protected FileUploader.FileUploaderBinder mUploaderBinder = null;
121 private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
122
123
124 @SuppressWarnings("deprecation")
125 @Override
126 public void onCreate(Bundle savedInstanceState) {
127 getDelegate().installViewFactory();
128 getDelegate().onCreate(savedInstanceState);
129 super.onCreate(savedInstanceState);
130 mDbHandler = new DbHandler(getBaseContext());
131 addPreferencesFromResource(R.xml.preferences);
132
133 ActionBar actionBar = getSupportActionBar();
134 actionBar.setDisplayHomeAsUpEnabled(true);
135 actionBar.setTitle(R.string.actionbar_settings);
136
137 // For adding content description tag to a title field in the action bar
138 int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
139 View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
140 if (actionBarTitleView != null) { // it's null in Android 2.x
141 getWindow().getDecorView().findViewById(actionBarTitleId).
142 setContentDescription(getString(R.string.actionbar_settings));
143 }
144
145 // Load the accounts category for adding the list of accounts
146 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
147
148 ListView listView = getListView();
149 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
150 @Override
151 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
152 ListView listView = (ListView) parent;
153 ListAdapter listAdapter = listView.getAdapter();
154 Object obj = listAdapter.getItem(position);
155
156 if (obj != null && obj instanceof RadioButtonPreference) {
157 mShowContextMenu = true;
158 mAccountName = ((RadioButtonPreference) obj).getKey();
159
160 String[] items = {
161 getResources().getString(R.string.change_password),
162 getResources().getString(R.string.delete_account)
163 };
164 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Preferences.this);
165 View convertView = getLayoutInflater().inflate(R.layout.alert_dialog_list_view, null);
166 alertDialogBuilder.setView(convertView);
167 ListView lv = (ListView) convertView.findViewById(R.id.list);
168 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
169 Preferences.this,R.layout.simple_dialog_list_item,items);
170 lv.setAdapter(adapter);
171
172 //Setup proper inline listener
173 final AlertDialog alertDialog = alertDialogBuilder.create();
174 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
175 @Override
176 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
177 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
178 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
179 for (Account a : accounts) {
180 if (a.name.equals(mAccountName)) {
181 if (position==0) {
182
183 // Change account password
184 Intent updateAccountCredentials = new Intent(Preferences.this, AuthenticatorActivity.class);
185 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
186 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
187 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
188 startActivity(updateAccountCredentials);
189 alertDialog.cancel();
190
191 } else if (position==1) {
192
193 // Remove account
194 am.removeAccount(a, Preferences.this, mHandler);
195 Log_OC.d(TAG, "Remove an account " + a.name);
196 alertDialog.cancel();
197 }
198 }
199 }
200 }
201 });
202 alertDialog.show();
203
204 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
205 return longListener.onLongClick(view);
206 }
207 return false;
208 }
209 });
210
211 // Load package info
212 String temp;
213 try {
214 PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
215 temp = pkg.versionName;
216 } catch (NameNotFoundException e) {
217 temp = "";
218 Log_OC.e(TAG, "Error while showing about dialog", e);
219 }
220 final String appVersion = temp;
221
222 // Register context menu for list of preferences.
223 registerForContextMenu(getListView());
224
225 pCode = (CheckBoxPreference) findPreference("set_pincode");
226 if (pCode != null) {
227 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
228 @Override
229 public boolean onPreferenceChange(Preference preference, Object newValue) {
230 Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
231 Boolean incoming = (Boolean) newValue;
232
233 i.setAction(
234 incoming.booleanValue() ? PassCodeActivity.ACTION_REQUEST_WITH_RESULT :
235 PassCodeActivity.ACTION_CHECK_WITH_RESULT
236 );
237
238 startActivityForResult(i, incoming.booleanValue() ? ACTION_REQUEST_PASSCODE :
239 ACTION_CONFIRM_PASSCODE);
240
241 // Don't update just yet, we will decide on it in onActivityResult
242 return false;
243 }
244 });
245
246 }
247
248
249
250 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
251
252 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
253 Preference pHelp = findPreference("help");
254 if (pHelp != null ){
255 if (helpEnabled) {
256 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
257 @Override
258 public boolean onPreferenceClick(Preference preference) {
259 String helpWeb =(String) getText(R.string.url_help);
260 if (helpWeb != null && helpWeb.length() > 0) {
261 Uri uriUrl = Uri.parse(helpWeb);
262 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
263 startActivity(intent);
264 }
265 return true;
266 }
267 });
268 } else {
269 preferenceCategory.removePreference(pHelp);
270 }
271
272 }
273
274 if (BuildConfig.DEBUG) {
275 Preference pLog = findPreference("log");
276 if (pLog != null ){
277 pLog.setOnPreferenceClickListener(new OnPreferenceClickListener() {
278 @Override
279 public boolean onPreferenceClick(Preference preference) {
280 Intent loggerIntent = new Intent(getApplicationContext(),
281 LogHistoryActivity.class);
282 startActivity(loggerIntent);
283 return true;
284 }
285 });
286 }
287 }
288
289 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
290 Preference pRecommend = findPreference("recommend");
291 if (pRecommend != null){
292 if (recommendEnabled) {
293 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
294 @Override
295 public boolean onPreferenceClick(Preference preference) {
296
297 Intent intent = new Intent(Intent.ACTION_SENDTO);
298 intent.setType("text/plain");
299 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
300 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
301
302 String appName = getString(R.string.app_name);
303 String downloadUrl = getString(R.string.url_app_download);
304 Account currentAccount = AccountUtils.
305 getCurrentOwnCloudAccount(Preferences.this);
306 String username = currentAccount.name.substring(0,
307 currentAccount.name.lastIndexOf('@'));
308
309 String recommendSubject = String.format(getString(R.string.recommend_subject),
310 appName);
311 String recommendText = String.format(getString(R.string.recommend_text),
312 appName, downloadUrl);
313
314 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
315 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
316 startActivity(intent);
317
318 return(true);
319
320 }
321 });
322 } else {
323 preferenceCategory.removePreference(pRecommend);
324 }
325
326 }
327
328 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
329 Preference pFeedback = findPreference("feedback");
330 if (pFeedback != null){
331 if (feedbackEnabled) {
332 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
333 @Override
334 public boolean onPreferenceClick(Preference preference) {
335 String feedbackMail =(String) getText(R.string.mail_feedback);
336 String feedback =(String) getText(R.string.prefs_feedback) + " - android v" + appVersion;
337 Intent intent = new Intent(Intent.ACTION_SENDTO);
338 intent.setType("text/plain");
339 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
340
341 intent.setData(Uri.parse(feedbackMail));
342 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
343 startActivity(intent);
344
345 return true;
346 }
347 });
348 } else {
349 preferenceCategory.removePreference(pFeedback);
350 }
351
352 }
353
354 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
355 Preference pImprint = findPreference("imprint");
356 if (pImprint != null) {
357 if (imprintEnabled) {
358 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
359 @Override
360 public boolean onPreferenceClick(Preference preference) {
361 String imprintWeb = (String) getText(R.string.url_imprint);
362 if (imprintWeb != null && imprintWeb.length() > 0) {
363 Uri uriUrl = Uri.parse(imprintWeb);
364 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
365 startActivity(intent);
366 }
367 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
368 return true;
369 }
370 });
371 } else {
372 preferenceCategory.removePreference(pImprint);
373 }
374 }
375
376 mPrefInstantUploadPath = findPreference("instant_upload_path");
377 if (mPrefInstantUploadPath != null){
378
379 mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
380 @Override
381 public boolean onPreferenceClick(Preference preference) {
382 if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
383 mUploadPath += OCFile.PATH_SEPARATOR;
384 }
385 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
386 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
387 startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
388 return true;
389 }
390 });
391 }
392
393 mPrefInstantUploadCategory =
394 (PreferenceCategory) findPreference("instant_uploading_category");
395
396 mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
397 mPrefInstantUpload = findPreference("instant_uploading");
398
399 toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
400
401 mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
402
403 @Override
404 public boolean onPreferenceChange(Preference preference, Object newValue) {
405 toggleInstantPictureOptions((Boolean) newValue);
406 return true;
407 }
408 });
409
410 mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
411 if (mPrefInstantVideoUploadPath != null){
412
413 mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
414 @Override
415 public boolean onPreferenceClick(Preference preference) {
416 if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
417 mUploadVideoPath += OCFile.PATH_SEPARATOR;
418 }
419 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
420 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH,
421 mUploadVideoPath);
422 startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
423 return true;
424 }
425 });
426 }
427
428 mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
429 mPrefInstantVideoUpload = findPreference("instant_video_uploading");
430 toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
431
432 mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
433
434 @Override
435 public boolean onPreferenceChange(Preference preference, Object newValue) {
436 toggleInstantVideoOptions((Boolean) newValue);
437 return true;
438 }
439 });
440
441 /* About App */
442 pAboutApp = (Preference) findPreference("about_app");
443 if (pAboutApp != null) {
444 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
445 pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
446 }
447
448 loadInstantUploadPath();
449 loadInstantUploadVideoPath();
450
451 /* ComponentsGetter */
452 mDownloadServiceConnection = newTransferenceServiceConnection();
453 if (mDownloadServiceConnection != null) {
454 bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
455 Context.BIND_AUTO_CREATE);
456 }
457 mUploadServiceConnection = newTransferenceServiceConnection();
458 if (mUploadServiceConnection != null) {
459 bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
460 Context.BIND_AUTO_CREATE);
461 }
462
463 }
464
465 private void toggleInstantPictureOptions(Boolean value){
466 if (value){
467 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
468 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
469 } else {
470 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
471 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
472 }
473 }
474
475 private void toggleInstantVideoOptions(Boolean value){
476 if (value){
477 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
478 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
479 } else {
480 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
481 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
482 }
483 }
484
485 @Override
486 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
487
488 // Filter for only showing contextual menu when long press on the
489 // accounts
490 if (mShowContextMenu) {
491 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
492 mShowContextMenu = false;
493 }
494 super.onCreateContextMenu(menu, v, menuInfo);
495 }
496
497 @Override
498 public void run(AccountManagerFuture<Boolean> future) {
499 if (future.isDone()) {
500 // after remove account
501 Account account = new Account(mAccountName, MainApp.getAccountType());
502 if (!AccountUtils.exists(account, MainApp.getAppContext())) {
503 // Cancel tranfers
504 if (mUploaderBinder != null) {
505 mUploaderBinder.cancel(account);
506 }
507 if (mDownloaderBinder != null) {
508 mDownloaderBinder.cancel(account);
509 }
510 }
511
512 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
513 String accountName = "";
514 if (a == null) {
515 Account[] accounts = AccountManager.get(this)
516 .getAccountsByType(MainApp.getAccountType());
517 if (accounts.length != 0)
518 accountName = accounts[0].name;
519 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
520 }
521 addAccountsCheckboxPreferences();
522 }
523 }
524
525 @Override
526 protected void onResume() {
527 super.onResume();
528 SharedPreferences appPrefs =
529 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
530 boolean state = appPrefs.getBoolean("set_pincode", false);
531 pCode.setChecked(state);
532
533 // Populate the accounts category with the list of accounts
534 addAccountsCheckboxPreferences();
535 }
536
537 @Override
538 public boolean onCreateOptionsMenu(Menu menu) {
539 super.onCreateOptionsMenu(menu);
540 return true;
541 }
542
543 @Override
544 public boolean onMenuItemSelected(int featureId, MenuItem item) {
545 super.onMenuItemSelected(featureId, item);
546 Intent intent;
547
548 switch (item.getItemId()) {
549 case android.R.id.home:
550 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
551 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
552 startActivity(intent);
553 break;
554 default:
555 Log_OC.w(TAG, "Unknown menu item triggered");
556 return false;
557 }
558 return true;
559 }
560
561 @Override
562 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
563 super.onActivityResult(requestCode, resultCode, data);
564
565 if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
566
567 OCFile folderToUpload =
568 (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
569
570 mUploadPath = folderToUpload.getRemotePath();
571
572 mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
573
574 // Show the path on summary preference
575 mPrefInstantUploadPath.setSummary(mUploadPath);
576
577 saveInstantUploadPathOnPreferences();
578
579 } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
580
581 OCFile folderToUploadVideo =
582 (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
583
584 mUploadVideoPath = folderToUploadVideo.getRemotePath();
585
586 mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
587
588 // Show the video path on summary preference
589 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
590
591 saveInstantUploadVideoPathOnPreferences();
592 } else if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
593 String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
594 if (passcode != null && passcode.length() == 4) {
595 SharedPreferences.Editor appPrefs = PreferenceManager
596 .getDefaultSharedPreferences(getApplicationContext()).edit();
597
598 for (int i = 1; i <= 4; ++i) {
599 appPrefs.putString("PrefPinCode" + i, passcode.substring(i-1, i));
600 }
601 appPrefs.putBoolean("set_pincode", true);
602 appPrefs.commit();
603 Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
604 }
605 } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
606 if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
607
608 SharedPreferences.Editor appPrefs = PreferenceManager
609 .getDefaultSharedPreferences(getApplicationContext()).edit();
610 appPrefs.putBoolean("set_pincode", false);
611 appPrefs.commit();
612
613 Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
614 }
615 }
616 }
617
618 public ActionBar getSupportActionBar() {
619 return getDelegate().getSupportActionBar();
620 }
621
622 public void setSupportActionBar(@Nullable Toolbar toolbar) {
623 getDelegate().setSupportActionBar(toolbar);
624 }
625
626 @Override
627 public MenuInflater getMenuInflater() {
628 return getDelegate().getMenuInflater();
629 }
630
631 @Override
632 public void setContentView(@LayoutRes int layoutResID) {
633 getDelegate().setContentView(layoutResID);
634 }
635 @Override
636 public void setContentView(View view) {
637 getDelegate().setContentView(view);
638 }
639 @Override
640 public void setContentView(View view, ViewGroup.LayoutParams params) {
641 getDelegate().setContentView(view, params);
642 }
643
644 @Override
645 public void addContentView(View view, ViewGroup.LayoutParams params) {
646 getDelegate().addContentView(view, params);
647 }
648
649 @Override
650 protected void onPostResume() {
651 super.onPostResume();
652 getDelegate().onPostResume();
653 }
654
655 @Override
656 protected void onTitleChanged(CharSequence title, int color) {
657 super.onTitleChanged(title, color);
658 getDelegate().setTitle(title);
659 }
660
661 @Override
662 public void onConfigurationChanged(Configuration newConfig) {
663 super.onConfigurationChanged(newConfig);
664 getDelegate().onConfigurationChanged(newConfig);
665 }
666
667 @Override
668 protected void onPostCreate(Bundle savedInstanceState) {
669 super.onPostCreate(savedInstanceState);
670 getDelegate().onPostCreate(savedInstanceState);
671 }
672
673 @Override
674 protected void onDestroy() {
675 mDbHandler.close();
676
677 if (mDownloadServiceConnection != null) {
678 unbindService(mDownloadServiceConnection);
679 mDownloadServiceConnection = null;
680 }
681 if (mUploadServiceConnection != null) {
682 unbindService(mUploadServiceConnection);
683 mUploadServiceConnection = null;
684 }
685
686 super.onDestroy();
687 getDelegate().onDestroy();
688 }
689
690 @Override
691 protected void onStop() {
692 super.onStop();
693 getDelegate().onStop();
694 }
695
696 public void invalidateOptionsMenu() {
697 getDelegate().invalidateOptionsMenu();
698 }
699
700 private AppCompatDelegate getDelegate() {
701 if (mDelegate == null) {
702 mDelegate = AppCompatDelegate.create(this, null);
703 }
704 return mDelegate;
705 }
706
707 /**
708 * Create the list of accounts that has been added into the app
709 */
710 @SuppressWarnings("deprecation")
711 private void addAccountsCheckboxPreferences() {
712
713 // Remove accounts in case list is refreshing for avoiding to have
714 // duplicate items
715 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
716 mAccountsPrefCategory.removeAll();
717 }
718
719 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
720 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
721 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
722
723 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
724 // Show create account screen if there isn't any account
725 am.addAccount(MainApp.getAccountType(), null, null, null, this,
726 null,
727 null);
728 }
729 else {
730
731 for (Account a : accounts) {
732 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
733 accountPreference.setKey(a.name);
734 // Handle internationalized domain names
735 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
736 mAccountsPrefCategory.addPreference(accountPreference);
737
738 // Check the current account that is being used
739 if (a.name.equals(currentAccount.name)) {
740 accountPreference.setChecked(true);
741 } else {
742 accountPreference.setChecked(false);
743 }
744
745 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
746 @Override
747 public boolean onPreferenceChange(Preference preference, Object newValue) {
748 String key = preference.getKey();
749 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
750 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
751 for (Account a : accounts) {
752 RadioButtonPreference p =
753 (RadioButtonPreference) findPreference(a.name);
754 if (key.equals(a.name)) {
755 boolean accountChanged = !p.isChecked();
756 p.setChecked(true);
757 AccountUtils.setCurrentOwnCloudAccount(
758 getApplicationContext(),
759 a.name
760 );
761 if (accountChanged) {
762 // restart the main activity
763 Intent i = new Intent(
764 Preferences.this,
765 FileDisplayActivity.class
766 );
767 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
768 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
769 startActivity(i);
770 } else {
771 finish();
772 }
773 } else {
774 p.setChecked(false);
775 }
776 }
777 return (Boolean) newValue;
778 }
779 });
780
781 }
782
783 // Add Create Account preference at the end of account list if
784 // Multiaccount is enabled
785 if (getResources().getBoolean(R.bool.multiaccount_support)) {
786 createAddAccountPreference();
787 }
788
789 }
790 }
791
792 /**
793 * Create the preference for allow adding new accounts
794 */
795 private void createAddAccountPreference() {
796 Preference addAccountPref = new Preference(this);
797 addAccountPref.setKey("add_account");
798 addAccountPref.setTitle(getString(R.string.prefs_add_account));
799 mAccountsPrefCategory.addPreference(addAccountPref);
800
801 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
802 @Override
803 public boolean onPreferenceClick(Preference preference) {
804 AccountManager am = AccountManager.get(getApplicationContext());
805 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
806 null, null);
807 return true;
808 }
809 });
810
811 }
812
813 /**
814 * Load upload path set on preferences
815 */
816 private void loadInstantUploadPath() {
817 SharedPreferences appPrefs =
818 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
819 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
820 mPrefInstantUploadPath.setSummary(mUploadPath);
821 }
822
823 /**
824 * Save the "Instant Upload Path" on preferences
825 */
826 private void saveInstantUploadPathOnPreferences() {
827 SharedPreferences appPrefs =
828 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
829 SharedPreferences.Editor editor = appPrefs.edit();
830 editor.putString("instant_upload_path", mUploadPath);
831 editor.commit();
832 }
833
834 /**
835 * Load upload video path set on preferences
836 */
837 private void loadInstantUploadVideoPath() {
838 SharedPreferences appPrefs =
839 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
840 mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
841 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
842 }
843
844 /**
845 * Save the "Instant Video Upload Path" on preferences
846 */
847 private void saveInstantUploadVideoPathOnPreferences() {
848 SharedPreferences appPrefs =
849 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
850 SharedPreferences.Editor editor = appPrefs.edit();
851 editor.putString("instant_video_upload_path", mUploadVideoPath);
852 editor.commit();
853 }
854
855 // Methods for ComponetsGetter
856 @Override
857 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
858 return mDownloaderBinder;
859 }
860
861
862 @Override
863 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
864 return mUploaderBinder;
865 }
866
867 @Override
868 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
869 return null;
870 }
871
872 @Override
873 public FileDataStorageManager getStorageManager() {
874 return null;
875 }
876
877 @Override
878 public FileOperationsHelper getFileOperationsHelper() {
879 return null;
880 }
881
882 protected ServiceConnection newTransferenceServiceConnection() {
883 return new PreferencesServiceConnection();
884 }
885
886 /** Defines callbacks for service binding, passed to bindService() */
887 private class PreferencesServiceConnection implements ServiceConnection {
888
889 @Override
890 public void onServiceConnected(ComponentName component, IBinder service) {
891
892 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
893 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
894
895 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
896 Log_OC.d(TAG, "Upload service connected");
897 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
898 } else {
899 return;
900 }
901
902 }
903
904 @Override
905 public void onServiceDisconnected(ComponentName component) {
906 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
907 Log_OC.d(TAG, "Download service suddenly disconnected");
908 mDownloaderBinder = null;
909 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
910 Log_OC.d(TAG, "Upload service suddenly disconnected");
911 mUploaderBinder = null;
912 }
913 }
914 };
915 }