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
.ui
.PreferenceMultiline
;
44 import com
.owncloud
.android
.utils
.DisplayUtils
;
45 import com
.owncloud
.android
.utils
.Log_OC
;
49 * An Activity that allows the user to change the application's settings.
51 * @author Bartek Przybylski
52 * @author David A. Velasco
54 public class Preferences
extends SherlockPreferenceActivity
{
56 private static final String TAG
= "OwnCloudPreferences";
58 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
60 private DbHandler mDbHandler
;
61 private CheckBoxPreference pCode
;
62 //private CheckBoxPreference pLogging;
63 //private Preference pLoggingHistory;
64 private Preference pAboutApp
;
66 private Account mPreviousAccount
= null
;
69 @SuppressWarnings("deprecation")
71 public void onCreate(Bundle savedInstanceState
) {
72 super.onCreate(savedInstanceState
);
73 mDbHandler
= new DbHandler(getBaseContext());
74 addPreferencesFromResource(R
.xml
.preferences
);
75 //populateAccountList();
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 PreferenceCategory accountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
89 // Populate the accounts category with the list of accounts
90 createAccountsCheckboxPreferences(accountsPrefCategory
);
92 // Show Create Account if Multiaccount is enabled
93 if (!getResources().getBoolean(R
.bool
.multiaccount_support
)) {
94 PreferenceMultiline addAccountPreference
= (PreferenceMultiline
) findPreference("add_account");
95 accountsPrefCategory
.removePreference(addAccountPreference
);
98 Preference pAddAccount
= findPreference("add_account");
99 if (pAddAccount
!= null
)
100 pAddAccount
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
102 public boolean onPreferenceClick(Preference preference
) {
103 AccountManager am
= AccountManager
.get(getApplicationContext());
104 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
110 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
112 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
114 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
115 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
116 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
117 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
126 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
128 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
129 Preference pHelp
= findPreference("help");
132 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
134 public boolean onPreferenceClick(Preference preference
) {
135 String helpWeb
=(String
) getText(R
.string
.url_help
);
136 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
137 Uri uriUrl
= Uri
.parse(helpWeb
);
138 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
139 startActivity(intent
);
145 preferenceCategory
.removePreference(pHelp
);
151 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
152 Preference pRecommend
= findPreference("recommend");
153 if (pRecommend
!= null
){
154 if (recommendEnabled
) {
155 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
157 public boolean onPreferenceClick(Preference preference
) {
159 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
160 intent
.setType("text/plain");
161 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
162 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
164 String appName
= getString(R
.string
.app_name
);
165 String downloadUrl
= getString(R
.string
.url_app_download
);
166 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
167 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
169 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
170 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
172 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
173 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
174 startActivity(intent
);
182 preferenceCategory
.removePreference(pRecommend
);
187 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
188 Preference pFeedback
= findPreference("feedback");
189 if (pFeedback
!= null
){
190 if (feedbackEnabled
) {
191 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
193 public boolean onPreferenceClick(Preference preference
) {
194 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
195 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
196 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
197 intent
.setType("text/plain");
198 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
200 intent
.setData(Uri
.parse(feedbackMail
));
201 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
202 startActivity(intent
);
208 preferenceCategory
.removePreference(pFeedback
);
213 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
214 Preference pImprint
= findPreference("imprint");
215 if (pImprint
!= null
) {
216 if (imprintEnabled
) {
217 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
219 public boolean onPreferenceClick(Preference preference
) {
220 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
221 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
222 Uri uriUrl
= Uri
.parse(imprintWeb
);
223 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
224 startActivity(intent
);
226 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
231 preferenceCategory
.removePreference(pImprint
);
236 pAboutApp
= (Preference
) findPreference("about_app");
237 if (pAboutApp
!= null
) {
238 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
241 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
242 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
243 } catch (NameNotFoundException e
) {
244 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
248 /* DISABLED FOR RELEASE UNTIL FIXED
249 pLogging = (CheckBoxPreference) findPreference("log_to_file");
250 if (pLogging != null) {
251 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
253 public boolean onPreferenceChange(Preference preference, Object newValue) {
255 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
257 if(!pLogging.isChecked()) {
258 Log_OC.d("Debug", "start logging");
259 Log_OC.v("PATH", logpath);
260 Log_OC.startLogging(logpath);
263 Log_OC.d("Debug", "stop logging");
264 Log_OC.stopLogging();
271 pLoggingHistory = (Preference) findPreference("log_history");
272 if (pLoggingHistory != null) {
273 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
276 public boolean onPreferenceClick(Preference preference) {
277 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
278 startActivity(intent);
288 protected void onResume() {
290 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
291 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
292 pCode
.setChecked(state
);
296 public boolean onCreateOptionsMenu(Menu menu
) {
297 super.onCreateOptionsMenu(menu
);
302 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
303 super.onMenuItemSelected(featureId
, item
);
306 switch (item
.getItemId()) {
307 case android
.R
.id
.home
:
308 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
309 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
310 startActivity(intent
);
313 Log_OC
.w(TAG
, "Unknown menu item triggered");
320 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
321 super.onActivityResult(requestCode
, resultCode
, data
);
325 protected void onDestroy() {
331 * Create the list of accounts that have been added into the app
333 * @param accountsPrefCategory
335 private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory
) {
336 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
337 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
338 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
339 for (Account a
: accounts
) {
340 CheckBoxPreference checkBoxPreference
= new CheckBoxPreference(this);
341 checkBoxPreference
.setKey(a
.name
);
342 checkBoxPreference
.setTitle(a
.name
);
344 // Check the current account that is being used
345 if (a
.name
.equals(currentAccount
.name
)) {
346 checkBoxPreference
.setChecked(true
);
349 checkBoxPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
351 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
352 String key
= preference
.getKey();
353 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
354 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
355 for (Account a
: accounts
) {
356 @SuppressWarnings("deprecation")
357 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
358 if (key
.equals(a
.name
)) {
360 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
365 return (Boolean
) newValue
;
369 accountsPrefCategory
.addPreference(checkBoxPreference
);
374 protected void onPause() {
375 if (this.isFinishing()) {
376 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
377 if ((mPreviousAccount
== null
&& current
!= null
)
378 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
379 // the account set as default changed since this activity was
382 // restart the main activity
383 Intent i
= new Intent(this, FileDisplayActivity
.class);
384 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);