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
.db
.DbHandler
;
54 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
55 import com
.owncloud
.android
.ui
.LongClickableCheckBoxPreference
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
60 * An Activity that allows the user to change the application's settings.
62 * @author Bartek Przybylski
63 * @author David A. Velasco
65 public class Preferences
extends SherlockPreferenceActivity
implements AccountManagerCallback
<Boolean
> {
67 private static final String TAG
= "OwnCloudPreferences";
69 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
71 private DbHandler mDbHandler
;
72 private CheckBoxPreference pCode
;
73 private Preference pAboutApp
;
75 private Account mPreviousAccount
= null
;
76 private PreferenceCategory mAccountsPrefCategory
= null
;
77 private final Handler mHandler
= new Handler();
78 private String mAccountName
;
79 private boolean mShowContextMenu
= false
;
82 @SuppressWarnings("deprecation")
84 public void onCreate(Bundle savedInstanceState
) {
85 super.onCreate(savedInstanceState
);
86 mDbHandler
= new DbHandler(getBaseContext());
87 addPreferencesFromResource(R
.xml
.preferences
);
89 ActionBar actionBar
= getSherlock().getActionBar();
90 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
91 actionBar
.setDisplayHomeAsUpEnabled(true
);
93 if (savedInstanceState
!= null
) {
94 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
96 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
99 // Load the accounts category for adding the list of accounts
100 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
102 ListView listView
= getListView();
103 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
105 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
106 ListView listView
= (ListView
) parent
;
107 ListAdapter listAdapter
= listView
.getAdapter();
108 Object obj
= listAdapter
.getItem(position
);
110 if (obj
!= null
&& obj
instanceof LongClickableCheckBoxPreference
) {
111 mShowContextMenu
= true
;
112 mAccountName
= obj
.toString();
114 Preferences
.this.openContextMenu(listView
);
116 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
117 return longListener
.onLongClick(view
);
123 // Register context menu for list of preferences.
124 registerForContextMenu(getListView());
126 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
128 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
130 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
131 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
132 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
133 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
142 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
144 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
145 Preference pHelp
= findPreference("help");
148 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
150 public boolean onPreferenceClick(Preference preference
) {
151 String helpWeb
=(String
) getText(R
.string
.url_help
);
152 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
153 Uri uriUrl
= Uri
.parse(helpWeb
);
154 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
155 startActivity(intent
);
161 preferenceCategory
.removePreference(pHelp
);
167 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
168 Preference pRecommend
= findPreference("recommend");
169 if (pRecommend
!= null
){
170 if (recommendEnabled
) {
171 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
173 public boolean onPreferenceClick(Preference preference
) {
175 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
176 intent
.setType("text/plain");
177 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
178 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
180 String appName
= getString(R
.string
.app_name
);
181 String downloadUrl
= getString(R
.string
.url_app_download
);
182 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
183 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
185 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
186 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
188 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
189 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
190 startActivity(intent
);
198 preferenceCategory
.removePreference(pRecommend
);
203 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
204 Preference pFeedback
= findPreference("feedback");
205 if (pFeedback
!= null
){
206 if (feedbackEnabled
) {
207 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
209 public boolean onPreferenceClick(Preference preference
) {
210 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
211 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
212 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
213 intent
.setType("text/plain");
214 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
216 intent
.setData(Uri
.parse(feedbackMail
));
217 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
218 startActivity(intent
);
224 preferenceCategory
.removePreference(pFeedback
);
229 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
230 Preference pImprint
= findPreference("imprint");
231 if (pImprint
!= null
) {
232 if (imprintEnabled
) {
233 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
235 public boolean onPreferenceClick(Preference preference
) {
236 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
237 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
238 Uri uriUrl
= Uri
.parse(imprintWeb
);
239 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
240 startActivity(intent
);
242 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
247 preferenceCategory
.removePreference(pImprint
);
252 pAboutApp
= (Preference
) findPreference("about_app");
253 if (pAboutApp
!= null
) {
254 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
257 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
258 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
259 } catch (NameNotFoundException e
) {
260 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
266 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
268 // Filter for only showing contextual menu when long press on the
270 if (mShowContextMenu
) {
271 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
272 mShowContextMenu
= false
;
274 super.onCreateContextMenu(menu
, v
, menuInfo
);
278 * Called when the user clicked on an item into the context menu created at
279 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
280 * every ownCloud {@link Account} , containing 'secondary actions' for them.
285 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
286 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
287 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
288 for (Account a
: accounts
) {
289 if (a
.name
.equals(mAccountName
)) {
290 if (item
.getItemId() == R
.id
.change_password
) {
292 // Change account password
293 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
294 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
295 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
296 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
297 startActivity(updateAccountCredentials
);
299 } else if (item
.getItemId() == R
.id
.delete_account
) {
302 am
.removeAccount(a
, this, mHandler
);
311 public void run(AccountManagerFuture
<Boolean
> future
) {
312 if (future
.isDone()) {
313 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
314 String accountName
= "";
316 Account
[] accounts
= AccountManager
.get(this).getAccountsByType(MainApp
.getAccountType());
317 if (accounts
.length
!= 0)
318 accountName
= accounts
[0].name
;
319 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
321 addAccountsCheckboxPreferences();
326 protected void onResume() {
328 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
329 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
330 pCode
.setChecked(state
);
332 // Populate the accounts category with the list of accounts
333 addAccountsCheckboxPreferences();
337 public boolean onCreateOptionsMenu(Menu menu
) {
338 super.onCreateOptionsMenu(menu
);
343 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
344 super.onMenuItemSelected(featureId
, item
);
347 switch (item
.getItemId()) {
348 case android
.R
.id
.home
:
349 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
350 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
351 startActivity(intent
);
354 Log_OC
.w(TAG
, "Unknown menu item triggered");
361 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
362 super.onActivityResult(requestCode
, resultCode
, data
);
366 protected void onDestroy() {
372 * Create the list of accounts that has been added into the app
374 @SuppressWarnings("deprecation")
375 private void addAccountsCheckboxPreferences() {
377 // Remove accounts in case list is refreshing for avoiding to have
379 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
380 mAccountsPrefCategory
.removeAll();
383 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
384 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
385 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
387 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
388 // Show create account screen if there isn't any account
389 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
395 for (Account a
: accounts
) {
396 LongClickableCheckBoxPreference accountPreference
= new LongClickableCheckBoxPreference(this);
397 accountPreference
.setKey(a
.name
);
398 accountPreference
.setTitle(a
.name
);
399 mAccountsPrefCategory
.addPreference(accountPreference
);
401 // Check the current account that is being used
402 if (a
.name
.equals(currentAccount
.name
)) {
403 accountPreference
.setChecked(true
);
405 accountPreference
.setChecked(false
);
408 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
410 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
411 String key
= preference
.getKey();
412 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
413 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
414 for (Account a
: accounts
) {
415 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
416 if (key
.equals(a
.name
)) {
418 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
423 return (Boolean
) newValue
;
429 // Add Create Account preference at the end of account list if
430 // Multiaccount is enabled
431 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
432 createAddAccountPreference();
439 * Create the preference for allow adding new accounts
441 private void createAddAccountPreference() {
442 Preference addAccountPref
= new Preference(this);
443 addAccountPref
.setKey("add_account");
444 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
445 mAccountsPrefCategory
.addPreference(addAccountPref
);
447 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
449 public boolean onPreferenceClick(Preference preference
) {
450 AccountManager am
= AccountManager
.get(getApplicationContext());
451 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
459 protected void onPause() {
460 if (this.isFinishing()) {
461 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
462 if ((mPreviousAccount
== null
&& current
!= null
)
463 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
464 // the account set as default changed since this activity was
467 // restart the main activity
468 Intent i
= new Intent(this, FileDisplayActivity
.class);
469 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);