Include 'add account' button after last account listed
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
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;
34
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;
45
46
47 /**
48 * An Activity that allows the user to change the application's settings.
49 *
50 * @author Bartek Przybylski
51 * @author David A. Velasco
52 */
53 public class Preferences extends SherlockPreferenceActivity {
54
55 private static final String TAG = "OwnCloudPreferences";
56
57 private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
58
59 private DbHandler mDbHandler;
60 private CheckBoxPreference pCode;
61 //private CheckBoxPreference pLogging;
62 //private Preference pLoggingHistory;
63 private Preference pAboutApp;
64
65 private Account mPreviousAccount = null;
66 private PreferenceCategory mAccountsPrefCategory = null;
67
68
69 @SuppressWarnings("deprecation")
70 @Override
71 public void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73 mDbHandler = new DbHandler(getBaseContext());
74 addPreferencesFromResource(R.xml.preferences);
75
76 ActionBar actionBar = getSherlock().getActionBar();
77 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
78 actionBar.setDisplayHomeAsUpEnabled(true);
79
80 if (savedInstanceState != null) {
81 mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
82 } else {
83 mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
84 }
85
86 // Load the accounts category for adding the list of accounts
87 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
88
89 // Populate the accounts category with the list of accounts
90 createAccountsCheckboxPreferences();
91
92 // Add Create Account preference if Multiaccount is enabled
93 if (getResources().getBoolean(R.bool.multiaccount_support)) {
94 createAddAccountPreference();
95 }
96
97 pCode = (CheckBoxPreference) findPreference("set_pincode");
98 if (pCode != null){
99 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
100 @Override
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());
105 startActivity(i);
106
107 return true;
108 }
109 });
110
111 }
112
113 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
114
115 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
116 Preference pHelp = findPreference("help");
117 if (pHelp != null ){
118 if (helpEnabled) {
119 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
120 @Override
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);
127 }
128 return true;
129 }
130 });
131 } else {
132 preferenceCategory.removePreference(pHelp);
133 }
134
135 }
136
137
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() {
143 @Override
144 public boolean onPreferenceClick(Preference preference) {
145
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);
150
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('@'));
155
156 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
157 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
158
159 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
160 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
161 startActivity(intent);
162
163
164 return(true);
165
166 }
167 });
168 } else {
169 preferenceCategory.removePreference(pRecommend);
170 }
171
172 }
173
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() {
179 @Override
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);
186
187 intent.setData(Uri.parse(feedbackMail));
188 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
189 startActivity(intent);
190
191 return true;
192 }
193 });
194 } else {
195 preferenceCategory.removePreference(pFeedback);
196 }
197
198 }
199
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() {
205 @Override
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);
212 }
213 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
214 return true;
215 }
216 });
217 } else {
218 preferenceCategory.removePreference(pImprint);
219 }
220 }
221
222 /* About App */
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)));
226 PackageInfo pkg;
227 try {
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);
232 }
233 }
234
235 /* DISABLED FOR RELEASE UNTIL FIXED
236 pLogging = (CheckBoxPreference) findPreference("log_to_file");
237 if (pLogging != null) {
238 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
239 @Override
240 public boolean onPreferenceChange(Preference preference, Object newValue) {
241
242 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
243
244 if(!pLogging.isChecked()) {
245 Log_OC.d("Debug", "start logging");
246 Log_OC.v("PATH", logpath);
247 Log_OC.startLogging(logpath);
248 }
249 else {
250 Log_OC.d("Debug", "stop logging");
251 Log_OC.stopLogging();
252 }
253 return true;
254 }
255 });
256 }
257
258 pLoggingHistory = (Preference) findPreference("log_history");
259 if (pLoggingHistory != null) {
260 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
261
262 @Override
263 public boolean onPreferenceClick(Preference preference) {
264 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
265 startActivity(intent);
266 return true;
267 }
268 });
269 }
270 */
271
272 }
273
274 @Override
275 protected void onResume() {
276 super.onResume();
277 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
278 boolean state = appPrefs.getBoolean("set_pincode", false);
279 pCode.setChecked(state);
280 }
281
282 @Override
283 public boolean onCreateOptionsMenu(Menu menu) {
284 super.onCreateOptionsMenu(menu);
285 return true;
286 }
287
288 @Override
289 public boolean onMenuItemSelected(int featureId, MenuItem item) {
290 super.onMenuItemSelected(featureId, item);
291 Intent intent;
292
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);
298 break;
299 default:
300 Log_OC.w(TAG, "Unknown menu item triggered");
301 return false;
302 }
303 return true;
304 }
305
306 @Override
307 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
308 super.onActivityResult(requestCode, resultCode, data);
309 }
310
311 @Override
312 protected void onDestroy() {
313 mDbHandler.close();
314 super.onDestroy();
315 }
316
317 /**
318 * Create the list of accounts that have been added into the app
319 */
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);
328
329 // Check the current account that is being used
330 if (a.name.equals(currentAccount.name)) {
331 checkBoxPreference.setChecked(true);
332 }
333
334 checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
335 @Override
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)) {
344 p.setChecked(true);
345 AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
346 } else {
347 p.setChecked(false);
348 }
349 }
350 return (Boolean) newValue;
351 }
352 });
353
354 mAccountsPrefCategory.addPreference(checkBoxPreference);
355 }
356 }
357
358 /**
359 * Create the preference for allow adding new accounts
360 */
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);
366
367 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
368 @Override
369 public boolean onPreferenceClick(Preference preference) {
370 AccountManager am = AccountManager.get(getApplicationContext());
371 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
372 return true;
373 }
374 });
375
376 }
377
378 @Override
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
385 // created
386
387 // restart the main activity
388 Intent i = new Intent(this, FileDisplayActivity.class);
389 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
390 startActivity(i);
391 }
392 }
393 super.onPause();
394 }
395
396 }