e7e76790c540c212c073bb5b09b25e89ff719c2e
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 package com.owncloud.android.ui.activity;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.accounts.AccountManagerCallback;
27 import android.accounts.AccountManagerFuture;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.ServiceConnection;
32 import android.content.SharedPreferences;
33 import android.content.pm.PackageInfo;
34 import android.content.pm.PackageManager.NameNotFoundException;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.os.Handler;
38 import android.os.IBinder;
39 import android.preference.CheckBoxPreference;
40 import android.preference.Preference;
41 import android.preference.Preference.OnPreferenceChangeListener;
42 import android.preference.Preference.OnPreferenceClickListener;
43 import android.preference.PreferenceCategory;
44 import android.preference.PreferenceManager;
45 import android.view.ContextMenu;
46 import android.view.ContextMenu.ContextMenuInfo;
47 import android.view.View;
48 import android.widget.AdapterView;
49 import android.widget.AdapterView.OnItemLongClickListener;
50 import android.widget.ListAdapter;
51 import android.widget.ListView;
52
53 import com.actionbarsherlock.app.ActionBar;
54 import com.actionbarsherlock.app.SherlockPreferenceActivity;
55 import com.actionbarsherlock.view.Menu;
56 import com.actionbarsherlock.view.MenuItem;
57 import com.owncloud.android.BuildConfig;
58 import com.owncloud.android.MainApp;
59 import com.owncloud.android.R;
60 import com.owncloud.android.authentication.AccountUtils;
61 import com.owncloud.android.authentication.AuthenticatorActivity;
62 import com.owncloud.android.datamodel.FileDataStorageManager;
63 import com.owncloud.android.datamodel.OCFile;
64 import com.owncloud.android.db.DbHandler;
65 import com.owncloud.android.files.FileOperationsHelper;
66 import com.owncloud.android.files.services.FileDownloader;
67 import com.owncloud.android.files.services.FileUploader;
68 import com.owncloud.android.lib.common.utils.Log_OC;
69 import com.owncloud.android.services.OperationsService;
70 import com.owncloud.android.ui.RadioButtonPreference;
71 import com.owncloud.android.utils.DisplayUtils;
72
73
74 /**
75 * An Activity that allows the user to change the application's settings.
76 */
77 public class Preferences extends SherlockPreferenceActivity
78 implements AccountManagerCallback<Boolean>, ComponentsGetter {
79
80 private static final String TAG = "OwnCloudPreferences";
81
82 private static final int ACTION_SELECT_UPLOAD_PATH = 1;
83 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
84
85 private DbHandler mDbHandler;
86 private CheckBoxPreference pCode;
87 private Preference pAboutApp;
88
89 private PreferenceCategory mAccountsPrefCategory = null;
90 private final Handler mHandler = new Handler();
91 private String mAccountName;
92 private boolean mShowContextMenu = false;
93 private String mUploadPath;
94 private PreferenceCategory mPrefInstantUploadCategory;
95 private Preference mPrefInstantUpload;
96 private Preference mPrefInstantUploadPath;
97 private Preference mPrefInstantUploadPathWiFi;
98 private Preference mPrefInstantVideoUpload;
99 private Preference mPrefInstantVideoUploadPath;
100 private Preference mPrefInstantVideoUploadPathWiFi;
101 private String mUploadVideoPath;
102
103 protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
104 protected FileUploader.FileUploaderBinder mUploaderBinder = null;
105 private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
106
107 @SuppressWarnings("deprecation")
108 @Override
109 public void onCreate(Bundle savedInstanceState) {
110 super.onCreate(savedInstanceState);
111 mDbHandler = new DbHandler(getBaseContext());
112 addPreferencesFromResource(R.xml.preferences);
113
114 ActionBar actionBar = getSherlock().getActionBar();
115 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
116 actionBar.setDisplayHomeAsUpEnabled(true);
117 actionBar.setTitle(R.string.actionbar_settings);
118
119 // For adding content description tag to a title field in the action bar
120 int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
121 View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
122 if (actionBarTitleView != null) { // it's null in Android 2.x
123 getWindow().getDecorView().findViewById(actionBarTitleId).
124 setContentDescription(getString(R.string.actionbar_settings));
125 }
126
127 // Load the accounts category for adding the list of accounts
128 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
129
130 ListView listView = getListView();
131 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
132 @Override
133 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
134 ListView listView = (ListView) parent;
135 ListAdapter listAdapter = listView.getAdapter();
136 Object obj = listAdapter.getItem(position);
137
138 if (obj != null && obj instanceof RadioButtonPreference) {
139 mShowContextMenu = true;
140 mAccountName = ((RadioButtonPreference) obj).getKey();
141
142 Preferences.this.openContextMenu(listView);
143
144 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
145 return longListener.onLongClick(view);
146 }
147 return false;
148 }
149 });
150
151 // Load package info
152 String temp;
153 try {
154 PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
155 temp = pkg.versionName;
156 } catch (NameNotFoundException e) {
157 temp = "";
158 Log_OC.e(TAG, "Error while showing about dialog", e);
159 }
160 final String appVersion = temp;
161
162 // Register context menu for list of preferences.
163 registerForContextMenu(getListView());
164
165 pCode = (CheckBoxPreference) findPreference("set_pincode");
166 if (pCode != null){
167 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
168 @Override
169 public boolean onPreferenceChange(Preference preference, Object newValue) {
170 Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
171 Boolean enable = (Boolean) newValue;
172 i.setAction(
173 enable.booleanValue() ? PassCodeActivity.ACTION_ENABLE : PassCodeActivity.ACTION_DISABLE
174 );
175 startActivity(i);
176
177 return true;
178 }
179 });
180
181 }
182
183 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
184
185 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
186 Preference pHelp = findPreference("help");
187 if (pHelp != null ){
188 if (helpEnabled) {
189 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
190 @Override
191 public boolean onPreferenceClick(Preference preference) {
192 String helpWeb =(String) getText(R.string.url_help);
193 if (helpWeb != null && helpWeb.length() > 0) {
194 Uri uriUrl = Uri.parse(helpWeb);
195 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
196 startActivity(intent);
197 }
198 return true;
199 }
200 });
201 } else {
202 preferenceCategory.removePreference(pHelp);
203 }
204
205 }
206
207 if (BuildConfig.DEBUG) {
208 Preference pLog = findPreference("log");
209 if (pLog != null ){
210 pLog.setOnPreferenceClickListener(new OnPreferenceClickListener() {
211 @Override
212 public boolean onPreferenceClick(Preference preference) {
213 Intent loggerIntent = new Intent(getApplicationContext(),LogHistoryActivity.class);
214 startActivity(loggerIntent);
215 return true;
216 }
217 });
218 }
219 }
220
221 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
222 Preference pRecommend = findPreference("recommend");
223 if (pRecommend != null){
224 if (recommendEnabled) {
225 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
226 @Override
227 public boolean onPreferenceClick(Preference preference) {
228
229 Intent intent = new Intent(Intent.ACTION_SENDTO);
230 intent.setType("text/plain");
231 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
232 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
233
234 String appName = getString(R.string.app_name);
235 String downloadUrl = getString(R.string.url_app_download);
236 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
237 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
238
239 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
240 String recommendText = String.format(getString(R.string.recommend_text),
241 appName, downloadUrl, username);
242
243 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
244 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
245 startActivity(intent);
246
247 return(true);
248
249 }
250 });
251 } else {
252 preferenceCategory.removePreference(pRecommend);
253 }
254
255 }
256
257 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
258 Preference pFeedback = findPreference("feedback");
259 if (pFeedback != null){
260 if (feedbackEnabled) {
261 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
262 @Override
263 public boolean onPreferenceClick(Preference preference) {
264 String feedbackMail =(String) getText(R.string.mail_feedback);
265 String feedback =(String) getText(R.string.prefs_feedback) + " - android v" + appVersion;
266 Intent intent = new Intent(Intent.ACTION_SENDTO);
267 intent.setType("text/plain");
268 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
269
270 intent.setData(Uri.parse(feedbackMail));
271 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
272 startActivity(intent);
273
274 return true;
275 }
276 });
277 } else {
278 preferenceCategory.removePreference(pFeedback);
279 }
280
281 }
282
283 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
284 Preference pImprint = findPreference("imprint");
285 if (pImprint != null) {
286 if (imprintEnabled) {
287 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
288 @Override
289 public boolean onPreferenceClick(Preference preference) {
290 String imprintWeb = (String) getText(R.string.url_imprint);
291 if (imprintWeb != null && imprintWeb.length() > 0) {
292 Uri uriUrl = Uri.parse(imprintWeb);
293 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
294 startActivity(intent);
295 }
296 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
297 return true;
298 }
299 });
300 } else {
301 preferenceCategory.removePreference(pImprint);
302 }
303 }
304
305 mPrefInstantUploadPath = findPreference("instant_upload_path");
306 if (mPrefInstantUploadPath != null){
307
308 mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
309 @Override
310 public boolean onPreferenceClick(Preference preference) {
311 if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
312 mUploadPath += OCFile.PATH_SEPARATOR;
313 }
314 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
315 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
316 startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
317 return true;
318 }
319 });
320 }
321
322 mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
323
324 mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
325 mPrefInstantUpload = findPreference("instant_uploading");
326
327 toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
328
329 mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
330
331 @Override
332 public boolean onPreferenceChange(Preference preference, Object newValue) {
333 toggleInstantPictureOptions((Boolean) newValue);
334 return true;
335 }
336 });
337
338 mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
339 if (mPrefInstantVideoUploadPath != null){
340
341 mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
342 @Override
343 public boolean onPreferenceClick(Preference preference) {
344 if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
345 mUploadVideoPath += OCFile.PATH_SEPARATOR;
346 }
347 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
348 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadVideoPath);
349 startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
350 return true;
351 }
352 });
353 }
354
355 mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
356 mPrefInstantVideoUpload = findPreference("instant_video_uploading");
357 toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
358
359 mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
360
361 @Override
362 public boolean onPreferenceChange(Preference preference, Object newValue) {
363 toggleInstantVideoOptions((Boolean) newValue);
364 return true;
365 }
366 });
367
368 /* About App */
369 pAboutApp = (Preference) findPreference("about_app");
370 if (pAboutApp != null) {
371 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
372 pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
373 }
374
375 loadInstantUploadPath();
376 loadInstantUploadVideoPath();
377
378 /* ComponentsGetter */
379 mDownloadServiceConnection = newTransferenceServiceConnection();
380 if (mDownloadServiceConnection != null) {
381 bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
382 Context.BIND_AUTO_CREATE);
383 }
384 mUploadServiceConnection = newTransferenceServiceConnection();
385 if (mUploadServiceConnection != null) {
386 bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
387 Context.BIND_AUTO_CREATE);
388 }
389
390 }
391
392 private void toggleInstantPictureOptions(Boolean value){
393 if (value){
394 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
395 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
396 } else {
397 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
398 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
399 }
400 }
401
402 private void toggleInstantVideoOptions(Boolean value){
403 if (value){
404 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
405 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
406 } else {
407 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
408 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
409 }
410 }
411
412 @Override
413 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
414
415 // Filter for only showing contextual menu when long press on the
416 // accounts
417 if (mShowContextMenu) {
418 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
419 mShowContextMenu = false;
420 }
421 super.onCreateContextMenu(menu, v, menuInfo);
422 }
423
424 /**
425 * Called when the user clicked on an item into the context menu created at
426 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
427 * every ownCloud {@link Account} , containing 'secondary actions' for them.
428 *
429 * {@inheritDoc}
430 */
431 @Override
432 public boolean onContextItemSelected(android.view.MenuItem item) {
433 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
434 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
435 for (Account a : accounts) {
436 if (a.name.equals(mAccountName)) {
437 if (item.getItemId() == R.id.change_password) {
438
439 // Change account password
440 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
441 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
442 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
443 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
444 startActivity(updateAccountCredentials);
445
446 } else if (item.getItemId() == R.id.delete_account) {
447
448 // Remove account
449 am.removeAccount(a, this, mHandler);
450 Log_OC.d(TAG, "Remove an account " + a.name);
451 }
452 }
453 }
454
455 return true;
456 }
457
458 @Override
459 public void run(AccountManagerFuture<Boolean> future) {
460 if (future.isDone()) {
461 // after remove account
462 Account account = new Account(mAccountName, MainApp.getAccountType());
463 if (!AccountUtils.exists(account, MainApp.getAppContext())) {
464 // Cancel tranfers
465 if (mUploaderBinder != null) {
466 mUploaderBinder.cancel(account);
467 }
468 if (mDownloaderBinder != null) {
469 mDownloaderBinder.cancel(account);
470 }
471 }
472
473 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
474 String accountName = "";
475 if (a == null) {
476 Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
477 if (accounts.length != 0)
478 accountName = accounts[0].name;
479 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
480 }
481 addAccountsCheckboxPreferences();
482 }
483 }
484
485 @Override
486 protected void onResume() {
487 super.onResume();
488 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
489 boolean state = appPrefs.getBoolean("set_pincode", false);
490 pCode.setChecked(state);
491
492 // Populate the accounts category with the list of accounts
493 addAccountsCheckboxPreferences();
494 }
495
496 @Override
497 public boolean onCreateOptionsMenu(Menu menu) {
498 super.onCreateOptionsMenu(menu);
499 return true;
500 }
501
502 @Override
503 public boolean onMenuItemSelected(int featureId, MenuItem item) {
504 super.onMenuItemSelected(featureId, item);
505 Intent intent;
506
507 switch (item.getItemId()) {
508 case android.R.id.home:
509 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
510 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
511 startActivity(intent);
512 break;
513 default:
514 Log_OC.w(TAG, "Unknown menu item triggered");
515 return false;
516 }
517 return true;
518 }
519
520 @Override
521 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
522 super.onActivityResult(requestCode, resultCode, data);
523
524 if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
525
526 OCFile folderToUpload = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
527
528 mUploadPath = folderToUpload.getRemotePath();
529
530 mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
531
532 // Show the path on summary preference
533 mPrefInstantUploadPath.setSummary(mUploadPath);
534
535 saveInstantUploadPathOnPreferences();
536
537 } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
538
539 OCFile folderToUploadVideo = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
540
541 mUploadVideoPath = folderToUploadVideo.getRemotePath();
542
543 mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
544
545 // Show the video path on summary preference
546 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
547
548 saveInstantUploadVideoPathOnPreferences();
549 }
550 }
551
552 @Override
553 protected void onDestroy() {
554 mDbHandler.close();
555
556 if (mDownloadServiceConnection != null) {
557 unbindService(mDownloadServiceConnection);
558 mDownloadServiceConnection = null;
559 }
560 if (mUploadServiceConnection != null) {
561 unbindService(mUploadServiceConnection);
562 mUploadServiceConnection = null;
563 }
564
565 super.onDestroy();
566 }
567
568 /**
569 * Create the list of accounts that has been added into the app
570 */
571 @SuppressWarnings("deprecation")
572 private void addAccountsCheckboxPreferences() {
573
574 // Remove accounts in case list is refreshing for avoiding to have
575 // duplicate items
576 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
577 mAccountsPrefCategory.removeAll();
578 }
579
580 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
581 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
582 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
583
584 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
585 // Show create account screen if there isn't any account
586 am.addAccount(MainApp.getAccountType(), null, null, null, this,
587 null,
588 null);
589 }
590 else {
591
592 for (Account a : accounts) {
593 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
594 accountPreference.setKey(a.name);
595 // Handle internationalized domain names
596 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
597 mAccountsPrefCategory.addPreference(accountPreference);
598
599 // Check the current account that is being used
600 if (a.name.equals(currentAccount.name)) {
601 accountPreference.setChecked(true);
602 } else {
603 accountPreference.setChecked(false);
604 }
605
606 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
607 @Override
608 public boolean onPreferenceChange(Preference preference, Object newValue) {
609 String key = preference.getKey();
610 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
611 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
612 for (Account a : accounts) {
613 RadioButtonPreference p = (RadioButtonPreference) findPreference(a.name);
614 if (key.equals(a.name)) {
615 boolean accountChanged = !p.isChecked();
616 p.setChecked(true);
617 AccountUtils.setCurrentOwnCloudAccount(
618 getApplicationContext(),
619 a.name
620 );
621 if (accountChanged) {
622 // restart the main activity
623 Intent i = new Intent(
624 Preferences.this,
625 FileDisplayActivity.class
626 );
627 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
628 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
629 startActivity(i);
630 } else {
631 finish();
632 }
633 } else {
634 p.setChecked(false);
635 }
636 }
637 return (Boolean) newValue;
638 }
639 });
640
641 }
642
643 // Add Create Account preference at the end of account list if
644 // Multiaccount is enabled
645 if (getResources().getBoolean(R.bool.multiaccount_support)) {
646 createAddAccountPreference();
647 }
648
649 }
650 }
651
652 /**
653 * Create the preference for allow adding new accounts
654 */
655 private void createAddAccountPreference() {
656 Preference addAccountPref = new Preference(this);
657 addAccountPref.setKey("add_account");
658 addAccountPref.setTitle(getString(R.string.prefs_add_account));
659 mAccountsPrefCategory.addPreference(addAccountPref);
660
661 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
662 @Override
663 public boolean onPreferenceClick(Preference preference) {
664 AccountManager am = AccountManager.get(getApplicationContext());
665 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
666 return true;
667 }
668 });
669
670 }
671
672 /**
673 * Load upload path set on preferences
674 */
675 private void loadInstantUploadPath() {
676 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
677 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
678 mPrefInstantUploadPath.setSummary(mUploadPath);
679 }
680
681 /**
682 * Save the "Instant Upload Path" on preferences
683 */
684 private void saveInstantUploadPathOnPreferences() {
685 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
686 SharedPreferences.Editor editor = appPrefs.edit();
687 editor.putString("instant_upload_path", mUploadPath);
688 editor.commit();
689 }
690
691 /**
692 * Load upload video path set on preferences
693 */
694 private void loadInstantUploadVideoPath() {
695 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
696 mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
697 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
698 }
699
700 /**
701 * Save the "Instant Video Upload Path" on preferences
702 */
703 private void saveInstantUploadVideoPathOnPreferences() {
704 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
705 SharedPreferences.Editor editor = appPrefs.edit();
706 editor.putString("instant_video_upload_path", mUploadVideoPath);
707 editor.commit();
708 }
709
710 // Methods for ComponetsGetter
711 @Override
712 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
713 return mDownloaderBinder;
714 }
715
716
717 @Override
718 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
719 return mUploaderBinder;
720 }
721
722 @Override
723 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
724 return null;
725 }
726
727 @Override
728 public FileDataStorageManager getStorageManager() {
729 return null;
730 }
731
732 @Override
733 public FileOperationsHelper getFileOperationsHelper() {
734 return null;
735 }
736
737 protected ServiceConnection newTransferenceServiceConnection() {
738 return new PreferencesServiceConnection();
739 }
740
741 /** Defines callbacks for service binding, passed to bindService() */
742 private class PreferencesServiceConnection implements ServiceConnection {
743
744 @Override
745 public void onServiceConnected(ComponentName component, IBinder service) {
746
747 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
748 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
749
750 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
751 Log_OC.d(TAG, "Upload service connected");
752 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
753 } else {
754 return;
755 }
756
757 }
758
759 @Override
760 public void onServiceDisconnected(ComponentName component) {
761 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
762 Log_OC.d(TAG, "Download service suddenly disconnected");
763 mDownloaderBinder = null;
764 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
765 Log_OC.d(TAG, "Upload service suddenly disconnected");
766 mUploaderBinder = null;
767 }
768 }
769 };
770 }