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