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
) {
105 * Intent intent = new Intent(
106 * android.provider.Settings.ACTION_ADD_ACCOUNT);
107 * intent.putExtra("authorities", new String[] {
108 * MainApp.getAuthTokenType() }); startActivity(intent);
110 AccountManager am
= AccountManager
.get(getApplicationContext());
111 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
116 Preference pManageAccount
= findPreference("manage_account");
117 if (pManageAccount
!= null
)
118 pManageAccount
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
120 public boolean onPreferenceClick(Preference preference
) {
121 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
127 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
129 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
131 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
132 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
133 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
134 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
145 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
147 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
148 Preference pHelp
= findPreference("help");
151 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
153 public boolean onPreferenceClick(Preference preference
) {
154 String helpWeb
=(String
) getText(R
.string
.url_help
);
155 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
156 Uri uriUrl
= Uri
.parse(helpWeb
);
157 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
158 startActivity(intent
);
164 preferenceCategory
.removePreference(pHelp
);
170 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
171 Preference pRecommend
= findPreference("recommend");
172 if (pRecommend
!= null
){
173 if (recommendEnabled
) {
174 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
176 public boolean onPreferenceClick(Preference preference
) {
178 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
179 intent
.setType("text/plain");
180 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
181 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
183 String appName
= getString(R
.string
.app_name
);
184 String downloadUrl
= getString(R
.string
.url_app_download
);
185 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
186 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
188 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
189 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
191 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
192 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
193 startActivity(intent
);
201 preferenceCategory
.removePreference(pRecommend
);
206 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
207 Preference pFeedback
= findPreference("feedback");
208 if (pFeedback
!= null
){
209 if (feedbackEnabled
) {
210 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
212 public boolean onPreferenceClick(Preference preference
) {
213 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
214 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
215 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
216 intent
.setType("text/plain");
217 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
219 intent
.setData(Uri
.parse(feedbackMail
));
220 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
221 startActivity(intent
);
227 preferenceCategory
.removePreference(pFeedback
);
232 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
233 Preference pImprint
= findPreference("imprint");
234 if (pImprint
!= null
) {
235 if (imprintEnabled
) {
236 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
238 public boolean onPreferenceClick(Preference preference
) {
239 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
240 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
241 Uri uriUrl
= Uri
.parse(imprintWeb
);
242 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
243 startActivity(intent
);
245 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
250 preferenceCategory
.removePreference(pImprint
);
255 pAboutApp
= (Preference
) findPreference("about_app");
256 if (pAboutApp
!= null
) {
257 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
260 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
261 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
262 } catch (NameNotFoundException e
) {
263 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
267 /* DISABLED FOR RELEASE UNTIL FIXED
268 pLogging = (CheckBoxPreference) findPreference("log_to_file");
269 if (pLogging != null) {
270 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
272 public boolean onPreferenceChange(Preference preference, Object newValue) {
274 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
276 if(!pLogging.isChecked()) {
277 Log_OC.d("Debug", "start logging");
278 Log_OC.v("PATH", logpath);
279 Log_OC.startLogging(logpath);
282 Log_OC.d("Debug", "stop logging");
283 Log_OC.stopLogging();
290 pLoggingHistory = (Preference) findPreference("log_history");
291 if (pLoggingHistory != null) {
292 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
295 public boolean onPreferenceClick(Preference preference) {
296 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
297 startActivity(intent);
307 protected void onResume() {
309 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
310 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
311 pCode
.setChecked(state
);
315 public boolean onCreateOptionsMenu(Menu menu
) {
316 super.onCreateOptionsMenu(menu
);
321 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
322 super.onMenuItemSelected(featureId
, item
);
325 switch (item
.getItemId()) {
326 case android
.R
.id
.home
:
327 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
328 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
329 startActivity(intent
);
332 Log_OC
.w(TAG
, "Unknown menu item triggered");
339 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
340 super.onActivityResult(requestCode
, resultCode
, data
);
344 protected void onDestroy() {
350 * Create the list of accounts that have been added into the app
352 * @param accountsPrefCategory
354 private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory
) {
355 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
356 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
357 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
358 for (Account a
: accounts
) {
359 CheckBoxPreference checkBoxPreference
= new CheckBoxPreference(this);
360 checkBoxPreference
.setKey(a
.name
);
361 checkBoxPreference
.setTitle(a
.name
);
363 // Check the current account that is being used
364 if (a
.name
.equals(currentAccount
.name
)) {
365 checkBoxPreference
.setChecked(true
);
368 checkBoxPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
370 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
371 String key
= preference
.getKey();
372 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
373 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
374 for (Account a
: accounts
) {
375 @SuppressWarnings("deprecation")
376 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
377 if (key
.equals(a
.name
)) {
379 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
384 return (Boolean
) newValue
;
388 accountsPrefCategory
.addPreference(checkBoxPreference
);
393 protected void onPause() {
394 if (this.isFinishing()) {
395 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
396 if ((mPreviousAccount
== null
&& current
!= null
)
397 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
398 // the account set as default changed since this activity was
401 // restart the main activity
402 Intent i
= new Intent(this, FileDisplayActivity
.class);
403 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);