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

import cgeo.geocaching.activity.AbstractActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

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

public class cgeotrackables extends AbstractActivity {
    private List<cgTrackable> trackables = new ArrayList<cgTrackable>();
    private String geocode = null;
    private LayoutInflater inflater = null;
    private LinearLayout addList = null;
    private ProgressDialog waitDialog = null;
    private Handler loadInventoryHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            try {
                if (inflater == null) {
                    inflater = getLayoutInflater();
                }

                if (addList == null) {
                    addList = (LinearLayout) findViewById(R.id.trackable_list);
                }

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

                    showToast(res.getString(R.string.err_cache_inventory_load_fail));

                    finish();
                    return;
                } else if (trackables.size() == 1) {
                    cgTrackable trackable = trackables.get(0);
                    cgeotrackable.startActivity(cgeotrackables.this, trackable.getGuid(), trackable.getGeocode(), trackable.getName());
                    finish();
                    return;
                } else {
                    LinearLayout oneTbPre = null;
                    for (cgTrackable trackable : trackables) {
                        oneTbPre = (LinearLayout) inflater.inflate(R.layout.trackable_button, null);

                        Button oneTb = (Button) oneTbPre.findViewById(R.id.button);

                        if (trackable.getName() != null) {
                            oneTb.setText(Html.fromHtml(trackable.getName()).toString());
                        } else {
                            oneTb.setText("some trackable");
                        }
                        oneTb.setClickable(true);
                        oneTb.setOnClickListener(new buttonListener(trackable.getGuid(), trackable.getGeocode(), trackable.getName()));
                        addList.addView(oneTbPre);
                    }
                }

                if (waitDialog != null) {
                    waitDialog.dismiss();
                }
            } catch (Exception e) {
                if (waitDialog != null) {
                    waitDialog.dismiss();
                }
                Log.e(Settings.tag, "cgeotrackables.loadInventoryHandler: " + e.toString());
            }
        }
    };

    public cgeotrackables() {
        super("c:geo-trackable-list");
    }

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

        setTheme();
        setContentView(R.layout.trackables);
        setTitle(res.getString(R.string.trackable));

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

        // try to get data from extras
        if (extras != null) {
            geocode = extras.getString("geocode");
        }

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

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

        (new loadInventory()).start();
    }

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

    }

    private class loadInventory extends Thread {

        @Override
        public void run() {
            try {
                trackables = app.loadInventory(geocode);

                loadInventoryHandler.sendMessage(new Message());
            } catch (Exception e) {
                Log.e(Settings.tag, "cgeotrackables.loadInventory.run: " + e.toString());
            }
        }
    }

    private class buttonListener implements View.OnClickListener {

        private String guid = null;
        private String geocode = null;
        private String name = null;

        public buttonListener(String guidIn, String geocodeIn, String nameIn) {
            guid = guidIn;
            geocode = geocodeIn;
            name = nameIn;
        }

        public void onClick(View arg0) {
            cgeotrackable.startActivity(cgeotrackables.this, guid, geocode, name);
            return;
        }
    }
}