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