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";
56 private DbHandler mDbHandler
;
57 private CheckBoxPreference pCode
;
58 //private CheckBoxPreference pLogging;
59 //private Preference pLoggingHistory;
60 private Preference pAboutApp
;
63 @SuppressWarnings("deprecation")
65 public void onCreate(Bundle savedInstanceState
) {
66 super.onCreate(savedInstanceState
);
67 mDbHandler
= new DbHandler(getBaseContext());
68 addPreferencesFromResource(R
.xml
.preferences
);
69 //populateAccountList();
70 ActionBar actionBar
= getSherlock().getActionBar();
71 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
72 actionBar
.setDisplayHomeAsUpEnabled(true
);
74 // Load the accounts category for adding the list of accounts
75 PreferenceCategory accountsPrefCategory
= (PreferenceCategory
) findPreference("accounts_category");
77 // Populate the accounts category with the list of accounts
78 createAccountsCheckboxPreferences(accountsPrefCategory
);
81 Preference p
= findPreference("manage_account");
83 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
85 public boolean onPreferenceClick(Preference preference
) {
86 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
92 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
94 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
96 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
97 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
98 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
99 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
110 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
112 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
113 Preference pHelp
= findPreference("help");
116 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
118 public boolean onPreferenceClick(Preference preference
) {
119 String helpWeb
=(String
) getText(R
.string
.url_help
);
120 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
121 Uri uriUrl
= Uri
.parse(helpWeb
);
122 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
123 startActivity(intent
);
129 preferenceCategory
.removePreference(pHelp
);
135 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
136 Preference pRecommend
= findPreference("recommend");
137 if (pRecommend
!= null
){
138 if (recommendEnabled
) {
139 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
141 public boolean onPreferenceClick(Preference preference
) {
143 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
144 intent
.setType("text/plain");
145 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
146 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
148 String appName
= getString(R
.string
.app_name
);
149 String downloadUrl
= getString(R
.string
.url_app_download
);
150 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this);
151 String username
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@'));
153 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
154 String recommendText
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
);
156 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
157 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
158 startActivity(intent
);
166 preferenceCategory
.removePreference(pRecommend
);
171 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
172 Preference pFeedback
= findPreference("feedback");
173 if (pFeedback
!= null
){
174 if (feedbackEnabled
) {
175 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
177 public boolean onPreferenceClick(Preference preference
) {
178 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
179 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
180 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
181 intent
.setType("text/plain");
182 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
184 intent
.setData(Uri
.parse(feedbackMail
));
185 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
186 startActivity(intent
);
192 preferenceCategory
.removePreference(pFeedback
);
197 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
198 Preference pImprint
= findPreference("imprint");
199 if (pImprint
!= null
) {
200 if (imprintEnabled
) {
201 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
203 public boolean onPreferenceClick(Preference preference
) {
204 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
205 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
206 Uri uriUrl
= Uri
.parse(imprintWeb
);
207 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
208 startActivity(intent
);
210 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
215 preferenceCategory
.removePreference(pImprint
);
220 pAboutApp
= (Preference
) findPreference("about_app");
221 if (pAboutApp
!= null
) {
222 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
225 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
226 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
227 } catch (NameNotFoundException e
) {
228 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
232 /* DISABLED FOR RELEASE UNTIL FIXED
233 pLogging = (CheckBoxPreference) findPreference("log_to_file");
234 if (pLogging != null) {
235 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
237 public boolean onPreferenceChange(Preference preference, Object newValue) {
239 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
241 if(!pLogging.isChecked()) {
242 Log_OC.d("Debug", "start logging");
243 Log_OC.v("PATH", logpath);
244 Log_OC.startLogging(logpath);
247 Log_OC.d("Debug", "stop logging");
248 Log_OC.stopLogging();
255 pLoggingHistory = (Preference) findPreference("log_history");
256 if (pLoggingHistory != null) {
257 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
260 public boolean onPreferenceClick(Preference preference) {
261 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
262 startActivity(intent);
272 protected void onResume() {
274 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
275 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
276 pCode
.setChecked(state
);
280 public boolean onCreateOptionsMenu(Menu menu
) {
281 super.onCreateOptionsMenu(menu
);
286 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
287 super.onMenuItemSelected(featureId
, item
);
290 switch (item
.getItemId()) {
291 case android
.R
.id
.home
:
292 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
293 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
294 startActivity(intent
);
297 Log_OC
.w(TAG
, "Unknown menu item triggered");
304 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
305 super.onActivityResult(requestCode
, resultCode
, data
);
309 protected void onDestroy() {
315 * Create the list of accounts that have been added into the app
317 * @param accountsPrefCategory
319 private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory
) {
320 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
321 Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType());
322 Account currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
323 for (Account a
: accounts
) {
324 CheckBoxPreference checkBoxPreference
= new CheckBoxPreference(this);
325 checkBoxPreference
.setKey(a
.name
);
326 checkBoxPreference
.setTitle(a
.name
);
328 // Check the current account that is being used
329 if (a
.name
.equals(currentAccount
.name
)) {
330 checkBoxPreference
.setChecked(true
);
333 accountsPrefCategory
.addPreference(checkBoxPreference
);