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
.Environment
;
39 import android
.os
.Handler
;
40 import android
.os
.IBinder
;
41 import android
.preference
.CheckBoxPreference
;
42 import android
.preference
.Preference
;
43 import android
.preference
.Preference
.OnPreferenceChangeListener
;
44 import android
.preference
.Preference
.OnPreferenceClickListener
;
45 import android
.preference
.PreferenceActivity
;
46 import android
.preference
.PreferenceCategory
;
47 import android
.preference
.PreferenceManager
;
48 import android
.support
.annotation
.LayoutRes
;
49 import android
.support
.annotation
.Nullable
;
50 import android
.support
.v7
.app
.ActionBar
;
51 import android
.support
.v7
.app
.AlertDialog
;
52 import android
.support
.v7
.app
.AppCompatDelegate
;
53 import android
.support
.v7
.widget
.Toolbar
;
54 import android
.view
.ContextMenu
;
55 import android
.view
.ContextMenu
.ContextMenuInfo
;
56 import android
.view
.Menu
;
57 import android
.view
.MenuInflater
;
58 import android
.view
.MenuItem
;
59 import android
.view
.View
;
60 import android
.view
.ViewGroup
;
61 import android
.widget
.AdapterView
;
62 import android
.widget
.AdapterView
.OnItemLongClickListener
;
63 import android
.widget
.ArrayAdapter
;
64 import android
.widget
.ListAdapter
;
65 import android
.widget
.ListView
;
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
.PreferenceWithLongSummary
;
81 import com
.owncloud
.android
.ui
.RadioButtonPreference
;
82 import com
.owncloud
.android
.utils
.DisplayUtils
;
86 * An Activity that allows the user to change the application's settings.
88 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
91 public class Preferences
extends PreferenceActivity
92 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
94 private static final String TAG
= "OwnCloudPreferences";
96 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
97 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
99 private DbHandler mDbHandler
;
100 private CheckBoxPreference pCode
;
101 private Preference pAboutApp
;
102 private AppCompatDelegate mDelegate
;
104 private PreferenceCategory mAccountsPrefCategory
= null
;
105 private final Handler mHandler
= new Handler();
106 private String mAccountName
;
107 private boolean mShowContextMenu
= false
;
108 private String mUploadPath
;
109 private PreferenceCategory mPrefInstantUploadCategory
;
110 private Preference mPrefInstantUpload
;
111 private Preference mPrefInstantUploadPath
;
112 private Preference mPrefInstantUploadPathWiFi
;
113 private Preference mPrefInstantVideoUpload
;
114 private Preference mPrefInstantVideoUploadPath
;
115 private Preference mPrefInstantVideoUploadPathWiFi
;
116 private String mUploadVideoPath
;
118 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
119 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
120 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
121 private PreferenceWithLongSummary mPrefStoragePath
;
122 private String mStoragePath
;
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 enable
= (Boolean
) newValue
;
233 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
234 PassCodeActivity
.ACTION_DISABLE
244 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
246 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
247 Preference pHelp
= findPreference("help");
250 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
252 public boolean onPreferenceClick(Preference preference
) {
253 String helpWeb
=(String
) getText(R
.string
.url_help
);
254 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
255 Uri uriUrl
= Uri
.parse(helpWeb
);
256 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
257 startActivity(intent
);
263 preferenceCategory
.removePreference(pHelp
);
268 if (BuildConfig
.DEBUG
) {
269 Preference pLog
= findPreference("log");
271 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
273 public boolean onPreferenceClick(Preference preference
) {
274 Intent loggerIntent
= new Intent(getApplicationContext(),
275 LogHistoryActivity
.class);
276 startActivity(loggerIntent
);
283 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
284 Preference pRecommend
= findPreference("recommend");
285 if (pRecommend
!= null
){
286 if (recommendEnabled
) {
287 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
289 public boolean onPreferenceClick(Preference preference
) {
291 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
292 intent
.setType("text/plain");
293 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
294 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
296 String appName
= getString(R
.string
.app_name
);
297 String downloadUrl
= getString(R
.string
.url_app_download
);
298 Account currentAccount
= AccountUtils
.
299 getCurrentOwnCloudAccount(Preferences
.this);
300 String username
= currentAccount
.name
.substring(0,
301 currentAccount
.name
.lastIndexOf('@'));
303 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
305 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
306 appName
, downloadUrl
);
308 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
309 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
310 startActivity(intent
);
317 preferenceCategory
.removePreference(pRecommend
);
322 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
323 Preference pFeedback
= findPreference("feedback");
324 if (pFeedback
!= null
){
325 if (feedbackEnabled
) {
326 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
328 public boolean onPreferenceClick(Preference preference
) {
329 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
330 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
331 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
332 intent
.setType("text/plain");
333 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
335 intent
.setData(Uri
.parse(feedbackMail
));
336 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
337 startActivity(intent
);
343 preferenceCategory
.removePreference(pFeedback
);
348 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
349 Preference pImprint
= findPreference("imprint");
350 if (pImprint
!= null
) {
351 if (imprintEnabled
) {
352 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
354 public boolean onPreferenceClick(Preference preference
) {
355 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
356 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
357 Uri uriUrl
= Uri
.parse(imprintWeb
);
358 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
359 startActivity(intent
);
361 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
366 preferenceCategory
.removePreference(pImprint
);
370 mPrefStoragePath
= (PreferenceWithLongSummary
)findPreference("storage_path");
371 if (mPrefStoragePath
!= null
){
373 mPrefStoragePath
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
375 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
376 MainApp
.setStoragePath((String
) newValue
);
381 // mPrefStoragePath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
383 // public boolean onPreferenceClick(Preference preference) {
385 //// if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
386 //// mUploadPath += OCFile.PATH_SEPARATOR;
388 //// Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
389 //// intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
390 //// startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
396 mPrefInstantUploadPath
= findPreference("instant_upload_path");
397 if (mPrefInstantUploadPath
!= null
){
399 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
401 public boolean onPreferenceClick(Preference preference
) {
402 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
403 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
405 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
406 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
407 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
413 mPrefInstantUploadCategory
=
414 (PreferenceCategory
) findPreference("instant_uploading_category");
416 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
417 mPrefInstantUpload
= findPreference("instant_uploading");
419 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
421 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
424 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
425 toggleInstantPictureOptions((Boolean
) newValue
);
430 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
431 if (mPrefInstantVideoUploadPath
!= null
){
433 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
435 public boolean onPreferenceClick(Preference preference
) {
436 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
437 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
439 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
440 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
442 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
448 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
449 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
450 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
452 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
455 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
456 toggleInstantVideoOptions((Boolean
) newValue
);
462 pAboutApp
= (Preference
) findPreference("about_app");
463 if (pAboutApp
!= null
) {
464 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
465 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
468 loadInstantUploadPath();
470 loadInstantUploadVideoPath();
472 /* ComponentsGetter */
473 mDownloadServiceConnection
= newTransferenceServiceConnection();
474 if (mDownloadServiceConnection
!= null
) {
475 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
476 Context
.BIND_AUTO_CREATE
);
478 mUploadServiceConnection
= newTransferenceServiceConnection();
479 if (mUploadServiceConnection
!= null
) {
480 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
481 Context
.BIND_AUTO_CREATE
);
486 private void toggleInstantPictureOptions(Boolean value
){
488 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
489 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
491 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
492 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
496 private void toggleInstantVideoOptions(Boolean value
){
498 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
499 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
501 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
502 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
507 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
509 // Filter for only showing contextual menu when long press on the
511 if (mShowContextMenu
) {
512 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
513 mShowContextMenu
= false
;
515 super.onCreateContextMenu(menu
, v
, menuInfo
);
519 public void run(AccountManagerFuture
<Boolean
> future
) {
520 if (future
.isDone()) {
521 // after remove account
522 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
523 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
525 if (mUploaderBinder
!= null
) {
526 mUploaderBinder
.cancel(account
);
528 if (mDownloaderBinder
!= null
) {
529 mDownloaderBinder
.cancel(account
);
533 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
534 String accountName
= "";
536 Account
[] accounts
= AccountManager
.get(this)
537 .getAccountsByType(MainApp
.getAccountType());
538 if (accounts
.length
!= 0)
539 accountName
= accounts
[0].name
;
540 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
542 addAccountsCheckboxPreferences();
547 protected void onResume() {
549 SharedPreferences appPrefs
=
550 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
551 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
552 pCode
.setChecked(state
);
554 // Populate the accounts category with the list of accounts
555 addAccountsCheckboxPreferences();
559 public boolean onCreateOptionsMenu(Menu menu
) {
560 super.onCreateOptionsMenu(menu
);
565 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
566 super.onMenuItemSelected(featureId
, item
);
569 switch (item
.getItemId()) {
570 case android
.R
.id
.home
:
571 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
572 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
573 startActivity(intent
);
576 Log_OC
.w(TAG
, "Unknown menu item triggered");
583 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
584 super.onActivityResult(requestCode
, resultCode
, data
);
586 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
588 OCFile folderToUpload
=
589 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
591 mUploadPath
= folderToUpload
.getRemotePath();
593 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
595 // Show the path on summary preference
596 mPrefInstantUploadPath
.setSummary(mUploadPath
);
598 saveInstantUploadPathOnPreferences();
600 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
602 OCFile folderToUploadVideo
=
603 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
605 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
607 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
609 // Show the video path on summary preference
610 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
612 saveInstantUploadVideoPathOnPreferences();
616 public ActionBar
getSupportActionBar() {
617 return getDelegate().getSupportActionBar();
620 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
621 getDelegate().setSupportActionBar(toolbar
);
625 public MenuInflater
getMenuInflater() {
626 return getDelegate().getMenuInflater();
630 public void setContentView(@LayoutRes int layoutResID
) {
631 getDelegate().setContentView(layoutResID
);
634 public void setContentView(View view
) {
635 getDelegate().setContentView(view
);
638 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
639 getDelegate().setContentView(view
, params
);
643 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
644 getDelegate().addContentView(view
, params
);
648 protected void onPostResume() {
649 super.onPostResume();
650 getDelegate().onPostResume();
654 protected void onTitleChanged(CharSequence title
, int color
) {
655 super.onTitleChanged(title
, color
);
656 getDelegate().setTitle(title
);
660 public void onConfigurationChanged(Configuration newConfig
) {
661 super.onConfigurationChanged(newConfig
);
662 getDelegate().onConfigurationChanged(newConfig
);
666 protected void onPostCreate(Bundle savedInstanceState
) {
667 super.onPostCreate(savedInstanceState
);
668 getDelegate().onPostCreate(savedInstanceState
);
672 protected void onDestroy() {
675 if (mDownloadServiceConnection
!= null
) {
676 unbindService(mDownloadServiceConnection
);
677 mDownloadServiceConnection
= null
;
679 if (mUploadServiceConnection
!= null
) {
680 unbindService(mUploadServiceConnection
);
681 mUploadServiceConnection
= null
;
685 getDelegate().onDestroy();
689 protected void onStop() {
691 getDelegate().onStop();
694 public void invalidateOptionsMenu() {
695 getDelegate().invalidateOptionsMenu();
698 private AppCompatDelegate
getDelegate() {
699 if (mDelegate
== null
) {
700 mDelegate
= AppCompatDelegate
.create(this, null
);
706 * Create the list of accounts that has been added into the app
708 @SuppressWarnings("deprecation")
709 private void addAccountsCheckboxPreferences() {
711 // Remove accounts in case list is refreshing for avoiding to have
713 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
714 mAccountsPrefCategory
.removeAll();
717 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
718 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
719 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
721 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
722 // Show create account screen if there isn't any account
723 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
729 for (Account a
: accounts
) {
730 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
731 accountPreference
.setKey(a
.name
);
732 // Handle internationalized domain names
733 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
734 mAccountsPrefCategory
.addPreference(accountPreference
);
736 // Check the current account that is being used
737 if (a
.name
.equals(currentAccount
.name
)) {
738 accountPreference
.setChecked(true
);
740 accountPreference
.setChecked(false
);
743 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
745 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
746 String key
= preference
.getKey();
747 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
748 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
749 for (Account a
: accounts
) {
750 RadioButtonPreference p
=
751 (RadioButtonPreference
) findPreference(a
.name
);
752 if (key
.equals(a
.name
)) {
753 boolean accountChanged
= !p
.isChecked();
755 AccountUtils
.setCurrentOwnCloudAccount(
756 getApplicationContext(),
759 if (accountChanged
) {
760 // restart the main activity
761 Intent i
= new Intent(
763 FileDisplayActivity
.class
765 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
766 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
775 return (Boolean
) newValue
;
781 // Add Create Account preference at the end of account list if
782 // Multiaccount is enabled
783 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
784 createAddAccountPreference();
791 * Create the preference for allow adding new accounts
793 private void createAddAccountPreference() {
794 Preference addAccountPref
= new Preference(this);
795 addAccountPref
.setKey("add_account");
796 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
797 mAccountsPrefCategory
.addPreference(addAccountPref
);
799 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
801 public boolean onPreferenceClick(Preference preference
) {
802 AccountManager am
= AccountManager
.get(getApplicationContext());
803 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
812 * Load upload path set on preferences
814 private void loadInstantUploadPath() {
815 SharedPreferences appPrefs
=
816 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
817 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
818 mPrefInstantUploadPath
.setSummary(mUploadPath
);
822 * Load storage path set on preferences
824 private void loadStoragePath() {
825 SharedPreferences appPrefs
=
826 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
827 mStoragePath
= appPrefs
.getString("storage_path", Environment
.getExternalStorageDirectory()
829 mPrefStoragePath
.setSummary(mStoragePath
);
833 * Save the "Instant Upload Path" on preferences
835 private void saveInstantUploadPathOnPreferences() {
836 SharedPreferences appPrefs
=
837 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
838 SharedPreferences
.Editor editor
= appPrefs
.edit();
839 editor
.putString("instant_upload_path", mUploadPath
);
844 * Load upload video path set on preferences
846 private void loadInstantUploadVideoPath() {
847 mPrefInstantVideoUploadPath
.setSummary(MainApp
.getStoragePath());
851 * Save the "Instant Video Upload Path" on preferences
853 private void saveInstantUploadVideoPathOnPreferences() {
854 SharedPreferences appPrefs
=
855 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
856 SharedPreferences
.Editor editor
= appPrefs
.edit();
857 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
861 // Methods for ComponetsGetter
863 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
864 return mDownloaderBinder
;
869 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
870 return mUploaderBinder
;
874 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
879 public FileDataStorageManager
getStorageManager() {
884 public FileOperationsHelper
getFileOperationsHelper() {
888 protected ServiceConnection
newTransferenceServiceConnection() {
889 return new PreferencesServiceConnection();
892 /** Defines callbacks for service binding, passed to bindService() */
893 private class PreferencesServiceConnection
implements ServiceConnection
{
896 public void onServiceConnected(ComponentName component
, IBinder service
) {
898 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
899 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
901 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
902 Log_OC
.d(TAG
, "Upload service connected");
903 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
911 public void onServiceDisconnected(ComponentName component
) {
912 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
913 Log_OC
.d(TAG
, "Download service suddenly disconnected");
914 mDownloaderBinder
= null
;
915 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
916 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
917 mUploaderBinder
= null
;