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
.ui
.LongClickableCheckBoxPreference
;
55 import com
.owncloud
.android
.utils
.DisplayUtils
;
56 import com
.owncloud
.android
.utils
.Log_OC
;
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 CheckBoxPreference pLogging;
74 //private Preference pLoggingHistory;
75 private Preference pAboutApp
;
77 private Account mPreviousAccount
= null
;
78 private PreferenceCategory mAccountsPrefCategory
= null
;
79 private final Handler mHandler
= new Handler();
80 private String mAccountName
;
81 private boolean mShowContextMenu
= false
;
84 @SuppressWarnings("deprecation")
86 public void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
88 mDbHandler
= new DbHandler(getBaseContext());
89 addPreferencesFromResource(R
.xml
.preferences
);
91 ActionBar actionBar
= getSherlock().getActionBar();
92 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
93 actionBar
.setDisplayHomeAsUpEnabled(true
);
95 if (savedInstanceState
!= null
) {
96 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
98 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
101 // Load the accounts category for adding the list of accounts
102 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
104 ListView listView
= getListView();
105 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
107 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
108 ListView listView
= (ListView
) parent
;
109 ListAdapter listAdapter
= listView
.getAdapter();
110 Object obj
= listAdapter
.getItem(position
);
112 if (obj
!= null
&& obj
instanceof LongClickableCheckBoxPreference
) {
113 mShowContextMenu
= true
;
114 mAccountName
= obj
.toString();
116 Preferences
.this.openContextMenu(listView
);
118 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
119 return longListener
.onLongClick(view
);
125 // Register context menu for list of preferences.
126 registerForContextMenu(getListView());
128 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
130 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
132 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
133 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
134 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
135 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
144 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
146 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
147 Preference pHelp
= findPreference("help");
150 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
152 public boolean onPreferenceClick(Preference preference
) {
153 String helpWeb
=(String
) getText(R
.string
.url_help
);
154 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
155 Uri uriUrl
= Uri
.parse(helpWeb
);
156 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
157 startActivity(intent
);
163 preferenceCategory
.removePreference(pHelp
);
169 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
170 Preference pRecommend
= findPreference("recommend");
171 if (pRecommend
!= null
){
172 if (recommendEnabled
) {
173 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
175 public boolean onPreferenceClick(Preference preference
) {
177 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
178 intent
.setType("text/plain");
179 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
180 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
182 String appName
= getString(R
.string
.app_name
);
183 String downloadUrl
= getString(R
.string
.url_app_download
);
184 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
185 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
187 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
188 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
190 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
191 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
192 startActivity(intent
);
200 preferenceCategory
.removePreference(pRecommend
);
205 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
206 Preference pFeedback
= findPreference("feedback");
207 if (pFeedback
!= null
){
208 if (feedbackEnabled
) {
209 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
211 public boolean onPreferenceClick(Preference preference
) {
212 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
213 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
214 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
215 intent
.setType("text/plain");
216 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
218 intent
.setData(Uri
.parse(feedbackMail
));
219 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
220 startActivity(intent
);
226 preferenceCategory
.removePreference(pFeedback
);
231 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
232 Preference pImprint
= findPreference("imprint");
233 if (pImprint
!= null
) {
234 if (imprintEnabled
) {
235 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
237 public boolean onPreferenceClick(Preference preference
) {
238 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
239 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
240 Uri uriUrl
= Uri
.parse(imprintWeb
);
241 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
242 startActivity(intent
);
244 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
249 preferenceCategory
.removePreference(pImprint
);
254 pAboutApp
= (Preference
) findPreference("about_app");
255 if (pAboutApp
!= null
) {
256 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
259 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
260 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
261 } catch (NameNotFoundException e
) {
262 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
266 /* DISABLED FOR RELEASE UNTIL FIXED
267 pLogging = (CheckBoxPreference) findPreference("log_to_file");
268 if (pLogging != null) {
269 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
271 public boolean onPreferenceChange(Preference preference, Object newValue) {
273 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
275 if(!pLogging.isChecked()) {
276 Log_OC.d("Debug", "start logging");
277 Log_OC.v("PATH", logpath);
278 Log_OC.startLogging(logpath);
281 Log_OC.d("Debug", "stop logging");
282 Log_OC.stopLogging();
289 pLoggingHistory = (Preference) findPreference("log_history");
290 if (pLoggingHistory != null) {
291 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
294 public boolean onPreferenceClick(Preference preference) {
295 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
296 startActivity(intent);
306 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
308 // Filter for only showing contextual menu when long press on the
310 if (mShowContextMenu
) {
311 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
312 mShowContextMenu
= false
;
314 super.onCreateContextMenu(menu
, v
, menuInfo
);
318 * Called when the user clicked on an item into the context menu created at
319 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
320 * every ownCloud {@link Account} , containing 'secondary actions' for them.
325 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
326 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
327 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
328 for (Account a
: accounts
) {
329 if (a
.name
.equals(mAccountName
)) {
330 if (item
.getItemId() == R
.id
.change_password
) {
332 // Change account password
333 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
334 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
335 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
336 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
337 startActivity(updateAccountCredentials
);
339 } else if (item
.getItemId() == R
.id
.delete_account
) {
342 am
.removeAccount(a
, this, mHandler
);
351 public void run(AccountManagerFuture
<Boolean
> future
) {
352 if (future
.isDone()) {
353 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
354 String accountName
= "";
356 Account
[] accounts
= AccountManager
.get(this).getAccountsByType(MainApp
.getAccountType());
357 if (accounts
.length
!= 0)
358 accountName
= accounts
[0].name
;
359 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
361 addAccountsCheckboxPreferences();
366 protected void onResume() {
368 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
369 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
370 pCode
.setChecked(state
);
372 // Populate the accounts category with the list of accounts
373 addAccountsCheckboxPreferences();
377 public boolean onCreateOptionsMenu(Menu menu
) {
378 super.onCreateOptionsMenu(menu
);
383 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
384 super.onMenuItemSelected(featureId
, item
);
387 switch (item
.getItemId()) {
388 case android
.R
.id
.home
:
389 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
390 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
391 startActivity(intent
);
394 Log_OC
.w(TAG
, "Unknown menu item triggered");
401 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
402 super.onActivityResult(requestCode
, resultCode
, data
);
406 protected void onDestroy() {
412 * Create the list of accounts that has been added into the app
414 @SuppressWarnings("deprecation")
415 private void addAccountsCheckboxPreferences() {
417 // Remove accounts in case list is refreshing for avoiding to have
419 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
420 mAccountsPrefCategory
.removeAll();
423 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
424 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
425 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
427 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
428 // Show create account screen if there isn't any account
429 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
435 for (Account a
: accounts
) {
436 LongClickableCheckBoxPreference accountPreference
= new LongClickableCheckBoxPreference(this);
437 accountPreference
.setKey(a
.name
);
438 accountPreference
.setTitle(a
.name
);
439 mAccountsPrefCategory
.addPreference(accountPreference
);
441 // Check the current account that is being used
442 if (a
.name
.equals(currentAccount
.name
)) {
443 accountPreference
.setChecked(true
);
445 accountPreference
.setChecked(false
);
448 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
450 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
451 String key
= preference
.getKey();
452 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
453 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
454 for (Account a
: accounts
) {
455 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
456 if (key
.equals(a
.name
)) {
458 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
463 return (Boolean
) newValue
;
469 // Add Create Account preference at the end of account list if
470 // Multiaccount is enabled
471 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
472 createAddAccountPreference();
479 * Create the preference for allow adding new accounts
481 private void createAddAccountPreference() {
482 Preference addAccountPref
= new Preference(this);
483 addAccountPref
.setKey("add_account");
484 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
485 mAccountsPrefCategory
.addPreference(addAccountPref
);
487 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
489 public boolean onPreferenceClick(Preference preference
) {
490 AccountManager am
= AccountManager
.get(getApplicationContext());
491 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
499 protected void onPause() {
500 if (this.isFinishing()) {
501 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
502 if ((mPreviousAccount
== null
&& current
!= null
)
503 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
504 // the account set as default changed since this activity was
507 // restart the main activity
508 Intent i
= new Intent(this, FileDisplayActivity
.class);
509 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);