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