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
.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
;
85 * An Activity that allows the user to change the application's settings.
87 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
90 public class Preferences
extends PreferenceActivity
91 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
93 private static final String TAG
= "OwnCloudPreferences";
95 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
96 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
98 private DbHandler mDbHandler
;
99 private CheckBoxPreference pCode
;
100 private Preference pAboutApp
;
101 private AppCompatDelegate mDelegate
;
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
;
117 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
118 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
119 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
121 @SuppressWarnings("deprecation")
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
);
130 ActionBar actionBar
= getSupportActionBar();
131 actionBar
.setDisplayHomeAsUpEnabled(true
);
132 actionBar
.setTitle(R
.string
.actionbar_settings
);
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
));
142 // Load the accounts category for adding the list of accounts
143 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
145 ListView listView
= getListView();
146 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
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
);
153 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
154 mShowContextMenu
= true
;
155 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
158 getResources().getString(R
.string
.change_password
),
159 getResources().getString(R
.string
.delete_account
)
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
);
169 //Setup proper inline listener
170 final AlertDialog alertDialog
= alertDialogBuilder
.create();
171 lv
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
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
)) {
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();
188 } else if (position
==1) {
191 am
.removeAccount(a
, Preferences
.this, mHandler
);
192 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
193 alertDialog
.cancel();
201 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
202 return longListener
.onLongClick(view
);
211 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
212 temp
= pkg
.versionName
;
213 } catch (NameNotFoundException e
) {
215 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
217 final String appVersion
= temp
;
219 // Register context menu for list of preferences.
220 registerForContextMenu(getListView());
222 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
224 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
226 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
227 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
228 Boolean enable
= (Boolean
) newValue
;
230 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
231 PassCodeActivity
.ACTION_DISABLE
241 final Preference pCacheSize
= findPreference("pref_cache_size");
242 if (pCacheSize
!= null
){
243 final SharedPreferences appPrefs
=
244 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
245 String savedSize
= appPrefs
.getString("pref_cache_size", "10");
246 pCacheSize
.setSummary(savedSize
+ " Mb");
247 pCacheSize
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
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");
263 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
265 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
266 Preference pHelp
= findPreference("help");
269 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
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
);
282 preferenceCategory
.removePreference(pHelp
);
287 if (BuildConfig
.DEBUG
) {
288 Preference pLog
= findPreference("log");
290 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
292 public boolean onPreferenceClick(Preference preference
) {
293 Intent loggerIntent
= new Intent(getApplicationContext(),
294 LogHistoryActivity
.class);
295 startActivity(loggerIntent
);
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() {
308 public boolean onPreferenceClick(Preference preference
) {
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
);
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('@'));
322 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
324 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
325 appName
, downloadUrl
);
327 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
328 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
329 startActivity(intent
);
336 preferenceCategory
.removePreference(pRecommend
);
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() {
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
);
354 intent
.setData(Uri
.parse(feedbackMail
));
355 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
356 startActivity(intent
);
362 preferenceCategory
.removePreference(pFeedback
);
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() {
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
);
380 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
385 preferenceCategory
.removePreference(pImprint
);
389 mPrefInstantUploadPath
= findPreference("instant_upload_path");
390 if (mPrefInstantUploadPath
!= null
){
392 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
394 public boolean onPreferenceClick(Preference preference
) {
395 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
396 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
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
);
406 mPrefInstantUploadCategory
=
407 (PreferenceCategory
) findPreference("instant_uploading_category");
409 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
410 mPrefInstantUpload
= findPreference("instant_uploading");
412 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
414 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
417 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
418 toggleInstantPictureOptions((Boolean
) newValue
);
423 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
424 if (mPrefInstantVideoUploadPath
!= null
){
426 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
428 public boolean onPreferenceClick(Preference preference
) {
429 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
430 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
432 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
433 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
435 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
441 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
442 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
443 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
445 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
448 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
449 toggleInstantVideoOptions((Boolean
) newValue
);
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
));
461 loadInstantUploadPath();
462 loadInstantUploadVideoPath();
464 /* ComponentsGetter */
465 mDownloadServiceConnection
= newTransferenceServiceConnection();
466 if (mDownloadServiceConnection
!= null
) {
467 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
468 Context
.BIND_AUTO_CREATE
);
470 mUploadServiceConnection
= newTransferenceServiceConnection();
471 if (mUploadServiceConnection
!= null
) {
472 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
473 Context
.BIND_AUTO_CREATE
);
478 private void toggleInstantPictureOptions(Boolean value
){
480 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
481 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
483 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
484 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
488 private void toggleInstantVideoOptions(Boolean value
){
490 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
491 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
493 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
494 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
499 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
501 // Filter for only showing contextual menu when long press on the
503 if (mShowContextMenu
) {
504 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
505 mShowContextMenu
= false
;
507 super.onCreateContextMenu(menu
, v
, menuInfo
);
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())) {
517 if (mUploaderBinder
!= null
) {
518 mUploaderBinder
.cancel(account
);
520 if (mDownloaderBinder
!= null
) {
521 mDownloaderBinder
.cancel(account
);
525 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
526 String accountName
= "";
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
);
534 addAccountsCheckboxPreferences();
539 protected void onResume() {
541 SharedPreferences appPrefs
=
542 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
543 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
544 pCode
.setChecked(state
);
546 // Populate the accounts category with the list of accounts
547 addAccountsCheckboxPreferences();
551 public boolean onCreateOptionsMenu(Menu menu
) {
552 super.onCreateOptionsMenu(menu
);
557 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
558 super.onMenuItemSelected(featureId
, item
);
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
);
568 Log_OC
.w(TAG
, "Unknown menu item triggered");
575 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
576 super.onActivityResult(requestCode
, resultCode
, data
);
578 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
580 OCFile folderToUpload
=
581 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
583 mUploadPath
= folderToUpload
.getRemotePath();
585 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
587 // Show the path on summary preference
588 mPrefInstantUploadPath
.setSummary(mUploadPath
);
590 saveInstantUploadPathOnPreferences();
592 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
594 OCFile folderToUploadVideo
=
595 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
597 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
599 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
601 // Show the video path on summary preference
602 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
604 saveInstantUploadVideoPathOnPreferences();
608 public ActionBar
getSupportActionBar() {
609 return getDelegate().getSupportActionBar();
612 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
613 getDelegate().setSupportActionBar(toolbar
);
617 public MenuInflater
getMenuInflater() {
618 return getDelegate().getMenuInflater();
622 public void setContentView(@LayoutRes int layoutResID
) {
623 getDelegate().setContentView(layoutResID
);
626 public void setContentView(View view
) {
627 getDelegate().setContentView(view
);
630 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
631 getDelegate().setContentView(view
, params
);
635 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
636 getDelegate().addContentView(view
, params
);
640 protected void onPostResume() {
641 super.onPostResume();
642 getDelegate().onPostResume();
646 protected void onTitleChanged(CharSequence title
, int color
) {
647 super.onTitleChanged(title
, color
);
648 getDelegate().setTitle(title
);
652 public void onConfigurationChanged(Configuration newConfig
) {
653 super.onConfigurationChanged(newConfig
);
654 getDelegate().onConfigurationChanged(newConfig
);
658 protected void onPostCreate(Bundle savedInstanceState
) {
659 super.onPostCreate(savedInstanceState
);
660 getDelegate().onPostCreate(savedInstanceState
);
664 protected void onDestroy() {
667 if (mDownloadServiceConnection
!= null
) {
668 unbindService(mDownloadServiceConnection
);
669 mDownloadServiceConnection
= null
;
671 if (mUploadServiceConnection
!= null
) {
672 unbindService(mUploadServiceConnection
);
673 mUploadServiceConnection
= null
;
677 getDelegate().onDestroy();
681 protected void onStop() {
683 getDelegate().onStop();
686 public void invalidateOptionsMenu() {
687 getDelegate().invalidateOptionsMenu();
690 private AppCompatDelegate
getDelegate() {
691 if (mDelegate
== null
) {
692 mDelegate
= AppCompatDelegate
.create(this, null
);
698 * Create the list of accounts that has been added into the app
700 @SuppressWarnings("deprecation")
701 private void addAccountsCheckboxPreferences() {
703 // Remove accounts in case list is refreshing for avoiding to have
705 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
706 mAccountsPrefCategory
.removeAll();
709 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
710 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
711 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
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,
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
);
728 // Check the current account that is being used
729 if (a
.name
.equals(currentAccount
.name
)) {
730 accountPreference
.setChecked(true
);
732 accountPreference
.setChecked(false
);
735 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
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();
747 AccountUtils
.setCurrentOwnCloudAccount(
748 getApplicationContext(),
751 if (accountChanged
) {
752 // restart the main activity
753 Intent i
= new Intent(
755 FileDisplayActivity
.class
757 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
758 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
767 return (Boolean
) newValue
;
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();
783 * Create the preference for allow adding new accounts
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
);
791 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
793 public boolean onPreferenceClick(Preference preference
) {
794 AccountManager am
= AccountManager
.get(getApplicationContext());
795 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
804 * Load upload path set on preferences
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
);
814 * Save the "Instant Upload Path" on preferences
816 private void saveInstantUploadPathOnPreferences() {
817 SharedPreferences appPrefs
=
818 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
819 SharedPreferences
.Editor editor
= appPrefs
.edit();
820 editor
.putString("instant_upload_path", mUploadPath
);
825 * Load upload video path set on preferences
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
);
835 * Save the "Instant Video Upload Path" on preferences
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
);
845 // Methods for ComponetsGetter
847 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
848 return mDownloaderBinder
;
853 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
854 return mUploaderBinder
;
858 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
863 public FileDataStorageManager
getStorageManager() {
868 public FileOperationsHelper
getFileOperationsHelper() {
872 protected ServiceConnection
newTransferenceServiceConnection() {
873 return new PreferencesServiceConnection();
876 /** Defines callbacks for service binding, passed to bindService() */
877 private class PreferencesServiceConnection
implements ServiceConnection
{
880 public void onServiceConnected(ComponentName component
, IBinder service
) {
882 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
883 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
885 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
886 Log_OC
.d(TAG
, "Upload service connected");
887 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
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
;