Merge branch 'develop' into better_loggin_system
[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.accounts.AccountManagerCallback;
23 import android.accounts.AccountManagerFuture;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.pm.PackageInfo;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.preference.CheckBoxPreference;
32 import android.preference.Preference;
33 import android.preference.Preference.OnPreferenceChangeListener;
34 import android.preference.Preference.OnPreferenceClickListener;
35 import android.preference.PreferenceCategory;
36 import android.preference.PreferenceManager;
37 import android.view.ContextMenu;
38 import android.view.ContextMenu.ContextMenuInfo;
39 import android.view.View;
40 import android.widget.AdapterView;
41 import android.widget.AdapterView.OnItemLongClickListener;
42 import android.widget.ListAdapter;
43 import android.widget.ListView;
44
45 import com.actionbarsherlock.app.ActionBar;
46 import com.actionbarsherlock.app.SherlockPreferenceActivity;
47 import com.actionbarsherlock.view.Menu;
48 import com.actionbarsherlock.view.MenuItem;
49 import com.owncloud.android.MainApp;
50 import com.owncloud.android.R;
51 import com.owncloud.android.authentication.AccountUtils;
52 import com.owncloud.android.authentication.AuthenticatorActivity;
53 import com.owncloud.android.db.DbHandler;
54 import com.owncloud.android.lib.common.utils.Log_OC;
55 import com.owncloud.android.ui.LongClickableCheckBoxPreference;
56 import com.owncloud.android.utils.DisplayUtils;
57
58
59 /**
60 * An Activity that allows the user to change the application's settings.
61 *
62 * @author Bartek Przybylski
63 * @author David A. Velasco
64 */
65 public class Preferences extends SherlockPreferenceActivity implements AccountManagerCallback<Boolean> {
66
67 private static final String TAG = "OwnCloudPreferences";
68
69 private DbHandler mDbHandler;
70 private CheckBoxPreference pCode;
71 private Preference pAboutApp;
72
73 private PreferenceCategory mAccountsPrefCategory = null;
74 private final Handler mHandler = new Handler();
75 private String mAccountName;
76 private boolean mShowContextMenu = false;
77
78
79 @SuppressWarnings("deprecation")
80 @Override
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83 mDbHandler = new DbHandler(getBaseContext());
84 addPreferencesFromResource(R.xml.preferences);
85
86 ActionBar actionBar = getSherlock().getActionBar();
87 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
88 actionBar.setDisplayHomeAsUpEnabled(true);
89 actionBar.setTitle(R.string.actionbar_settings);
90
91 // Load the accounts category for adding the list of accounts
92 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
93
94 ListView listView = getListView();
95 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
96 @Override
97 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
98 ListView listView = (ListView) parent;
99 ListAdapter listAdapter = listView.getAdapter();
100 Object obj = listAdapter.getItem(position);
101
102 if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
103 mShowContextMenu = true;
104 mAccountName = obj.toString();
105
106 Preferences.this.openContextMenu(listView);
107
108 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
109 return longListener.onLongClick(view);
110 }
111 return false;
112 }
113 });
114
115 // Register context menu for list of preferences.
116 registerForContextMenu(getListView());
117
118 pCode = (CheckBoxPreference) findPreference("set_pincode");
119 if (pCode != null){
120 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
121 @Override
122 public boolean onPreferenceChange(Preference preference, Object newValue) {
123 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
124 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
125 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
126 startActivity(i);
127
128 return true;
129 }
130 });
131
132 }
133
134 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
135
136 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
137 Preference pHelp = findPreference("help");
138 if (pHelp != null ){
139 if (helpEnabled) {
140 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
141 @Override
142 public boolean onPreferenceClick(Preference preference) {
143 String helpWeb =(String) getText(R.string.url_help);
144 if (helpWeb != null && helpWeb.length() > 0) {
145 Uri uriUrl = Uri.parse(helpWeb);
146 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
147 startActivity(intent);
148 }
149 return true;
150 }
151 });
152 } else {
153 preferenceCategory.removePreference(pHelp);
154 }
155
156 }
157
158
159 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
160 Preference pRecommend = findPreference("recommend");
161 if (pRecommend != null){
162 if (recommendEnabled) {
163 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
164 @Override
165 public boolean onPreferenceClick(Preference preference) {
166
167 Intent intent = new Intent(Intent.ACTION_SENDTO);
168 intent.setType("text/plain");
169 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
170 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
171
172 String appName = getString(R.string.app_name);
173 String downloadUrl = getString(R.string.url_app_download);
174 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
175 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
176
177 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
178 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
179
180 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
181 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
182 startActivity(intent);
183
184
185 return(true);
186
187 }
188 });
189 } else {
190 preferenceCategory.removePreference(pRecommend);
191 }
192
193 }
194
195 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
196 Preference pFeedback = findPreference("feedback");
197 if (pFeedback != null){
198 if (feedbackEnabled) {
199 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
200 @Override
201 public boolean onPreferenceClick(Preference preference) {
202 String feedbackMail =(String) getText(R.string.mail_feedback);
203 String feedback =(String) getText(R.string.prefs_feedback);
204 Intent intent = new Intent(Intent.ACTION_SENDTO);
205 intent.setType("text/plain");
206 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
207
208 intent.setData(Uri.parse(feedbackMail));
209 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
210 startActivity(intent);
211
212 return true;
213 }
214 });
215 } else {
216 preferenceCategory.removePreference(pFeedback);
217 }
218
219 }
220
221 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
222 Preference pImprint = findPreference("imprint");
223 if (pImprint != null) {
224 if (imprintEnabled) {
225 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
226 @Override
227 public boolean onPreferenceClick(Preference preference) {
228 String imprintWeb = (String) getText(R.string.url_imprint);
229 if (imprintWeb != null && imprintWeb.length() > 0) {
230 Uri uriUrl = Uri.parse(imprintWeb);
231 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
232 startActivity(intent);
233 }
234 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
235 return true;
236 }
237 });
238 } else {
239 preferenceCategory.removePreference(pImprint);
240 }
241 }
242
243 /* About App */
244 pAboutApp = (Preference) findPreference("about_app");
245 if (pAboutApp != null) {
246 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
247 PackageInfo pkg;
248 try {
249 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
250 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
251 } catch (NameNotFoundException e) {
252 Log_OC.e(TAG, "Error while showing about dialog", e);
253 }
254 }
255 }
256
257 @Override
258 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
259
260 // Filter for only showing contextual menu when long press on the
261 // accounts
262 if (mShowContextMenu) {
263 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
264 mShowContextMenu = false;
265 }
266 super.onCreateContextMenu(menu, v, menuInfo);
267 }
268
269 /**
270 * Called when the user clicked on an item into the context menu created at
271 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
272 * every ownCloud {@link Account} , containing 'secondary actions' for them.
273 *
274 * {@inheritDoc}
275 */
276 @Override
277 public boolean onContextItemSelected(android.view.MenuItem item) {
278 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
279 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
280 for (Account a : accounts) {
281 if (a.name.equals(mAccountName)) {
282 if (item.getItemId() == R.id.change_password) {
283
284 // Change account password
285 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
286 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
287 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
288 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
289 startActivity(updateAccountCredentials);
290
291 } else if (item.getItemId() == R.id.delete_account) {
292
293 // Remove account
294 am.removeAccount(a, this, mHandler);
295 }
296 }
297 }
298
299 return true;
300 }
301
302 @Override
303 public void run(AccountManagerFuture<Boolean> future) {
304 if (future.isDone()) {
305 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
306 String accountName = "";
307 if (a == null) {
308 Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
309 if (accounts.length != 0)
310 accountName = accounts[0].name;
311 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
312 }
313 addAccountsCheckboxPreferences();
314 }
315 }
316
317 @Override
318 protected void onResume() {
319 super.onResume();
320 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
321 boolean state = appPrefs.getBoolean("set_pincode", false);
322 pCode.setChecked(state);
323
324 // Populate the accounts category with the list of accounts
325 addAccountsCheckboxPreferences();
326 }
327
328 @Override
329 public boolean onCreateOptionsMenu(Menu menu) {
330 super.onCreateOptionsMenu(menu);
331 return true;
332 }
333
334 @Override
335 public boolean onMenuItemSelected(int featureId, MenuItem item) {
336 super.onMenuItemSelected(featureId, item);
337 Intent intent;
338
339 switch (item.getItemId()) {
340 case android.R.id.home:
341 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
342 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
343 startActivity(intent);
344 break;
345 default:
346 Log_OC.w(TAG, "Unknown menu item triggered");
347 return false;
348 }
349 return true;
350 }
351
352 @Override
353 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
354 super.onActivityResult(requestCode, resultCode, data);
355 }
356
357 @Override
358 protected void onDestroy() {
359 mDbHandler.close();
360 super.onDestroy();
361 }
362
363 /**
364 * Create the list of accounts that has been added into the app
365 */
366 @SuppressWarnings("deprecation")
367 private void addAccountsCheckboxPreferences() {
368
369 // Remove accounts in case list is refreshing for avoiding to have
370 // duplicate items
371 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
372 mAccountsPrefCategory.removeAll();
373 }
374
375 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
376 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
377 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
378
379 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
380 // Show create account screen if there isn't any account
381 am.addAccount(MainApp.getAccountType(), null, null, null, this,
382 null,
383 null);
384 }
385 else {
386
387 for (Account a : accounts) {
388 LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
389 accountPreference.setKey(a.name);
390 accountPreference.setTitle(a.name);
391 mAccountsPrefCategory.addPreference(accountPreference);
392
393 // Check the current account that is being used
394 if (a.name.equals(currentAccount.name)) {
395 accountPreference.setChecked(true);
396 } else {
397 accountPreference.setChecked(false);
398 }
399
400 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
401 @Override
402 public boolean onPreferenceChange(Preference preference, Object newValue) {
403 String key = preference.getKey();
404 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
405 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
406 for (Account a : accounts) {
407 CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
408 if (key.equals(a.name)) {
409 boolean accountChanged = !p.isChecked();
410 p.setChecked(true);
411 AccountUtils.setCurrentOwnCloudAccount(
412 getApplicationContext(),
413 a.name
414 );
415 if (accountChanged) {
416 // restart the main activity
417 Intent i = new Intent(
418 Preferences.this,
419 FileDisplayActivity.class
420 );
421 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
422 startActivity(i);
423 } else {
424 finish();
425 }
426 } else {
427 p.setChecked(false);
428 }
429 }
430 return (Boolean) newValue;
431 }
432 });
433
434 }
435
436 // Add Create Account preference at the end of account list if
437 // Multiaccount is enabled
438 if (getResources().getBoolean(R.bool.multiaccount_support)) {
439 createAddAccountPreference();
440 }
441
442 }
443 }
444
445 /**
446 * Create the preference for allow adding new accounts
447 */
448 private void createAddAccountPreference() {
449 Preference addAccountPref = new Preference(this);
450 addAccountPref.setKey("add_account");
451 addAccountPref.setTitle(getString(R.string.prefs_add_account));
452 mAccountsPrefCategory.addPreference(addAccountPref);
453
454 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
455 @Override
456 public boolean onPreferenceClick(Preference preference) {
457 AccountManager am = AccountManager.get(getApplicationContext());
458 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
459 return true;
460 }
461 });
462
463 }
464
465 }