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 Long cacheSize
= ThumbnailsCacheManager
.getMaxSize();
246 pCacheSize
.setSummary(cacheSize
+ " Mb");
247 pCacheSize
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
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");
262 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
264 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
265 Preference pHelp
= findPreference("help");
268 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
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
);
281 preferenceCategory
.removePreference(pHelp
);
286 if (BuildConfig
.DEBUG
) {
287 Preference pLog
= findPreference("log");
289 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
291 public boolean onPreferenceClick(Preference preference
) {
292 Intent loggerIntent
= new Intent(getApplicationContext(),
293 LogHistoryActivity
.class);
294 startActivity(loggerIntent
);
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() {
307 public boolean onPreferenceClick(Preference preference
) {
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
);
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('@'));
321 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
323 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
324 appName
, downloadUrl
);
326 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
327 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
328 startActivity(intent
);
335 preferenceCategory
.removePreference(pRecommend
);
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() {
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
);
353 intent
.setData(Uri
.parse(feedbackMail
));
354 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
355 startActivity(intent
);
361 preferenceCategory
.removePreference(pFeedback
);
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() {
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
);
379 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
384 preferenceCategory
.removePreference(pImprint
);
388 mPrefInstantUploadPath
= findPreference("instant_upload_path");
389 if (mPrefInstantUploadPath
!= null
){
391 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
393 public boolean onPreferenceClick(Preference preference
) {
394 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
395 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
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
);
405 mPrefInstantUploadCategory
=
406 (PreferenceCategory
) findPreference("instant_uploading_category");
408 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
409 mPrefInstantUpload
= findPreference("instant_uploading");
411 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
413 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
416 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
417 toggleInstantPictureOptions((Boolean
) newValue
);
422 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
423 if (mPrefInstantVideoUploadPath
!= null
){
425 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
427 public boolean onPreferenceClick(Preference preference
) {
428 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
429 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
431 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
432 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
434 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
440 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
441 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
442 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
444 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
447 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
448 toggleInstantVideoOptions((Boolean
) newValue
);
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
));
460 loadInstantUploadPath();
461 loadInstantUploadVideoPath();
463 /* ComponentsGetter */
464 mDownloadServiceConnection
= newTransferenceServiceConnection();
465 if (mDownloadServiceConnection
!= null
) {
466 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
467 Context
.BIND_AUTO_CREATE
);
469 mUploadServiceConnection
= newTransferenceServiceConnection();
470 if (mUploadServiceConnection
!= null
) {
471 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
472 Context
.BIND_AUTO_CREATE
);
475 /* Link to Beta apks */
476 Preference pBetaLink
= findPreference("beta_link");
477 if (pBetaLink
!= null
){
478 pBetaLink
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
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
);
493 private void toggleInstantPictureOptions(Boolean value
){
495 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
496 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
498 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
499 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
503 private void toggleInstantVideoOptions(Boolean value
){
505 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
506 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
508 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
509 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
514 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
516 // Filter for only showing contextual menu when long press on the
518 if (mShowContextMenu
) {
519 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
520 mShowContextMenu
= false
;
522 super.onCreateContextMenu(menu
, v
, menuInfo
);
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())) {
532 if (mUploaderBinder
!= null
) {
533 mUploaderBinder
.cancel(account
);
535 if (mDownloaderBinder
!= null
) {
536 mDownloaderBinder
.cancel(account
);
540 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
541 String accountName
= "";
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
);
549 addAccountsCheckboxPreferences();
554 protected void onResume() {
556 SharedPreferences appPrefs
=
557 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
558 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
559 pCode
.setChecked(state
);
561 // Populate the accounts category with the list of accounts
562 addAccountsCheckboxPreferences();
566 public boolean onCreateOptionsMenu(Menu menu
) {
567 super.onCreateOptionsMenu(menu
);
572 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
573 super.onMenuItemSelected(featureId
, item
);
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
);
583 Log_OC
.w(TAG
, "Unknown menu item triggered");
590 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
591 super.onActivityResult(requestCode
, resultCode
, data
);
593 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
595 OCFile folderToUpload
=
596 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
598 mUploadPath
= folderToUpload
.getRemotePath();
600 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
602 // Show the path on summary preference
603 mPrefInstantUploadPath
.setSummary(mUploadPath
);
605 saveInstantUploadPathOnPreferences();
607 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
609 OCFile folderToUploadVideo
=
610 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
612 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
614 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
616 // Show the video path on summary preference
617 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
619 saveInstantUploadVideoPathOnPreferences();
623 public ActionBar
getSupportActionBar() {
624 return getDelegate().getSupportActionBar();
627 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
628 getDelegate().setSupportActionBar(toolbar
);
632 public MenuInflater
getMenuInflater() {
633 return getDelegate().getMenuInflater();
637 public void setContentView(@LayoutRes int layoutResID
) {
638 getDelegate().setContentView(layoutResID
);
641 public void setContentView(View view
) {
642 getDelegate().setContentView(view
);
645 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
646 getDelegate().setContentView(view
, params
);
650 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
651 getDelegate().addContentView(view
, params
);
655 protected void onPostResume() {
656 super.onPostResume();
657 getDelegate().onPostResume();
661 protected void onTitleChanged(CharSequence title
, int color
) {
662 super.onTitleChanged(title
, color
);
663 getDelegate().setTitle(title
);
667 public void onConfigurationChanged(Configuration newConfig
) {
668 super.onConfigurationChanged(newConfig
);
669 getDelegate().onConfigurationChanged(newConfig
);
673 protected void onPostCreate(Bundle savedInstanceState
) {
674 super.onPostCreate(savedInstanceState
);
675 getDelegate().onPostCreate(savedInstanceState
);
679 protected void onDestroy() {
682 if (mDownloadServiceConnection
!= null
) {
683 unbindService(mDownloadServiceConnection
);
684 mDownloadServiceConnection
= null
;
686 if (mUploadServiceConnection
!= null
) {
687 unbindService(mUploadServiceConnection
);
688 mUploadServiceConnection
= null
;
692 getDelegate().onDestroy();
696 protected void onStop() {
698 getDelegate().onStop();
701 public void invalidateOptionsMenu() {
702 getDelegate().invalidateOptionsMenu();
705 private AppCompatDelegate
getDelegate() {
706 if (mDelegate
== null
) {
707 mDelegate
= AppCompatDelegate
.create(this, null
);
713 * Create the list of accounts that has been added into the app
715 @SuppressWarnings("deprecation")
716 private void addAccountsCheckboxPreferences() {
718 // Remove accounts in case list is refreshing for avoiding to have
720 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
721 mAccountsPrefCategory
.removeAll();
724 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
725 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
726 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
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,
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
);
743 // Check the current account that is being used
744 if (a
.name
.equals(currentAccount
.name
)) {
745 accountPreference
.setChecked(true
);
747 accountPreference
.setChecked(false
);
750 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
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();
762 AccountUtils
.setCurrentOwnCloudAccount(
763 getApplicationContext(),
766 if (accountChanged
) {
767 // restart the main activity
768 Intent i
= new Intent(
770 FileDisplayActivity
.class
772 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
773 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
782 return (Boolean
) newValue
;
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();
798 * Create the preference for allow adding new accounts
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
);
806 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
808 public boolean onPreferenceClick(Preference preference
) {
809 AccountManager am
= AccountManager
.get(getApplicationContext());
810 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
819 * Load upload path set on preferences
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
);
829 * Save the "Instant Upload Path" on preferences
831 private void saveInstantUploadPathOnPreferences() {
832 SharedPreferences appPrefs
=
833 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
834 SharedPreferences
.Editor editor
= appPrefs
.edit();
835 editor
.putString("instant_upload_path", mUploadPath
);
840 * Load upload video path set on preferences
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);
850 * Save the "Instant Video Upload Path" on preferences
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
);
860 // Methods for ComponetsGetter
862 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
863 return mDownloaderBinder
;
868 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
869 return mUploaderBinder
;
873 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
878 public FileDataStorageManager
getStorageManager() {
883 public FileOperationsHelper
getFileOperationsHelper() {
887 protected ServiceConnection
newTransferenceServiceConnection() {
888 return new PreferencesServiceConnection();
891 /** Defines callbacks for service binding, passed to bindService() */
892 private class PreferencesServiceConnection
implements ServiceConnection
{
895 public void onServiceConnected(ComponentName component
, IBinder service
) {
897 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
898 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
900 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
901 Log_OC
.d(TAG
, "Upload service connected");
902 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
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
;