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 DbHandler mDbHandler
;
70 private CheckBoxPreference pCode
;
71 //private CheckBoxPreference pLogging;
72 //private Preference pLoggingHistory;
73 private Preference pAboutApp
;
75 private PreferenceCategory mAccountsPrefCategory
= null
;
76 private final Handler mHandler
= new Handler();
77 private String mAccountName
;
78 private boolean mShowContextMenu
= false
;
81 @SuppressWarnings("deprecation")
83 public void onCreate(Bundle savedInstanceState
) {
84 super.onCreate(savedInstanceState
);
85 mDbHandler
= new DbHandler(getBaseContext());
86 addPreferencesFromResource(R
.xml
.preferences
);
88 ActionBar actionBar
= getSherlock().getActionBar();
89 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
90 actionBar
.setDisplayHomeAsUpEnabled(true
);
92 // Load the accounts category for adding the list of accounts
93 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
95 ListView listView
= getListView();
96 listView
.setOnItemLongClickListener(new OnItemLongClickListener() {
98 public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) {
99 ListView listView
= (ListView
) parent
;
100 ListAdapter listAdapter
= listView
.getAdapter();
101 Object obj
= listAdapter
.getItem(position
);
103 if (obj
!= null
&& obj
instanceof LongClickableCheckBoxPreference
) {
104 mShowContextMenu
= true
;
105 mAccountName
= obj
.toString();
107 Preferences
.this.openContextMenu(listView
);
109 View
.OnLongClickListener longListener
= (View
.OnLongClickListener
) obj
;
110 return longListener
.onLongClick(view
);
116 // Register context menu for list of preferences.
117 registerForContextMenu(getListView());
119 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
121 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
123 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
124 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
125 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
126 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
135 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
137 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
138 Preference pHelp
= findPreference("help");
141 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
143 public boolean onPreferenceClick(Preference preference
) {
144 String helpWeb
=(String
) getText(R
.string
.url_help
);
145 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
146 Uri uriUrl
= Uri
.parse(helpWeb
);
147 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
148 startActivity(intent
);
154 preferenceCategory
.removePreference(pHelp
);
160 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
161 Preference pRecommend
= findPreference("recommend");
162 if (pRecommend
!= null
){
163 if (recommendEnabled
) {
164 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
166 public boolean onPreferenceClick(Preference preference
) {
168 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
169 intent
.setType("text/plain");
170 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
171 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
173 String appName
= getString(R
.string
.app_name
);
174 String downloadUrl
= getString(R
.string
.url_app_download
);
175 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
176 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
178 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
179 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
181 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
182 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
183 startActivity(intent
);
191 preferenceCategory
.removePreference(pRecommend
);
196 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
197 Preference pFeedback
= findPreference("feedback");
198 if (pFeedback
!= null
){
199 if (feedbackEnabled
) {
200 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
202 public boolean onPreferenceClick(Preference preference
) {
203 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
204 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
205 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
206 intent
.setType("text/plain");
207 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
209 intent
.setData(Uri
.parse(feedbackMail
));
210 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
211 startActivity(intent
);
217 preferenceCategory
.removePreference(pFeedback
);
222 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
223 Preference pImprint
= findPreference("imprint");
224 if (pImprint
!= null
) {
225 if (imprintEnabled
) {
226 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
228 public boolean onPreferenceClick(Preference preference
) {
229 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
230 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
231 Uri uriUrl
= Uri
.parse(imprintWeb
);
232 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
233 startActivity(intent
);
235 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
240 preferenceCategory
.removePreference(pImprint
);
245 pAboutApp
= (Preference
) findPreference("about_app");
246 if (pAboutApp
!= null
) {
247 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
250 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
251 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
252 } catch (NameNotFoundException e
) {
253 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
257 /* DISABLED FOR RELEASE UNTIL FIXED
258 pLogging = (CheckBoxPreference) findPreference("log_to_file");
259 if (pLogging != null) {
260 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
262 public boolean onPreferenceChange(Preference preference, Object newValue) {
264 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
266 if(!pLogging.isChecked()) {
267 Log_OC.d("Debug", "start logging");
268 Log_OC.v("PATH", logpath);
269 Log_OC.startLogging(logpath);
272 Log_OC.d("Debug", "stop logging");
273 Log_OC.stopLogging();
280 pLoggingHistory = (Preference) findPreference("log_history");
281 if (pLoggingHistory != null) {
282 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
285 public boolean onPreferenceClick(Preference preference) {
286 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
287 startActivity(intent);
297 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
299 // Filter for only showing contextual menu when long press on the
301 if (mShowContextMenu
) {
302 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
303 mShowContextMenu
= false
;
305 super.onCreateContextMenu(menu
, v
, menuInfo
);
309 * Called when the user clicked on an item into the context menu created at
310 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
311 * every ownCloud {@link Account} , containing 'secondary actions' for them.
316 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
317 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
318 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
319 for (Account a
: accounts
) {
320 if (a
.name
.equals(mAccountName
)) {
321 if (item
.getItemId() == R
.id
.change_password
) {
323 // Change account password
324 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
325 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
326 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
,
327 AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
328 startActivity(updateAccountCredentials
);
330 } else if (item
.getItemId() == R
.id
.delete_account
) {
333 am
.removeAccount(a
, this, mHandler
);
342 public void run(AccountManagerFuture
<Boolean
> future
) {
343 if (future
.isDone()) {
344 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
345 String accountName
= "";
347 Account
[] accounts
= AccountManager
.get(this).getAccountsByType(MainApp
.getAccountType());
348 if (accounts
.length
!= 0)
349 accountName
= accounts
[0].name
;
350 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
352 addAccountsCheckboxPreferences();
357 protected void onResume() {
359 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
360 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
361 pCode
.setChecked(state
);
363 // Populate the accounts category with the list of accounts
364 addAccountsCheckboxPreferences();
368 public boolean onCreateOptionsMenu(Menu menu
) {
369 super.onCreateOptionsMenu(menu
);
374 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
375 super.onMenuItemSelected(featureId
, item
);
378 switch (item
.getItemId()) {
379 case android
.R
.id
.home
:
380 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
381 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
382 startActivity(intent
);
385 Log_OC
.w(TAG
, "Unknown menu item triggered");
392 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
393 super.onActivityResult(requestCode
, resultCode
, data
);
397 protected void onDestroy() {
403 * Create the list of accounts that has been added into the app
405 @SuppressWarnings("deprecation")
406 private void addAccountsCheckboxPreferences() {
408 // Remove accounts in case list is refreshing for avoiding to have
410 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
411 mAccountsPrefCategory
.removeAll();
414 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
415 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
416 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
418 if (am
.getAccountsByType(MainApp
.getAccountType()).length
== 0) {
419 // Show create account screen if there isn't any account
420 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this,
426 for (Account a
: accounts
) {
427 LongClickableCheckBoxPreference accountPreference
= new LongClickableCheckBoxPreference(this);
428 accountPreference
.setKey(a
.name
);
429 accountPreference
.setTitle(a
.name
);
430 mAccountsPrefCategory
.addPreference(accountPreference
);
432 // Check the current account that is being used
433 if (a
.name
.equals(currentAccount
.name
)) {
434 accountPreference
.setChecked(true
);
436 accountPreference
.setChecked(false
);
439 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
441 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
442 String key
= preference
.getKey();
443 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
444 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
445 for (Account a
: accounts
) {
446 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
447 if (key
.equals(a
.name
)) {
448 boolean accountChanged
= !p
.isChecked();
450 AccountUtils
.setCurrentOwnCloudAccount(
451 getApplicationContext(),
454 if (accountChanged
) {
455 // restart the main activity
456 Intent i
= new Intent(
458 FileDisplayActivity
.class
460 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
469 return (Boolean
) newValue
;
475 // Add Create Account preference at the end of account list if
476 // Multiaccount is enabled
477 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
478 createAddAccountPreference();
485 * Create the preference for allow adding new accounts
487 private void createAddAccountPreference() {
488 Preference addAccountPref
= new Preference(this);
489 addAccountPref
.setKey("add_account");
490 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
491 mAccountsPrefCategory
.addPreference(addAccountPref
);
493 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
495 public boolean onPreferenceClick(Preference preference
) {
496 AccountManager am
= AccountManager
.get(getApplicationContext());
497 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);