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
;
88 * An Activity that allows the user to change the application's settings.
90 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
93 public class Preferences
extends PreferenceActivity
94 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
96 private static final String TAG
= "OwnCloudPreferences";
98 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
99 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
100 private static final int ACTION_SELECT_STORAGE_PATH
= 3;
101 private static final int ACTION_PERFORM_MIGRATION
= 4;
103 private DbHandler mDbHandler
;
104 private CheckBoxPreference pCode
;
105 private Preference pAboutApp
;
106 private AppCompatDelegate mDelegate
;
108 private PreferenceCategory mAccountsPrefCategory
= null
;
109 private final Handler mHandler
= new Handler();
110 private String mAccountName
;
111 private boolean mShowContextMenu
= false
;
112 private String mUploadPath
;
113 private PreferenceCategory mPrefInstantUploadCategory
;
114 private Preference mPrefInstantUpload
;
115 private Preference mPrefInstantUploadPath
;
116 private Preference mPrefInstantUploadPathWiFi
;
117 private Preference mPrefInstantVideoUpload
;
118 private Preference mPrefInstantVideoUploadPath
;
119 private Preference mPrefInstantVideoUploadPathWiFi
;
120 private String mUploadVideoPath
;
122 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
123 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
124 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
125 private PreferenceWithLongSummary mPrefStoragePath
;
126 private String mStoragePath
;
128 @SuppressWarnings("deprecation")
130 public void onCreate(Bundle savedInstanceState
) {
131 getDelegate().installViewFactory();
132 getDelegate().onCreate(savedInstanceState
);
133 super.onCreate(savedInstanceState
);
134 mDbHandler
= new DbHandler(getBaseContext());
135 addPreferencesFromResource(R
.xml
.preferences
);
137 ActionBar actionBar
= getSupportActionBar();
138 actionBar
.setDisplayHomeAsUpEnabled(true
);
139 actionBar
.setTitle(R
.string
.actionbar_settings
);
141 // For adding content description tag to a title field in the action bar
142 int actionBarTitleId
= getResources().getIdentifier("action_bar_title", "id", "android");
143 View actionBarTitleView
= getWindow().getDecorView().findViewById(actionBarTitleId
);
144 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
145 getWindow().getDecorView().findViewById(actionBarTitleId
).
146 setContentDescription(getString(R
.string
.actionbar_settings
));
149 // Load the accounts category for adding the list of accounts
150 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
152 ListView listView
= getListView();
153 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
155 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
156 ListView listView
= (ListView
) parent
;
157 ListAdapter listAdapter
= listView
.getAdapter();
158 Object obj
= listAdapter
.getItem(position
);
160 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
161 mShowContextMenu
= true
;
162 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
165 getResources().getString(R
.string
.change_password
),
166 getResources().getString(R
.string
.delete_account
)
168 final AlertDialog
.Builder alertDialogBuilder
= new AlertDialog
.Builder(Preferences
.this);
169 View convertView
= getLayoutInflater().inflate(R
.layout
.alert_dialog_list_view
, null
);
170 alertDialogBuilder
.setView(convertView
);
171 ListView lv
= (ListView
) convertView
.findViewById(R
.id
.list
);
172 ArrayAdapter
<String
> adapter
= new ArrayAdapter
<String
>(
173 Preferences
.this,R
.layout
.simple_dialog_list_item
,items
);
174 lv
.setAdapter(adapter
);
176 //Setup proper inline listener
177 final AlertDialog alertDialog
= alertDialogBuilder
.create();
178 lv
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
180 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
181 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
182 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
183 for (Account a
: accounts
) {
184 if (a
.name
.equals(mAccountName
)) {
187 // Change account password
188 Intent updateAccountCredentials
= new Intent(Preferences
.this, AuthenticatorActivity
.class);
189 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
190 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
191 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
192 startActivity(updateAccountCredentials
);
193 alertDialog
.cancel();
195 } else if (position
==1) {
198 am
.removeAccount(a
, Preferences
.this, mHandler
);
199 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
200 alertDialog
.cancel();
208 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
209 return longListener
.onLongClick(view
);
218 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
219 temp
= pkg
.versionName
;
220 } catch (NameNotFoundException e
) {
222 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
224 final String appVersion
= temp
;
226 // Register context menu for list of preferences.
227 registerForContextMenu(getListView());
229 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
231 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
233 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
234 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
235 Boolean enable
= (Boolean
) newValue
;
237 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
238 PassCodeActivity
.ACTION_DISABLE
248 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
250 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
251 Preference pHelp
= findPreference("help");
254 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
256 public boolean onPreferenceClick(Preference preference
) {
257 String helpWeb
=(String
) getText(R
.string
.url_help
);
258 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
259 Uri uriUrl
= Uri
.parse(helpWeb
);
260 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
261 startActivity(intent
);
267 preferenceCategory
.removePreference(pHelp
);
272 if (BuildConfig
.DEBUG
) {
273 Preference pLog
= findPreference("log");
275 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
277 public boolean onPreferenceClick(Preference preference
) {
278 Intent loggerIntent
= new Intent(getApplicationContext(),
279 LogHistoryActivity
.class);
280 startActivity(loggerIntent
);
287 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
288 Preference pRecommend
= findPreference("recommend");
289 if (pRecommend
!= null
){
290 if (recommendEnabled
) {
291 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
293 public boolean onPreferenceClick(Preference preference
) {
295 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
296 intent
.setType("text/plain");
297 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
298 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
300 String appName
= getString(R
.string
.app_name
);
301 String downloadUrl
= getString(R
.string
.url_app_download
);
302 Account currentAccount
= AccountUtils
.
303 getCurrentOwnCloudAccount(Preferences
.this);
304 String username
= currentAccount
.name
.substring(0,
305 currentAccount
.name
.lastIndexOf('@'));
307 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
309 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
310 appName
, downloadUrl
);
312 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
313 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
314 startActivity(intent
);
321 preferenceCategory
.removePreference(pRecommend
);
326 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
327 Preference pFeedback
= findPreference("feedback");
328 if (pFeedback
!= null
){
329 if (feedbackEnabled
) {
330 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
332 public boolean onPreferenceClick(Preference preference
) {
333 String feedbackMail
= (String
) getText(R
.string
.mail_feedback
);
334 String feedback
= String
.format("%s - android v%s", getText(R
.string
.prefs_feedback
), appVersion
);
335 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
337 intent
.setType("text/plain");
338 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
340 intent
.setData(Uri
.parse(feedbackMail
));
341 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
342 startActivity(intent
);
348 preferenceCategory
.removePreference(pFeedback
);
353 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
354 Preference pImprint
= findPreference("imprint");
355 if (pImprint
!= null
) {
356 if (imprintEnabled
) {
357 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
359 public boolean onPreferenceClick(Preference preference
) {
360 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
361 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
362 Uri uriUrl
= Uri
.parse(imprintWeb
);
363 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
364 startActivity(intent
);
366 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
371 preferenceCategory
.removePreference(pImprint
);
375 mPrefStoragePath
= (PreferenceWithLongSummary
)findPreference("storage_path");
376 if (mPrefStoragePath
!= null
) {
378 mPrefStoragePath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
380 public boolean onPreferenceClick(Preference preference
) {
381 Intent intent
= new Intent(Preferences
.this, LocalDirectorySelectorActivity
.class);
382 intent
.putExtra(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mStoragePath
);
383 startActivityForResult(intent
, ACTION_SELECT_STORAGE_PATH
);
388 mPrefStoragePath
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
390 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
391 MainApp
.setStoragePath((String
) newValue
);
397 mPrefInstantUploadPath
= (PreferenceWithLongSummary
)findPreference("instant_upload_path");
398 if (mPrefInstantUploadPath
!= null
){
400 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
402 public boolean onPreferenceClick(Preference preference
) {
403 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
404 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
406 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
407 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
408 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
414 mPrefInstantUploadCategory
=
415 (PreferenceCategory
) findPreference("instant_uploading_category");
417 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
418 mPrefInstantUpload
= findPreference("instant_uploading");
420 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
422 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
425 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
426 toggleInstantPictureOptions((Boolean
) newValue
);
431 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
432 if (mPrefInstantVideoUploadPath
!= null
){
434 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
436 public boolean onPreferenceClick(Preference preference
) {
437 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
438 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
440 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
441 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
,
443 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
449 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
450 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
451 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantVideoUpload
).isChecked());
453 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
456 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
457 toggleInstantVideoOptions((Boolean
) newValue
);
463 pAboutApp
= findPreference("about_app");
464 if (pAboutApp
!= null
) {
465 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
466 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
469 loadInstantUploadPath();
471 loadInstantUploadVideoPath();
473 /* ComponentsGetter */
474 mDownloadServiceConnection
= newTransferenceServiceConnection();
475 if (mDownloadServiceConnection
!= null
) {
476 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
477 Context
.BIND_AUTO_CREATE
);
479 mUploadServiceConnection
= newTransferenceServiceConnection();
480 if (mUploadServiceConnection
!= null
) {
481 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
482 Context
.BIND_AUTO_CREATE
);
487 private void toggleInstantPictureOptions(Boolean value
){
489 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
490 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
492 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
493 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
497 private void toggleInstantVideoOptions(Boolean value
){
499 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
500 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
502 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
503 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
508 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
510 // Filter for only showing contextual menu when long press on the
512 if (mShowContextMenu
) {
513 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
514 mShowContextMenu
= false
;
516 super.onCreateContextMenu(menu
, v
, menuInfo
);
520 public void run(AccountManagerFuture
<Boolean
> future
) {
521 if (future
.isDone()) {
522 // after remove account
523 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
524 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
526 if (mUploaderBinder
!= null
) {
527 mUploaderBinder
.cancel(account
);
529 if (mDownloaderBinder
!= null
) {
530 mDownloaderBinder
.cancel(account
);
534 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
535 String accountName
= "";
537 Account
[] accounts
= AccountManager
.get(this)
538 .getAccountsByType(MainApp
.getAccountType());
539 if (accounts
.length
!= 0)
540 accountName
= accounts
[0].name
;
541 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
543 addAccountsCheckboxPreferences();
548 protected void onResume() {
550 SharedPreferences appPrefs
=
551 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
552 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
553 pCode
.setChecked(state
);
555 // Populate the accounts category with the list of accounts
556 addAccountsCheckboxPreferences();
560 public boolean onCreateOptionsMenu(Menu menu
) {
561 super.onCreateOptionsMenu(menu
);
566 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
567 super.onMenuItemSelected(featureId
, item
);
570 switch (item
.getItemId()) {
571 case android
.R
.id
.home
:
572 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
573 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
574 startActivity(intent
);
577 Log_OC
.w(TAG
, "Unknown menu item triggered");
584 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
585 super.onActivityResult(requestCode
, resultCode
, data
);
587 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
589 OCFile folderToUpload
= 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
= data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
604 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
606 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
608 // Show the video path on summary preference
609 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
611 saveInstantUploadVideoPathOnPreferences();
612 } else if (requestCode
== ACTION_SELECT_STORAGE_PATH
&& resultCode
== RESULT_OK
) {
613 File currentStorageDir
= new File(mStoragePath
);
614 File upcomingStorageDir
= new File(data
.getStringExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
));
616 if (currentStorageDir
!= upcomingStorageDir
) {
617 Intent migrationIntent
= new Intent(this, StorageMigrationActivity
.class);
618 migrationIntent
.putExtra(StorageMigrationActivity
.KEY_MIGRATION_SOURCE_DIR
,
619 currentStorageDir
.getAbsolutePath());
620 migrationIntent
.putExtra(StorageMigrationActivity
.KEY_MIGRATION_TARGET_DIR
,
621 upcomingStorageDir
.getAbsolutePath());
622 startActivityForResult(migrationIntent
, ACTION_PERFORM_MIGRATION
);
624 } else if (requestCode
== ACTION_PERFORM_MIGRATION
&& resultCode
== RESULT_OK
) {
625 String resultStorageDir
= data
.getStringExtra(StorageMigrationActivity
.KEY_MIGRATION_TARGET_DIR
);
626 saveStoragePath(resultStorageDir
);
630 public ActionBar
getSupportActionBar() {
631 return getDelegate().getSupportActionBar();
634 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
635 getDelegate().setSupportActionBar(toolbar
);
639 public MenuInflater
getMenuInflater() {
640 return getDelegate().getMenuInflater();
644 public void setContentView(@LayoutRes int layoutResID
) {
645 getDelegate().setContentView(layoutResID
);
648 public void setContentView(View view
) {
649 getDelegate().setContentView(view
);
652 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
653 getDelegate().setContentView(view
, params
);
657 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
658 getDelegate().addContentView(view
, params
);
662 protected void onPostResume() {
663 super.onPostResume();
664 getDelegate().onPostResume();
668 protected void onTitleChanged(CharSequence title
, int color
) {
669 super.onTitleChanged(title
, color
);
670 getDelegate().setTitle(title
);
674 public void onConfigurationChanged(Configuration newConfig
) {
675 super.onConfigurationChanged(newConfig
);
676 getDelegate().onConfigurationChanged(newConfig
);
680 protected void onPostCreate(Bundle savedInstanceState
) {
681 super.onPostCreate(savedInstanceState
);
682 getDelegate().onPostCreate(savedInstanceState
);
686 protected void onDestroy() {
689 if (mDownloadServiceConnection
!= null
) {
690 unbindService(mDownloadServiceConnection
);
691 mDownloadServiceConnection
= null
;
693 if (mUploadServiceConnection
!= null
) {
694 unbindService(mUploadServiceConnection
);
695 mUploadServiceConnection
= null
;
699 getDelegate().onDestroy();
703 protected void onStop() {
705 getDelegate().onStop();
708 public void invalidateOptionsMenu() {
709 getDelegate().invalidateOptionsMenu();
712 private AppCompatDelegate
getDelegate() {
713 if (mDelegate
== null
) {
714 mDelegate
= AppCompatDelegate
.create(this, null
);
720 * Create the list of accounts that has been added into the app
722 @SuppressWarnings("deprecation")
723 private void addAccountsCheckboxPreferences() {
725 // Remove accounts in case list is refreshing for avoiding to have
727 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
728 mAccountsPrefCategory
.removeAll();
731 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
732 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
733 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
735 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
736 // Show create account screen if there isn't any account
737 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
743 for (Account a
: accounts
) {
744 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
745 accountPreference
.setKey(a
.name
);
746 // Handle internationalized domain names
747 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
748 mAccountsPrefCategory
.addPreference(accountPreference
);
750 // Check the current account that is being used
751 if (a
.name
.equals(currentAccount
.name
)) {
752 accountPreference
.setChecked(true
);
754 accountPreference
.setChecked(false
);
757 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
759 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
760 String key
= preference
.getKey();
761 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
762 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
763 for (Account a
: accounts
) {
764 RadioButtonPreference p
=
765 (RadioButtonPreference
) findPreference(a
.name
);
766 if (key
.equals(a
.name
)) {
767 boolean accountChanged
= !p
.isChecked();
769 AccountUtils
.setCurrentOwnCloudAccount(
770 getApplicationContext(),
773 if (accountChanged
) {
774 // restart the main activity
775 Intent i
= new Intent(
777 FileDisplayActivity
.class
779 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
780 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
789 return (Boolean
) newValue
;
795 // Add Create Account preference at the end of account list if
796 // Multiaccount is enabled
797 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
798 createAddAccountPreference();
805 * Create the preference for allow adding new accounts
807 private void createAddAccountPreference() {
808 Preference addAccountPref
= new Preference(this);
809 addAccountPref
.setKey("add_account");
810 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
811 mAccountsPrefCategory
.addPreference(addAccountPref
);
813 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
815 public boolean onPreferenceClick(Preference preference
) {
816 AccountManager am
= AccountManager
.get(getApplicationContext());
817 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
826 * Load upload path set on preferences
828 private void loadInstantUploadPath() {
829 SharedPreferences appPrefs
=
830 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
831 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
832 mPrefInstantUploadPath
.setSummary(mUploadPath
);
838 private void saveStoragePath(String newStoragePath
) {
839 SharedPreferences appPrefs
=
840 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
841 mStoragePath
= newStoragePath
;
842 MainApp
.setStoragePath(mStoragePath
);
843 SharedPreferences
.Editor editor
= appPrefs
.edit();
844 editor
.putString("storage_path", mStoragePath
);
846 mPrefStoragePath
.setSummary(mStoragePath
);
850 * Load storage path set on preferences
852 private void loadStoragePath() {
853 SharedPreferences appPrefs
=
854 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
855 mStoragePath
= appPrefs
.getString("storage_path", Environment
.getExternalStorageDirectory()
857 mPrefStoragePath
.setSummary(mStoragePath
);
861 * Save the "Instant Upload Path" on preferences
863 private void saveInstantUploadPathOnPreferences() {
864 SharedPreferences appPrefs
=
865 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
866 SharedPreferences
.Editor editor
= appPrefs
.edit();
867 editor
.putString("instant_upload_path", mUploadPath
);
872 * Load upload video path set on preferences
874 private void loadInstantUploadVideoPath() {
875 mPrefInstantVideoUploadPath
.setSummary(MainApp
.getStoragePath());
879 * Save the "Instant Video Upload Path" on preferences
881 private void saveInstantUploadVideoPathOnPreferences() {
882 SharedPreferences appPrefs
=
883 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
884 SharedPreferences
.Editor editor
= appPrefs
.edit();
885 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
889 // Methods for ComponentsGetter
891 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
892 return mDownloaderBinder
;
897 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
898 return mUploaderBinder
;
902 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
907 public FileDataStorageManager
getStorageManager() {
912 public FileOperationsHelper
getFileOperationsHelper() {
916 protected ServiceConnection
newTransferenceServiceConnection() {
917 return new PreferencesServiceConnection();
920 /** Defines callbacks for service binding, passed to bindService() */
921 private class PreferencesServiceConnection
implements ServiceConnection
{
924 public void onServiceConnected(ComponentName component
, IBinder service
) {
926 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
927 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
928 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
929 Log_OC
.d(TAG
, "Upload service connected");
930 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
935 public void onServiceDisconnected(ComponentName component
) {
936 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
937 Log_OC
.d(TAG
, "Download service suddenly disconnected");
938 mDownloaderBinder
= null
;
939 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
940 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
941 mUploaderBinder
= null
;