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