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