1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import android
.accounts
.Account
;
21 import android
.accounts
.AccountManager
;
22 import android
.accounts
.AccountManagerCallback
;
23 import android
.accounts
.AccountManagerFuture
;
24 import android
.content
.Intent
;
25 import android
.content
.SharedPreferences
;
26 import android
.content
.pm
.PackageInfo
;
27 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
28 import android
.net
.Uri
;
29 import android
.os
.Bundle
;
30 import android
.os
.Handler
;
31 import android
.preference
.CheckBoxPreference
;
32 import android
.preference
.Preference
;
33 import android
.preference
.Preference
.OnPreferenceChangeListener
;
34 import android
.preference
.Preference
.OnPreferenceClickListener
;
35 import android
.preference
.PreferenceCategory
;
36 import android
.preference
.PreferenceManager
;
37 import android
.view
.ContextMenu
;
38 import android
.view
.ContextMenu
.ContextMenuInfo
;
39 import android
.view
.View
;
40 import android
.widget
.AdapterView
;
41 import android
.widget
.AdapterView
.OnItemLongClickListener
;
42 import android
.widget
.ListAdapter
;
43 import android
.widget
.ListView
;
45 import com
.actionbarsherlock
.app
.ActionBar
;
46 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
47 import com
.actionbarsherlock
.view
.Menu
;
48 import com
.actionbarsherlock
.view
.MenuItem
;
49 import com
.owncloud
.android
.MainApp
;
50 import com
.owncloud
.android
.R
;
51 import com
.owncloud
.android
.authentication
.AccountUtils
;
52 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
53 import com
.owncloud
.android
.datamodel
.OCFile
;
54 import com
.owncloud
.android
.db
.DbHandler
;
55 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
56 import com
.owncloud
.android
.ui
.RadioButtonPreference
;
57 import com
.owncloud
.android
.utils
.DisplayUtils
;
61 * An Activity that allows the user to change the application's settings.
63 * @author Bartek Przybylski
64 * @author David A. Velasco
66 public class Preferences
extends SherlockPreferenceActivity
implements AccountManagerCallback
<Boolean
> {
68 private static final String TAG
= "OwnCloudPreferences";
70 private static final int ACTION_SELECT_UPLOAD_PATH
= 1;
71 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH
= 2;
73 private DbHandler mDbHandler
;
74 private CheckBoxPreference pCode
;
75 private Preference pAboutApp
;
77 private PreferenceCategory mAccountsPrefCategory
= null
;
78 private final Handler mHandler
= new Handler();
79 private String mAccountName
;
80 private boolean mShowContextMenu
= false
;
81 private String mUploadPath
;
82 private PreferenceCategory mPrefInstantUploadCategory
;
83 private Preference mPrefInstantUpload
;
84 private Preference mPrefInstantUploadPath
;
85 private Preference mPrefInstantUploadPathWiFi
;
86 private Preference mPrefInstantVideoUpload
;
87 private Preference mPrefInstantVideoUploadPath
;
88 private Preference mPrefInstantVideoUploadPathWiFi
;
89 private String mUploadVideoPath
;
92 @SuppressWarnings("deprecation")
94 public void onCreate(Bundle savedInstanceState
) {
95 super.onCreate(savedInstanceState
);
96 mDbHandler
= new DbHandler(getBaseContext());
97 addPreferencesFromResource(R
.xml
.preferences
);
99 ActionBar actionBar
= getSherlock().getActionBar();
100 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
101 actionBar
.setDisplayHomeAsUpEnabled(true
);
102 actionBar
.setTitle(R
.string
.actionbar_settings
);
104 // Load the accounts category for adding the list of accounts
105 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
107 ListView listView
= getListView();
108 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
110 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
111 ListView listView
= (ListView
) parent
;
112 ListAdapter listAdapter
= listView
.getAdapter();
113 Object obj
= listAdapter
.getItem(position
);
115 if (obj
!= null
&& obj
instanceof RadioButtonPreference
) {
116 mShowContextMenu
= true
;
117 mAccountName
= ((RadioButtonPreference
) obj
).getKey();
119 Preferences
.this.openContextMenu(listView
);
121 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
122 return longListener
.onLongClick(view
);
131 PackageInfo pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
132 temp
= pkg
.versionName
;
133 } catch (NameNotFoundException e
) {
135 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
137 final String appVersion
= temp
;
139 // Register context menu for list of preferences.
140 registerForContextMenu(getListView());
142 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
144 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
146 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
147 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
148 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
149 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
158 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
160 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
161 Preference pHelp
= findPreference("help");
164 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
166 public boolean onPreferenceClick(Preference preference
) {
167 String helpWeb
=(String
) getText(R
.string
.url_help
);
168 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
169 Uri uriUrl
= Uri
.parse(helpWeb
);
170 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
171 startActivity(intent
);
177 preferenceCategory
.removePreference(pHelp
);
183 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
184 Preference pRecommend
= findPreference("recommend");
185 if (pRecommend
!= null
){
186 if (recommendEnabled
) {
187 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
189 public boolean onPreferenceClick(Preference preference
) {
191 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
192 intent
.setType("text/plain");
193 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
194 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
196 String appName
= getString(R
.string
.app_name
);
197 String downloadUrl
= getString(R
.string
.url_app_download
);
198 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
199 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
201 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
202 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
204 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
205 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
206 startActivity(intent
);
214 preferenceCategory
.removePreference(pRecommend
);
219 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
220 Preference pFeedback
= findPreference("feedback");
221 if (pFeedback
!= null
){
222 if (feedbackEnabled
) {
223 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
225 public boolean onPreferenceClick(Preference preference
) {
226 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
227 String feedback
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
;
228 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
229 intent
.setType("text/plain");
230 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
232 intent
.setData(Uri
.parse(feedbackMail
));
233 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
234 startActivity(intent
);
240 preferenceCategory
.removePreference(pFeedback
);
245 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
246 Preference pImprint
= findPreference("imprint");
247 if (pImprint
!= null
) {
248 if (imprintEnabled
) {
249 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
251 public boolean onPreferenceClick(Preference preference
) {
252 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
253 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
254 Uri uriUrl
= Uri
.parse(imprintWeb
);
255 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
256 startActivity(intent
);
258 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
263 preferenceCategory
.removePreference(pImprint
);
267 mPrefInstantUploadPath
= findPreference("instant_upload_path");
268 if (mPrefInstantUploadPath
!= null
){
270 mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
272 public boolean onPreferenceClick(Preference preference
) {
273 if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
274 mUploadPath
+= OCFile
.PATH_SEPARATOR
;
276 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
277 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
);
278 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
);
284 mPrefInstantUploadCategory
= (PreferenceCategory
) findPreference("instant_uploading_category");
286 mPrefInstantUploadPathWiFi
= findPreference("instant_upload_on_wifi");
287 mPrefInstantUpload
= findPreference("instant_uploading");
289 toggleInstantPictureOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
291 mPrefInstantUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
294 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
295 toggleInstantPictureOptions((Boolean
) newValue
);
300 mPrefInstantVideoUploadPath
= findPreference("instant_video_upload_path");
301 if (mPrefInstantVideoUploadPath
!= null
){
303 mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
305 public boolean onPreferenceClick(Preference preference
) {
306 if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
307 mUploadVideoPath
+= OCFile
.PATH_SEPARATOR
;
309 Intent intent
= new Intent(Preferences
.this, UploadPathActivity
.class);
310 intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadVideoPath
);
311 startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
);
317 mPrefInstantVideoUploadPathWiFi
= findPreference("instant_video_upload_on_wifi");
318 mPrefInstantVideoUpload
= findPreference("instant_video_uploading");
319 toggleInstantVideoOptions(((CheckBoxPreference
) mPrefInstantUpload
).isChecked());
321 mPrefInstantVideoUpload
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
324 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
325 toggleInstantVideoOptions((Boolean
) newValue
);
331 pAboutApp
= (Preference
) findPreference("about_app");
332 if (pAboutApp
!= null
) {
333 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
334 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
));
337 loadInstantUploadPath();
338 loadInstantUploadVideoPath();
342 private void toggleInstantPictureOptions(Boolean value
){
344 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPathWiFi
);
345 mPrefInstantUploadCategory
.addPreference(mPrefInstantUploadPath
);
347 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPathWiFi
);
348 mPrefInstantUploadCategory
.removePreference(mPrefInstantUploadPath
);
352 private void toggleInstantVideoOptions(Boolean value
){
354 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPathWiFi
);
355 mPrefInstantUploadCategory
.addPreference(mPrefInstantVideoUploadPath
);
357 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPathWiFi
);
358 mPrefInstantUploadCategory
.removePreference(mPrefInstantVideoUploadPath
);
363 protected void onPause() {
368 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
370 // Filter for only showing contextual menu when long press on the
372 if (mShowContextMenu
) {
373 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
374 mShowContextMenu
= false
;
376 super.onCreateContextMenu(menu
, v
, menuInfo
);
380 * Called when the user clicked on an item into the context menu created at
381 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
382 * every ownCloud {@link Account} , containing 'secondary actions' for them.
387 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
388 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
389 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
390 for (Account a
: accounts
) {
391 if (a
.name
.equals(mAccountName
)) {
392 if (item
.getItemId() == R
.id
.change_password
) {
394 // Change account password
395 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
396 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
397 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
398 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
399 startActivity(updateAccountCredentials
);
401 } else if (item
.getItemId() == R
.id
.delete_account
) {
404 am
.removeAccount(a
, this, mHandler
);
413 public void run(AccountManagerFuture
<Boolean
> future
) {
414 if (future
.isDone()) {
415 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
416 String accountName
= "";
418 Account
[] accounts
= AccountManager
.get(this).getAccountsByType(MainApp
.getAccountType());
419 if (accounts
.length
!= 0)
420 accountName
= accounts
[0].name
;
421 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
423 addAccountsCheckboxPreferences();
428 protected void onResume() {
430 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
431 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
432 pCode
.setChecked(state
);
434 // Populate the accounts category with the list of accounts
435 addAccountsCheckboxPreferences();
439 public boolean onCreateOptionsMenu(Menu menu
) {
440 super.onCreateOptionsMenu(menu
);
445 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
446 super.onMenuItemSelected(featureId
, item
);
449 switch (item
.getItemId()) {
450 case android
.R
.id
.home
:
451 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
452 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
453 startActivity(intent
);
456 Log_OC
.w(TAG
, "Unknown menu item triggered");
463 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
464 super.onActivityResult(requestCode
, resultCode
, data
);
466 if (requestCode
== ACTION_SELECT_UPLOAD_PATH
&& resultCode
== RESULT_OK
){
468 OCFile folderToUpload
= (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
470 mUploadPath
= folderToUpload
.getRemotePath();
472 mUploadPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
);
474 // Show the path on summary preference
475 mPrefInstantUploadPath
.setSummary(mUploadPath
);
477 saveInstantUploadPathOnPreferences();
479 } else if (requestCode
== ACTION_SELECT_UPLOAD_VIDEO_PATH
&& resultCode
== RESULT_OK
){
481 OCFile folderToUploadVideo
= (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
);
483 mUploadVideoPath
= folderToUploadVideo
.getRemotePath();
485 mUploadVideoPath
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
);
487 // Show the video path on summary preference
488 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
490 saveInstantUploadVideoPathOnPreferences();
495 protected void onDestroy() {
501 * Create the list of accounts that has been added into the app
503 @SuppressWarnings("deprecation")
504 private void addAccountsCheckboxPreferences() {
506 // Remove accounts in case list is refreshing for avoiding to have
508 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
509 mAccountsPrefCategory
.removeAll();
512 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
513 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
514 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
516 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
517 // Show create account screen if there isn't any account
518 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
524 for (Account a
: accounts
) {
525 RadioButtonPreference accountPreference
= new RadioButtonPreference(this);
526 accountPreference
.setKey(a
.name
);
527 // Handle internationalized domain names
528 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
));
529 mAccountsPrefCategory
.addPreference(accountPreference
);
531 // Check the current account that is being used
532 if (a
.name
.equals(currentAccount
.name
)) {
533 accountPreference
.setChecked(true
);
535 accountPreference
.setChecked(false
);
538 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
540 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
541 String key
= preference
.getKey();
542 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
543 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
544 for (Account a
: accounts
) {
545 RadioButtonPreference p
= (RadioButtonPreference
) findPreference(a
.name
);
546 if (key
.equals(a
.name
)) {
547 boolean accountChanged
= !p
.isChecked();
549 AccountUtils
.setCurrentOwnCloudAccount(
550 getApplicationContext(),
553 if (accountChanged
) {
554 // restart the main activity
555 Intent i
= new Intent(
557 FileDisplayActivity
.class
559 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
568 return (Boolean
) newValue
;
574 // Add Create Account preference at the end of account list if
575 // Multiaccount is enabled
576 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
577 createAddAccountPreference();
584 * Create the preference for allow adding new accounts
586 private void createAddAccountPreference() {
587 Preference addAccountPref
= new Preference(this);
588 addAccountPref
.setKey("add_account");
589 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
590 mAccountsPrefCategory
.addPreference(addAccountPref
);
592 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
594 public boolean onPreferenceClick(Preference preference
) {
595 AccountManager am
= AccountManager
.get(getApplicationContext());
596 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
604 * Load upload path set on preferences
606 private void loadInstantUploadPath() {
607 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
608 mUploadPath
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
));
609 mPrefInstantUploadPath
.setSummary(mUploadPath
);
613 * Save the "Instant Upload Path" on preferences
615 private void saveInstantUploadPathOnPreferences() {
616 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
617 SharedPreferences
.Editor editor
= appPrefs
.edit();
618 editor
.putString("instant_upload_path", mUploadPath
);
623 * Load upload video path set on preferences
625 private void loadInstantUploadVideoPath() {
626 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
627 mUploadVideoPath
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
));
628 mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
);
632 * Save the "Instant Video Upload Path" on preferences
634 private void saveInstantUploadVideoPathOnPreferences() {
635 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
636 SharedPreferences
.Editor editor
= appPrefs
.edit();
637 editor
.putString("instant_video_upload_path", mUploadVideoPath
);