update
[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 /* Link to Beta apks */
476 Preference pBetaLink = findPreference("beta_link");
477 if (pBetaLink != null ){
478 pBetaLink.setOnPreferenceClickListener(new OnPreferenceClickListener() {
479 @Override
480 public boolean onPreferenceClick(Preference preference) {
481 String betaLinkWeb = (String) getText(R.string.beta_link);
482 if (betaLinkWeb != null && betaLinkWeb.length() > 0) {
483 Uri uriUrl = Uri.parse(betaLinkWeb);
484 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
485 startActivity(intent);
486 }
487 return true;
488 }
489 });
490 }
491 }
492
493 private void toggleInstantPictureOptions(Boolean value){
494 if (value){
495 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
496 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
497 } else {
498 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
499 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
500 }
501 }
502
503 private void toggleInstantVideoOptions(Boolean value){
504 if (value){
505 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
506 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
507 } else {
508 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
509 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
510 }
511 }
512
513 @Override
514 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
515
516 // Filter for only showing contextual menu when long press on the
517 // accounts
518 if (mShowContextMenu) {
519 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
520 mShowContextMenu = false;
521 }
522 super.onCreateContextMenu(menu, v, menuInfo);
523 }
524
525 @Override
526 public void run(AccountManagerFuture<Boolean> future) {
527 if (future.isDone()) {
528 // after remove account
529 Account account = new Account(mAccountName, MainApp.getAccountType());
530 if (!AccountUtils.exists(account, MainApp.getAppContext())) {
531 // Cancel tranfers
532 if (mUploaderBinder != null) {
533 mUploaderBinder.cancel(account);
534 }
535 if (mDownloaderBinder != null) {
536 mDownloaderBinder.cancel(account);
537 }
538 }
539
540 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
541 String accountName = "";
542 if (a == null) {
543 Account[] accounts = AccountManager.get(this)
544 .getAccountsByType(MainApp.getAccountType());
545 if (accounts.length != 0)
546 accountName = accounts[0].name;
547 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
548 }
549 addAccountsCheckboxPreferences();
550 }
551 }
552
553 @Override
554 protected void onResume() {
555 super.onResume();
556 SharedPreferences appPrefs =
557 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
558 boolean state = appPrefs.getBoolean("set_pincode", false);
559 pCode.setChecked(state);
560
561 // Populate the accounts category with the list of accounts
562 addAccountsCheckboxPreferences();
563 }
564
565 @Override
566 public boolean onCreateOptionsMenu(Menu menu) {
567 super.onCreateOptionsMenu(menu);
568 return true;
569 }
570
571 @Override
572 public boolean onMenuItemSelected(int featureId, MenuItem item) {
573 super.onMenuItemSelected(featureId, item);
574 Intent intent;
575
576 switch (item.getItemId()) {
577 case android.R.id.home:
578 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
579 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
580 startActivity(intent);
581 break;
582 default:
583 Log_OC.w(TAG, "Unknown menu item triggered");
584 return false;
585 }
586 return true;
587 }
588
589 @Override
590 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
591 super.onActivityResult(requestCode, resultCode, data);
592
593 if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
594
595 OCFile folderToUpload =
596 (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
597
598 mUploadPath = folderToUpload.getRemotePath();
599
600 mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
601
602 // Show the path on summary preference
603 mPrefInstantUploadPath.setSummary(mUploadPath);
604
605 saveInstantUploadPathOnPreferences();
606
607 } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
608
609 OCFile folderToUploadVideo =
610 (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
611
612 mUploadVideoPath = folderToUploadVideo.getRemotePath();
613
614 mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
615
616 // Show the video path on summary preference
617 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
618
619 saveInstantUploadVideoPathOnPreferences();
620 }
621 }
622
623 public ActionBar getSupportActionBar() {
624 return getDelegate().getSupportActionBar();
625 }
626
627 public void setSupportActionBar(@Nullable Toolbar toolbar) {
628 getDelegate().setSupportActionBar(toolbar);
629 }
630
631 @Override
632 public MenuInflater getMenuInflater() {
633 return getDelegate().getMenuInflater();
634 }
635
636 @Override
637 public void setContentView(@LayoutRes int layoutResID) {
638 getDelegate().setContentView(layoutResID);
639 }
640 @Override
641 public void setContentView(View view) {
642 getDelegate().setContentView(view);
643 }
644 @Override
645 public void setContentView(View view, ViewGroup.LayoutParams params) {
646 getDelegate().setContentView(view, params);
647 }
648
649 @Override
650 public void addContentView(View view, ViewGroup.LayoutParams params) {
651 getDelegate().addContentView(view, params);
652 }
653
654 @Override
655 protected void onPostResume() {
656 super.onPostResume();
657 getDelegate().onPostResume();
658 }
659
660 @Override
661 protected void onTitleChanged(CharSequence title, int color) {
662 super.onTitleChanged(title, color);
663 getDelegate().setTitle(title);
664 }
665
666 @Override
667 public void onConfigurationChanged(Configuration newConfig) {
668 super.onConfigurationChanged(newConfig);
669 getDelegate().onConfigurationChanged(newConfig);
670 }
671
672 @Override
673 protected void onPostCreate(Bundle savedInstanceState) {
674 super.onPostCreate(savedInstanceState);
675 getDelegate().onPostCreate(savedInstanceState);
676 }
677
678 @Override
679 protected void onDestroy() {
680 mDbHandler.close();
681
682 if (mDownloadServiceConnection != null) {
683 unbindService(mDownloadServiceConnection);
684 mDownloadServiceConnection = null;
685 }
686 if (mUploadServiceConnection != null) {
687 unbindService(mUploadServiceConnection);
688 mUploadServiceConnection = null;
689 }
690
691 super.onDestroy();
692 getDelegate().onDestroy();
693 }
694
695 @Override
696 protected void onStop() {
697 super.onStop();
698 getDelegate().onStop();
699 }
700
701 public void invalidateOptionsMenu() {
702 getDelegate().invalidateOptionsMenu();
703 }
704
705 private AppCompatDelegate getDelegate() {
706 if (mDelegate == null) {
707 mDelegate = AppCompatDelegate.create(this, null);
708 }
709 return mDelegate;
710 }
711
712 /**
713 * Create the list of accounts that has been added into the app
714 */
715 @SuppressWarnings("deprecation")
716 private void addAccountsCheckboxPreferences() {
717
718 // Remove accounts in case list is refreshing for avoiding to have
719 // duplicate items
720 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
721 mAccountsPrefCategory.removeAll();
722 }
723
724 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
725 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
726 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
727
728 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
729 // Show create account screen if there isn't any account
730 am.addAccount(MainApp.getAccountType(), null, null, null, this,
731 null,
732 null);
733 }
734 else {
735
736 for (Account a : accounts) {
737 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
738 accountPreference.setKey(a.name);
739 // Handle internationalized domain names
740 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
741 mAccountsPrefCategory.addPreference(accountPreference);
742
743 // Check the current account that is being used
744 if (a.name.equals(currentAccount.name)) {
745 accountPreference.setChecked(true);
746 } else {
747 accountPreference.setChecked(false);
748 }
749
750 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
751 @Override
752 public boolean onPreferenceChange(Preference preference, Object newValue) {
753 String key = preference.getKey();
754 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
755 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
756 for (Account a : accounts) {
757 RadioButtonPreference p =
758 (RadioButtonPreference) findPreference(a.name);
759 if (key.equals(a.name)) {
760 boolean accountChanged = !p.isChecked();
761 p.setChecked(true);
762 AccountUtils.setCurrentOwnCloudAccount(
763 getApplicationContext(),
764 a.name
765 );
766 if (accountChanged) {
767 // restart the main activity
768 Intent i = new Intent(
769 Preferences.this,
770 FileDisplayActivity.class
771 );
772 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
773 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
774 startActivity(i);
775 } else {
776 finish();
777 }
778 } else {
779 p.setChecked(false);
780 }
781 }
782 return (Boolean) newValue;
783 }
784 });
785
786 }
787
788 // Add Create Account preference at the end of account list if
789 // Multiaccount is enabled
790 if (getResources().getBoolean(R.bool.multiaccount_support)) {
791 createAddAccountPreference();
792 }
793
794 }
795 }
796
797 /**
798 * Create the preference for allow adding new accounts
799 */
800 private void createAddAccountPreference() {
801 Preference addAccountPref = new Preference(this);
802 addAccountPref.setKey("add_account");
803 addAccountPref.setTitle(getString(R.string.prefs_add_account));
804 mAccountsPrefCategory.addPreference(addAccountPref);
805
806 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
807 @Override
808 public boolean onPreferenceClick(Preference preference) {
809 AccountManager am = AccountManager.get(getApplicationContext());
810 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
811 null, null);
812 return true;
813 }
814 });
815
816 }
817
818 /**
819 * Load upload path set on preferences
820 */
821 private void loadInstantUploadPath() {
822 SharedPreferences appPrefs =
823 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
824 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
825 mPrefInstantUploadPath.setSummary(mUploadPath);
826 }
827
828 /**
829 * Save the "Instant Upload Path" on preferences
830 */
831 private void saveInstantUploadPathOnPreferences() {
832 SharedPreferences appPrefs =
833 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
834 SharedPreferences.Editor editor = appPrefs.edit();
835 editor.putString("instant_upload_path", mUploadPath);
836 editor.commit();
837 }
838
839 /**
840 * Load upload video path set on preferences
841 */
842 private void loadInstantUploadVideoPath() {
843 SharedPreferences appPrefs =
844 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
845 mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
846 // mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
847 }
848
849 /**
850 * Save the "Instant Video Upload Path" on preferences
851 */
852 private void saveInstantUploadVideoPathOnPreferences() {
853 SharedPreferences appPrefs =
854 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
855 SharedPreferences.Editor editor = appPrefs.edit();
856 editor.putString("instant_video_upload_path", mUploadVideoPath);
857 editor.commit();
858 }
859
860 // Methods for ComponetsGetter
861 @Override
862 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
863 return mDownloaderBinder;
864 }
865
866
867 @Override
868 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
869 return mUploaderBinder;
870 }
871
872 @Override
873 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
874 return null;
875 }
876
877 @Override
878 public FileDataStorageManager getStorageManager() {
879 return null;
880 }
881
882 @Override
883 public FileOperationsHelper getFileOperationsHelper() {
884 return null;
885 }
886
887 protected ServiceConnection newTransferenceServiceConnection() {
888 return new PreferencesServiceConnection();
889 }
890
891 /** Defines callbacks for service binding, passed to bindService() */
892 private class PreferencesServiceConnection implements ServiceConnection {
893
894 @Override
895 public void onServiceConnected(ComponentName component, IBinder service) {
896
897 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
898 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
899
900 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
901 Log_OC.d(TAG, "Upload service connected");
902 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
903 } else {
904 return;
905 }
906
907 }
908
909 @Override
910 public void onServiceDisconnected(ComponentName component) {
911 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
912 Log_OC.d(TAG, "Download service suddenly disconnected");
913 mDownloaderBinder = null;
914 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
915 Log_OC.d(TAG, "Upload service suddenly disconnected");
916 mUploaderBinder = null;
917 }
918 }
919 };
920 }