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
.AsyncTask
;
38 import android
.os
.Bundle
;
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
;
66 import android
.widget
.Toast
;
68 import com
.owncloud
.android
.BuildConfig
;
69 import com
.owncloud
.android
.MainApp
;
70 import com
.owncloud
.android
.R
;
71 import com
.owncloud
.android
.authentication
.AccountUtils
;
72 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
73 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
74 import com
.owncloud
.android
.datamodel
.OCFile
;
75 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
76 import com
.owncloud
.android
.db
.DbHandler
;
77 import com
.owncloud
.android
.files
.FileOperationsHelper
;
78 import com
.owncloud
.android
.files
.services
.FileDownloader
;
79 import com
.owncloud
.android
.files
.services
.FileUploader
;
80 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
81 import com
.owncloud
.android
.services
.OperationsService
;
82 import com
.owncloud
.android
.ui
.RadioButtonPreference
;
83 import com
.owncloud
.android
.utils
.DisplayUtils
;
85 import java
.io
.BufferedReader
;
86 import java
.io
.IOException
;
87 import java
.io
.InputStreamReader
;
88 import java
.net
.MalformedURLException
;
90 import java
.util
.concurrent
.ExecutionException
;
94 * An Activity that allows the user to change the application's settings.
96 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
99 public class Preferences
extends PreferenceActivity
100 implements AccountManagerCallback
<Boolean
>, ComponentsGetter
{
102 private static final String TAG
= "OwnCloudPreferences";
104 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
105 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
107 private DbHandler mDbHandler
;
108 private CheckBoxPreference pCode
;
109 private Preference pAboutApp
;
110 private AppCompatDelegate mDelegate
;
112 private PreferenceCategory mAccountsPrefCategory
= null
;
113 private final Handler mHandler
= new Handler();
114 private String mAccountName
;
115 private boolean mShowContextMenu
= false
;
116 private String mUploadPath
;
117 private PreferenceCategory mPrefInstantUploadCategory
;
118 private Preference mPrefInstantUpload
;
119 private Preference mPrefInstantUploadPath
;
120 private Preference mPrefInstantUploadPathWiFi
;
121 private Preference mPrefInstantVideoUpload
;
122 private Preference mPrefInstantVideoUploadPath
;
123 private Preference mPrefInstantVideoUploadPathWiFi
;
124 private String mUploadVideoPath
;
126 protected FileDownloader
.FileDownloaderBinder mDownloaderBinder
= null
;
127 protected FileUploader
.FileUploaderBinder mUploaderBinder
= null
;
128 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
130 @SuppressWarnings("deprecation")
132 public void onCreate(Bundle savedInstanceState
) {
133 getDelegate().installViewFactory();
134 getDelegate().onCreate(savedInstanceState
);
135 super.onCreate(savedInstanceState
);
136 mDbHandler
= new DbHandler(getBaseContext());
137 addPreferencesFromResource(R
.xml
.preferences
);
139 ActionBar actionBar
= getSupportActionBar();
140 actionBar
.setDisplayHomeAsUpEnabled(true
);
141 actionBar
.setTitle(R
.string
.actionbar_settings
);
143 // For adding content description tag to a title field in the action bar
144 int actionBarTitleId
= getResources().getIdentifier("action_bar_title", "id", "android");
145 View actionBarTitleView
= getWindow().getDecorView().findViewById(actionBarTitleId
);
146 if (actionBarTitleView
!= null
) { // it's null in Android 2.x
147 getWindow().getDecorView().findViewById(actionBarTitleId
).
148 setContentDescription(getString(R
.string
.actionbar_settings
));
151 // Load the accounts category for adding the list of accounts
152 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
154 ListView listView
= getListView();
155 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
157 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
158 ListView listView
= (ListView
) parent
;
159 ListAdapter listAdapter
= listView
.getAdapter();
160 Object obj
= listAdapter
.getItem(position
);
162 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
163 mShowContextMenu
= true
;
164 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
167 getResources().getString(R
.string
.change_password
),
168 getResources().getString(R
.string
.delete_account
)
170 final AlertDialog
.Builder alertDialogBuilder
= new AlertDialog
.Builder(Preferences
.this);
171 View convertView
= getLayoutInflater().inflate(R
.layout
.alert_dialog_list_view
, null
);
172 alertDialogBuilder
.setView(convertView
);
173 ListView lv
= (ListView
) convertView
.findViewById(R
.id
.list
);
174 ArrayAdapter
<String
> adapter
= new ArrayAdapter
<String
>(
175 Preferences
.this,R
.layout
.simple_dialog_list_item
,items
);
176 lv
.setAdapter(adapter
);
178 //Setup proper inline listener
179 final AlertDialog alertDialog
= alertDialogBuilder
.create();
180 lv
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
182 public void onItemClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
183 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
184 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
185 for (Account a
: accounts
) {
186 if (a
.name
.equals(mAccountName
)) {
189 // Change account password
190 Intent updateAccountCredentials
= new Intent(Preferences
.this, AuthenticatorActivity
.class);
191 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
192 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
193 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
194 startActivity(updateAccountCredentials
);
195 alertDialog
.cancel();
197 } else if (position
==1) {
200 am
.removeAccount(a
, Preferences
.this, mHandler
);
201 Log_OC
.d(TAG
, "Remove an account " + a
.name
);
202 alertDialog
.cancel();
210 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
211 return longListener
.onLongClick(view
);
220 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
221 temp
= pkg
.versionName
;
222 } catch (NameNotFoundException e
) {
224 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
226 final String appVersion
= temp
;
228 // Register context menu for list of preferences.
229 registerForContextMenu(getListView());
231 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
233 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
235 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
236 Intent i
= new Intent(getApplicationContext(), PassCodeActivity
.class);
237 Boolean enable
= (Boolean
) newValue
;
239 enable
.booleanValue() ? PassCodeActivity
.ACTION_ENABLE
:
240 PassCodeActivity
.ACTION_DISABLE
250 final Preference pCacheSize
= findPreference("pref_cache_size");
251 if (pCacheSize
!= null
){
252 final SharedPreferences appPrefs
=
253 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
254 Long cacheSize
= ThumbnailsCacheManager
.getMaxSize();
255 pCacheSize
.setSummary(cacheSize
+ " Mb");
256 pCacheSize
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
258 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
259 Long size
= Long
.decode((String
) newValue
);
260 if (ThumbnailsCacheManager
.setMaxSize(size
)){
261 appPrefs
.edit().putString("pref_cache_size", size
.toString());
262 pCacheSize
.setSummary(size
+ " MB");
271 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
273 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
274 Preference pHelp
= findPreference("help");
277 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
279 public boolean onPreferenceClick(Preference preference
) {
280 String helpWeb
=(String
) getText(R
.string
.url_help
);
281 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
282 Uri uriUrl
= Uri
.parse(helpWeb
);
283 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
284 startActivity(intent
);
290 preferenceCategory
.removePreference(pHelp
);
295 if (BuildConfig
.DEBUG
) {
296 Preference pLog
= findPreference("log");
298 pLog
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
300 public boolean onPreferenceClick(Preference preference
) {
301 Intent loggerIntent
= new Intent(getApplicationContext(),
302 LogHistoryActivity
.class);
303 startActivity(loggerIntent
);
310 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
311 Preference pRecommend
= findPreference("recommend");
312 if (pRecommend
!= null
){
313 if (recommendEnabled
) {
314 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
316 public boolean onPreferenceClick(Preference preference
) {
318 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
319 intent
.setType("text/plain");
320 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
321 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
323 String appName
= getString(R
.string
.app_name
);
324 String downloadUrl
= getString(R
.string
.url_app_download
);
325 Account currentAccount
= AccountUtils
.
326 getCurrentOwnCloudAccount(Preferences
.this);
327 String username
= currentAccount
.name
.substring(0,
328 currentAccount
.name
.lastIndexOf('@'));
330 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
),
332 String recommendText
= String
.format(getString(R
.string
.recommend_text
),
333 appName
, downloadUrl
);
335 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
336 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
337 startActivity(intent
);
344 preferenceCategory
.removePreference(pRecommend
);
349 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
350 Preference pFeedback
= findPreference("feedback");
351 if (pFeedback
!= null
){
352 if (feedbackEnabled
) {
353 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
355 public boolean onPreferenceClick(Preference preference
) {
356 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
357 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
358 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
359 intent
.setType("text/plain");
360 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
362 intent
.setData(Uri
.parse(feedbackMail
));
363 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
364 startActivity(intent
);
370 preferenceCategory
.removePreference(pFeedback
);
375 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
376 Preference pImprint
= findPreference("imprint");
377 if (pImprint
!= null
) {
378 if (imprintEnabled
) {
379 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
381 public boolean onPreferenceClick(Preference preference
) {
382 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
383 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
384 Uri uriUrl
= Uri
.parse(imprintWeb
);
385 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
386 startActivity(intent
);
388 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
393 preferenceCategory
.removePreference(pImprint
);
397 mPrefInstantUploadPath
= 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
= (Preference
) findPreference("about_app");
464 if (pAboutApp
!= null
) {
465 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
),
466 getString(R
.string
.app_name
)));
468 Integer currentVersion
= getPackageManager().getPackageInfo
469 (getPackageName(), 0).versionCode
;
470 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
),
472 } catch (NameNotFoundException e
) {
476 loadInstantUploadPath();
477 loadInstantUploadVideoPath();
479 /* ComponentsGetter */
480 mDownloadServiceConnection
= newTransferenceServiceConnection();
481 if (mDownloadServiceConnection
!= null
) {
482 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
,
483 Context
.BIND_AUTO_CREATE
);
485 mUploadServiceConnection
= newTransferenceServiceConnection();
486 if (mUploadServiceConnection
!= null
) {
487 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
,
488 Context
.BIND_AUTO_CREATE
);
491 /* Link to Beta apks */
492 Preference pBetaLink
= findPreference("beta_link");
493 if (pBetaLink
!= null
){
494 pBetaLink
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
496 public boolean onPreferenceClick(Preference preference
) {
497 Integer latestVersion
= -1;
498 Integer currentVersion
= -1;
500 currentVersion
= getPackageManager().getPackageInfo
501 (getPackageName(), 0).versionCode
;
502 LoadingVersionNumberTask loadTask
= new LoadingVersionNumberTask();
504 latestVersion
= loadTask
.get();
505 } catch (InterruptedException
| ExecutionException e
) {
507 } catch (NameNotFoundException e
) {
510 if (latestVersion
== -1 || currentVersion
== -1) {
511 Toast
.makeText(getApplicationContext(), "No information available!",
512 Toast
.LENGTH_SHORT
).show();
514 if (latestVersion
> currentVersion
) {
515 String betaLinkWeb
= (String
) getText(R
.string
.beta_link
) +
516 latestVersion
+ ".apk";
517 if (betaLinkWeb
!= null
&& betaLinkWeb
.length() > 0) {
518 Uri uriUrl
= Uri
.parse(betaLinkWeb
);
519 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
520 startActivity(intent
);
524 Toast
.makeText(getApplicationContext(), "No new version available!",
525 Toast
.LENGTH_SHORT
).show();
533 /* Link to Beta apks */
534 Preference pChangelogLink
= findPreference("changelog_link");
535 if (pChangelogLink
!= null
) {
536 pChangelogLink
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
538 public boolean onPreferenceClick(Preference preference
) {
539 String betaLinkWeb
= getString(R
.string
.changelog
);
540 if (betaLinkWeb
!= null
&& betaLinkWeb
.length() > 0) {
541 Uri uriUrl
= Uri
.parse(betaLinkWeb
);
542 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
543 startActivity(intent
);
552 private void toggleInstantPictureOptions(Boolean value
){
554 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
555 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
557 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
558 // mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
562 private void toggleInstantVideoOptions(Boolean value
){
564 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
565 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
567 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
568 // mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
573 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
575 // Filter for only showing contextual menu when long press on the
577 if (mShowContextMenu
) {
578 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
579 mShowContextMenu
= false
;
581 super.onCreateContextMenu(menu
, v
, menuInfo
);
585 public void run(AccountManagerFuture
<Boolean
> future
) {
586 if (future
.isDone()) {
587 // after remove account
588 Account account
= new Account(mAccountName
, MainApp
.getAccountType());
589 if (!AccountUtils
.exists(account
, MainApp
.getAppContext())) {
591 if (mUploaderBinder
!= null
) {
592 mUploaderBinder
.cancel(account
);
594 if (mDownloaderBinder
!= null
) {
595 mDownloaderBinder
.cancel(account
);
599 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
600 String accountName
= "";
602 Account
[] accounts
= AccountManager
.get(this)
603 .getAccountsByType(MainApp
.getAccountType());
604 if (accounts
.length
!= 0)
605 accountName
= accounts
[0].name
;
606 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
608 addAccountsCheckboxPreferences();
613 protected void onResume() {
615 SharedPreferences appPrefs
=
616 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
617 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
618 pCode
.setChecked(state
);
620 // Populate the accounts category with the list of accounts
621 addAccountsCheckboxPreferences();
625 public boolean onCreateOptionsMenu(Menu menu
) {
626 super.onCreateOptionsMenu(menu
);
631 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
632 super.onMenuItemSelected(featureId
, item
);
635 switch (item
.getItemId()) {
636 case android
.R
.id
.home
:
637 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
638 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
639 startActivity(intent
);
642 Log_OC
.w(TAG
, "Unknown menu item triggered");
649 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
650 super.onActivityResult(requestCode
, resultCode
, data
);
652 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
654 OCFile folderToUpload
=
655 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
657 mUploadPath
= folderToUpload
.getRemotePath();
659 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
661 // Show the path on summary preference
662 mPrefInstantUploadPath
.setSummary(mUploadPath
);
664 saveInstantUploadPathOnPreferences();
666 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
668 OCFile folderToUploadVideo
=
669 (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
671 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
673 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
675 // Show the video path on summary preference
676 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
678 saveInstantUploadVideoPathOnPreferences();
682 public ActionBar
getSupportActionBar() {
683 return getDelegate().getSupportActionBar();
686 public void setSupportActionBar(@Nullable Toolbar toolbar
) {
687 getDelegate().setSupportActionBar(toolbar
);
691 public MenuInflater
getMenuInflater() {
692 return getDelegate().getMenuInflater();
696 public void setContentView(@LayoutRes int layoutResID
) {
697 getDelegate().setContentView(layoutResID
);
700 public void setContentView(View view
) {
701 getDelegate().setContentView(view
);
705 public void setContentView(View view
, ViewGroup
.LayoutParams params
) {
706 getDelegate().setContentView(view
, params
);
710 public void addContentView(View view
, ViewGroup
.LayoutParams params
) {
711 getDelegate().addContentView(view
, params
);
715 protected void onPostResume() {
716 super.onPostResume();
717 getDelegate().onPostResume();
721 protected void onTitleChanged(CharSequence title
, int color
) {
722 super.onTitleChanged(title
, color
);
723 getDelegate().setTitle(title
);
727 public void onConfigurationChanged(Configuration newConfig
) {
728 super.onConfigurationChanged(newConfig
);
729 getDelegate().onConfigurationChanged(newConfig
);
733 protected void onPostCreate(Bundle savedInstanceState
) {
734 super.onPostCreate(savedInstanceState
);
735 getDelegate().onPostCreate(savedInstanceState
);
739 protected void onDestroy() {
742 if (mDownloadServiceConnection
!= null
) {
743 unbindService(mDownloadServiceConnection
);
744 mDownloadServiceConnection
= null
;
746 if (mUploadServiceConnection
!= null
) {
747 unbindService(mUploadServiceConnection
);
748 mUploadServiceConnection
= null
;
752 getDelegate().onDestroy();
756 protected void onStop() {
758 getDelegate().onStop();
761 public void invalidateOptionsMenu() {
762 getDelegate().invalidateOptionsMenu();
765 private AppCompatDelegate
getDelegate() {
766 if (mDelegate
== null
) {
767 mDelegate
= AppCompatDelegate
.create(this, null
);
773 * Create the list of accounts that has been added into the app
775 @SuppressWarnings("deprecation")
776 private void addAccountsCheckboxPreferences() {
778 // Remove accounts in case list is refreshing for avoiding to have
780 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
781 mAccountsPrefCategory
.removeAll();
784 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
785 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
786 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
788 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
789 // Show create account screen if there isn't any account
790 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
796 for (Account a
: accounts
) {
797 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
798 accountPreference
.setKey(a
.name
);
799 // Handle internationalized domain names
800 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
801 mAccountsPrefCategory
.addPreference(accountPreference
);
803 // Check the current account that is being used
804 if (a
.name
.equals(currentAccount
.name
)) {
805 accountPreference
.setChecked(true
);
807 accountPreference
.setChecked(false
);
810 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
812 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
813 String key
= preference
.getKey();
814 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
815 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
816 for (Account a
: accounts
) {
817 RadioButtonPreference p
=
818 (RadioButtonPreference
) findPreference(a
.name
);
819 if (key
.equals(a
.name
)) {
820 boolean accountChanged
= !p
.isChecked();
822 AccountUtils
.setCurrentOwnCloudAccount(
823 getApplicationContext(),
826 if (accountChanged
) {
827 // restart the main activity
828 Intent i
= new Intent(
830 FileDisplayActivity
.class
832 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
833 i
.addFlags(Intent
.FLAG_ACTIVITY_SINGLE_TOP
);
842 return (Boolean
) newValue
;
848 // Add Create Account preference at the end of account list if
849 // Multiaccount is enabled
850 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
851 createAddAccountPreference();
858 * Create the preference for allow adding new accounts
860 private void createAddAccountPreference() {
861 Preference addAccountPref
= new Preference(this);
862 addAccountPref
.setKey("add_account");
863 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
864 mAccountsPrefCategory
.addPreference(addAccountPref
);
866 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
868 public boolean onPreferenceClick(Preference preference
) {
869 AccountManager am
= AccountManager
.get(getApplicationContext());
870 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this,
879 * Load upload path set on preferences
881 private void loadInstantUploadPath() {
882 SharedPreferences appPrefs
=
883 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
884 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
885 mPrefInstantUploadPath
.setSummary(mUploadPath
);
889 * Save the "Instant Upload Path" on preferences
891 private void saveInstantUploadPathOnPreferences() {
892 SharedPreferences appPrefs
=
893 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
894 SharedPreferences
.Editor editor
= appPrefs
.edit();
895 editor
.putString("instant_upload_path", mUploadPath
);
900 * Load upload video path set on preferences
902 private void loadInstantUploadVideoPath() {
903 SharedPreferences appPrefs
=
904 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
905 mUploadVideoPath
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
));
906 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
910 * Save the "Instant Video Upload Path" on preferences
912 private void saveInstantUploadVideoPathOnPreferences() {
913 SharedPreferences appPrefs
=
914 PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
915 SharedPreferences
.Editor editor
= appPrefs
.edit();
916 editor
.putString("instant_video_upload_path", mUploadVideoPath
);
920 // Methods for ComponetsGetter
922 public FileDownloader
.FileDownloaderBinder
getFileDownloaderBinder() {
923 return mDownloaderBinder
;
928 public FileUploader
.FileUploaderBinder
getFileUploaderBinder() {
929 return mUploaderBinder
;
933 public OperationsService
.OperationsServiceBinder
getOperationsServiceBinder() {
938 public FileDataStorageManager
getStorageManager() {
943 public FileOperationsHelper
getFileOperationsHelper() {
947 protected ServiceConnection
newTransferenceServiceConnection() {
948 return new PreferencesServiceConnection();
951 /** Defines callbacks for service binding, passed to bindService() */
952 private class PreferencesServiceConnection
implements ServiceConnection
{
955 public void onServiceConnected(ComponentName component
, IBinder service
) {
957 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
958 mDownloaderBinder
= (FileDownloader
.FileDownloaderBinder
) service
;
960 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
961 Log_OC
.d(TAG
, "Upload service connected");
962 mUploaderBinder
= (FileUploader
.FileUploaderBinder
) service
;
970 public void onServiceDisconnected(ComponentName component
) {
971 if (component
.equals(new ComponentName(Preferences
.this, FileDownloader
.class))) {
972 Log_OC
.d(TAG
, "Download service suddenly disconnected");
973 mDownloaderBinder
= null
;
974 } else if (component
.equals(new ComponentName(Preferences
.this, FileUploader
.class))) {
975 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
976 mUploaderBinder
= null
;
983 * Class for loading the version number
986 private class LoadingVersionNumberTask
extends AsyncTask
<Void
, Void
, Integer
> {
987 protected Integer
doInBackground(Void
... args
) {
989 URL url
= new URL("https://github.com/owncloud/android/raw/beta/apks/latest");
990 BufferedReader
in = new BufferedReader(new InputStreamReader(url
.openStream()));
992 Integer latestVersion
= Integer
.parseInt(in.readLine());
995 return latestVersion
;
997 } catch (MalformedURLException e
) {
999 } catch (IOException e
) {
1000 e
.printStackTrace();