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