aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/PocketQueryList.java
blob: 9d1110d93e2a3d8754e40570344e53a803d01750 (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
package cgeo.geocaching;

import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.connector.gc.GCParser;
import cgeo.geocaching.utils.RunnableWithArgument;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;

import java.util.List;

public final class PocketQueryList {

    private final String guid;
    private final int maxCaches;
    private final String name;

    public PocketQueryList(String guid, String name, int maxCaches) {
        this.guid = guid;
        this.name = name;
        this.maxCaches = maxCaches;
    }

    public static class UserInterface {

        List<PocketQueryList> pocketQueryList = null;
        RunnableWithArgument<PocketQueryList> runAfterwards;

        private Handler loadPocketQueryHandler = new Handler() {

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

                    ActivityMixin.showToast(activity, res.getString(R.string.warn_no_pocket_query_found));

                    return;
                }

                if (waitDialog != null) {
                    waitDialog.dismiss();
                }

                final CharSequence[] items = new CharSequence[pocketQueryList.size()];

                for (int i = 0; i < pocketQueryList.size(); i++) {
                    PocketQueryList pq = pocketQueryList.get(i);
                    items[i] = pq.name;
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle(res.getString(R.string.search_pocket_select));
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int itemId) {
                        final PocketQueryList query = pocketQueryList.get(itemId);
                        dialogInterface.dismiss();
                        runAfterwards.run(query);
                    }
                });
                builder.create().show();

            }
        };

        private class LoadPocketQueryListThread extends Thread {
            final private Handler handler;

            public LoadPocketQueryListThread(Handler handlerIn) {
                handler = handlerIn;
            }

            @Override
            public void run() {
                pocketQueryList = GCParser.searchPocketQueryList();
                handler.sendMessage(Message.obtain());
            }
        }

        private final Activity activity;
        private final CgeoApplication app;
        private final Resources res;
        private ProgressDialog waitDialog = null;

        public UserInterface(final Activity activity) {
            this.activity = activity;
            app = CgeoApplication.getInstance();
            res = app.getResources();
        }

        public void promptForListSelection(final RunnableWithArgument<PocketQueryList> runAfterwards) {

            this.runAfterwards = runAfterwards;

            waitDialog = ProgressDialog.show(activity, res.getString(R.string.search_pocket_title), res.getString(R.string.search_pocket_loading), true, true);

            LoadPocketQueryListThread thread = new LoadPocketQueryListThread(loadPocketQueryHandler);
            thread.start();
        }


    }

    public String getGuid() {
        return guid;
    }

    public int getMaxCaches() {
        return maxCaches;
    }

    public String getName() {
        return name;
    }

}