aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/AbstractDialogFragment.java
blob: 6e58bd348b36b87ffc31066b19c9376d86f2a4c3 (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
package cgeo.geocaching;

import butterknife.ButterKnife;

import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.gcvote.GCVote;
import cgeo.geocaching.gcvote.GCVoteRating;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.location.Units;
import cgeo.geocaching.sensors.GeoData;
import cgeo.geocaching.sensors.GeoDirHandler;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.CacheDetailsCreator;
import cgeo.geocaching.ui.LoggingUI;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RxUtils;

import rx.Observable;
import rx.Subscription;
import rx.android.app.AppObservable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.subscriptions.Subscriptions;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.PopupMenu;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class AbstractDialogFragment extends DialogFragment implements CacheMenuHandler.ActivityInterface, PopupMenu.OnMenuItemClickListener, MenuItem.OnMenuItemClickListener {
    protected Resources res = null;
    protected String geocode;
    protected CacheDetailsCreator details;

    private Subscription resumeSubscription = Subscriptions.empty();
    private TextView cacheDistance = null;


    protected static final String GEOCODE_ARG= "GEOCODE";
    protected static final String WAYPOINT_ARG= "WAYPOINT";

    protected Geocache cache;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        res = getResources();
        setHasOptionsMenu(true);
    }

    protected void initCustomActionBar(final View v)
    {
        final ImageView defaultNavigationImageView = ButterKnife.findById(v, R.id.defaultNavigation);
        defaultNavigationImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View v) {
                startDefaultNavigation2();
                return true;
            }
        });
        defaultNavigationImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                navigateTo();
            }
        });

        final View overflowActionBar = v.findViewById(R.id.overflowActionBar);
        overflowActionBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                showPopup(v);
            }
        });
        /* Use a context menu instead popup where the popup menu is not working */
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            registerForContextMenu(overflowActionBar);
        }

    }

    final public void setTitle(final CharSequence title) {
        final TextView titleview = (TextView) getView().findViewById(R.id.actionbar_title);
        if (titleview != null) {
            titleview.setText(title);

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        geocode = getArguments().getString(GEOCODE_ARG);
    }


    protected void showPopup(final View view)
    {
        // For reason I totally not understand the PopupMenu from Appcompat is broken beyond
        // repair. Chicken out here and show the old menu on Gingerbread.
        // The "correct" way of implementing this is stil in
        // showPopupCompat(view)

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            view.showContextMenu();
        } else {
            showPopupHoneycomb(view);
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void showPopupHoneycomb(final View view) {
        final android.widget.PopupMenu popupMenu = new android.widget.PopupMenu(getActivity(), view);
        CacheMenuHandler.addMenuItems(new MenuInflater(getActivity()), popupMenu.getMenu(), cache);
        popupMenu.setOnMenuItemClickListener(
                new android.widget.PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(final MenuItem item) {
                       return AbstractDialogFragment.this.onMenuItemClick(item);
                    }
                }
        );
        popupMenu.show();
    }

    protected void showPopupCompat(final View view)
    {
        final PopupMenu popupMenu = new PopupMenu(getActivity(), view);

        // Directly instantiate SupportMenuInflater instead of getActivity().getMenuinflator
        // getMenuinflator will throw a NPE since it tries to get the not displayed ActionBar
        // menuinflator = getActivity().getMenuInflater();
        // MenuInflater menuinflator = new SupportMenuInflater(getActivity());
        CacheMenuHandler.addMenuItems(popupMenu.getMenuInflater(), popupMenu.getMenu(), cache);
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.show();
    }


    protected void init()
    {
        cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);

        if (cache == null) {
            ((AbstractActivity) getActivity()).showToast(res.getString(R.string.err_detail_cache_find));

            getActivity().finish();
            return;
        }

        geocode = cache.getGeocode();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.resumeSubscription = geoUpdate.start(GeoDirHandler.UPDATE_GEODATA);
        init();
    }


    @Override
    public void onPause() {
        resumeSubscription.unsubscribe();
        super.onPause();
    }


    private void aquireGCVote() {
        if (!Settings.isRatingWanted()) {
            return;
        }
        if (!cache.supportsGCVote()) {
            return;
        }
        AppObservable.bindActivity(getActivity(), Observable.defer(new Func0<Observable<GCVoteRating>>() {
            @Override
            public Observable<GCVoteRating> call() {
                final GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode);
                return rating != null ? Observable.just(rating) : Observable.<GCVoteRating>empty();
            }
        })).subscribeOn(RxUtils.networkScheduler).subscribe(new Action1<GCVoteRating>() {
            @Override
            public void call(final GCVoteRating rating) {
                cache.setRating(rating.getRating());
                cache.setVotes(rating.getVotes());
                DataStore.saveChangedCache(cache);
                details.addRating(cache);
            }
        });
    }

    protected final void addCacheDetails() {
        assert cache != null;
        // cache type
        final String cacheType = cache.getType().getL10n();
        final String cacheSize = cache.showSize() ? " (" + 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) getView().findViewById(R.id.more_details);

        buttonMore.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View arg0) {
                CacheDetailActivity.startActivity(getActivity(), geocode);
                getActivity().finish();
            }
        });

        /* Only working combination as it seems */
        registerForContextMenu(buttonMore);
    }

    public final void showToast(final String text) {
        ActivityMixin.showToast(getActivity(), text);
    }

    private final GeoDirHandler geoUpdate = new GeoDirHandler() {

        @Override
        public void updateGeoData(final GeoData geo) {
            try {
                if (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 update location", e);
            }
        }
    };

    /**
     * @param geo
     *            location
     */
    protected void onUpdateGeoData(final GeoData geo) {
        // do nothing by default
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        CacheMenuHandler.addMenuItems(inflater, menu, cache);

    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        CacheMenuHandler.addMenuItems(new MenuInflater(getActivity()), menu, cache);
        for (int i=0;i<menu.size();i++) {
            final MenuItem m = menu.getItem(i);
            m.setOnMenuItemClickListener(this);
        }
    }

    @Override
    public boolean onContextItemSelected(final MenuItem item) {
        return onOptionsItemSelected(item);
    }


    @Override
    public boolean onMenuItemClick(final MenuItem menuItem) {
        return onOptionsItemSelected(menuItem);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        if (CacheMenuHandler.onMenuItemSelected(item, this, cache)) {
            return true;
        }
        if (LoggingUI.onMenuItemSelected(item, getActivity(), cache)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

        try {
            CacheMenuHandler.onPrepareOptionsMenu(menu, cache);
            LoggingUI.onPrepareOptionsMenu(menu, cache);
        } catch (final RuntimeException ignored) {
            // nothing
        }
    }


    protected abstract Geopoint getCoordinates();

    protected abstract void startDefaultNavigation2();


    @Override
    public void cachesAround() {
        final Geopoint coords = getCoordinates();
        if (coords == null) {
            showToast(res.getString(R.string.err_location_unknown));
            return;
        }
        CacheListActivity.startActivityCoordinates((AbstractActivity) getActivity(), coords, cache != null ? cache.getName() : null);
        getActivity().finish();
    }

    @Override
    public void onCancel(final DialogInterface dialog) {
        super.onCancel(dialog);
        getActivity().finish();
    }

}