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