-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.java
More file actions
330 lines (269 loc) · 12 KB
/
code.java
File metadata and controls
330 lines (269 loc) · 12 KB
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
package com.saferoutesapp.saferoutesapp;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Filter;
import android.widget.Filterable;
import android.util.Log;
import android.Manifest;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by avniv on 4/14/2017.
*/
public class InputActivity extends AppCompatActivity {
final private String TAG = this.getClass().getSimpleName();
private LocationListener listener;
private LocationManager locationManager;
private LatLng src=null;
private LatLng dest=null;
private Geocoder geocoder;
private AutoCompleteTextView sourceACTextView;
private AutoCompleteTextView destinationACTextView;
private ArrayList<String> placeIDs;
private GoogleApiClient mGoogleApiClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input_activity);
sourceACTextView = (AutoCompleteTextView) findViewById(R.id.from_input);
if(!runtime_permissions())
enable_buttons();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
AutocompleteFilter filter = new AutocompleteFilter.Builder().setCountry("US").build();
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@@@"+src);
LatLngBounds bounds = new LatLngBounds(src, dest);
placeIDs = new ArrayList<>();
destinationACTextView = (AutoCompleteTextView) findViewById(R.id.to_input);
destinationACTextView.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.autocomplete_list_item, mGoogleApiClient, bounds,filter));
destinationACTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String description = (String) parent.getItemAtPosition(position);
List<Address> addressList;
try {
addressList = geocoder.getFromLocationName(description, 1);
Address addr1 = addressList.get(0);
dest = new LatLng(addr1.getLatitude(), addr1.getLongitude());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void enable_buttons() {
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// getting the current location
src = new LatLng(location.getLatitude(), location.getLongitude());
// getting the current address from the lat, long
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geocoder.getFromLocation(src.latitude, src.longitude, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i=0; i<maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
String finalAddress = builder.toString(); //This is the complete address.
sourceACTextView.setText(finalAddress);
} catch (IOException e) {}
catch (NullPointerException e) {}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,listener);
}
private boolean runtime_permissions() {
if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100){
if( grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
enable_buttons();
}else {
runtime_permissions();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar, menu);
return true;
}
public void onFindRoutes(View v) {
// if the current location is null, set it to tempe explicitly
if(src==null)
src = new LatLng(33.4284307, -111.9504421);
//dest = new LatLng(33.4236, -111.9393);
Log.d(TAG, "----------------src :: "+src);
Log.d(TAG, "----------------dest :: "+dest);
Intent intent = new Intent(this, MapsActivity.class);
Bundle args = new Bundle();
args.putParcelable("src", src);
args.putParcelable("dest", dest);
intent.putExtra("bundle", args);
startActivity(intent);
}
class PlaceAPI {
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "AIzaSyDmNBpYDBoxkwYTW5Aw9H3YrEXaSi-tnAo";
public ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&types=geocode");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Log.d(TAG, jsonResults.toString());
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
placeIDs.add(predsJsonArray.getJSONObject(i).getString("place_id"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
ArrayList<String> resultList;
Context mContext;
int mResource;
private LatLngBounds mBounds;
private GoogleApiClient mGoogleApiClient;
private AutocompleteFilter mPlaceFilter;
PlaceAPI mPlaceAPI = new PlaceAPI();
public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient,
LatLngBounds bounds, AutocompleteFilter filter) {
super(context, resource);
mContext = context;
mResource = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
mPlaceFilter = filter;
}
@Override
public int getCount() {
// Last item will be the footer
return resultList.size();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
resultList = mPlaceAPI.autocomplete(constraint.toString());
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
}