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