Update the list of accounts in Preferences after adding a new account
[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 pCode = (CheckBoxPreference) findPreference("set_pincode");
90 if (pCode != null){
91 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
92 @Override
93 public boolean onPreferenceChange(Preference preference, Object newValue) {
94 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
95 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
96 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
97 startActivity(i);
98
99 return true;
100 }
101 });
102
103 }
104
105 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
106
107 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
108 Preference pHelp = findPreference("help");
109 if (pHelp != null ){
110 if (helpEnabled) {
111 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
112 @Override
113 public boolean onPreferenceClick(Preference preference) {
114 String helpWeb =(String) getText(R.string.url_help);
115 if (helpWeb != null && helpWeb.length() > 0) {
116 Uri uriUrl = Uri.parse(helpWeb);
117 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
118 startActivity(intent);
119 }
120 return true;
121 }
122 });
123 } else {
124 preferenceCategory.removePreference(pHelp);
125 }
126
127 }
128
129
130 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
131 Preference pRecommend = findPreference("recommend");
132 if (pRecommend != null){
133 if (recommendEnabled) {
134 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
135 @Override
136 public boolean onPreferenceClick(Preference preference) {
137
138 Intent intent = new Intent(Intent.ACTION_SENDTO);
139 intent.setType("text/plain");
140 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
141 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
142
143 String appName = getString(R.string.app_name);
144 String downloadUrl = getString(R.string.url_app_download);
145 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
146 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
147
148 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
149 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
150
151 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
152 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
153 startActivity(intent);
154
155
156 return(true);
157
158 }
159 });
160 } else {
161 preferenceCategory.removePreference(pRecommend);
162 }
163
164 }
165
166 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
167 Preference pFeedback = findPreference("feedback");
168 if (pFeedback != null){
169 if (feedbackEnabled) {
170 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
171 @Override
172 public boolean onPreferenceClick(Preference preference) {
173 String feedbackMail =(String) getText(R.string.mail_feedback);
174 String feedback =(String) getText(R.string.prefs_feedback);
175 Intent intent = new Intent(Intent.ACTION_SENDTO);
176 intent.setType("text/plain");
177 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
178
179 intent.setData(Uri.parse(feedbackMail));
180 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
181 startActivity(intent);
182
183 return true;
184 }
185 });
186 } else {
187 preferenceCategory.removePreference(pFeedback);
188 }
189
190 }
191
192 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
193 Preference pImprint = findPreference("imprint");
194 if (pImprint != null) {
195 if (imprintEnabled) {
196 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
197 @Override
198 public boolean onPreferenceClick(Preference preference) {
199 String imprintWeb = (String) getText(R.string.url_imprint);
200 if (imprintWeb != null && imprintWeb.length() > 0) {
201 Uri uriUrl = Uri.parse(imprintWeb);
202 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
203 startActivity(intent);
204 }
205 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
206 return true;
207 }
208 });
209 } else {
210 preferenceCategory.removePreference(pImprint);
211 }
212 }
213
214 /* About App */
215 pAboutApp = (Preference) findPreference("about_app");
216 if (pAboutApp != null) {
217 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
218 PackageInfo pkg;
219 try {
220 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
221 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
222 } catch (NameNotFoundException e) {
223 Log_OC.e(TAG, "Error while showing about dialog", e);
224 }
225 }
226
227 /* DISABLED FOR RELEASE UNTIL FIXED
228 pLogging = (CheckBoxPreference) findPreference("log_to_file");
229 if (pLogging != null) {
230 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
231 @Override
232 public boolean onPreferenceChange(Preference preference, Object newValue) {
233
234 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
235
236 if(!pLogging.isChecked()) {
237 Log_OC.d("Debug", "start logging");
238 Log_OC.v("PATH", logpath);
239 Log_OC.startLogging(logpath);
240 }
241 else {
242 Log_OC.d("Debug", "stop logging");
243 Log_OC.stopLogging();
244 }
245 return true;
246 }
247 });
248 }
249
250 pLoggingHistory = (Preference) findPreference("log_history");
251 if (pLoggingHistory != null) {
252 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
253
254 @Override
255 public boolean onPreferenceClick(Preference preference) {
256 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
257 startActivity(intent);
258 return true;
259 }
260 });
261 }
262 */
263
264 }
265
266 @Override
267 protected void onResume() {
268 super.onResume();
269 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
270 boolean state = appPrefs.getBoolean("set_pincode", false);
271 pCode.setChecked(state);
272
273 // Populate the accounts category with the list of accounts
274 createAccountsCheckboxPreferences();
275 }
276
277 @Override
278 public boolean onCreateOptionsMenu(Menu menu) {
279 super.onCreateOptionsMenu(menu);
280 return true;
281 }
282
283 @Override
284 public boolean onMenuItemSelected(int featureId, MenuItem item) {
285 super.onMenuItemSelected(featureId, item);
286 Intent intent;
287
288 switch (item.getItemId()) {
289 case android.R.id.home:
290 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
291 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
292 startActivity(intent);
293 break;
294 default:
295 Log_OC.w(TAG, "Unknown menu item triggered");
296 return false;
297 }
298 return true;
299 }
300
301 @Override
302 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
303 super.onActivityResult(requestCode, resultCode, data);
304 }
305
306 @Override
307 protected void onDestroy() {
308 mDbHandler.close();
309 super.onDestroy();
310 }
311
312 /**
313 * Create the list of accounts that has been added into the app
314 */
315 private void createAccountsCheckboxPreferences() {
316
317 // Remove accounts in case list is refreshing for avoiding to have
318 // duplicate items
319 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
320 mAccountsPrefCategory.removeAll();
321 }
322
323 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
324 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
325 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
326 for (Account a : accounts) {
327 CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
328 checkBoxPreference.setKey(a.name);
329 checkBoxPreference.setTitle(a.name);
330
331 // Check the current account that is being used
332 if (a.name.equals(currentAccount.name)) {
333 checkBoxPreference.setChecked(true);
334 }
335
336 checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
337 @Override
338 public boolean onPreferenceChange(Preference preference, Object newValue) {
339 String key = preference.getKey();
340 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
341 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
342 for (Account a : accounts) {
343 @SuppressWarnings("deprecation")
344 CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
345 if (key.equals(a.name)) {
346 p.setChecked(true);
347 AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
348 } else {
349 p.setChecked(false);
350 }
351 }
352 return (Boolean) newValue;
353 }
354 });
355
356 mAccountsPrefCategory.addPreference(checkBoxPreference);
357 }
358
359 // Add Create Account preference at the end of account list if Multiaccount is enabled
360 if (getResources().getBoolean(R.bool.multiaccount_support)) {
361 createAddAccountPreference();
362 }
363
364 }
365
366 /**
367 * Create the preference for allow adding new accounts
368 */
369 private void createAddAccountPreference() {
370 Preference addAccountPref = new Preference(this);
371 addAccountPref.setKey("add_account");
372 addAccountPref.setTitle(getString(R.string.prefs_add_account));
373 mAccountsPrefCategory.addPreference(addAccountPref);
374
375 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
376 @Override
377 public boolean onPreferenceClick(Preference preference) {
378 AccountManager am = AccountManager.get(getApplicationContext());
379 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
380 return true;
381 }
382 });
383
384 }
385
386 @Override
387 protected void onPause() {
388 if (this.isFinishing()) {
389 Account current = AccountUtils.getCurrentOwnCloudAccount(this);
390 if ((mPreviousAccount == null && current != null)
391 || (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
392 // the account set as default changed since this activity was
393 // created
394
395 // restart the main activity
396 Intent i = new Intent(this, FileDisplayActivity.class);
397 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
398 startActivity(i);
399 }
400 }
401 super.onPause();
402 }
403
404 }