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 // Populate the accounts category with the list of accounts
90 createAccountsCheckboxPreferences();
92 // Add Create Account preference if Multiaccount is enabled
93 if (getResources().getBoolean(R
.bool
.multiaccount_support
)) {
94 createAddAccountPreference();
97 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
99 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
101 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
102 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
103 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
104 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
113 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
115 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
116 Preference pHelp
= findPreference("help");
119 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
121 public boolean onPreferenceClick(Preference preference
) {
122 String helpWeb
=(String
) getText(R
.string
.url_help
);
123 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
124 Uri uriUrl
= Uri
.parse(helpWeb
);
125 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
126 startActivity(intent
);
132 preferenceCategory
.removePreference(pHelp
);
138 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
139 Preference pRecommend
= findPreference("recommend");
140 if (pRecommend
!= null
){
141 if (recommendEnabled
) {
142 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
144 public boolean onPreferenceClick(Preference preference
) {
146 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
147 intent
.setType("text/plain");
148 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
149 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
151 String appName
= getString(R
.string
.app_name
);
152 String downloadUrl
= getString(R
.string
.url_app_download
);
153 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
154 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
156 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
157 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
159 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
160 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
161 startActivity(intent
);
169 preferenceCategory
.removePreference(pRecommend
);
174 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
175 Preference pFeedback
= findPreference("feedback");
176 if (pFeedback
!= null
){
177 if (feedbackEnabled
) {
178 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
180 public boolean onPreferenceClick(Preference preference
) {
181 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
182 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
183 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
184 intent
.setType("text/plain");
185 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
187 intent
.setData(Uri
.parse(feedbackMail
));
188 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
189 startActivity(intent
);
195 preferenceCategory
.removePreference(pFeedback
);
200 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
201 Preference pImprint
= findPreference("imprint");
202 if (pImprint
!= null
) {
203 if (imprintEnabled
) {
204 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
206 public boolean onPreferenceClick(Preference preference
) {
207 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
208 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
209 Uri uriUrl
= Uri
.parse(imprintWeb
);
210 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
211 startActivity(intent
);
213 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
218 preferenceCategory
.removePreference(pImprint
);
223 pAboutApp
= (Preference
) findPreference("about_app");
224 if (pAboutApp
!= null
) {
225 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
228 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
229 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
230 } catch (NameNotFoundException e
) {
231 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
235 /* DISABLED FOR RELEASE UNTIL FIXED
236 pLogging = (CheckBoxPreference) findPreference("log_to_file");
237 if (pLogging != null) {
238 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
240 public boolean onPreferenceChange(Preference preference, Object newValue) {
242 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
244 if(!pLogging.isChecked()) {
245 Log_OC.d("Debug", "start logging");
246 Log_OC.v("PATH", logpath);
247 Log_OC.startLogging(logpath);
250 Log_OC.d("Debug", "stop logging");
251 Log_OC.stopLogging();
258 pLoggingHistory = (Preference) findPreference("log_history");
259 if (pLoggingHistory != null) {
260 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
263 public boolean onPreferenceClick(Preference preference) {
264 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
265 startActivity(intent);
275 protected void onResume() {
277 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
278 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
279 pCode
.setChecked(state
);
283 public boolean onCreateOptionsMenu(Menu menu
) {
284 super.onCreateOptionsMenu(menu
);
289 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
290 super.onMenuItemSelected(featureId
, item
);
293 switch (item
.getItemId()) {
294 case android
.R
.id
.home
:
295 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
296 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
297 startActivity(intent
);
300 Log_OC
.w(TAG
, "Unknown menu item triggered");
307 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
308 super.onActivityResult(requestCode
, resultCode
, data
);
312 protected void onDestroy() {
318 * Create the list of accounts that have been added into the app
320 private void createAccountsCheckboxPreferences() {
321 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
322 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
323 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
324 for (Account a
: accounts
) {
325 CheckBoxPreference checkBoxPreference
= new CheckBoxPreference(this);
326 checkBoxPreference
.setKey(a
.name
);
327 checkBoxPreference
.setTitle(a
.name
);
329 // Check the current account that is being used
330 if (a
.name
.equals(currentAccount
.name
)) {
331 checkBoxPreference
.setChecked(true
);
334 checkBoxPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
336 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
337 String key
= preference
.getKey();
338 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
339 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
340 for (Account a
: accounts
) {
341 @SuppressWarnings("deprecation")
342 CheckBoxPreference p
= (CheckBoxPreference
) findPreference(a
.name
);
343 if (key
.equals(a
.name
)) {
345 AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), a
.name
);
350 return (Boolean
) newValue
;
354 mAccountsPrefCategory
.addPreference(checkBoxPreference
);
359 * Create the preference for allow adding new accounts
361 private void createAddAccountPreference() {
362 Preference addAccountPref
= new Preference(this);
363 addAccountPref
.setKey("add_account");
364 addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
));
365 mAccountsPrefCategory
.addPreference(addAccountPref
);
367 addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
369 public boolean onPreferenceClick(Preference preference
) {
370 AccountManager am
= AccountManager
.get(getApplicationContext());
371 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
);
379 protected void onPause() {
380 if (this.isFinishing()) {
381 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
382 if ((mPreviousAccount
== null
&& current
!= null
)
383 || (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
384 // the account set as default changed since this activity was
387 // restart the main activity
388 Intent i
= new Intent(this, FileDisplayActivity
.class);
389 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);