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