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