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