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
.content
.Intent
;
23 import android
.content
.SharedPreferences
;
24 import android
.content
.pm
.PackageInfo
;
25 import android
.content
.pm
.PackageManager
.NameNotFoundException
;
26 import android
.net
.Uri
;
27 import android
.os
.Bundle
;
28 import android
.preference
.CheckBoxPreference
;
29 import android
.preference
.Preference
;
30 import android
.preference
.Preference
.OnPreferenceChangeListener
;
31 import android
.preference
.Preference
.OnPreferenceClickListener
;
32 import android
.preference
.PreferenceCategory
;
33 import android
.preference
.PreferenceManager
;
35 import com
.actionbarsherlock
.app
.ActionBar
;
36 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
37 import com
.actionbarsherlock
.view
.Menu
;
38 import com
.actionbarsherlock
.view
.MenuItem
;
39 import com
.owncloud
.android
.MainApp
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
42 import com
.owncloud
.android
.db
.DbHandler
;
43 import com
.owncloud
.android
.utils
.DisplayUtils
;
44 import com
.owncloud
.android
.utils
.Log_OC
;
48 * An Activity that allows the user to change the application's settings.
50 * @author Bartek Przybylski
51 * @author David A. Velasco
53 public class Preferences
extends SherlockPreferenceActivity
{
55 private static final String TAG
= "OwnCloudPreferences";
57 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
59 private DbHandler mDbHandler
;
60 private CheckBoxPreference pCode
;
61 //private CheckBoxPreference pLogging;
62 //private Preference pLoggingHistory;
63 private Preference pAboutApp
;
65 private Account mPreviousAccount
= null
;
66 private PreferenceCategory mAccountsPrefCategory
= null
;
69 @SuppressWarnings("deprecation")
71 public void onCreate(Bundle savedInstanceState
) {
72 super.onCreate(savedInstanceState
);
73 mDbHandler
= new DbHandler(getBaseContext());
74 addPreferencesFromResource(R
.xml
.preferences
);
76 ActionBar actionBar
= getSherlock().getActionBar();
77 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
78 actionBar
.setDisplayHomeAsUpEnabled(true
);
80 if (savedInstanceState
!= null
) {
81 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
83 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
86 // Load the accounts category for adding the list of accounts
87 mAccountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
89 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
91 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
93 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
94 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
95 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
96 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
105 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
107 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
108 Preference pHelp
= findPreference("help");
111 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
113 public boolean onPreferenceClick(Preference preference
) {
114 String helpWeb
=(String
) getText(R
.string
.url_help
);
115 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
116 Uri uriUrl
= Uri
.parse(helpWeb
);
117 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
118 startActivity(intent
);
124 preferenceCategory
.removePreference(pHelp
);
130 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
131 Preference pRecommend
= findPreference("recommend");
132 if (pRecommend
!= null
){
133 if (recommendEnabled
) {
134 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
136 public boolean onPreferenceClick(Preference preference
) {
138 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
139 intent
.setType("text/plain");
140 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
141 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
143 String appName
= getString(R
.string
.app_name
);
144 String downloadUrl
= getString(R
.string
.url_app_download
);
145 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
146 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
148 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
149 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
151 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
152 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
153 startActivity(intent
);
161 preferenceCategory
.removePreference(pRecommend
);
166 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
167 Preference pFeedback
= findPreference("feedback");
168 if (pFeedback
!= null
){
169 if (feedbackEnabled
) {
170 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
172 public boolean onPreferenceClick(Preference preference
) {
173 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
174 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
175 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
176 intent
.setType("text/plain");
177 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
179 intent
.setData(Uri
.parse(feedbackMail
));
180 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
181 startActivity(intent
);
187 preferenceCategory
.removePreference(pFeedback
);
192 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
193 Preference pImprint
= findPreference("imprint");
194 if (pImprint
!= null
) {
195 if (imprintEnabled
) {
196 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
198 public boolean onPreferenceClick(Preference preference
) {
199 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
200 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
201 Uri uriUrl
= Uri
.parse(imprintWeb
);
202 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
203 startActivity(intent
);
205 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
210 preferenceCategory
.removePreference(pImprint
);
215 pAboutApp
= (Preference
) findPreference("about_app");
216 if (pAboutApp
!= null
) {
217 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
220 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
221 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
222 } catch (NameNotFoundException e
) {
223 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
227 /* DISABLED FOR RELEASE UNTIL FIXED
228 pLogging = (CheckBoxPreference) findPreference("log_to_file");
229 if (pLogging != null) {
230 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
232 public boolean onPreferenceChange(Preference preference, Object newValue) {
234 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
236 if(!pLogging.isChecked()) {
237 Log_OC.d("Debug", "start logging");
238 Log_OC.v("PATH", logpath);
239 Log_OC.startLogging(logpath);
242 Log_OC.d("Debug", "stop logging");
243 Log_OC.stopLogging();
250 pLoggingHistory = (Preference) findPreference("log_history");
251 if (pLoggingHistory != null) {
252 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
255 public boolean onPreferenceClick(Preference preference) {
256 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
257 startActivity(intent);
267 protected void onResume() {
269 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
270 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
271 pCode
.setChecked(state
);
273 // Populate the accounts category with the list of accounts
274 createAccountsCheckboxPreferences();
278 public boolean onCreateOptionsMenu(Menu menu
) {
279 super.onCreateOptionsMenu(menu
);
284 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
285 super.onMenuItemSelected(featureId
, item
);
288 switch (item
.getItemId()) {
289 case android
.R
.id
.home
:
290 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
291 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
292 startActivity(intent
);
295 Log_OC
.w(TAG
, "Unknown menu item triggered");
302 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
303 super.onActivityResult(requestCode
, resultCode
, data
);
307 protected void onDestroy() {
313 * Create the list of accounts that has been added into the app
315 private void createAccountsCheckboxPreferences() {
317 // Remove accounts in case list is refreshing for avoiding to have
319 if (mAccountsPrefCategory
.getPreferenceCount() > 0) {
320 mAccountsPrefCategory
.removeAll();
323 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
324 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
325 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
326 for (Account a
: accounts
) {
327 CheckBoxPreference checkBoxPreference
= new CheckBoxPreference(this);
328 checkBoxPreference
.setKey(a
.name
);
329 checkBoxPreference
.setTitle(a
.name
);
331 // Check the current account that is being used
332 if (a
.name
.equals(currentAccount
.name
)) {
333 checkBoxPreference
.setChecked(true
);
336 checkBoxPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
338 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
339 String key
= preference
.getKey();
340 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
341 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
342 for (Account a
: accounts
) {
343 @SuppressWarnings("deprecation")
344 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
345 if (key
.equals(a
.name
)) {
347 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
352 return (Boolean
) newValue
;
356 mAccountsPrefCategory
.addPreference(checkBoxPreference
);
359 // Add Create Account preference at the end of account list if Multiaccount is enabled
360 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
361 createAddAccountPreference();
367 * Create the preference for allow adding new accounts
369 private void createAddAccountPreference() {
370 Preference addAccountPref
= new Preference(this);
371 addAccountPref
.setKey("add_account");
372 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
373 mAccountsPrefCategory
.addPreference(addAccountPref
);
375 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
377 public boolean onPreferenceClick(Preference preference
) {
378 AccountManager am
= AccountManager
.get(getApplicationContext());
379 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
387 protected void onPause() {
388 if (this.isFinishing()) {
389 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
390 if ((mPreviousAccount
== null
&& current
!= null
)
391 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
392 // the account set as default changed since this activity was
395 // restart the main activity
396 Intent i
= new Intent(this, FileDisplayActivity
.class);
397 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);