Show all the active accounts on settings view and make behaviour of accounts checkbox...
[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.ui.PreferenceMultiline;
44 import com.owncloud.android.utils.DisplayUtils;
45 import com.owncloud.android.utils.Log_OC;
46
47
48 /**
49 * An Activity that allows the user to change the application's settings.
50 *
51 * @author Bartek Przybylski
52 * @author David A. Velasco
53 */
54 public class Preferences extends SherlockPreferenceActivity {
55
56 private static final String TAG = "OwnCloudPreferences";
57
58 private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
59
60 private DbHandler mDbHandler;
61 private CheckBoxPreference pCode;
62 //private CheckBoxPreference pLogging;
63 //private Preference pLoggingHistory;
64 private Preference pAboutApp;
65
66 private Account mPreviousAccount = 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 //populateAccountList();
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 PreferenceCategory accountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
88
89 // Populate the accounts category with the list of accounts
90 createAccountsCheckboxPreferences(accountsPrefCategory);
91
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);
96 }
97
98 Preference pAddAccount = findPreference("add_account");
99 if (pAddAccount != null)
100 pAddAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
101 @Override
102 public boolean onPreferenceClick(Preference preference) {
103
104 /*
105 * Intent intent = new Intent(
106 * android.provider.Settings.ACTION_ADD_ACCOUNT);
107 * intent.putExtra("authorities", new String[] {
108 * MainApp.getAuthTokenType() }); startActivity(intent);
109 */
110 AccountManager am = AccountManager.get(getApplicationContext());
111 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
112 return true;
113 }
114 });
115
116 Preference pManageAccount = findPreference("manage_account");
117 if (pManageAccount != null)
118 pManageAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
119 @Override
120 public boolean onPreferenceClick(Preference preference) {
121 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
122 startActivity(i);
123 return true;
124 }
125 });
126
127 pCode = (CheckBoxPreference) findPreference("set_pincode");
128 if (pCode != null){
129 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
130 @Override
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());
135 startActivity(i);
136
137 return true;
138 }
139 });
140
141 }
142
143
144
145 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
146
147 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
148 Preference pHelp = findPreference("help");
149 if (pHelp != null ){
150 if (helpEnabled) {
151 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
152 @Override
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);
159 }
160 return true;
161 }
162 });
163 } else {
164 preferenceCategory.removePreference(pHelp);
165 }
166
167 }
168
169
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() {
175 @Override
176 public boolean onPreferenceClick(Preference preference) {
177
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);
182
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('@'));
187
188 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
189 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
190
191 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
192 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
193 startActivity(intent);
194
195
196 return(true);
197
198 }
199 });
200 } else {
201 preferenceCategory.removePreference(pRecommend);
202 }
203
204 }
205
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() {
211 @Override
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);
218
219 intent.setData(Uri.parse(feedbackMail));
220 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
221 startActivity(intent);
222
223 return true;
224 }
225 });
226 } else {
227 preferenceCategory.removePreference(pFeedback);
228 }
229
230 }
231
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() {
237 @Override
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);
244 }
245 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
246 return true;
247 }
248 });
249 } else {
250 preferenceCategory.removePreference(pImprint);
251 }
252 }
253
254 /* About App */
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)));
258 PackageInfo pkg;
259 try {
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);
264 }
265 }
266
267 /* DISABLED FOR RELEASE UNTIL FIXED
268 pLogging = (CheckBoxPreference) findPreference("log_to_file");
269 if (pLogging != null) {
270 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
271 @Override
272 public boolean onPreferenceChange(Preference preference, Object newValue) {
273
274 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
275
276 if(!pLogging.isChecked()) {
277 Log_OC.d("Debug", "start logging");
278 Log_OC.v("PATH", logpath);
279 Log_OC.startLogging(logpath);
280 }
281 else {
282 Log_OC.d("Debug", "stop logging");
283 Log_OC.stopLogging();
284 }
285 return true;
286 }
287 });
288 }
289
290 pLoggingHistory = (Preference) findPreference("log_history");
291 if (pLoggingHistory != null) {
292 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
293
294 @Override
295 public boolean onPreferenceClick(Preference preference) {
296 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
297 startActivity(intent);
298 return true;
299 }
300 });
301 }
302 */
303
304 }
305
306 @Override
307 protected void onResume() {
308 super.onResume();
309 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
310 boolean state = appPrefs.getBoolean("set_pincode", false);
311 pCode.setChecked(state);
312 }
313
314 @Override
315 public boolean onCreateOptionsMenu(Menu menu) {
316 super.onCreateOptionsMenu(menu);
317 return true;
318 }
319
320 @Override
321 public boolean onMenuItemSelected(int featureId, MenuItem item) {
322 super.onMenuItemSelected(featureId, item);
323 Intent intent;
324
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);
330 break;
331 default:
332 Log_OC.w(TAG, "Unknown menu item triggered");
333 return false;
334 }
335 return true;
336 }
337
338 @Override
339 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
340 super.onActivityResult(requestCode, resultCode, data);
341 }
342
343 @Override
344 protected void onDestroy() {
345 mDbHandler.close();
346 super.onDestroy();
347 }
348
349 /**
350 * Create the list of accounts that have been added into the app
351 *
352 * @param accountsPrefCategory
353 */
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);
362
363 // Check the current account that is being used
364 if (a.name.equals(currentAccount.name)) {
365 checkBoxPreference.setChecked(true);
366 }
367
368 checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
369 @Override
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)) {
378 p.setChecked(true);
379 AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
380 } else {
381 p.setChecked(false);
382 }
383 }
384 return (Boolean) newValue;
385 }
386 });
387
388 accountsPrefCategory.addPreference(checkBoxPreference);
389 }
390 }
391
392 @Override
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
399 // created
400
401 // restart the main activity
402 Intent i = new Intent(this, FileDisplayActivity.class);
403 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
404 startActivity(i);
405 }
406 }
407 super.onPause();
408 }
409
410 }