2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.ui
.activity
;
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
;
66 import com
.owncloud
.android
.BuildConfig
;
67 import com
.owncloud
.android
.MainApp
;
68 import com
.owncloud
.android
.R
;
69 import com
.owncloud
.android
.authentication
.AccountUtils
;
70 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
71 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
72 import com
.owncloud
.android
.datamodel
.OCFile
;
73 import com
.owncloud
.android
.db
.DbHandler
;
74 import com
.owncloud
.android
.files
.FileOperationsHelper
;
75 import com
.owncloud
.android
.files
.services
.FileDownloader
;
76 import com
.owncloud
.android
.files
.services
.FileUploader
;
77 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
78 import com
.owncloud
.android
.services
.OperationsService
;
79 import com
.owncloud
.android
.ui
.RadioButtonPreference
;
80 import com
.owncloud
.android
.utils
.DisplayUtils
;
84 * An Activity that allows the user to change the application's settings.
86 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
89 public class Preferences
extends PreferenceActivity
90 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
92 private static final String TAG
= "OwnCloudPreferences";
94 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
95 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
97 private DbHandler mDbHandler
;
98 private CheckBoxPreference pCode
;
99 private Preference pAboutApp
;
100 private AppCompatDelegate mDelegate
;
102 private PreferenceCategory mAccountsPrefCategory
= null
;
103 private final Handler mHandler
= new Handler();
104 private String mAccountName
;
105 private boolean mShowContextMenu
= false
;
106 private String mUploadPath
;
107 private PreferenceCategory mPrefInstantUploadCategory
;
108 private Preference mPrefInstantUpload
;
109 private Preference mPrefInstantUploadPath
;
110 private Preference mPrefInstantUploadPathWiFi
;
111 private Preference mPrefInstantVideoUpload
;
112 private Preference mPrefInstantVideoUploadPath
;
113 private Preference mPrefInstantVideoUploadPathWiFi
;
114 private String mUploadVideoPath
;
116 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
117 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
118 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
120 @SuppressWarnings("deprecation")
122 public void onCreate(Bundle savedInstanceState
) {
123 getDelegate().installViewFactory();
124 getDelegate().onCreate(savedInstanceState
);
125 super.onCreate(savedInstanceState
);
126 mDbHandler
= new DbHandler(getBaseContext());
127 addPreferencesFromResource(R
.xml
.preferences
);
129 ActionBar actionBar
= getSupportActionBar();
130 actionBar
.setDisplayHomeAsUpEnabled(true
);
131 actionBar
.setTitle(R
.string
.actionbar_settings
);
133 // For adding content description tag to a title field in the action bar
134 int actionBarTitleId
= getResources().getIdentifier("action_bar_title", "id", "android");
135 View actionBarTitleView
= getWindow().getDecorView().findViewById(actionBarTitleId
);
136 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
137 getWindow().getDecorView().findViewById(actionBarTitleId
).
138 setContentDescription(getString(R
.string
.actionbar_settings
));
141 // Load the accounts category for adding the list of accounts
142 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
144 ListView listView
= getListView();
145 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
147 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
148 ListView listView
= (ListView
) parent
;
149 ListAdapter listAdapter
= listView
.getAdapter();
150 Object obj
= listAdapter
.getItem(position
);
152 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
153 mShowContextMenu
= true
;
154 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
157 getResources().getString(R
.string
.change_password
),
158 getResources().getString(R
.string
.delete_account
)
160 final AlertDialog
.Builder alertDialogBuilder
= new AlertDialog
.Builder(Preferences
.this);
161 View convertView
= getLayoutInflater().inflate(R
.layout
.alert_dialog_list_view
, null
);
162 alertDialogBuilder
.setView(convertView
);
163 ListView lv
= (ListView
) convertView
.findViewById(R
.id
.list
);
164 ArrayAdapter
<String
> adapter
= new ArrayAdapter
<String
>(
165 Preferences
.this,R
.layout
.simple_dialog_list_item
,items
);
166 lv
.setAdapter(adapter
);
168 //Setup proper inline listener
169 final AlertDialog alertDialog
= alertDialogBuilder
.create();
170 lv
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
172 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
173 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
174 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
175 for (Account a
: accounts
) {
176 if (a
.name
.equals(mAccountName
)) {
179 // Change account password
180 Intent updateAccountCredentials
= new Intent(Preferences
.this, AuthenticatorActivity
.class);
181 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
182 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
183 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
184 startActivity(updateAccountCredentials
);
185 alertDialog
.cancel();
187 } else if (position
==1) {
190 am
.removeAccount(a
, Preferences
.this, mHandler
);
191 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
192 alertDialog
.cancel();
200 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
201 return longListener
.onLongClick(view
);
210 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
211 temp
= pkg
.versionName
;
212 } catch (NameNotFoundException e
) {
214 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
216 final String appVersion
= temp
;
218 // Register context menu for list of preferences.
219 registerForContextMenu(getListView());
221 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
223 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
225 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
226 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
227 Boolean enable
= (Boolean
) newValue
;
229 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
230 PassCodeActivity
.ACTION_DISABLE
240 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
242 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
243 Preference pHelp
= findPreference("help");
246 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
248 public boolean onPreferenceClick(Preference preference
) {
249 String helpWeb
=(String
) getText(R
.string
.url_help
);
250 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
251 Uri uriUrl
= Uri
.parse(helpWeb
);
252 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
253 startActivity(intent
);
259 preferenceCategory
.removePreference(pHelp
);
264 if (BuildConfig
.DEBUG
) {
265 Preference pLog
= findPreference("log");
267 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
269 public boolean onPreferenceClick(Preference preference
) {
270 Intent loggerIntent
= new Intent(getApplicationContext(),
271 LogHistoryActivity
.class);
272 startActivity(loggerIntent
);
279 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
280 Preference pRecommend
= findPreference("recommend");
281 if (pRecommend
!= null
){
282 if (recommendEnabled
) {
283 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
285 public boolean onPreferenceClick(Preference preference
) {
287 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
288 intent
.setType("text/plain");
289 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
290 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
292 String appName
= getString(R
.string
.app_name
);
293 String downloadUrl
= getString(R
.string
.url_app_download
);
294 Account currentAccount
= AccountUtils
.
295 getCurrentOwnCloudAccount(Preferences
.this);
296 String username
= currentAccount
.name
.substring(0,
297 currentAccount
.name
.lastIndexOf('@'));
299 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
301 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
302 appName
, downloadUrl
);
304 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
305 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
306 startActivity(intent
);
313 preferenceCategory
.removePreference(pRecommend
);
318 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
319 Preference pFeedback
= findPreference("feedback");
320 if (pFeedback
!= null
){
321 if (feedbackEnabled
) {
322 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
324 public boolean onPreferenceClick(Preference preference
) {
325 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
326 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
327 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
328 intent
.setType("text/plain");
329 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
331 intent
.setData(Uri
.parse(feedbackMail
));
332 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
333 startActivity(intent
);
339 preferenceCategory
.removePreference(pFeedback
);
344 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
345 Preference pImprint
= findPreference("imprint");
346 if (pImprint
!= null
) {
347 if (imprintEnabled
) {
348 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
350 public boolean onPreferenceClick(Preference preference
) {
351 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
352 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
353 Uri uriUrl
= Uri
.parse(imprintWeb
);
354 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
355 startActivity(intent
);
357 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
362 preferenceCategory
.removePreference(pImprint
);
366 mPrefInstantUploadPath
= findPreference("instant_upload_path");
367 if (mPrefInstantUploadPath
!= null
){
369 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
371 public boolean onPreferenceClick(Preference preference
) {
372 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
373 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
375 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
376 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
377 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
383 mPrefInstantUploadCategory
=
384 (PreferenceCategory
) findPreference("instant_uploading_category");
386 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
387 mPrefInstantUpload
= findPreference("instant_uploading");
389 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
391 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
394 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
395 toggleInstantPictureOptions((Boolean
) newValue
);
400 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
401 if (mPrefInstantVideoUploadPath
!= null
){
403 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
405 public boolean onPreferenceClick(Preference preference
) {
406 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
407 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
409 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
410 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
412 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
418 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
419 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
420 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
422 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
425 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
426 toggleInstantVideoOptions((Boolean
) newValue
);
432 pAboutApp
= (Preference
) findPreference("about_app");
433 if (pAboutApp
!= null
) {
434 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
435 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
438 loadInstantUploadPath();
439 loadInstantUploadVideoPath();
441 /* ComponentsGetter */
442 mDownloadServiceConnection
= newTransferenceServiceConnection();
443 if (mDownloadServiceConnection
!= null
) {
444 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
445 Context
.BIND_AUTO_CREATE
);
447 mUploadServiceConnection
= newTransferenceServiceConnection();
448 if (mUploadServiceConnection
!= null
) {
449 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
450 Context
.BIND_AUTO_CREATE
);
455 private void toggleInstantPictureOptions(Boolean value
){
457 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
458 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
460 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
461 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
465 private void toggleInstantVideoOptions(Boolean value
){
467 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
468 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
470 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
471 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
476 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
478 // Filter for only showing contextual menu when long press on the
480 if (mShowContextMenu
) {
481 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
482 mShowContextMenu
= false
;
484 super.onCreateContextMenu(menu
, v
, menuInfo
);
488 public void run(AccountManagerFuture
<Boolean
> future
) {
489 if (future
.isDone()) {
490 // after remove account
491 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
492 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
494 if (mUploaderBinder
!= null
) {
495 mUploaderBinder
.cancel(account
);
497 if (mDownloaderBinder
!= null
) {
498 mDownloaderBinder
.cancel(account
);
502 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
503 String accountName
= "";
505 Account
[] accounts
= AccountManager
.get(this)
506 .getAccountsByType(MainApp
.getAccountType());
507 if (accounts
.length
!= 0)
508 accountName
= accounts
[0].name
;
509 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
511 addAccountsCheckboxPreferences();
516 protected void onResume() {
518 SharedPreferences appPrefs
=
519 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
520 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
521 pCode
.setChecked(state
);
523 // Populate the accounts category with the list of accounts
524 addAccountsCheckboxPreferences();
528 public boolean onCreateOptionsMenu(Menu menu
) {
529 super.onCreateOptionsMenu(menu
);
534 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
535 super.onMenuItemSelected(featureId
, item
);
538 switch (item
.getItemId()) {
539 case android
.R
.id
.home
:
540 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
541 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
542 startActivity(intent
);
545 Log_OC
.w(TAG
, "Unknown menu item triggered");
552 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
553 super.onActivityResult(requestCode
, resultCode
, data
);
555 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
557 OCFile folderToUpload
=
558 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
560 mUploadPath
= folderToUpload
.getRemotePath();
562 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
564 // Show the path on summary preference
565 mPrefInstantUploadPath
.setSummary(mUploadPath
);
567 saveInstantUploadPathOnPreferences();
569 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
571 OCFile folderToUploadVideo
=
572 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
574 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
576 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
578 // Show the video path on summary preference
579 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
581 saveInstantUploadVideoPathOnPreferences();
585 public ActionBar
getSupportActionBar() {
586 return getDelegate().getSupportActionBar();
589 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
590 getDelegate().setSupportActionBar(toolbar
);
594 public MenuInflater
getMenuInflater() {
595 return getDelegate().getMenuInflater();
599 public void setContentView(@LayoutRes int layoutResID
) {
600 getDelegate().setContentView(layoutResID
);
603 public void setContentView(View view
) {
604 getDelegate().setContentView(view
);
607 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
608 getDelegate().setContentView(view
, params
);
612 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
613 getDelegate().addContentView(view
, params
);
617 protected void onPostResume() {
618 super.onPostResume();
619 getDelegate().onPostResume();
623 protected void onTitleChanged(CharSequence title
, int color
) {
624 super.onTitleChanged(title
, color
);
625 getDelegate().setTitle(title
);
629 public void onConfigurationChanged(Configuration newConfig
) {
630 super.onConfigurationChanged(newConfig
);
631 getDelegate().onConfigurationChanged(newConfig
);
635 protected void onPostCreate(Bundle savedInstanceState
) {
636 super.onPostCreate(savedInstanceState
);
637 getDelegate().onPostCreate(savedInstanceState
);
641 protected void onDestroy() {
644 if (mDownloadServiceConnection
!= null
) {
645 unbindService(mDownloadServiceConnection
);
646 mDownloadServiceConnection
= null
;
648 if (mUploadServiceConnection
!= null
) {
649 unbindService(mUploadServiceConnection
);
650 mUploadServiceConnection
= null
;
654 getDelegate().onDestroy();
658 protected void onStop() {
660 getDelegate().onStop();
663 public void invalidateOptionsMenu() {
664 getDelegate().invalidateOptionsMenu();
667 private AppCompatDelegate
getDelegate() {
668 if (mDelegate
== null
) {
669 mDelegate
= AppCompatDelegate
.create(this, null
);
675 * Create the list of accounts that has been added into the app
677 @SuppressWarnings("deprecation")
678 private void addAccountsCheckboxPreferences() {
680 // Remove accounts in case list is refreshing for avoiding to have
682 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
683 mAccountsPrefCategory
.removeAll();
686 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
687 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
688 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
690 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
691 // Show create account screen if there isn't any account
692 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
698 for (Account a
: accounts
) {
699 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
700 accountPreference
.setKey(a
.name
);
701 // Handle internationalized domain names
702 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
703 mAccountsPrefCategory
.addPreference(accountPreference
);
705 // Check the current account that is being used
706 if (a
.name
.equals(currentAccount
.name
)) {
707 accountPreference
.setChecked(true
);
709 accountPreference
.setChecked(false
);
712 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
714 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
715 String key
= preference
.getKey();
716 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
717 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
718 for (Account a
: accounts
) {
719 RadioButtonPreference p
=
720 (RadioButtonPreference
) findPreference(a
.name
);
721 if (key
.equals(a
.name
)) {
722 boolean accountChanged
= !p
.isChecked();
724 AccountUtils
.setCurrentOwnCloudAccount(
725 getApplicationContext(),
728 if (accountChanged
) {
729 // restart the main activity
730 Intent i
= new Intent(
732 FileDisplayActivity
.class
734 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
735 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
744 return (Boolean
) newValue
;
750 // Add Create Account preference at the end of account list if
751 // Multiaccount is enabled
752 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
753 createAddAccountPreference();
760 * Create the preference for allow adding new accounts
762 private void createAddAccountPreference() {
763 Preference addAccountPref
= new Preference(this);
764 addAccountPref
.setKey("add_account");
765 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
766 mAccountsPrefCategory
.addPreference(addAccountPref
);
768 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
770 public boolean onPreferenceClick(Preference preference
) {
771 AccountManager am
= AccountManager
.get(getApplicationContext());
772 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
781 * Load upload path set on preferences
783 private void loadInstantUploadPath() {
784 SharedPreferences appPrefs
=
785 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
786 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
787 mPrefInstantUploadPath
.setSummary(mUploadPath
);
791 * Save the "Instant Upload Path" on preferences
793 private void saveInstantUploadPathOnPreferences() {
794 SharedPreferences appPrefs
=
795 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
796 SharedPreferences
.Editor editor
= appPrefs
.edit();
797 editor
.putString("instant_upload_path", mUploadPath
);
802 * Load upload video path set on preferences
804 private void loadInstantUploadVideoPath() {
805 SharedPreferences appPrefs
=
806 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
807 mUploadVideoPath
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
));
808 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
812 * Save the "Instant Video Upload Path" on preferences
814 private void saveInstantUploadVideoPathOnPreferences() {
815 SharedPreferences appPrefs
=
816 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
817 SharedPreferences
.Editor editor
= appPrefs
.edit();
818 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
822 // Methods for ComponetsGetter
824 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
825 return mDownloaderBinder
;
830 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
831 return mUploaderBinder
;
835 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
840 public FileDataStorageManager
getStorageManager() {
845 public FileOperationsHelper
getFileOperationsHelper() {
849 protected ServiceConnection
newTransferenceServiceConnection() {
850 return new PreferencesServiceConnection();
853 /** Defines callbacks for service binding, passed to bindService() */
854 private class PreferencesServiceConnection
implements ServiceConnection
{
857 public void onServiceConnected(ComponentName component
, IBinder service
) {
859 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
860 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
862 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
863 Log_OC
.d(TAG
, "Upload service connected");
864 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
872 public void onServiceDisconnected(ComponentName component
) {
873 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
874 Log_OC
.d(TAG
, "Download service suddenly disconnected");
875 mDownloaderBinder
= null
;
876 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
877 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
878 mUploaderBinder
= null
;