aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgeowaypoint.java
blob: 4874f1b2b11294556e396503ac4a22303cf747ad (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package cgeo.geocaching;

import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory.NavigationAppsEnum;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.utils.IObserver;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class cgeowaypoint extends AbstractActivity implements IObserver<IGeoData> {

    private static final int MENU_ID_NAVIGATION = 0;
    private static final int MENU_ID_CACHES_AROUND = 5;
    private static final int MENU_ID_DEFAULT_NAVIGATION = 2;
    private static final int MENU_ID_OPEN_GEOCACHE = 6;
    private cgWaypoint waypoint = null;
    private int id = -1;
    private ProgressDialog waitDialog = null;
    private Handler loadWaypointHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (waitDialog != null) {
                waitDialog.dismiss();
                waitDialog = null;
            }

            if (waypoint == null) {
                showToast(res.getString(R.string.err_waypoint_load_failed));
                finish();
                return;
            }

            final TextView identification = (TextView) findViewById(R.id.identification);
            final TextView coords = (TextView) findViewById(R.id.coordinates);
            final ImageView defaultNavigation = (ImageView) findViewById(R.id.defaultNavigation);
            final View separator = findViewById(R.id.separator);

            final View headline = findViewById(R.id.headline);
            registerNavigationMenu(headline);

            if (StringUtils.isNotBlank(waypoint.getName())) {
                setTitle(Html.fromHtml(waypoint.getName()).toString());
            } else {
                setTitle(res.getString(R.string.waypoint_title));
            }

            if (!waypoint.getPrefix().equalsIgnoreCase("OWN")) {
                identification.setText(waypoint.getPrefix() + "/" + waypoint.getLookup());
            } else {
                identification.setText(res.getString(R.string.waypoint_custom));
            }
            registerNavigationMenu(identification);
            waypoint.setIcon(res, identification);

            if (waypoint.getCoords() != null) {
                coords.setText(Html.fromHtml(waypoint.getCoords().toString()), TextView.BufferType.SPANNABLE);
                defaultNavigation.setVisibility(View.VISIBLE);
                separator.setVisibility(View.VISIBLE);
            } else {
                coords.setText(res.getString(R.string.waypoint_unknown_coordinates));
                defaultNavigation.setVisibility(View.GONE);
                separator.setVisibility(View.GONE);
            }
            registerNavigationMenu(coords);

            if (StringUtils.isNotBlank(waypoint.getNote())) {
                final TextView note = (TextView) findViewById(R.id.note);
                note.setText(Html.fromHtml(waypoint.getNote()), TextView.BufferType.SPANNABLE);
                registerNavigationMenu(note);
            }

            final Button buttonEdit = (Button) findViewById(R.id.edit);
            buttonEdit.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    cgeowaypointadd.startActivityEditWaypoint(cgeowaypoint.this, id);
                }
            });

            if (waypoint.isUserDefined()) {
                final Button buttonDelete = (Button) findViewById(R.id.delete);
                buttonDelete.setOnClickListener(new deleteWaypointListener());
                buttonDelete.setVisibility(View.VISIBLE);
            }
        }

        private void registerNavigationMenu(View view) {
            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    registerForContextMenu(v);
                    if (navigationPossible()) {
                        openContextMenu(v);
                    }
                }
            });
        }
    };

    public cgeowaypoint() {
        super("c:geo-waypoint-details");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTheme();
        setContentView(R.layout.waypoint);
        setTitle(R.string.waypoint_title);

        // get parameters
        Bundle extras = getIntent().getExtras();

        // try to get data from extras
        if (extras != null) {
            id = extras.getInt("waypoint");
        }

        if (id <= 0) {
            showToast(res.getString(R.string.err_waypoint_unknown));
            finish();
            return;
        }

        waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
        waitDialog.setCancelable(true);

        (new loadWaypoint()).start();

        ImageView defaultNavigationImageView = (ImageView) findViewById(R.id.defaultNavigation);
        defaultNavigationImageView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startDefaultNavigation2();
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        app.addGeoObserver(this);

        if (waitDialog == null) {
            waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
            waitDialog.setCancelable(true);

            (new loadWaypoint()).start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onPause() {
        app.deleteGeoObserver(this);
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_ID_DEFAULT_NAVIGATION, 0, NavigationAppFactory.getDefaultNavigationApplication(this).getName()).setIcon(R.drawable.ic_menu_compass); // default navigation tool
        menu.add(0, MENU_ID_NAVIGATION, 0, res.getString(R.string.cache_menu_navigate)).setIcon(R.drawable.ic_menu_mapmode);
        menu.add(0, MENU_ID_CACHES_AROUND, 0, res.getString(R.string.cache_menu_around)).setIcon(R.drawable.ic_menu_rotate); // caches around
        menu.add(0, MENU_ID_OPEN_GEOCACHE, 0, res.getString(R.string.waypoint_menu_open_cache)).setIcon(R.drawable.ic_menu_mylocation); // open geocache

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        try {
            boolean visible = waypoint != null && waypoint.getCoords() != null;
            menu.findItem(MENU_ID_NAVIGATION).setVisible(visible);
            menu.findItem(MENU_ID_DEFAULT_NAVIGATION).setVisible(visible);
            menu.findItem(MENU_ID_CACHES_AROUND).setVisible(visible);

            boolean openGeocache = waypoint != null && StringUtils.isNotEmpty(waypoint.getGeocode());
            menu.findItem(MENU_ID_OPEN_GEOCACHE).setVisible(openGeocache);
        } catch (Exception e) {
            // nothing
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ID_DEFAULT_NAVIGATION:
                goDefaultNavigation(null);
                return true;
            case MENU_ID_CACHES_AROUND:
                cachesAround();
                return true;
            case MENU_ID_OPEN_GEOCACHE:
                goToGeocache();
                return true;
            case MENU_ID_NAVIGATION:
                NavigationAppFactory.showNavigationMenu(app.currentGeo(), this, null, waypoint, null);
                return true;
            default:
                return false;
        }
    }

    private void cachesAround() {
        if (waypoint == null || waypoint.getCoords() == null) {
            showToast(res.getString(R.string.err_location_unknown));
            return;
        }

        cgeocaches.startActivityCoordinates(this, waypoint.getCoords());

        finish();
    }

    private void goToGeocache() {
        if (waypoint == null || waypoint.getGeocode() == null) {
            showToast(res.getString(R.string.err_waypoint_open_cache_failed));
            return;
        }

        CacheDetailActivity.startActivity(this, waypoint.getGeocode());

        finish();
    }

    private class loadWaypoint extends Thread {

        @Override
        public void run() {
            try {
                waypoint = app.loadWaypoint(id);

                loadWaypointHandler.sendMessage(Message.obtain());
            } catch (Exception e) {
                Log.e("cgeowaypoint.loadWaypoint.run: " + e.toString());
            }
        }
    }

    @Override
    public void update(final IGeoData geo) {
        // nothing
    }

    private class deleteWaypointListener implements View.OnClickListener {

        public void onClick(View arg0) {
            String geocode = waypoint.getGeocode();
            cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
            if (null != cache && cache.deleteWaypoint(waypoint)) {
                app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));

                StaticMapsProvider.removeWpStaticMaps(id, geocode);

                finish();
            } else {
                showToast(res.getString(R.string.err_waypoint_delete_failed));
            }
        }
    }

    /**
     * @param view
     *            this method is also referenced from XML layout
     */
    public void goDefaultNavigation(View view) {
        if (!navigationPossible()) {
            return;
        }

        NavigationAppFactory.startDefaultNavigationApplication(app.currentGeo(), this, null, waypoint, null);
    }

    /**
     * Tries to navigate to the {@link cgCache} of this activity.
     */
    private void startDefaultNavigation2() {
        if (!navigationPossible()) {
            return;
        }

        NavigationAppFactory.startDefaultNavigationApplication2(app.currentGeo(), this, null, waypoint, null);
    }

    private boolean navigationPossible() {
        if (waypoint == null || waypoint.getCoords() == null) {
            showToast(res.getString(R.string.err_location_unknown));
            return false;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        if (navigationPossible()) {
            menu.setHeaderTitle(res.getString(R.string.cache_menu_navigate));
            List<NavigationAppsEnum> filter = new ArrayList<NavigationAppsEnum>(1);
            if (StaticMapsProvider.doesExistStaticMapForWaypoint(waypoint.getGeocode(), waypoint.getId())) {
                filter.add(NavigationAppsEnum.DOWNLOAD_STATIC_MAPS);
            } else {
                filter.add(NavigationAppsEnum.STATIC_MAP);
            }
            NavigationAppFactory.addMenuItems(menu, this, filter);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        boolean handled = onOptionsItemSelected(item);
        if (handled) {
            return true;
        }
        return NavigationAppFactory.onMenuItemSelected(item, app.currentGeo(), this, null, waypoint, null);
    }

    public static void startActivity(final Context context, final int waypointId) {
        Intent popupIntent = new Intent(context, cgeowaypoint.class);
        popupIntent.putExtra("waypoint", waypointId);
        context.startActivity(popupIntent);
    }
}