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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.gcvote.GCVote;
import cgeo.geocaching.gcvote.GCVoteRating;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Units;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.CacheDetailsCreator;
import cgeo.geocaching.ui.LoggingUI;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public abstract class AbstractPopupActivity extends AbstractActivity implements CacheMenuHandler.ActivityInterface {
protected Geocache cache = null;
protected String geocode = null;
protected CacheDetailsCreator details;
private TextView cacheDistance = null;
private final int layout;
private final GeoDirHandler geoUpdate = new GeoDirHandler() {
@Override
protected void updateGeoData(final IGeoData geo) {
try {
if (geo.getCoords() != null && cache != null && cache.getCoords() != null) {
cacheDistance.setText(Units.getDistanceFromKilometers(geo.getCoords().distanceTo(cache.getCoords())));
cacheDistance.bringToFront();
}
onUpdateGeoData(geo);
} catch (final RuntimeException e) {
Log.w("Failed to UpdateLocation location.");
}
}
};
/**
* Callback to run when new location information is available.
* This may be overridden by deriving classes. The default implementation does nothing.
*
* @param geo
* the new data
*/
public void onUpdateGeoData(final IGeoData geo) {
}
protected AbstractPopupActivity(int layout) {
this.layout = layout;
}
private void aquireGCVote() {
if (!Settings.isRatingWanted()) {
return;
}
if (!cache.supportsGCVote()) {
return;
}
(new Thread("Load GCVote") {
@Override
public void run() {
final GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode);
if (rating == null) {
return;
}
cache.setRating(rating.getRating());
cache.setVotes(rating.getVotes());
runOnUiThread(new Runnable() {
@Override
public void run() {
details.addRating(cache); }
});
}
}).start();
}
protected void init() {
cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
if (cache == null) {
showToast(res.getString(R.string.err_detail_cache_find));
finish();
return;
}
geocode = cache.getGeocode();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set theme
this.setTheme(ActivityMixin.getDialogTheme());
// set layout
setContentView(layout);
// get parameters
final Bundle extras = getIntent().getExtras();
if (extras != null) {
geocode = extras.getString(Intents.EXTRA_GEOCODE);
}
if (StringUtils.isBlank(geocode)) {
showToast(res.getString(R.string.err_detail_cache_find));
finish();
return;
}
final ImageView defaultNavigationImageView = (ImageView) findViewById(R.id.defaultNavigation);
defaultNavigationImageView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
startDefaultNavigation2();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
CacheMenuHandler.addMenuItems(this, menu, cache);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (CacheMenuHandler.onMenuItemSelected(item, this, cache)) {
return true;
}
if (LoggingUI.onMenuItemSelected(item, this, cache)) {
return true;
}
return true;
}
@Override
public void onPause() {
geoUpdate.stopGeo();
super.onPause();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
try {
CacheMenuHandler.onPrepareOptionsMenu(menu, cache);
LoggingUI.onPrepareOptionsMenu(menu, cache);
} catch (final RuntimeException e) {
// nothing
}
return true;
}
protected abstract Geopoint getCoordinates();
@Override
public void onResume() {
super.onResume();
init();
geoUpdate.startGeo();
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
final Rect r = new Rect(0, 0, 0, 0);
getWindow().getDecorView().getHitRect(r);
if (!r.contains((int) event.getX(), (int) event.getY())) {
finish();
return true;
}
}
return super.onTouchEvent(event);
}
protected abstract void startDefaultNavigation2();
protected final void addCacheDetails() {
assert cache != null;
// cache type
final String cacheType = cache.getType().getL10n();
final String cacheSize = cache.getSize() != CacheSize.UNKNOWN ? " (" + cache.getSize().getL10n() + ")" : "";
details.add(R.string.cache_type, cacheType + cacheSize);
details.add(R.string.cache_geocode, cache.getGeocode());
details.addCacheState(cache);
details.addDistance(cache, cacheDistance);
cacheDistance = details.getValueView();
details.addDifficulty(cache);
details.addTerrain(cache);
details.addEventDate(cache);
// rating
if (cache.getRating() > 0) {
details.addRating(cache);
} else {
aquireGCVote();
}
// favorite count
details.add(R.string.cache_favorite, cache.getFavoritePoints() + "×");
// more details
final Button buttonMore = (Button) findViewById(R.id.more_details);
buttonMore.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CacheDetailActivity.startActivity(AbstractPopupActivity.this, geocode);
finish();
}
});
}
@Override
public void cachesAround() {
final Geopoint coords = getCoordinates();
if (coords == null) {
showToast(res.getString(R.string.err_location_unknown));
return;
}
CacheListActivity.startActivityCoordinates(this, coords);
finish();
}
/**
* @param view
* unused here but needed since this method is referenced from XML layout
*/
public final void goDefaultNavigation(View view) {
navigateTo();
finish();
}
}
|