1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package cgeo.geocaching;
import cgeo.geocaching.sensors.DirectionProvider;
import cgeo.geocaching.sensors.GeoDataProvider;
import cgeo.geocaching.sensors.IGeoData;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.tuple.ImmutablePair;
import rx.Observable;
import rx.functions.Func2;
import android.app.Application;
public class CgeoApplication extends Application {
private boolean forceRelog = false; // c:geo needs to log into cache providers
public boolean showLoginToast = true; //login toast shown just once.
private boolean liveMapHintShownInThisSession = false; // livemap hint has been shown
private static CgeoApplication instance;
private Observable<ImmutablePair<IGeoData,Float>> geoDir;
public CgeoApplication() {
setInstance(this);
}
private static void setInstance(final CgeoApplication application) {
instance = application;
}
public static CgeoApplication getInstance() {
return instance;
}
@Override
public void onLowMemory() {
Log.i("Cleaning applications cache.");
DataStore.removeAllFromCache();
}
public synchronized Observable<ImmutablePair<IGeoData, Float>> geoDirObservable() {
if (geoDir == null) {
final Observable<IGeoData> geo = GeoDataProvider.create(this);
final Observable<Float> dir = DirectionProvider.create(this);
final Observable<ImmutablePair<IGeoData, Float>> combined = Observable.combineLatest(geo, dir, new Func2<IGeoData, Float, ImmutablePair<IGeoData, Float>>() {
@Override
public ImmutablePair<IGeoData, Float> call(final IGeoData geoData, final Float dir) {
return ImmutablePair.of(geoData, dir);
}
});
geoDir = combined.publish().refCount();
}
return geoDir;
}
private ImmutablePair<IGeoData, Float> currentGeoDir() {
return geoDirObservable().first().toBlockingObservable().single();
}
public IGeoData currentGeo() {
return currentGeoDir().left;
}
public Float currentDirection() {
return currentGeoDir().right;
}
public boolean isLiveMapHintShownInThisSession() {
return liveMapHintShownInThisSession;
}
public void setLiveMapHintShownInThisSession() {
liveMapHintShownInThisSession = true;
}
/**
* Check if cgeo must relog even if already logged in.
*
* @return <code>true</code> if it is necessary to relog
*/
public boolean mustRelog() {
final boolean mustLogin = forceRelog;
forceRelog = false;
return mustLogin;
}
/**
* Force cgeo to relog when reaching the main activity.
*/
public void forceRelog() {
forceRelog = true;
}
}
|