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