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