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