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