diff options
Diffstat (limited to 'main/src/cgeo/geocaching/MainActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/MainActivity.java | 227 |
1 files changed, 95 insertions, 132 deletions
diff --git a/main/src/cgeo/geocaching/MainActivity.java b/main/src/cgeo/geocaching/MainActivity.java index 924c66d..b83bef2 100644 --- a/main/src/cgeo/geocaching/MainActivity.java +++ b/main/src/cgeo/geocaching/MainActivity.java @@ -10,23 +10,28 @@ import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Units; +import cgeo.geocaching.list.PseudoList; import cgeo.geocaching.list.StoredList; import cgeo.geocaching.maps.CGeoMap; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.SettingsActivity; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.DatabaseBackupUtils; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; -import cgeo.geocaching.utils.RunnableWithArgument; import cgeo.geocaching.utils.Version; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; - -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import rx.Observable; +import rx.Observable.OnSubscribe; +import rx.Subscriber; +import rx.android.observables.AndroidObservable; +import rx.schedulers.Schedulers; +import rx.functions.Action1; import android.app.AlertDialog; import android.app.AlertDialog.Builder; @@ -47,8 +52,8 @@ import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import rx.subscriptions.Subscriptions; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -77,8 +82,6 @@ public class MainActivity extends AbstractActivity { private boolean cleanupRunning = false; private int countBubbleCnt = 0; private Geopoint addCoords = null; - private List<Address> addresses = null; - private boolean addressObtaining = false; private boolean initialized = false; private final UpdateLocation locationUpdater = new UpdateLocation(); @@ -104,7 +107,7 @@ public class MainActivity extends AbstractActivity { if (conn.isLoggedIn()) { userInfo.append(conn.getUserName()); if (conn.getCachesFound() >= 0) { - userInfo.append(" (").append(String.valueOf(conn.getCachesFound())).append(')'); + userInfo.append(" (").append(conn.getCachesFound()).append(')'); } userInfo.append(Formatter.SEPARATOR); } @@ -115,40 +118,24 @@ public class MainActivity extends AbstractActivity { } }; - private Handler obtainAddressHandler = new Handler() { + private static String formatAddress(final Address address) { + final ArrayList<String> addressParts = new ArrayList<String>(); - @Override - public void handleMessage(final Message msg) { - try { - if (CollectionUtils.isNotEmpty(addresses)) { - final Address address = addresses.get(0); - final ArrayList<String> addressParts = new ArrayList<String>(); - - final String countryName = address.getCountryName(); - if (countryName != null) { - addressParts.add(countryName); - } - final String locality = address.getLocality(); - if (locality != null) { - addressParts.add(locality); - } else { - final String adminArea = address.getAdminArea(); - if (adminArea != null) { - addressParts.add(adminArea); - } - } - - addCoords = app.currentGeo().getCoords(); - - navLocation.setText(StringUtils.join(addressParts, ", ")); - } - } catch (RuntimeException e) { - // nothing + final String countryName = address.getCountryName(); + if (countryName != null) { + addressParts.add(countryName); + } + final String locality = address.getLocality(); + if (locality != null) { + addressParts.add(locality); + } else { + final String adminArea = address.getAdminArea(); + if (adminArea != null) { + addressParts.add(adminArea); } - - addresses = null; } - }; + return StringUtils.join(addressParts, ", "); + } private class SatellitesHandler extends GeoDirHandler { @@ -157,7 +144,7 @@ public class MainActivity extends AbstractActivity { private int satellitesVisible = 0; @Override - public void updateGeoData(final IGeoData data) { + public void updateGeoDir(final IGeoData data, final float dir) { if (data.getGpsEnabled() == gpsEnabled && data.getSatellitesFixed() == satellitesFixed && data.getSatellitesVisible() == satellitesVisible) { @@ -228,9 +215,7 @@ public class MainActivity extends AbstractActivity { @Override public void onResume() { - super.onResume(); - locationUpdater.startGeo(); - satellitesHandler.startGeo(); + super.onResume(Subscriptions.from(locationUpdater.start(), satellitesHandler.start())); updateUserInfoHandler.sendEmptyMessage(-1); startBackgroundLogin(); init(); @@ -271,8 +256,6 @@ public class MainActivity extends AbstractActivity { @Override public void onPause() { initialized = false; - locationUpdater.stopGeo(); - satellitesHandler.stopGeo(); super.onPause(); } @@ -285,7 +268,7 @@ public class MainActivity extends AbstractActivity { @Override public boolean onPrepareOptionsMenu(final Menu menu) { super.onPrepareOptionsMenu(menu); - menu.findItem(R.id.menu_pocket_queries).setVisible(Settings.isPremiumMember()); + menu.findItem(R.id.menu_pocket_queries).setVisible(Settings.isGCPremiumMember()); return true; } @@ -309,13 +292,13 @@ public class MainActivity extends AbstractActivity { startScannerApplication(); return true; case R.id.menu_pocket_queries: - if (!Settings.isPremiumMember()) { + if (!Settings.isGCPremiumMember()) { return true; } - new PocketQueryList.UserInterface(MainActivity.this).promptForListSelection(new RunnableWithArgument<PocketQueryList>() { + PocketQueryList.promptForListSelection(this, new Action1<PocketQueryList>() { @Override - public void run(final PocketQueryList pql) { + public void call(final PocketQueryList pql) { CacheListActivity.startActivityPocket(MainActivity.this, pql); } }); @@ -388,14 +371,14 @@ public class MainActivity extends AbstractActivity { @Override public boolean onLongClick(final View v) { - new StoredList.UserInterface(MainActivity.this).promptForListSelection(R.string.list_title, new RunnableWithArgument<Integer>() { + new StoredList.UserInterface(MainActivity.this).promptForListSelection(R.string.list_title, new Action1<Integer>() { @Override - public void run(final Integer selectedListId) { + public void call(final Integer selectedListId) { Settings.saveLastList(selectedListId); CacheListActivity.startActivityOffline(MainActivity.this); } - }); + }, false, PseudoList.HISTORY_LIST.id); return true; } }); @@ -428,7 +411,8 @@ public class MainActivity extends AbstractActivity { @Override public boolean onLongClick(final View v) { - selectGlobalTypeFilter(); + Settings.setCacheType(CacheType.ALL); + setFilterTitle(); return true; } }); @@ -524,53 +508,61 @@ public class MainActivity extends AbstractActivity { private class UpdateLocation extends GeoDirHandler { @Override - public void updateGeoData(final IGeoData geo) { - try { - if (geo.getCoords() != null) { - if (!nearestView.isClickable()) { - nearestView.setFocusable(true); - nearestView.setClickable(true); - nearestView.setOnClickListener(new OnClickListener() { - @Override - public void onClick(final View v) { - cgeoFindNearest(v); - } - }); - nearestView.setBackgroundResource(R.drawable.main_nearby); + public void updateGeoDir(final IGeoData geo, final float dir) { + if (!nearestView.isClickable()) { + nearestView.setFocusable(true); + nearestView.setClickable(true); + nearestView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(final View v) { + cgeoFindNearest(v); } + }); + nearestView.setBackgroundResource(R.drawable.main_nearby); + } - navType.setText(res.getString(geo.getLocationProvider().resourceId)); + navType.setText(res.getString(geo.getLocationProvider().resourceId)); - if (geo.getAccuracy() >= 0) { - int speed = Math.round(geo.getSpeed()) * 60 * 60 / 1000; - navAccuracy.setText("±" + Units.getDistanceFromMeters(geo.getAccuracy()) + Formatter.SEPARATOR + Units.getSpeed(speed)); - } else { - navAccuracy.setText(null); - } + if (geo.getAccuracy() >= 0) { + int speed = Math.round(geo.getSpeed()) * 60 * 60 / 1000; + navAccuracy.setText("±" + Units.getDistanceFromMeters(geo.getAccuracy()) + Formatter.SEPARATOR + Units.getSpeed(speed)); + } else { + navAccuracy.setText(null); + } - if (Settings.isShowAddress()) { - if (addCoords == null) { - navLocation.setText(res.getString(R.string.loc_no_addr)); - } - if (addCoords == null || (geo.getCoords().distanceTo(addCoords) > 0.5 && !addressObtaining)) { - (new ObtainAddressThread()).start(); + if (Settings.isShowAddress()) { + if (addCoords == null) { + navLocation.setText(R.string.loc_no_addr); + } + if (addCoords == null || (geo.getCoords().distanceTo(addCoords) > 0.5)) { + final Observable<String> address = Observable.create(new OnSubscribe<String>() { + @Override + public void call(final Subscriber<? super String> subscriber) { + try { + addCoords = geo.getCoords(); + final Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); + final Geopoint coords = app.currentGeo().getCoords(); + final List<Address> addresses = geocoder.getFromLocation(coords.getLatitude(), coords.getLongitude(), 1); + if (!addresses.isEmpty()) { + subscriber.onNext(formatAddress(addresses.get(0))); + } + subscriber.onCompleted(); + } catch (final Exception e) { + subscriber.onError(e); + } } - } else { - navLocation.setText(geo.getCoords().toString()); - } - } else { - if (nearestView.isClickable()) { - nearestView.setFocusable(false); - nearestView.setClickable(false); - nearestView.setOnClickListener(null); - nearestView.setBackgroundResource(R.drawable.main_nearby_disabled); - } - navType.setText(null); - navAccuracy.setText(null); - navLocation.setText(res.getString(R.string.loc_trying)); + }).subscribeOn(Schedulers.io()); + AndroidObservable.fromActivity(MainActivity.this, address) + .onErrorResumeNext(Observable.from(geo.getCoords().toString())) + .subscribe(new Action1<String>() { + @Override + public void call(final String address) { + navLocation.setText(address); + } + }); } - } catch (RuntimeException e) { - Log.w("Failed to update location."); + } else { + navLocation.setText(geo.getCoords().toString()); } } } @@ -579,7 +571,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoFindOnMap(final View v) { + public void cgeoFindOnMap(@SuppressWarnings("unused") final View v) { findOnMap.setPressed(true); CGeoMap.startActivityLiveMap(this); } @@ -588,7 +580,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoFindNearest(final View v) { + public void cgeoFindNearest(@SuppressWarnings("unused") final View v) { if (app.currentGeo().getCoords() == null) { return; } @@ -601,7 +593,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoFindByOffline(final View v) { + public void cgeoFindByOffline(@SuppressWarnings("unused") final View v) { findByOffline.setPressed(true); CacheListActivity.startActivityOffline(this); } @@ -610,7 +602,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoSearch(final View v) { + public void cgeoSearch(@SuppressWarnings("unused") final View v) { advanced.setPressed(true); startActivity(new Intent(this, SearchActivity.class)); } @@ -619,7 +611,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoPoint(final View v) { + public void cgeoPoint(@SuppressWarnings("unused") final View v) { any.setPressed(true); startActivity(new Intent(this, NavigateAnyPointActivity.class)); } @@ -628,7 +620,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoFilter(final View v) { + public void cgeoFilter(@SuppressWarnings("unused") final View v) { filter.setPressed(true); filter.performClick(); } @@ -637,7 +629,7 @@ public class MainActivity extends AbstractActivity { * @param v * unused here but needed since this method is referenced from XML layout */ - public void cgeoNavSettings(final View v) { + public void cgeoNavSettings(@SuppressWarnings("unused") final View v) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } @@ -714,40 +706,11 @@ public class MainActivity extends AbstractActivity { } } - private class ObtainAddressThread extends Thread { - - public ObtainAddressThread() { - setPriority(Thread.MIN_PRIORITY); - } - - @Override - public void run() { - if (addressObtaining) { - return; - } - addressObtaining = true; - - try { - final Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); - final Geopoint coords = app.currentGeo().getCoords(); - addresses = geocoder.getFromLocation(coords.getLatitude(), coords.getLongitude(), 1); - } catch (final IOException e) { - Log.i("Failed to obtain address"); - } catch (final IllegalArgumentException e) { - Log.w("ObtainAddressThread.run", e); - } - - obtainAddressHandler.sendEmptyMessage(0); - - addressObtaining = false; - } - } - /** * @param view * unused here but needed since this method is referenced from XML layout */ - public void showAbout(final View view) { + public void showAbout(@SuppressWarnings("unused") final View view) { startActivity(new Intent(this, AboutActivity.class)); } @@ -755,7 +718,7 @@ public class MainActivity extends AbstractActivity { * @param view * unused here but needed since this method is referenced from XML layout */ - public void goSearch(final View view) { + public void goSearch(@SuppressWarnings("unused") final View view) { onSearchRequested(); } |
