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