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

import cgeo.geocaching.activity.AbstractActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.List;
import java.util.Locale;

public class cgeoaddresses extends AbstractActivity {
    private String keyword = null;
    private LayoutInflater inflater = null;
    private LinearLayout addList = null;
    private ProgressDialog waitDialog = null;

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

        // init
        inflater = getLayoutInflater();

        setTheme();
        setContentView(R.layout.addresses);
        setTitle(res.getString(R.string.search_address_result));

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

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

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

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

        new AsyncTask<Void, Void, List<Address>>() {

            @Override
            protected List<Address> doInBackground(Void... params) {
                final Geocoder geocoder = new Geocoder(cgeoaddresses.this, Locale.getDefault());
                try {
                    return geocoder.getFromLocationName(keyword, 20);
                } catch (Exception e) {
                    Log.e(Settings.tag, "cgeoaddresses.doInBackground", e);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(final List<Address> addresses) {
                waitDialog.dismiss();
                try {
                    if (addList == null) {
                        addList = (LinearLayout) findViewById(R.id.address_list);
                    }

                    if (addresses == null || addresses.isEmpty()) {
                        showToast(res.getString(R.string.err_search_address_no_match));
                        finish();
                        return;
                    } else {
                        LinearLayout oneAddPre = null;
                        for (Address address : addresses) {
                            oneAddPre = (LinearLayout) inflater.inflate(R.layout.address_button, null);

                            Button oneAdd = (Button) oneAddPre.findViewById(R.id.button);
                            int index = 0;
                            StringBuilder allAdd = new StringBuilder();
                            StringBuilder allAddLine = new StringBuilder();

                            while (address.getAddressLine(index) != null) {
                                if (allAdd.length() > 0) {
                                    allAdd.append('\n');
                                }
                                if (allAddLine.length() > 0) {
                                    allAddLine.append("; ");
                                }

                                allAdd.append(address.getAddressLine(index));
                                allAddLine.append(address.getAddressLine(index));

                                index++;
                            }

                            oneAdd.setText(allAdd.toString());
                            oneAdd.setLines(allAdd.toString().split("\n").length);
                            oneAdd.setClickable(true);
                            oneAdd.setOnClickListener(new buttonListener(address.getLatitude(), address.getLongitude(), allAddLine.toString()));
                            addList.addView(oneAddPre);
                        }
                    }
                } catch (Exception e) {
                    Log.e(Settings.tag, "cgeoaddresses.onPostExecute", e);
                }
            }

        }.execute();
    }

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

    }

    private class buttonListener implements View.OnClickListener {

        private Double latitude = null;
        private Double longitude = null;
        private String address = null;

        public buttonListener(Double latitudeIn, Double longitudeIn, String addressIn) {
            latitude = latitudeIn;
            longitude = longitudeIn;
            address = addressIn;
        }

        public void onClick(View arg0) {
            Intent addressIntent = new Intent(cgeoaddresses.this, cgeocaches.class);
            addressIntent.putExtra("type", "address");
            addressIntent.putExtra("latitude", (Double) latitude);
            addressIntent.putExtra("longitude", (Double) longitude);
            addressIntent.putExtra("address", (String) address);
            addressIntent.putExtra("cachetype", Settings.getCacheType());
            startActivity(addressIntent);

            finish();
            return;
        }
    }
}