Guest User

Untitled

a guest
Dec 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. private static final String TAG = "PlaceArrayAdapter";
  2. private GoogleApiClient mGoogleApiClient;
  3. private AutocompleteFilter mPlaceFilter;
  4. private LatLngBounds mBounds;
  5. private ArrayList<PlaceAutocomplete> mResultList;
  6.  
  7. /**
  8. * Constructor
  9. *
  10. * @param context Context
  11. * @param resource Layout resource
  12. * @param bounds Used to specify the search bounds
  13. * @param filter Used to specify place types
  14. */
  15. public PlaceArrayAdapter(Context context, int resource, LatLngBounds bounds,
  16. AutocompleteFilter filter) {
  17. super(context, resource);
  18. mBounds = bounds;
  19. mPlaceFilter = filter;
  20. }
  21.  
  22. public void setGoogleApiClient(GoogleApiClient googleApiClient) {
  23. if (googleApiClient == null || !googleApiClient.isConnected()) {
  24. mGoogleApiClient = null;
  25. } else {
  26. mGoogleApiClient = googleApiClient;
  27. }
  28. }
  29.  
  30. @Override
  31. public int getCount() {
  32. return mResultList.size();
  33. }
  34.  
  35. @Override
  36. public PlaceAutocomplete getItem(int position) {
  37. return mResultList.get(position);
  38. }
  39.  
  40. private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
  41. if (mGoogleApiClient != null) {
  42. Log.i(TAG, "Executing autocomplete query for: " + constraint);
  43. PendingResult<AutocompletePredictionBuffer> results =
  44. Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint.toString(), mBounds, null);
  45. // Wait for predictions, set the timeout.
  46. AutocompletePredictionBuffer autocompletePredictions = results
  47. .await(60, TimeUnit.SECONDS);
  48. final Status status = autocompletePredictions.getStatus();
  49. if (!status.isSuccess()) {
  50. Toast.makeText(getContext(), "Error: " + status.toString(),
  51. Toast.LENGTH_SHORT).show();
  52. Log.e(TAG, "Error getting place predictions: " + status
  53. .toString());
  54. autocompletePredictions.release();
  55. return null;
  56. }
  57.  
  58. Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
  59. + " predictions.");
  60. Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
  61. ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
  62. while (iterator.hasNext()) {
  63. AutocompletePrediction prediction = iterator.next();
  64. resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
  65. prediction.getFullText(null)));
  66. }
  67. // Buffer release
  68. autocompletePredictions.release();
  69. return resultList;
  70. }
  71. Log.e(TAG, "Google API client is not connected.");
  72. return null;
  73. }
  74.  
  75. @Override
  76. public Filter getFilter() {
  77. Filter filter = new Filter() {
  78. @Override
  79. protected FilterResults performFiltering(CharSequence constraint) {
  80. FilterResults results = new FilterResults();
  81. if (constraint != null) {
  82. // Query the autocomplete API for the entered constraint
  83. mResultList = getPredictions(constraint);
  84. if (mResultList != null) {
  85. // Results
  86. results.values = mResultList;
  87. results.count = mResultList.size();
  88. }
  89. }
  90. return results;
  91. }
  92.  
  93. @Override
  94. protected void publishResults(CharSequence constraint, FilterResults results) {
  95. if (results != null && results.count > 0) {
  96. // The API returned at least one result, update the data.
  97. notifyDataSetChanged();
  98. } else {
  99. // The API did not return any results, invalidate the data set.
  100. notifyDataSetInvalidated();
  101. }
  102. }
  103. };
  104. return filter;
  105. }
  106.  
  107. class PlaceAutocomplete {
  108.  
  109. public CharSequence placeId;
  110. public CharSequence description;
  111.  
  112. PlaceAutocomplete(CharSequence placeId, CharSequence description) {
  113. this.placeId = placeId;
  114. this.description = description;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return description.toString();
  120. }
  121. }
  122.  
  123. public class ConfirmActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
  124.  
  125. private static final String LOG_TAG = "ConfirmActivity";
  126. private static final int GOOGLE_API_CLIENT_ID = 0;
  127. private AutoCompleteTextView mAutocompleteTextView;
  128. private TextView mNameTextView;
  129. private TextView mAddressTextView;
  130. private TextView mIdTextView;
  131. private TextView mPhoneTextView;
  132. private TextView mWebTextView;
  133. private TextView mAttTextView;
  134. private GoogleApiClient mGoogleApiClient;
  135. private PlaceArrayAdapter mPlaceArrayAdapter;
  136. private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
  137. new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
  138.  
  139.  
  140. @Override
  141. protected void onCreate(Bundle savedInstanceState) {
  142. super.onCreate(savedInstanceState);
  143. setContentView(R.layout.activity_confirm);
  144.  
  145. mGoogleApiClient = new GoogleApiClient.Builder(ConfirmActivity.this)
  146. .addApi(Places.GEO_DATA_API)
  147. .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
  148. .addConnectionCallbacks(this)
  149. .build();
  150. mAutocompleteTextView = (AutoCompleteTextView) findViewById(R.id
  151. .autoCompleteTextView);
  152. mAutocompleteTextView.setThreshold(3);
  153. mNameTextView = (TextView) findViewById(R.id.name);
  154. mAddressTextView = (TextView) findViewById(R.id.address);
  155. mIdTextView = (TextView) findViewById(R.id.place_id);
  156. mPhoneTextView = (TextView) findViewById(R.id.phone);
  157. mWebTextView = (TextView) findViewById(R.id.web);
  158. mAttTextView = (TextView) findViewById(R.id.att);
  159. mAutocompleteTextView.setOnItemClickListener(mAutocompleteClickListener);
  160. mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
  161. BOUNDS_MOUNTAIN_VIEW, null);
  162. mAutocompleteTextView.setAdapter(mPlaceArrayAdapter);
  163. }
  164.  
  165. private AdapterView.OnItemClickListener mAutocompleteClickListener
  166. = new AdapterView.OnItemClickListener() {
  167. @Override
  168. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  169. final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position);
  170. final String placeId = String.valueOf(item.placeId);
  171. Log.i(LOG_TAG, "Selected: " + item.description);
  172. PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
  173. .getPlaceById(mGoogleApiClient, placeId);
  174. placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
  175. Log.i(LOG_TAG, "Fetching details for ID: " + item.placeId);
  176. }
  177. };
  178.  
  179. private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
  180. = new ResultCallback<PlaceBuffer>() {
  181. @Override
  182. public void onResult(PlaceBuffer places) {
  183. if (!places.getStatus().isSuccess()) {
  184. Log.e(LOG_TAG, "Place query did not complete. Error: " +
  185. places.getStatus().toString());
  186. return;
  187. }
  188. // Selecting the first object buffer.
  189. final Place place = places.get(0);
  190. CharSequence attributions = places.getAttributions();
  191.  
  192. mNameTextView.setText(Html.fromHtml(place.getName() + ""));
  193. mAddressTextView.setText(Html.fromHtml(place.getAddress() + ""));
  194. mIdTextView.setText(Html.fromHtml(place.getId() + ""));
  195. mPhoneTextView.setText(Html.fromHtml(place.getPhoneNumber() + ""));
  196. mWebTextView.setText(place.getWebsiteUri() + "");
  197. if (attributions != null) {
  198. mAttTextView.setText(Html.fromHtml(attributions.toString()));
  199. }
  200. }
  201. };
  202.  
  203. @Override
  204. public void onConnected(Bundle bundle) {
  205. mPlaceArrayAdapter.setGoogleApiClient(mGoogleApiClient);
  206. Log.i(LOG_TAG, "Google Places API connected.");
  207.  
  208. }
  209.  
  210. @Override
  211. public void onConnectionFailed(ConnectionResult connectionResult) {
  212. Log.e(LOG_TAG, "Google Places API connection failed with error code: "
  213. + connectionResult.getErrorCode());
  214.  
  215. Toast.makeText(this,
  216. "Google Places API connection failed with error code:" +
  217. connectionResult.getErrorCode(),
  218. Toast.LENGTH_LONG).show();
  219. }
  220.  
  221. @Override
  222. public void onConnectionSuspended(int i) {
  223. mPlaceArrayAdapter.setGoogleApiClient(null);
  224. Log.e(LOG_TAG, "Google Places API connection suspended.");
  225. }
  226.  
  227. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  228. xmlns:tools="http://schemas.android.com/tools"
  229. android:layout_width="match_parent"
  230. android:layout_height="match_parent"
  231. android:paddingBottom="@dimen/activity_vertical_margin"
  232. android:paddingLeft="@dimen/activity_horizontal_margin"
  233. android:paddingRight="@dimen/activity_horizontal_margin"
  234. android:paddingTop="@dimen/activity_vertical_margin"
  235. tools:context=".ConfirmActivity">
  236.  
  237. <AutoCompleteTextView
  238. android:id="@+id/autoCompleteTextView"
  239. android:layout_width="match_parent"
  240. android:layout_height="wrap_content"
  241. android:layout_centerHorizontal="true"
  242. android:layout_marginTop="50dp"
  243. android:hint="Enter Place Here"/>
  244.  
  245. <TextView
  246. android:id="@+id/header"
  247. android:layout_width="wrap_content"
  248. android:layout_height="wrap_content"
  249. android:layout_alignParentLeft="true"
  250. android:layout_alignParentStart="true"
  251. android:layout_below="@+id/autoCompleteTextView"
  252. android:layout_marginTop="20dp"
  253. android:text="Selected Place:"
  254. android:textStyle="bold"/>
  255.  
  256. <TextView
  257. android:id="@+id/name"
  258. android:layout_width="wrap_content"
  259. android:layout_height="wrap_content"
  260. android:layout_alignParentLeft="true"
  261. android:layout_alignParentStart="true"
  262. android:layout_below="@+id/header"
  263. android:layout_marginTop="20dp"/>
  264.  
  265. <TextView
  266. android:id="@+id/address"
  267. android:layout_width="wrap_content"
  268. android:layout_height="wrap_content"
  269. android:layout_alignParentLeft="true"
  270. android:layout_alignParentStart="true"
  271. android:layout_below="@+id/name"/>
  272.  
  273. <TextView
  274. android:id="@+id/place_id"
  275. android:layout_width="wrap_content"
  276. android:layout_height="wrap_content"
  277. android:layout_alignParentLeft="true"
  278. android:layout_alignParentStart="true"
  279. android:layout_below="@+id/address"/>
  280.  
  281. <TextView
  282. android:id="@+id/phone"
  283. android:layout_width="wrap_content"
  284. android:layout_height="wrap_content"
  285. android:layout_alignParentLeft="true"
  286. android:layout_alignParentStart="true"
  287. android:layout_below="@+id/place_id"
  288. android:autoLink="phone"/>
  289.  
  290. <TextView
  291. android:id="@+id/web"
  292. android:layout_width="wrap_content"
  293. android:layout_height="wrap_content"
  294. android:layout_alignParentLeft="true"
  295. android:layout_alignParentStart="true"
  296. android:layout_below="@+id/phone"
  297. android:autoLink="web"/>
  298.  
  299. <TextView
  300. android:id="@+id/att"
  301. android:layout_width="wrap_content"
  302. android:layout_height="wrap_content"
  303. android:layout_alignParentBottom="true"
  304. android:layout_alignParentEnd="true"
  305. android:layout_alignParentRight="true"
  306. android:autoLink="web"/>
  307.  
  308. <ImageView
  309. android:id="@+id/poweredBy"
  310. android:layout_width="wrap_content"
  311. android:layout_height="wrap_content"
  312. android:layout_above="@+id/att"
  313. android:layout_alignParentEnd="true"
  314. android:layout_alignParentRight="true"
  315. android:src="@drawable/powered_by_google_light"/>
  316.  
  317. <ImageView
  318. android:id="@+id/truiton_image"
  319. android:layout_width="100dp"
  320. android:layout_height="100dp"
  321. android:layout_above="@+id/poweredBy"
  322. android:layout_alignParentEnd="true"
  323. android:layout_alignParentRight="true"
  324. android:layout_marginBottom="-20dp"
  325. />
Add Comment
Please, Sign In to add comment