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
;
65 import android
.widget
.Toast
;
67 import com
.owncloud
.android
.BuildConfig
;
68 import com
.owncloud
.android
.MainApp
;
69 import com
.owncloud
.android
.R
;
70 import com
.owncloud
.android
.authentication
.AccountUtils
;
71 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
72 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
73 import com
.owncloud
.android
.datamodel
.OCFile
;
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;
97 private static final int ACTION_REQUEST_PASSCODE
= 5;
98 private static final int ACTION_CONFIRM_PASSCODE
= 6;
100 private DbHandler mDbHandler
;
101 private CheckBoxPreference pCode
;
102 private Preference pAboutApp
;
103 private AppCompatDelegate mDelegate
;
105 private PreferenceCategory mAccountsPrefCategory
= null
;
106 private final Handler mHandler
= new Handler();
107 private String mAccountName
;
108 private boolean mShowContextMenu
= false
;
109 private String mUploadPath
;
110 private PreferenceCategory mPrefInstantUploadCategory
;
111 private Preference mPrefInstantUpload
;
112 private Preference mPrefInstantUploadPath
;
113 private Preference mPrefInstantUploadPathWiFi
;
114 private Preference mPrefInstantVideoUpload
;
115 private Preference mPrefInstantVideoUploadPath
;
116 private Preference mPrefInstantVideoUploadPathWiFi
;
117 private String mUploadVideoPath
;
119 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
120 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
121 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
124 @SuppressWarnings("deprecation")
126 public void onCreate(Bundle savedInstanceState
) {
127 getDelegate().installViewFactory();
128 getDelegate().onCreate(savedInstanceState
);
129 super.onCreate(savedInstanceState
);
130 mDbHandler
= new DbHandler(getBaseContext());
131 addPreferencesFromResource(R
.xml
.preferences
);
133 ActionBar actionBar
= getSupportActionBar();
134 actionBar
.setDisplayHomeAsUpEnabled(true
);
135 actionBar
.setTitle(R
.string
.actionbar_settings
);
137 // For adding content description tag to a title field in the action bar
138 int actionBarTitleId
= getResources().getIdentifier("action_bar_title", "id", "android");
139 View actionBarTitleView
= getWindow().getDecorView().findViewById(actionBarTitleId
);
140 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
141 getWindow().getDecorView().findViewById(actionBarTitleId
).
142 setContentDescription(getString(R
.string
.actionbar_settings
));
145 // Load the accounts category for adding the list of accounts
146 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
148 ListView listView
= getListView();
149 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
151 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
152 ListView listView
= (ListView
) parent
;
153 ListAdapter listAdapter
= listView
.getAdapter();
154 Object obj
= listAdapter
.getItem(position
);
156 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
157 mShowContextMenu
= true
;
158 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
161 getResources().getString(R
.string
.change_password
),
162 getResources().getString(R
.string
.delete_account
)
164 final AlertDialog
.Builder alertDialogBuilder
= new AlertDialog
.Builder(Preferences
.this);
165 View convertView
= getLayoutInflater().inflate(R
.layout
.alert_dialog_list_view
, null
);
166 alertDialogBuilder
.setView(convertView
);
167 ListView lv
= (ListView
) convertView
.findViewById(R
.id
.list
);
168 ArrayAdapter
<String
> adapter
= new ArrayAdapter
<String
>(
169 Preferences
.this,R
.layout
.simple_dialog_list_item
,items
);
170 lv
.setAdapter(adapter
);
172 //Setup proper inline listener
173 final AlertDialog alertDialog
= alertDialogBuilder
.create();
174 lv
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
176 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
177 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
178 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
179 for (Account a
: accounts
) {
180 if (a
.name
.equals(mAccountName
)) {
183 // Change account password
184 Intent updateAccountCredentials
= new Intent(Preferences
.this, AuthenticatorActivity
.class);
185 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
186 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
187 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
188 startActivity(updateAccountCredentials
);
189 alertDialog
.cancel();
191 } else if (position
==1) {
194 am
.removeAccount(a
, Preferences
.this, mHandler
);
195 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
196 alertDialog
.cancel();
204 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
205 return longListener
.onLongClick(view
);
214 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
215 temp
= pkg
.versionName
;
216 } catch (NameNotFoundException e
) {
218 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
220 final String appVersion
= temp
;
222 // Register context menu for list of preferences.
223 registerForContextMenu(getListView());
225 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
227 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
229 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
230 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
231 Boolean incoming
= (Boolean
) newValue
;
234 incoming
.booleanValue() ? PassCodeActivity
.ACTION_REQUEST_WITH_RESULT
:
235 PassCodeActivity
.ACTION_CHECK_WITH_RESULT
238 startActivityForResult(i
, incoming
.booleanValue() ? ACTION_REQUEST_PASSCODE
:
239 ACTION_CONFIRM_PASSCODE
);
241 // Don't update just yet, we will decide on it in onActivityResult
250 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
252 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
253 Preference pHelp
= findPreference("help");
256 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
258 public boolean onPreferenceClick(Preference preference
) {
259 String helpWeb
=(String
) getText(R
.string
.url_help
);
260 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
261 Uri uriUrl
= Uri
.parse(helpWeb
);
262 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
263 startActivity(intent
);
269 preferenceCategory
.removePreference(pHelp
);
274 if (BuildConfig
.DEBUG
) {
275 Preference pLog
= findPreference("log");
277 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
279 public boolean onPreferenceClick(Preference preference
) {
280 Intent loggerIntent
= new Intent(getApplicationContext(),
281 LogHistoryActivity
.class);
282 startActivity(loggerIntent
);
289 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
290 Preference pRecommend
= findPreference("recommend");
291 if (pRecommend
!= null
){
292 if (recommendEnabled
) {
293 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
295 public boolean onPreferenceClick(Preference preference
) {
297 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
298 intent
.setType("text/plain");
299 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
300 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
302 String appName
= getString(R
.string
.app_name
);
303 String downloadUrl
= getString(R
.string
.url_app_download
);
304 Account currentAccount
= AccountUtils
.
305 getCurrentOwnCloudAccount(Preferences
.this);
306 String username
= currentAccount
.name
.substring(0,
307 currentAccount
.name
.lastIndexOf('@'));
309 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
311 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
312 appName
, downloadUrl
);
314 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
315 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
316 startActivity(intent
);
323 preferenceCategory
.removePreference(pRecommend
);
328 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
329 Preference pFeedback
= findPreference("feedback");
330 if (pFeedback
!= null
){
331 if (feedbackEnabled
) {
332 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
334 public boolean onPreferenceClick(Preference preference
) {
335 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
336 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
337 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
338 intent
.setType("text/plain");
339 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
341 intent
.setData(Uri
.parse(feedbackMail
));
342 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
343 startActivity(intent
);
349 preferenceCategory
.removePreference(pFeedback
);
354 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
355 Preference pImprint
= findPreference("imprint");
356 if (pImprint
!= null
) {
357 if (imprintEnabled
) {
358 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
360 public boolean onPreferenceClick(Preference preference
) {
361 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
362 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
363 Uri uriUrl
= Uri
.parse(imprintWeb
);
364 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
365 startActivity(intent
);
367 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
372 preferenceCategory
.removePreference(pImprint
);
376 mPrefInstantUploadPath
= findPreference("instant_upload_path");
377 if (mPrefInstantUploadPath
!= null
){
379 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
381 public boolean onPreferenceClick(Preference preference
) {
382 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
383 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
385 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
386 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
387 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
393 mPrefInstantUploadCategory
=
394 (PreferenceCategory
) findPreference("instant_uploading_category");
396 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
397 mPrefInstantUpload
= findPreference("instant_uploading");
399 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
401 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
404 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
405 toggleInstantPictureOptions((Boolean
) newValue
);
410 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
411 if (mPrefInstantVideoUploadPath
!= null
){
413 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
415 public boolean onPreferenceClick(Preference preference
) {
416 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
417 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
419 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
420 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
422 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
428 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
429 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
430 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
432 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
435 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
436 toggleInstantVideoOptions((Boolean
) newValue
);
442 pAboutApp
= (Preference
) findPreference("about_app");
443 if (pAboutApp
!= null
) {
444 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
445 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
448 loadInstantUploadPath();
449 loadInstantUploadVideoPath();
451 /* ComponentsGetter */
452 mDownloadServiceConnection
= newTransferenceServiceConnection();
453 if (mDownloadServiceConnection
!= null
) {
454 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
455 Context
.BIND_AUTO_CREATE
);
457 mUploadServiceConnection
= newTransferenceServiceConnection();
458 if (mUploadServiceConnection
!= null
) {
459 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
460 Context
.BIND_AUTO_CREATE
);
465 private void toggleInstantPictureOptions(Boolean value
){
467 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
468 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
470 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
471 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
475 private void toggleInstantVideoOptions(Boolean value
){
477 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
478 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
480 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
481 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
486 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
488 // Filter for only showing contextual menu when long press on the
490 if (mShowContextMenu
) {
491 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
492 mShowContextMenu
= false
;
494 super.onCreateContextMenu(menu
, v
, menuInfo
);
498 public void run(AccountManagerFuture
<Boolean
> future
) {
499 if (future
.isDone()) {
500 // after remove account
501 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
502 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
504 if (mUploaderBinder
!= null
) {
505 mUploaderBinder
.cancel(account
);
507 if (mDownloaderBinder
!= null
) {
508 mDownloaderBinder
.cancel(account
);
512 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
513 String accountName
= "";
515 Account
[] accounts
= AccountManager
.get(this)
516 .getAccountsByType(MainApp
.getAccountType());
517 if (accounts
.length
!= 0)
518 accountName
= accounts
[0].name
;
519 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
521 addAccountsCheckboxPreferences();
526 protected void onResume() {
528 SharedPreferences appPrefs
=
529 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
530 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
531 pCode
.setChecked(state
);
533 // Populate the accounts category with the list of accounts
534 addAccountsCheckboxPreferences();
538 public boolean onCreateOptionsMenu(Menu menu
) {
539 super.onCreateOptionsMenu(menu
);
544 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
545 super.onMenuItemSelected(featureId
, item
);
548 switch (item
.getItemId()) {
549 case android
.R
.id
.home
:
550 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
551 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
552 startActivity(intent
);
555 Log_OC
.w(TAG
, "Unknown menu item triggered");
562 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
563 super.onActivityResult(requestCode
, resultCode
, data
);
565 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
567 OCFile folderToUpload
=
568 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
570 mUploadPath
= folderToUpload
.getRemotePath();
572 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
574 // Show the path on summary preference
575 mPrefInstantUploadPath
.setSummary(mUploadPath
);
577 saveInstantUploadPathOnPreferences();
579 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
581 OCFile folderToUploadVideo
=
582 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
584 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
586 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
588 // Show the video path on summary preference
589 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
591 saveInstantUploadVideoPathOnPreferences();
592 } else if (requestCode
== ACTION_REQUEST_PASSCODE
&& resultCode
== RESULT_OK
) {
593 String passcode
= data
.getStringExtra(PassCodeActivity
.KEY_PASSCODE
);
594 if (passcode
!= null
&& passcode
.length() == 4) {
595 SharedPreferences
.Editor appPrefs
= PreferenceManager
596 .getDefaultSharedPreferences(getApplicationContext()).edit();
598 for (int i
= 1; i
<= 4; ++i
) {
599 appPrefs
.putString("PrefPinCode" + i
, passcode
.substring(i
-1, i
));
601 appPrefs
.putBoolean("set_pincode", true
);
603 Toast
.makeText(this, R
.string
.pass_code_stored
, Toast
.LENGTH_LONG
).show();
605 } else if (requestCode
== ACTION_CONFIRM_PASSCODE
&& resultCode
== RESULT_OK
) {
606 if (data
.getBooleanExtra(PassCodeActivity
.KEY_CHECK_RESULT
, false
)) {
608 SharedPreferences
.Editor appPrefs
= PreferenceManager
609 .getDefaultSharedPreferences(getApplicationContext()).edit();
610 appPrefs
.putBoolean("set_pincode", false
);
613 Toast
.makeText(this, R
.string
.pass_code_removed
, Toast
.LENGTH_LONG
).show();
618 public ActionBar
getSupportActionBar() {
619 return getDelegate().getSupportActionBar();
622 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
623 getDelegate().setSupportActionBar(toolbar
);
627 public MenuInflater
getMenuInflater() {
628 return getDelegate().getMenuInflater();
632 public void setContentView(@LayoutRes int layoutResID
) {
633 getDelegate().setContentView(layoutResID
);
636 public void setContentView(View view
) {
637 getDelegate().setContentView(view
);
640 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
641 getDelegate().setContentView(view
, params
);
645 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
646 getDelegate().addContentView(view
, params
);
650 protected void onPostResume() {
651 super.onPostResume();
652 getDelegate().onPostResume();
656 protected void onTitleChanged(CharSequence title
, int color
) {
657 super.onTitleChanged(title
, color
);
658 getDelegate().setTitle(title
);
662 public void onConfigurationChanged(Configuration newConfig
) {
663 super.onConfigurationChanged(newConfig
);
664 getDelegate().onConfigurationChanged(newConfig
);
668 protected void onPostCreate(Bundle savedInstanceState
) {
669 super.onPostCreate(savedInstanceState
);
670 getDelegate().onPostCreate(savedInstanceState
);
674 protected void onDestroy() {
677 if (mDownloadServiceConnection
!= null
) {
678 unbindService(mDownloadServiceConnection
);
679 mDownloadServiceConnection
= null
;
681 if (mUploadServiceConnection
!= null
) {
682 unbindService(mUploadServiceConnection
);
683 mUploadServiceConnection
= null
;
687 getDelegate().onDestroy();
691 protected void onStop() {
693 getDelegate().onStop();
696 public void invalidateOptionsMenu() {
697 getDelegate().invalidateOptionsMenu();
700 private AppCompatDelegate
getDelegate() {
701 if (mDelegate
== null
) {
702 mDelegate
= AppCompatDelegate
.create(this, null
);
708 * Create the list of accounts that has been added into the app
710 @SuppressWarnings("deprecation")
711 private void addAccountsCheckboxPreferences() {
713 // Remove accounts in case list is refreshing for avoiding to have
715 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
716 mAccountsPrefCategory
.removeAll();
719 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
720 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
721 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
723 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
724 // Show create account screen if there isn't any account
725 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
731 for (Account a
: accounts
) {
732 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
733 accountPreference
.setKey(a
.name
);
734 // Handle internationalized domain names
735 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
736 mAccountsPrefCategory
.addPreference(accountPreference
);
738 // Check the current account that is being used
739 if (a
.name
.equals(currentAccount
.name
)) {
740 accountPreference
.setChecked(true
);
742 accountPreference
.setChecked(false
);
745 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
747 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
748 String key
= preference
.getKey();
749 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
750 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
751 for (Account a
: accounts
) {
752 RadioButtonPreference p
=
753 (RadioButtonPreference
) findPreference(a
.name
);
754 if (key
.equals(a
.name
)) {
755 boolean accountChanged
= !p
.isChecked();
757 AccountUtils
.setCurrentOwnCloudAccount(
758 getApplicationContext(),
761 if (accountChanged
) {
762 // restart the main activity
763 Intent i
= new Intent(
765 FileDisplayActivity
.class
767 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
768 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
777 return (Boolean
) newValue
;
783 // Add Create Account preference at the end of account list if
784 // Multiaccount is enabled
785 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
786 createAddAccountPreference();
793 * Create the preference for allow adding new accounts
795 private void createAddAccountPreference() {
796 Preference addAccountPref
= new Preference(this);
797 addAccountPref
.setKey("add_account");
798 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
799 mAccountsPrefCategory
.addPreference(addAccountPref
);
801 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
803 public boolean onPreferenceClick(Preference preference
) {
804 AccountManager am
= AccountManager
.get(getApplicationContext());
805 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
814 * Load upload path set on preferences
816 private void loadInstantUploadPath() {
817 SharedPreferences appPrefs
=
818 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
819 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
820 mPrefInstantUploadPath
.setSummary(mUploadPath
);
824 * Save the "Instant Upload Path" on preferences
826 private void saveInstantUploadPathOnPreferences() {
827 SharedPreferences appPrefs
=
828 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
829 SharedPreferences
.Editor editor
= appPrefs
.edit();
830 editor
.putString("instant_upload_path", mUploadPath
);
835 * Load upload video path set on preferences
837 private void loadInstantUploadVideoPath() {
838 SharedPreferences appPrefs
=
839 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
840 mUploadVideoPath
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
));
841 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
845 * Save the "Instant Video Upload Path" on preferences
847 private void saveInstantUploadVideoPathOnPreferences() {
848 SharedPreferences appPrefs
=
849 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
850 SharedPreferences
.Editor editor
= appPrefs
.edit();
851 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
855 // Methods for ComponetsGetter
857 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
858 return mDownloaderBinder
;
863 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
864 return mUploaderBinder
;
868 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
873 public FileDataStorageManager
getStorageManager() {
878 public FileOperationsHelper
getFileOperationsHelper() {
882 protected ServiceConnection
newTransferenceServiceConnection() {
883 return new PreferencesServiceConnection();
886 /** Defines callbacks for service binding, passed to bindService() */
887 private class PreferencesServiceConnection
implements ServiceConnection
{
890 public void onServiceConnected(ComponentName component
, IBinder service
) {
892 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
893 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
895 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
896 Log_OC
.d(TAG
, "Upload service connected");
897 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
905 public void onServiceDisconnected(ComponentName component
) {
906 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
907 Log_OC
.d(TAG
, "Download service suddenly disconnected");
908 mDownloaderBinder
= null
;
909 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
910 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
911 mUploaderBinder
= null
;