aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/MainActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/MainActivity.java')
-rw-r--r--main/src/cgeo/geocaching/MainActivity.java255
1 files changed, 96 insertions, 159 deletions
diff --git a/main/src/cgeo/geocaching/MainActivity.java b/main/src/cgeo/geocaching/MainActivity.java
index c0c6712..45ce698 100644
--- a/main/src/cgeo/geocaching/MainActivity.java
+++ b/main/src/cgeo/geocaching/MainActivity.java
@@ -7,17 +7,21 @@ import cgeo.geocaching.activity.AbstractActionBarActivity;
import cgeo.geocaching.activity.ShowcaseViewBuilder;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.capability.ILogin;
-import cgeo.geocaching.connector.gc.GCConnector;
-import cgeo.geocaching.connector.gc.GCLogin;
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.location.AndroidGeocoder;
+import cgeo.geocaching.location.Geopoint;
+import cgeo.geocaching.location.MapQuestGeocoder;
+import cgeo.geocaching.location.Units;
import cgeo.geocaching.maps.CGeoMap;
+import cgeo.geocaching.network.Network;
+import cgeo.geocaching.sensors.GeoData;
import cgeo.geocaching.sensors.GeoDirHandler;
-import cgeo.geocaching.sensors.IGeoData;
+import cgeo.geocaching.sensors.GpsStatusProvider;
+import cgeo.geocaching.sensors.GpsStatusProvider.Status;
+import cgeo.geocaching.sensors.Sensors;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.settings.SettingsActivity;
import cgeo.geocaching.ui.dialog.Dialogs;
@@ -35,21 +39,23 @@ import com.google.zxing.integration.android.IntentResult;
import org.apache.commons.lang3.StringUtils;
import rx.Observable;
-import rx.Observable.OnSubscribe;
-import rx.Subscriber;
-import rx.android.observables.AndroidObservable;
+import rx.android.app.AppObservable;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Action0;
import rx.functions.Action1;
-import rx.subscriptions.Subscriptions;
+import rx.functions.Func1;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.SearchManager;
+import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.res.Configuration;
import android.location.Address;
-import android.location.Geocoder;
+import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -64,12 +70,12 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
+import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
-import java.util.Locale;
public class MainActivity extends AbstractActionBarActivity {
@InjectView(R.id.nav_satellites) protected TextView navSatellites;
@@ -88,11 +94,9 @@ public class MainActivity extends AbstractActionBarActivity {
public static final int SEARCH_REQUEST_CODE = 2;
- private int version = 0;
- private boolean cleanupRunning = false;
- private int countBubbleCnt = 0;
private Geopoint addCoords = null;
private boolean initialized = false;
+ private ConnectivityChangeReceiver connectivityChangeReceiver;
private final UpdateLocation locationUpdater = new UpdateLocation();
@@ -110,7 +114,7 @@ public class MainActivity extends AbstractActionBarActivity {
for (final ILogin conn : loginConns) {
- final TextView connectorInfo = (TextView) inflater.inflate(R.layout.main_activity_connectorstatus, null);
+ final TextView connectorInfo = (TextView) inflater.inflate(R.layout.main_activity_connectorstatus, infoArea, false);
infoArea.addView(connectorInfo);
final StringBuilder userInfo = new StringBuilder(conn.getName()).append(Formatter.SEPARATOR);
@@ -128,6 +132,19 @@ public class MainActivity extends AbstractActionBarActivity {
}
};
+ private final class ConnectivityChangeReceiver extends BroadcastReceiver {
+ private boolean isConnected = Network.isNetworkConnected();
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ final boolean wasConnected = isConnected;
+ isConnected = Network.isNetworkConnected();
+ if (isConnected && !wasConnected) {
+ startBackgroundLogin();
+ }
+ }
+ }
+
private static String formatAddress(final Address address) {
final ArrayList<String> addressParts = new ArrayList<>();
@@ -147,37 +164,16 @@ public class MainActivity extends AbstractActionBarActivity {
return StringUtils.join(addressParts, ", ");
}
- private class SatellitesHandler extends GeoDirHandler {
-
- private boolean gpsEnabled = false;
- private int satellitesFixed = 0;
- private int satellitesVisible = 0;
-
+ private final Action1<GpsStatusProvider.Status> satellitesHandler = new Action1<Status>() {
@Override
- public void updateGeoData(final IGeoData data) {
- if (data.getGpsEnabled() == gpsEnabled &&
- data.getSatellitesFixed() == satellitesFixed &&
- data.getSatellitesVisible() == satellitesVisible) {
- return;
- }
- gpsEnabled = data.getGpsEnabled();
- satellitesFixed = data.getSatellitesFixed();
- satellitesVisible = data.getSatellitesVisible();
-
- if (gpsEnabled) {
- if (satellitesFixed > 0) {
- navSatellites.setText(res.getString(R.string.loc_sat) + ": " + satellitesFixed + '/' + satellitesVisible);
- } else if (satellitesVisible >= 0) {
- navSatellites.setText(res.getString(R.string.loc_sat) + ": 0/" + satellitesVisible);
- }
+ public void call(final Status gpsStatus) {
+ if (gpsStatus.gpsEnabled) {
+ navSatellites.setText(res.getString(R.string.loc_sat) + ": " + gpsStatus.satellitesFixed + '/' + gpsStatus.satellitesVisible);
} else {
navSatellites.setText(res.getString(R.string.loc_gps_disabled));
}
}
-
- }
-
- private final SatellitesHandler satellitesHandler = new SatellitesHandler();
+ };
private final Handler firstLoginHandler = new Handler() {
@@ -214,8 +210,7 @@ public class MainActivity extends AbstractActionBarActivity {
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); // type to search
- version = Version.getVersionCode(this);
- Log.i("Starting " + getPackageName() + ' ' + version + " a.k.a " + Version.getVersionName(this));
+ Log.i("Starting " + getPackageName() + ' ' + Version.getVersionCode(this) + " a.k.a " + Version.getVersionName(this));
init();
@@ -231,30 +226,34 @@ public class MainActivity extends AbstractActionBarActivity {
@Override
public void onResume() {
- super.onResume(Subscriptions.from(locationUpdater.start(GeoDirHandler.UPDATE_GEODATA), satellitesHandler.start(GeoDirHandler.UPDATE_GEODATA)));
+ super.onResume(locationUpdater.start(GeoDirHandler.UPDATE_GEODATA | GeoDirHandler.LOW_POWER),
+ Sensors.getInstance().gpsStatusObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(satellitesHandler));
updateUserInfoHandler.sendEmptyMessage(-1);
startBackgroundLogin();
init();
+
+ connectivityChangeReceiver = new ConnectivityChangeReceiver();
+ registerReceiver(connectivityChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private void startBackgroundLogin() {
- assert(app != null);
+ assert app != null;
final boolean mustLogin = app.mustRelog();
for (final ILogin conn : ConnectorFactory.getActiveLiveConnectors()) {
if (mustLogin || !conn.isLoggedIn()) {
- new Thread() {
+ RxUtils.networkScheduler.createWorker().schedule(new Action0() {
@Override
- public void run() {
- if (mustLogin && conn == GCConnector.getInstance()) {
+ public void call() {
+ if (mustLogin) {
// Properly log out from geocaching.com
- GCLogin.getInstance().logout();
+ conn.logout();
}
conn.login(firstLoginHandler, MainActivity.this);
updateUserInfoHandler.sendEmptyMessage(-1);
}
- }.start();
+ });
}
}
}
@@ -276,6 +275,7 @@ public class MainActivity extends AbstractActionBarActivity {
@Override
public void onPause() {
initialized = false;
+ unregisterReceiver(connectivityChangeReceiver);
super.onPause();
}
@@ -315,7 +315,7 @@ public class MainActivity extends AbstractActionBarActivity {
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.menu_history:
- CacheListActivity.startActivityHistory(this);
+ startActivity(CacheListActivity.getHistoryIntent(this));
return true;
case R.id.menu_scan:
startScannerApplication();
@@ -447,7 +447,7 @@ public class MainActivity extends AbstractActionBarActivity {
setFilterTitle();
checkRestore();
- (new CleanDatabaseThread()).start();
+ DataStore.cleanIfNeeded(this);
}
protected void selectGlobalTypeFilter() {
@@ -464,11 +464,11 @@ public class MainActivity extends AbstractActionBarActivity {
sorted.addAll(Arrays.asList(CacheType.values()));
sorted.removeAll(cacheTypes);
+ final Collator collator = TextUtils.getCollator();
Collections.sort(sorted, new Comparator<CacheType>() {
-
@Override
public int compare(final CacheType left, final CacheType right) {
- return left.getL10n().compareToIgnoreCase(right.getL10n());
+ return collator.compare(left.getL10n(), right.getL10n());
}
});
@@ -501,7 +501,23 @@ public class MainActivity extends AbstractActionBarActivity {
}
public void updateCacheCounter() {
- (new CountBubbleUpdateThread()).start();
+ AppObservable.bindActivity(this, DataStore.getAllCachesCountObservable()).subscribe(new Action1<Integer>() {
+ @Override
+ public void call(final Integer countBubbleCnt1) {
+ if (countBubbleCnt1 == 0) {
+ countBubble.setVisibility(View.GONE);
+ } else {
+ countBubble.setText(Integer.toString(countBubbleCnt1));
+ countBubble.bringToFront();
+ countBubble.setVisibility(View.VISIBLE);
+ }
+ }
+ }, new Action1<Throwable>() {
+ @Override
+ public void call(final Throwable throwable) {
+ Log.e("Unable to add bubble count", throwable);
+ }
+ });
}
private void checkRestore() {
@@ -534,7 +550,7 @@ public class MainActivity extends AbstractActionBarActivity {
private class UpdateLocation extends GeoDirHandler {
@Override
- public void updateGeoData(final IGeoData geo) {
+ public void updateGeoData(final GeoData geo) {
if (!nearestView.isClickable()) {
nearestView.setFocusable(true);
nearestView.setClickable(true);
@@ -556,29 +572,21 @@ public class MainActivity extends AbstractActionBarActivity {
navAccuracy.setText(null);
}
+ final Geopoint currentCoords = geo.getCoords();
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>() {
+ if (addCoords == null || (currentCoords.distanceTo(addCoords) > 0.5)) {
+ addCoords = currentCoords;
+ final Observable<String> address = (new AndroidGeocoder(MainActivity.this).getFromLocation(currentCoords)
+ .onErrorResumeNext(MapQuestGeocoder.getFromLocation(currentCoords))).map(new Func1<Address, 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);
- }
+ public String call(final Address address) {
+ return formatAddress(address);
}
- });
- AndroidObservable.bindActivity(MainActivity.this, address.onErrorResumeNext(Observable.from(geo.getCoords().toString())))
+ }).onErrorResumeNext(Observable.just(currentCoords.toString()));
+ AppObservable.bindActivity(MainActivity.this, address)
.subscribeOn(RxUtils.networkScheduler)
.subscribe(new Action1<String>() {
@Override
@@ -588,7 +596,7 @@ public class MainActivity extends AbstractActionBarActivity {
});
}
} else {
- navLocation.setText(geo.getCoords().toString());
+ navLocation.setText(currentCoords.toString());
}
}
}
@@ -599,7 +607,7 @@ public class MainActivity extends AbstractActionBarActivity {
*/
public void cgeoFindOnMap(final View v) {
findOnMap.setPressed(true);
- CGeoMap.startActivityLiveMap(this);
+ startActivity(CGeoMap.getLiveMapIntent(this));
}
/**
@@ -607,12 +615,8 @@ public class MainActivity extends AbstractActionBarActivity {
* unused here but needed since this method is referenced from XML layout
*/
public void cgeoFindNearest(final View v) {
- if (app.currentGeo().getCoords() == null) {
- return;
- }
-
nearestView.setPressed(true);
- CacheListActivity.startActivityNearest(this, app.currentGeo().getCoords());
+ startActivity(CacheListActivity.getNearestIntent(this));
}
/**
@@ -659,86 +663,19 @@ public class MainActivity extends AbstractActionBarActivity {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
- private class CountBubbleUpdateThread extends Thread {
- private final Handler countBubbleHandler = new Handler() {
-
- @Override
- public void handleMessage(final Message msg) {
- try {
- if (countBubbleCnt == 0) {
- countBubble.setVisibility(View.GONE);
- } else {
- countBubble.setText(Integer.toString(countBubbleCnt));
- countBubble.bringToFront();
- countBubble.setVisibility(View.VISIBLE);
- }
- } catch (final Exception e) {
- Log.w("MainActivity.countBubbleHander", e);
- }
- }
- };
-
- @Override
- public void run() {
- if (app == null) {
- return;
- }
-
- int checks = 0;
- while (!DataStore.isInitialized()) {
- try {
- sleep(500);
- checks++;
- } catch (final Exception e) {
- Log.e("MainActivity.CountBubbleUpdateThread.run", e);
- }
-
- if (checks > 10) {
- return;
- }
- }
-
- countBubbleCnt = DataStore.getAllCachesCount();
-
- countBubbleHandler.sendEmptyMessage(0);
- }
- }
-
- private class CleanDatabaseThread extends Thread {
-
- @Override
- public void run() {
- if (app == null) {
- return;
- }
- if (cleanupRunning) {
- return;
- }
-
- boolean more = false;
- if (version != Settings.getVersion()) {
- Log.i("Initializing hard cleanup - version changed from " + Settings.getVersion() + " to " + version + ".");
-
- more = true;
- }
-
- cleanupRunning = true;
- DataStore.clean(more);
- cleanupRunning = false;
-
- if (version > 0) {
- Settings.setVersion(version);
- }
- }
- }
-
private void checkShowChangelog() {
- final long lastChecksum = Settings.getLastChangelogChecksum();
- final long checksum = TextUtils.checksum(getString(R.string.changelog_master) + getString(R.string.changelog_release));
- Settings.setLastChangelogChecksum(checksum);
- // don't show change log after new install...
- if (lastChecksum > 0 && lastChecksum != checksum) {
- AboutActivity.showChangeLog(this);
+ // temporary workaround for #4143
+ //TODO: understand and avoid if possible
+ try {
+ final long lastChecksum = Settings.getLastChangelogChecksum();
+ final long checksum = TextUtils.checksum(getString(R.string.changelog_master) + getString(R.string.changelog_release));
+ Settings.setLastChangelogChecksum(checksum);
+ // don't show change log after new install...
+ if (lastChecksum > 0 && lastChecksum != checksum) {
+ AboutActivity.showChangeLog(this);
+ }
+ } catch (final Exception ex) {
+ Log.e("Error checking/showing changelog!", ex);
}
}