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
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
335 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
336 intent
.setType("text/plain");
337 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
339 intent
.setData(Uri
.parse(feedbackMail
));
340 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
341 startActivity(intent
);
347 preferenceCategory
.removePreference(pFeedback
);
352 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
353 Preference pImprint
= findPreference("imprint");
354 if (pImprint
!= null
) {
355 if (imprintEnabled
) {
356 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
358 public boolean onPreferenceClick(Preference preference
) {
359 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
360 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
361 Uri uriUrl
= Uri
.parse(imprintWeb
);
362 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
363 startActivity(intent
);
365 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
370 preferenceCategory
.removePreference(pImprint
);
374 mPrefStoragePath
= findPreference("storage_path");
375 if (mPrefStoragePath
!= null
) {
377 mPrefStoragePath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
379 public boolean onPreferenceClick(Preference preference
) {
380 Intent intent
= new Intent(Preferences
.this, LocalDirectorySelectorActivity
.class);
381 intent
.putExtra(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mStoragePath
);
382 startActivityForResult(intent
, ACTION_SELECT_STORAGE_PATH
);
387 mPrefStoragePath
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
389 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
390 MainApp
.setStoragePath((String
) newValue
);
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();
613 } else if (requestCode
== ACTION_SELECT_STORAGE_PATH
&& resultCode
== RESULT_OK
) {
614 File currentStorageDir
= new File(mStoragePath
);
615 File upcomingStorageDir
= new File(data
.getStringExtra(UploadFilesActivity
.EXTRA_CHOSEN_FILES
));
617 if (currentStorageDir
!= upcomingStorageDir
) {
618 Intent migrationIntent
= new Intent(this, StorageMigrationActivity
.class);
619 migrationIntent
.putExtra(StorageMigrationActivity
.KEY_MIGRATION_SOURCE_DIR
,
620 currentStorageDir
.getAbsolutePath());
621 migrationIntent
.putExtra(StorageMigrationActivity
.KEY_MIGRATION_TARGET_DIR
,
622 upcomingStorageDir
.getAbsolutePath());
623 startActivityForResult(migrationIntent
, ACTION_PERFORM_MIGRATION
);
625 } else if (requestCode
== ACTION_PERFORM_MIGRATION
&& resultCode
== RESULT_OK
) {
626 String resultStorageDir
= data
.getStringExtra(StorageMigrationActivity
.KEY_MIGRATION_TARGET_DIR
);
627 saveStoragePath(resultStorageDir
);
631 public ActionBar
getSupportActionBar() {
632 return getDelegate().getSupportActionBar();
635 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
636 getDelegate().setSupportActionBar(toolbar
);
640 public MenuInflater
getMenuInflater() {
641 return getDelegate().getMenuInflater();
645 public void setContentView(@LayoutRes int layoutResID
) {
646 getDelegate().setContentView(layoutResID
);
649 public void setContentView(View view
) {
650 getDelegate().setContentView(view
);
653 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
654 getDelegate().setContentView(view
, params
);
658 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
659 getDelegate().addContentView(view
, params
);
663 protected void onPostResume() {
664 super.onPostResume();
665 getDelegate().onPostResume();
669 protected void onTitleChanged(CharSequence title
, int color
) {
670 super.onTitleChanged(title
, color
);
671 getDelegate().setTitle(title
);
675 public void onConfigurationChanged(Configuration newConfig
) {
676 super.onConfigurationChanged(newConfig
);
677 getDelegate().onConfigurationChanged(newConfig
);
681 protected void onPostCreate(Bundle savedInstanceState
) {
682 super.onPostCreate(savedInstanceState
);
683 getDelegate().onPostCreate(savedInstanceState
);
687 protected void onDestroy() {
690 if (mDownloadServiceConnection
!= null
) {
691 unbindService(mDownloadServiceConnection
);
692 mDownloadServiceConnection
= null
;
694 if (mUploadServiceConnection
!= null
) {
695 unbindService(mUploadServiceConnection
);
696 mUploadServiceConnection
= null
;
700 getDelegate().onDestroy();
704 protected void onStop() {
706 getDelegate().onStop();
709 public void invalidateOptionsMenu() {
710 getDelegate().invalidateOptionsMenu();
713 private AppCompatDelegate
getDelegate() {
714 if (mDelegate
== null
) {
715 mDelegate
= AppCompatDelegate
.create(this, null
);
721 * Create the list of accounts that has been added into the app
723 @SuppressWarnings("deprecation")
724 private void addAccountsCheckboxPreferences() {
726 // Remove accounts in case list is refreshing for avoiding to have
728 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
729 mAccountsPrefCategory
.removeAll();
732 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
733 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
734 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
736 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
737 // Show create account screen if there isn't any account
738 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
744 for (Account a
: accounts
) {
745 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
746 accountPreference
.setKey(a
.name
);
747 // Handle internationalized domain names
748 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
749 mAccountsPrefCategory
.addPreference(accountPreference
);
751 // Check the current account that is being used
752 if (a
.name
.equals(currentAccount
.name
)) {
753 accountPreference
.setChecked(true
);
755 accountPreference
.setChecked(false
);
758 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
760 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
761 String key
= preference
.getKey();
762 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
763 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
764 for (Account a
: accounts
) {
765 RadioButtonPreference p
=
766 (RadioButtonPreference
) findPreference(a
.name
);
767 if (key
.equals(a
.name
)) {
768 boolean accountChanged
= !p
.isChecked();
770 AccountUtils
.setCurrentOwnCloudAccount(
771 getApplicationContext(),
774 if (accountChanged
) {
775 // restart the main activity
776 Intent i
= new Intent(
778 FileDisplayActivity
.class
780 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
781 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
790 return (Boolean
) newValue
;
796 // Add Create Account preference at the end of account list if
797 // Multiaccount is enabled
798 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
799 createAddAccountPreference();
806 * Create the preference for allow adding new accounts
808 private void createAddAccountPreference() {
809 Preference addAccountPref
= new Preference(this);
810 addAccountPref
.setKey("add_account");
811 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
812 mAccountsPrefCategory
.addPreference(addAccountPref
);
814 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
816 public boolean onPreferenceClick(Preference preference
) {
817 AccountManager am
= AccountManager
.get(getApplicationContext());
818 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
827 * Load upload path set on preferences
829 private void loadInstantUploadPath() {
830 SharedPreferences appPrefs
=
831 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
832 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
833 mPrefInstantUploadPath
.setSummary(mUploadPath
);
839 private void saveStoragePath(String newStoragePath
) {
840 SharedPreferences appPrefs
=
841 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
842 mStoragePath
= newStoragePath
;
843 MainApp
.setStoragePath(mStoragePath
);
844 SharedPreferences
.Editor editor
= appPrefs
.edit();
845 editor
.putString("storage_path", mStoragePath
);
847 mPrefStoragePath
.setSummary(mStoragePath
);
851 * Load storage path set on preferences
853 private void loadStoragePath() {
854 SharedPreferences appPrefs
=
855 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
856 mStoragePath
= appPrefs
.getString("storage_path", Environment
.getExternalStorageDirectory()
858 mPrefStoragePath
.setSummary(mStoragePath
);
862 * Save the "Instant Upload Path" on preferences
864 private void saveInstantUploadPathOnPreferences() {
865 SharedPreferences appPrefs
=
866 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
867 SharedPreferences
.Editor editor
= appPrefs
.edit();
868 editor
.putString("instant_upload_path", mUploadPath
);
873 * Load upload video path set on preferences
875 private void loadInstantUploadVideoPath() {
876 mPrefInstantVideoUploadPath
.setSummary(MainApp
.getStoragePath());
880 * Save the "Instant Video Upload Path" on preferences
882 private void saveInstantUploadVideoPathOnPreferences() {
883 SharedPreferences appPrefs
=
884 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
885 SharedPreferences
.Editor editor
= appPrefs
.edit();
886 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
890 // Methods for ComponetsGetter
892 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
893 return mDownloaderBinder
;
898 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
899 return mUploaderBinder
;
903 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
908 public FileDataStorageManager
getStorageManager() {
913 public FileOperationsHelper
getFileOperationsHelper() {
917 protected ServiceConnection
newTransferenceServiceConnection() {
918 return new PreferencesServiceConnection();
921 /** Defines callbacks for service binding, passed to bindService() */
922 private class PreferencesServiceConnection
implements ServiceConnection
{
925 public void onServiceConnected(ComponentName component
, IBinder service
) {
927 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
928 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
930 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
931 Log_OC
.d(TAG
, "Upload service connected");
932 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
940 public void onServiceDisconnected(ComponentName component
) {
941 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
942 Log_OC
.d(TAG
, "Download service suddenly disconnected");
943 mDownloaderBinder
= null
;
944 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
945 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
946 mUploaderBinder
= null
;