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