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