Add new preference on settings for setting the upload video path
[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.datamodel.OCFile;
54 import com.owncloud.android.db.DbHandler;
55 import com.owncloud.android.lib.common.utils.Log_OC;
56 import com.owncloud.android.ui.LongClickableCheckBoxPreference;
57 import com.owncloud.android.utils.DisplayUtils;
58
59
60 /**
61 * An Activity that allows the user to change the application's settings.
62 *
63 * @author Bartek Przybylski
64 * @author David A. Velasco
65 */
66 public class Preferences extends SherlockPreferenceActivity implements AccountManagerCallback<Boolean> {
67
68 private static final String TAG = "OwnCloudPreferences";
69
70 private static final int ACTION_SELECT_UPLOAD_PATH = 1;
71 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
72
73
74 private DbHandler mDbHandler;
75 private CheckBoxPreference pCode;
76 private Preference pAboutApp;
77
78 private PreferenceCategory mAccountsPrefCategory = null;
79 private final Handler mHandler = new Handler();
80 private String mAccountName;
81 private boolean mShowContextMenu = false;
82 private String mUploadPath;
83 private Preference mPrefInstantUploadPath;
84 private Preference mPrefInstantVideoUploadPath;
85 private String mUploadVideoPath;
86
87
88 @SuppressWarnings("deprecation")
89 @Override
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 mDbHandler = new DbHandler(getBaseContext());
93 addPreferencesFromResource(R.xml.preferences);
94
95 ActionBar actionBar = getSherlock().getActionBar();
96 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
97 actionBar.setDisplayHomeAsUpEnabled(true);
98 actionBar.setTitle(R.string.actionbar_settings);
99
100 // Load the accounts category for adding the list of accounts
101 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
102
103 ListView listView = getListView();
104 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
105 @Override
106 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
107 ListView listView = (ListView) parent;
108 ListAdapter listAdapter = listView.getAdapter();
109 Object obj = listAdapter.getItem(position);
110
111 if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
112 mShowContextMenu = true;
113 mAccountName = ((LongClickableCheckBoxPreference) obj).getKey();
114
115 Preferences.this.openContextMenu(listView);
116
117 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
118 return longListener.onLongClick(view);
119 }
120 return false;
121 }
122 });
123
124 // Register context menu for list of preferences.
125 registerForContextMenu(getListView());
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 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
144
145 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
146 Preference pHelp = findPreference("help");
147 if (pHelp != null ){
148 if (helpEnabled) {
149 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
150 @Override
151 public boolean onPreferenceClick(Preference preference) {
152 String helpWeb =(String) getText(R.string.url_help);
153 if (helpWeb != null && helpWeb.length() > 0) {
154 Uri uriUrl = Uri.parse(helpWeb);
155 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
156 startActivity(intent);
157 }
158 return true;
159 }
160 });
161 } else {
162 preferenceCategory.removePreference(pHelp);
163 }
164
165 }
166
167
168 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
169 Preference pRecommend = findPreference("recommend");
170 if (pRecommend != null){
171 if (recommendEnabled) {
172 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
173 @Override
174 public boolean onPreferenceClick(Preference preference) {
175
176 Intent intent = new Intent(Intent.ACTION_SENDTO);
177 intent.setType("text/plain");
178 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
179 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
180
181 String appName = getString(R.string.app_name);
182 String downloadUrl = getString(R.string.url_app_download);
183 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
184 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
185
186 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
187 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
188
189 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
190 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
191 startActivity(intent);
192
193
194 return(true);
195
196 }
197 });
198 } else {
199 preferenceCategory.removePreference(pRecommend);
200 }
201
202 }
203
204 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
205 Preference pFeedback = findPreference("feedback");
206 if (pFeedback != null){
207 if (feedbackEnabled) {
208 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
209 @Override
210 public boolean onPreferenceClick(Preference preference) {
211 String feedbackMail =(String) getText(R.string.mail_feedback);
212 String feedback =(String) getText(R.string.prefs_feedback);
213 Intent intent = new Intent(Intent.ACTION_SENDTO);
214 intent.setType("text/plain");
215 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
216
217 intent.setData(Uri.parse(feedbackMail));
218 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
219 startActivity(intent);
220
221 return true;
222 }
223 });
224 } else {
225 preferenceCategory.removePreference(pFeedback);
226 }
227
228 }
229
230 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
231 Preference pImprint = findPreference("imprint");
232 if (pImprint != null) {
233 if (imprintEnabled) {
234 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
235 @Override
236 public boolean onPreferenceClick(Preference preference) {
237 String imprintWeb = (String) getText(R.string.url_imprint);
238 if (imprintWeb != null && imprintWeb.length() > 0) {
239 Uri uriUrl = Uri.parse(imprintWeb);
240 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
241 startActivity(intent);
242 }
243 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
244 return true;
245 }
246 });
247 } else {
248 preferenceCategory.removePreference(pImprint);
249 }
250 }
251
252 mPrefInstantUploadPath = findPreference("instant_upload_path");
253 if (mPrefInstantUploadPath != null){
254
255 mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
256 @Override
257 public boolean onPreferenceClick(Preference preference) {
258 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
259 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
260 startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
261 return true;
262 }
263 });
264 }
265
266 mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
267 if (mPrefInstantVideoUploadPath != null){
268
269 mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
270 @Override
271 public boolean onPreferenceClick(Preference preference) {
272 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
273 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadVideoPath);
274 startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
275 return true;
276 }
277 });
278 }
279
280 /* About App */
281 pAboutApp = (Preference) findPreference("about_app");
282 if (pAboutApp != null) {
283 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
284 PackageInfo pkg;
285 try {
286 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
287 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
288 } catch (NameNotFoundException e) {
289 Log_OC.e(TAG, "Error while showing about dialog", e);
290 }
291 }
292
293 loadInstantUploadPath();
294 loadInstantUploadVideoPath();
295
296 }
297
298 @Override
299 protected void onPause() {
300 super.onPause();
301 }
302
303 @Override
304 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
305
306 // Filter for only showing contextual menu when long press on the
307 // accounts
308 if (mShowContextMenu) {
309 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
310 mShowContextMenu = false;
311 }
312 super.onCreateContextMenu(menu, v, menuInfo);
313 }
314
315 /**
316 * Called when the user clicked on an item into the context menu created at
317 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
318 * every ownCloud {@link Account} , containing 'secondary actions' for them.
319 *
320 * {@inheritDoc}
321 */
322 @Override
323 public boolean onContextItemSelected(android.view.MenuItem item) {
324 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
325 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
326 for (Account a : accounts) {
327 if (a.name.equals(mAccountName)) {
328 if (item.getItemId() == R.id.change_password) {
329
330 // Change account password
331 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
332 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
333 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
334 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
335 startActivity(updateAccountCredentials);
336
337 } else if (item.getItemId() == R.id.delete_account) {
338
339 // Remove account
340 am.removeAccount(a, this, mHandler);
341 }
342 }
343 }
344
345 return true;
346 }
347
348 @Override
349 public void run(AccountManagerFuture<Boolean> future) {
350 if (future.isDone()) {
351 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
352 String accountName = "";
353 if (a == null) {
354 Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
355 if (accounts.length != 0)
356 accountName = accounts[0].name;
357 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
358 }
359 addAccountsCheckboxPreferences();
360 }
361 }
362
363 @Override
364 protected void onResume() {
365 super.onResume();
366 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
367 boolean state = appPrefs.getBoolean("set_pincode", false);
368 pCode.setChecked(state);
369
370 // Populate the accounts category with the list of accounts
371 addAccountsCheckboxPreferences();
372 }
373
374 @Override
375 public boolean onCreateOptionsMenu(Menu menu) {
376 super.onCreateOptionsMenu(menu);
377 return true;
378 }
379
380 @Override
381 public boolean onMenuItemSelected(int featureId, MenuItem item) {
382 super.onMenuItemSelected(featureId, item);
383 Intent intent;
384
385 switch (item.getItemId()) {
386 case android.R.id.home:
387 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
388 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
389 startActivity(intent);
390 break;
391 default:
392 Log_OC.w(TAG, "Unknown menu item triggered");
393 return false;
394 }
395 return true;
396 }
397
398 @Override
399 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
400 super.onActivityResult(requestCode, resultCode, data);
401
402 if (requestCode == ACTION_SELECT_UPLOAD_PATH && (resultCode == RESULT_OK ||
403 resultCode == UploadPathActivity.RESULT_OK_SET_UPLOAD_PATH)){
404
405 OCFile folderToUpload = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_CURRENT_FOLDER);
406
407 mUploadPath = folderToUpload.getRemotePath();
408
409 saveInstantUploadPathOnPreferences();
410 } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && (resultCode == RESULT_OK ||
411 resultCode == UploadPathActivity.RESULT_OK_SET_UPLOAD_PATH)){
412
413 OCFile folderToUploadVideo = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_CURRENT_FOLDER);
414
415 mUploadVideoPath = folderToUploadVideo.getRemotePath();
416
417 saveInstantUploadVideoPathOnPreferences();
418 }
419 }
420
421 @Override
422 protected void onDestroy() {
423 mDbHandler.close();
424 super.onDestroy();
425 }
426
427 /**
428 * Create the list of accounts that has been added into the app
429 */
430 @SuppressWarnings("deprecation")
431 private void addAccountsCheckboxPreferences() {
432
433 // Remove accounts in case list is refreshing for avoiding to have
434 // duplicate items
435 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
436 mAccountsPrefCategory.removeAll();
437 }
438
439 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
440 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
441 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
442
443 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
444 // Show create account screen if there isn't any account
445 am.addAccount(MainApp.getAccountType(), null, null, null, this,
446 null,
447 null);
448 }
449 else {
450
451 for (Account a : accounts) {
452 LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
453 accountPreference.setKey(a.name);
454 // Handle internationalized domain names
455 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
456 mAccountsPrefCategory.addPreference(accountPreference);
457
458 // Check the current account that is being used
459 if (a.name.equals(currentAccount.name)) {
460 accountPreference.setChecked(true);
461 } else {
462 accountPreference.setChecked(false);
463 }
464
465 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
466 @Override
467 public boolean onPreferenceChange(Preference preference, Object newValue) {
468 String key = preference.getKey();
469 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
470 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
471 for (Account a : accounts) {
472 CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
473 if (key.equals(a.name)) {
474 boolean accountChanged = !p.isChecked();
475 p.setChecked(true);
476 AccountUtils.setCurrentOwnCloudAccount(
477 getApplicationContext(),
478 a.name
479 );
480 if (accountChanged) {
481 // restart the main activity
482 Intent i = new Intent(
483 Preferences.this,
484 FileDisplayActivity.class
485 );
486 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
487 startActivity(i);
488 } else {
489 finish();
490 }
491 } else {
492 p.setChecked(false);
493 }
494 }
495 return (Boolean) newValue;
496 }
497 });
498
499 }
500
501 // Add Create Account preference at the end of account list if
502 // Multiaccount is enabled
503 if (getResources().getBoolean(R.bool.multiaccount_support)) {
504 createAddAccountPreference();
505 }
506
507 }
508 }
509
510 /**
511 * Create the preference for allow adding new accounts
512 */
513 private void createAddAccountPreference() {
514 Preference addAccountPref = new Preference(this);
515 addAccountPref.setKey("add_account");
516 addAccountPref.setTitle(getString(R.string.prefs_add_account));
517 mAccountsPrefCategory.addPreference(addAccountPref);
518
519 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
520 @Override
521 public boolean onPreferenceClick(Preference preference) {
522 AccountManager am = AccountManager.get(getApplicationContext());
523 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
524 return true;
525 }
526 });
527
528 }
529
530 /**
531 * Load upload path set on preferences
532 */
533 private void loadInstantUploadPath() {
534 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
535 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
536 mPrefInstantUploadPath.setSummary(mUploadPath);
537 }
538
539 /**
540 * Save the "Instant Upload Path" on preferences
541 */
542 private void saveInstantUploadPathOnPreferences() {
543 mPrefInstantUploadPath.setSummary(mUploadPath);
544 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
545 SharedPreferences.Editor editor = appPrefs.edit();
546 editor.putString("instant_upload_path", mUploadPath);
547 editor.commit();
548 }
549
550 /**
551 * Load upload video path set on preferences
552 */
553 private void loadInstantUploadVideoPath() {
554 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
555 mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
556 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
557 }
558
559 /**
560 * Save the "Instant Video Upload Path" on preferences
561 */
562 private void saveInstantUploadVideoPathOnPreferences() {
563 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
564 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
565 SharedPreferences.Editor editor = appPrefs.edit();
566 editor.putString("instant_video_upload_path", mUploadVideoPath);
567 editor.commit();
568 }
569 }