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
.AppCompatDelegate
;
51 import android
.support
.v7
.widget
.Toolbar
;
52 import android
.view
.ContextMenu
;
53 import android
.view
.ContextMenu
.ContextMenuInfo
;
54 import android
.view
.Menu
;
55 import android
.view
.MenuInflater
;
56 import android
.view
.MenuItem
;
57 import android
.view
.View
;
58 import android
.view
.ViewGroup
;
59 import android
.widget
.AdapterView
;
60 import android
.widget
.AdapterView
.OnItemLongClickListener
;
61 import android
.widget
.ListAdapter
;
62 import android
.widget
.ListView
;
64 import com
.owncloud
.android
.BuildConfig
;
65 import com
.owncloud
.android
.MainApp
;
66 import com
.owncloud
.android
.R
;
67 import com
.owncloud
.android
.authentication
.AccountUtils
;
68 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
69 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
70 import com
.owncloud
.android
.datamodel
.OCFile
;
71 import com
.owncloud
.android
.db
.DbHandler
;
72 import com
.owncloud
.android
.files
.FileOperationsHelper
;
73 import com
.owncloud
.android
.files
.services
.FileDownloader
;
74 import com
.owncloud
.android
.files
.services
.FileUploader
;
75 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
76 import com
.owncloud
.android
.services
.OperationsService
;
77 import com
.owncloud
.android
.ui
.RadioButtonPreference
;
78 import com
.owncloud
.android
.utils
.DisplayUtils
;
82 * An Activity that allows the user to change the application's settings.
84 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
87 public class Preferences
extends PreferenceActivity
88 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
90 private static final String TAG
= "OwnCloudPreferences";
92 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
93 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
95 private DbHandler mDbHandler
;
96 private CheckBoxPreference pCode
;
97 private Preference pAboutApp
;
98 private AppCompatDelegate mDelegate
;
100 private PreferenceCategory mAccountsPrefCategory
= null
;
101 private final Handler mHandler
= new Handler();
102 private String mAccountName
;
103 private boolean mShowContextMenu
= false
;
104 private String mUploadPath
;
105 private PreferenceCategory mPrefInstantUploadCategory
;
106 private Preference mPrefInstantUpload
;
107 private Preference mPrefInstantUploadPath
;
108 private Preference mPrefInstantUploadPathWiFi
;
109 private Preference mPrefInstantVideoUpload
;
110 private Preference mPrefInstantVideoUploadPath
;
111 private Preference mPrefInstantVideoUploadPathWiFi
;
112 private String mUploadVideoPath
;
114 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
115 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
116 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
118 @SuppressWarnings("deprecation")
120 public void onCreate(Bundle savedInstanceState
) {
121 getDelegate().installViewFactory();
122 getDelegate().onCreate(savedInstanceState
);
123 super.onCreate(savedInstanceState
);
124 mDbHandler
= new DbHandler(getBaseContext());
125 addPreferencesFromResource(R
.xml
.preferences
);
127 ActionBar actionBar
= getSupportActionBar();
128 actionBar
.setDisplayHomeAsUpEnabled(true
);
129 actionBar
.setTitle(R
.string
.actionbar_settings
);
131 // For adding content description tag to a title field in the action bar
132 int actionBarTitleId
= getResources().getIdentifier("action_bar_title", "id", "android");
133 View actionBarTitleView
= getWindow().getDecorView().findViewById(actionBarTitleId
);
134 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
135 getWindow().getDecorView().findViewById(actionBarTitleId
).
136 setContentDescription(getString(R
.string
.actionbar_settings
));
139 // Load the accounts category for adding the list of accounts
140 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
142 ListView listView
= getListView();
143 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
145 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
146 ListView listView
= (ListView
) parent
;
147 ListAdapter listAdapter
= listView
.getAdapter();
148 Object obj
= listAdapter
.getItem(position
);
150 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
151 mShowContextMenu
= true
;
152 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
154 Preferences
.this.openContextMenu(listView
);
156 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
157 return longListener
.onLongClick(view
);
166 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
167 temp
= pkg
.versionName
;
168 } catch (NameNotFoundException e
) {
170 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
172 final String appVersion
= temp
;
174 // Register context menu for list of preferences.
175 registerForContextMenu(getListView());
177 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
179 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
181 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
182 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
183 Boolean enable
= (Boolean
) newValue
;
185 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
186 PassCodeActivity
.ACTION_DISABLE
196 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
198 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
199 Preference pHelp
= findPreference("help");
202 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
204 public boolean onPreferenceClick(Preference preference
) {
205 String helpWeb
=(String
) getText(R
.string
.url_help
);
206 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
207 Uri uriUrl
= Uri
.parse(helpWeb
);
208 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
209 startActivity(intent
);
215 preferenceCategory
.removePreference(pHelp
);
220 if (BuildConfig
.DEBUG
) {
221 Preference pLog
= findPreference("log");
223 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
225 public boolean onPreferenceClick(Preference preference
) {
226 Intent loggerIntent
= new Intent(getApplicationContext(),
227 LogHistoryActivity
.class);
228 startActivity(loggerIntent
);
235 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
236 Preference pRecommend
= findPreference("recommend");
237 if (pRecommend
!= null
){
238 if (recommendEnabled
) {
239 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
241 public boolean onPreferenceClick(Preference preference
) {
243 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
244 intent
.setType("text/plain");
245 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
246 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
248 String appName
= getString(R
.string
.app_name
);
249 String downloadUrl
= getString(R
.string
.url_app_download
);
250 Account currentAccount
= AccountUtils
.
251 getCurrentOwnCloudAccount(Preferences
.this);
252 String username
= currentAccount
.name
.substring(0,
253 currentAccount
.name
.lastIndexOf('@'));
255 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
257 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
258 appName
, downloadUrl
);
260 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
261 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
262 startActivity(intent
);
269 preferenceCategory
.removePreference(pRecommend
);
274 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
275 Preference pFeedback
= findPreference("feedback");
276 if (pFeedback
!= null
){
277 if (feedbackEnabled
) {
278 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
280 public boolean onPreferenceClick(Preference preference
) {
281 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
282 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
283 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
284 intent
.setType("text/plain");
285 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
287 intent
.setData(Uri
.parse(feedbackMail
));
288 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
289 startActivity(intent
);
295 preferenceCategory
.removePreference(pFeedback
);
300 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
301 Preference pImprint
= findPreference("imprint");
302 if (pImprint
!= null
) {
303 if (imprintEnabled
) {
304 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
306 public boolean onPreferenceClick(Preference preference
) {
307 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
308 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
309 Uri uriUrl
= Uri
.parse(imprintWeb
);
310 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
311 startActivity(intent
);
313 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
318 preferenceCategory
.removePreference(pImprint
);
322 mPrefInstantUploadPath
= findPreference("instant_upload_path");
323 if (mPrefInstantUploadPath
!= null
){
325 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
327 public boolean onPreferenceClick(Preference preference
) {
328 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
329 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
331 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
332 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
333 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
339 mPrefInstantUploadCategory
=
340 (PreferenceCategory
) findPreference("instant_uploading_category");
342 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
343 mPrefInstantUpload
= findPreference("instant_uploading");
345 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
347 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
350 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
351 toggleInstantPictureOptions((Boolean
) newValue
);
356 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
357 if (mPrefInstantVideoUploadPath
!= null
){
359 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
361 public boolean onPreferenceClick(Preference preference
) {
362 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
363 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
365 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
366 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
368 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
374 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
375 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
376 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
378 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
381 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
382 toggleInstantVideoOptions((Boolean
) newValue
);
388 pAboutApp
= (Preference
) findPreference("about_app");
389 if (pAboutApp
!= null
) {
390 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
391 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
394 loadInstantUploadPath();
395 loadInstantUploadVideoPath();
397 /* ComponentsGetter */
398 mDownloadServiceConnection
= newTransferenceServiceConnection();
399 if (mDownloadServiceConnection
!= null
) {
400 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
401 Context
.BIND_AUTO_CREATE
);
403 mUploadServiceConnection
= newTransferenceServiceConnection();
404 if (mUploadServiceConnection
!= null
) {
405 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
406 Context
.BIND_AUTO_CREATE
);
411 private void toggleInstantPictureOptions(Boolean value
){
413 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
414 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
416 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
417 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
421 private void toggleInstantVideoOptions(Boolean value
){
423 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
424 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
426 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
427 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
432 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
434 // Filter for only showing contextual menu when long press on the
436 if (mShowContextMenu
) {
437 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
438 mShowContextMenu
= false
;
440 super.onCreateContextMenu(menu
, v
, menuInfo
);
444 * Called when the user clicked on an item into the context menu created at
445 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
446 * every ownCloud {@link Account} , containing 'secondary actions' for them.
451 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
452 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
453 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
454 for (Account a
: accounts
) {
455 if (a
.name
.equals(mAccountName
)) {
456 if (item
.getItemId() == R
.id
.change_password
) {
458 // Change account password
459 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
460 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
461 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
462 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
463 startActivity(updateAccountCredentials
);
465 } else if (item
.getItemId() == R
.id
.delete_account
) {
468 am
.removeAccount(a
, this, mHandler
);
469 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
478 public void run(AccountManagerFuture
<Boolean
> future
) {
479 if (future
.isDone()) {
480 // after remove account
481 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
482 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
484 if (mUploaderBinder
!= null
) {
485 mUploaderBinder
.cancel(account
);
487 if (mDownloaderBinder
!= null
) {
488 mDownloaderBinder
.cancel(account
);
492 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
493 String accountName
= "";
495 Account
[] accounts
= AccountManager
.get(this)
496 .getAccountsByType(MainApp
.getAccountType());
497 if (accounts
.length
!= 0)
498 accountName
= accounts
[0].name
;
499 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
501 addAccountsCheckboxPreferences();
506 protected void onResume() {
508 SharedPreferences appPrefs
=
509 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
510 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
511 pCode
.setChecked(state
);
513 // Populate the accounts category with the list of accounts
514 addAccountsCheckboxPreferences();
518 public boolean onCreateOptionsMenu(Menu menu
) {
519 super.onCreateOptionsMenu(menu
);
524 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
525 super.onMenuItemSelected(featureId
, item
);
528 switch (item
.getItemId()) {
529 case android
.R
.id
.home
:
530 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
531 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
532 startActivity(intent
);
535 Log_OC
.w(TAG
, "Unknown menu item triggered");
542 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
543 super.onActivityResult(requestCode
, resultCode
, data
);
545 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
547 OCFile folderToUpload
=
548 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
550 mUploadPath
= folderToUpload
.getRemotePath();
552 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
554 // Show the path on summary preference
555 mPrefInstantUploadPath
.setSummary(mUploadPath
);
557 saveInstantUploadPathOnPreferences();
559 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
561 OCFile folderToUploadVideo
=
562 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
564 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
566 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
568 // Show the video path on summary preference
569 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
571 saveInstantUploadVideoPathOnPreferences();
575 public ActionBar
getSupportActionBar() {
576 return getDelegate().getSupportActionBar();
579 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
580 getDelegate().setSupportActionBar(toolbar
);
584 public MenuInflater
getMenuInflater() {
585 return getDelegate().getMenuInflater();
589 public void setContentView(@LayoutRes int layoutResID
) {
590 getDelegate().setContentView(layoutResID
);
593 public void setContentView(View view
) {
594 getDelegate().setContentView(view
);
597 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
598 getDelegate().setContentView(view
, params
);
602 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
603 getDelegate().addContentView(view
, params
);
607 protected void onPostResume() {
608 super.onPostResume();
609 getDelegate().onPostResume();
613 protected void onTitleChanged(CharSequence title
, int color
) {
614 super.onTitleChanged(title
, color
);
615 getDelegate().setTitle(title
);
619 public void onConfigurationChanged(Configuration newConfig
) {
620 super.onConfigurationChanged(newConfig
);
621 getDelegate().onConfigurationChanged(newConfig
);
625 protected void onPostCreate(Bundle savedInstanceState
) {
626 super.onPostCreate(savedInstanceState
);
627 getDelegate().onPostCreate(savedInstanceState
);
631 protected void onDestroy() {
634 if (mDownloadServiceConnection
!= null
) {
635 unbindService(mDownloadServiceConnection
);
636 mDownloadServiceConnection
= null
;
638 if (mUploadServiceConnection
!= null
) {
639 unbindService(mUploadServiceConnection
);
640 mUploadServiceConnection
= null
;
644 getDelegate().onDestroy();
648 protected void onStop() {
650 getDelegate().onStop();
653 public void invalidateOptionsMenu() {
654 getDelegate().invalidateOptionsMenu();
657 private AppCompatDelegate
getDelegate() {
658 if (mDelegate
== null
) {
659 mDelegate
= AppCompatDelegate
.create(this, null
);
665 * Create the list of accounts that has been added into the app
667 @SuppressWarnings("deprecation")
668 private void addAccountsCheckboxPreferences() {
670 // Remove accounts in case list is refreshing for avoiding to have
672 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
673 mAccountsPrefCategory
.removeAll();
676 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
677 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
678 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
680 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
681 // Show create account screen if there isn't any account
682 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
688 for (Account a
: accounts
) {
689 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
690 accountPreference
.setKey(a
.name
);
691 // Handle internationalized domain names
692 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
693 mAccountsPrefCategory
.addPreference(accountPreference
);
695 // Check the current account that is being used
696 if (a
.name
.equals(currentAccount
.name
)) {
697 accountPreference
.setChecked(true
);
699 accountPreference
.setChecked(false
);
702 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
704 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
705 String key
= preference
.getKey();
706 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
707 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
708 for (Account a
: accounts
) {
709 RadioButtonPreference p
=
710 (RadioButtonPreference
) findPreference(a
.name
);
711 if (key
.equals(a
.name
)) {
712 boolean accountChanged
= !p
.isChecked();
714 AccountUtils
.setCurrentOwnCloudAccount(
715 getApplicationContext(),
718 if (accountChanged
) {
719 // restart the main activity
720 Intent i
= new Intent(
722 FileDisplayActivity
.class
724 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
725 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
734 return (Boolean
) newValue
;
740 // Add Create Account preference at the end of account list if
741 // Multiaccount is enabled
742 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
743 createAddAccountPreference();
750 * Create the preference for allow adding new accounts
752 private void createAddAccountPreference() {
753 Preference addAccountPref
= new Preference(this);
754 addAccountPref
.setKey("add_account");
755 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
756 mAccountsPrefCategory
.addPreference(addAccountPref
);
758 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
760 public boolean onPreferenceClick(Preference preference
) {
761 AccountManager am
= AccountManager
.get(getApplicationContext());
762 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
771 * Load upload path set on preferences
773 private void loadInstantUploadPath() {
774 SharedPreferences appPrefs
=
775 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
776 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
777 mPrefInstantUploadPath
.setSummary(mUploadPath
);
781 * Save the "Instant Upload Path" on preferences
783 private void saveInstantUploadPathOnPreferences() {
784 SharedPreferences appPrefs
=
785 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
786 SharedPreferences
.Editor editor
= appPrefs
.edit();
787 editor
.putString("instant_upload_path", mUploadPath
);
792 * Load upload video path set on preferences
794 private void loadInstantUploadVideoPath() {
795 SharedPreferences appPrefs
=
796 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
797 mUploadVideoPath
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
));
798 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
802 * Save the "Instant Video Upload Path" on preferences
804 private void saveInstantUploadVideoPathOnPreferences() {
805 SharedPreferences appPrefs
=
806 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
807 SharedPreferences
.Editor editor
= appPrefs
.edit();
808 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
812 // Methods for ComponetsGetter
814 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
815 return mDownloaderBinder
;
820 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
821 return mUploaderBinder
;
825 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
830 public FileDataStorageManager
getStorageManager() {
835 public FileOperationsHelper
getFileOperationsHelper() {
839 protected ServiceConnection
newTransferenceServiceConnection() {
840 return new PreferencesServiceConnection();
843 /** Defines callbacks for service binding, passed to bindService() */
844 private class PreferencesServiceConnection
implements ServiceConnection
{
847 public void onServiceConnected(ComponentName component
, IBinder service
) {
849 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
850 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
852 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
853 Log_OC
.d(TAG
, "Upload service connected");
854 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
862 public void onServiceDisconnected(ComponentName component
) {
863 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
864 Log_OC
.d(TAG
, "Download service suddenly disconnected");
865 mDownloaderBinder
= null
;
866 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
867 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
868 mUploaderBinder
= null
;