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