Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.08 KB | None | 0 0
  1. package com.example.foodhygiene;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.content.ContextCompat;
  13. import android.support.v7.app.AlertDialog;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.os.Bundle;
  16. import android.view.View;
  17. import android.widget.AdapterView;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.CheckBox;
  20. import android.widget.ListView;
  21. import android.widget.ProgressBar;
  22. import android.widget.Spinner;
  23. import android.widget.TextView;
  24.  
  25. import com.android.volley.AuthFailureError;
  26. import com.android.volley.Request;
  27. import com.android.volley.RequestQueue;
  28. import com.android.volley.Response;
  29. import com.android.volley.VolleyError;
  30. import com.android.volley.toolbox.JsonArrayRequest;
  31. import com.android.volley.toolbox.JsonObjectRequest;
  32. import com.android.volley.toolbox.StringRequest;
  33. import com.android.volley.toolbox.Volley;
  34.  
  35. import org.json.JSONArray;
  36. import org.json.JSONException;
  37. import org.json.JSONObject;
  38.  
  39. import java.util.ArrayList;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42.  
  43. public class MainActivity extends AppCompatActivity {
  44. private ListView listView;
  45. private ArrayList names;
  46. private ArrayList names1;
  47.  
  48. private ArrayAdapter namesAdapter;
  49. private ArrayAdapter namesAdapter1;
  50.  
  51. private String searchParam;
  52. private LocationManager locationManager;
  53. private LocationListener locationListener;
  54. private final int FINE_LOCATION_PERMISSION = 1;
  55.  
  56. final String url = "http://api.ratings.food.gov.uk/Establishments/?pageNumber=1&pageSize=10&";
  57.  
  58. private String nameExtra = "";
  59. private String locationExtra = "";
  60. ProgressBar pb;
  61.  
  62. private Location currentLocation;
  63. private TextView textView;
  64. ArrayList<String> RegionName;
  65.  
  66.  
  67. @Override
  68. protected void onCreate(Bundle savedInstanceState) {
  69.  
  70. super.onCreate(savedInstanceState);
  71. setContentView(R.layout.activity_main);
  72.  
  73. pb = (ProgressBar) findViewById(R.id.progressBarSearch);
  74. pb.setMax(100);
  75.  
  76.  
  77. names1 = new ArrayList<Region>();
  78. regionSpinner();
  79. namesAdapter1 = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, names1);
  80.  
  81. names = new ArrayList<Establishment>();
  82. namesAdapter = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, names);
  83. listView = findViewById(R.id.display_list);
  84. listView.setAdapter(namesAdapter);
  85. Spinner spinner = findViewById(R.id.regionSpinner1);
  86. spinner.setAdapter(namesAdapter1);
  87.  
  88.  
  89. final AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
  90. @Override
  91. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  92. Establishment clickedItem = (Establishment) namesAdapter.getItem(position);
  93. Intent intent = new Intent(MainActivity.this, RestaurantDetailsActivity.class);
  94. intent.putExtra("apiID", clickedItem.getApiID());
  95. startActivity(intent);
  96. }
  97. };
  98.  
  99. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  100.  
  101. locationListener = new LocationListener() {
  102.  
  103. @Override
  104. public void onLocationChanged(Location location) {
  105. currentLocation = location;
  106. }
  107.  
  108. @Override
  109. public void onStatusChanged(String provider, int status, Bundle extras) {
  110.  
  111. }
  112.  
  113. @Override
  114. public void onProviderEnabled(String provider) {
  115.  
  116.  
  117. }
  118.  
  119. @Override
  120. public void onProviderDisabled(String provider) {
  121.  
  122. }
  123.  
  124.  
  125. };
  126. if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
  127. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
  128. new AlertDialog.Builder(this)
  129. .setMessage("The application is about to request access to your location.")
  130. .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  131. @Override
  132. public void onClick(DialogInterface dialogInterface, int i) {
  133. requestLocPerms();
  134. }
  135. })
  136. .create()
  137. .show();
  138. } else {
  139. requestLocPerms();
  140. }
  141. } else {
  142. attachLocManager();
  143. }
  144.  
  145.  
  146. listView.setOnItemClickListener(itemClickListener);
  147. currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  148. locationListener.onLocationChanged(currentLocation);
  149. }
  150.  
  151. @Override
  152. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  153. switch (requestCode) {
  154. case FINE_LOCATION_PERMISSION: {
  155. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  156. attachLocManager();
  157. } else {
  158. }
  159. return;
  160. }
  161. }
  162. }
  163.  
  164. public void attachLocManager() {
  165. try {
  166. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
  167. } catch (SecurityException err) {
  168. }
  169. }
  170.  
  171. public void requestLocPerms() {
  172. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_LOCATION_PERMISSION);
  173. }
  174.  
  175. public void queryAPI(View view) {
  176. RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
  177.  
  178.  
  179. textView = findViewById(R.id.search_text);
  180. searchParam = textView.getText().toString().toLowerCase().trim();
  181. if (searchParam == null) {
  182. //print a dialogue
  183. }
  184.  
  185. String urlExtra = url;
  186.  
  187. String filterChoice = checkSpinner();
  188. nameExtra = filterChoice + searchParam;
  189. urlExtra += nameExtra;
  190. urlExtra += locationExtra;
  191.  
  192.  
  193. // Request a string response from the provided URL.
  194. JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, urlExtra, null,
  195. new Response.Listener<JSONObject>() {
  196. @Override
  197. public void onResponse(JSONObject response) {
  198. try {
  199. pb.setVisibility(View.VISIBLE);
  200. pb.setProgress(0);
  201. JSONArray data = response.getJSONArray("establishments");
  202. if (data != null) {
  203. populateList(data);
  204. System.out.println(response.toString());
  205.  
  206. }
  207. } catch (JSONException e) {
  208. e.printStackTrace();
  209. } finally {
  210. // pb.setProgress(100);
  211. }
  212.  
  213. }
  214.  
  215.  
  216. }, new Response.ErrorListener() {
  217. @Override
  218. public void onErrorResponse(VolleyError error) {
  219. textView.setText("That didn't work!");
  220. System.err.println(error.toString());
  221. }
  222.  
  223. }) {
  224. @Override
  225. public Map<String, String> getHeaders() {
  226. Map<String, String> params = new HashMap<String, String>();
  227. params.put("x-api-version", "2");
  228. params.put("Accept-Language", "cy-GB");
  229. params.put("Content-Type", "application/json");
  230.  
  231. return params;
  232. }
  233.  
  234. };
  235.  
  236. // Add the request to the RequestQueue.
  237. requestQueue.add(objectRequest);
  238. }
  239.  
  240.  
  241. public void populateList(JSONArray data) throws JSONException {
  242.  
  243.  
  244. for (int i = 0; i < data.length(); i++) {
  245. JSONObject jo = data.getJSONObject(i);
  246. names.add(new Establishment(jo.getInt("FHRSID"), jo.getString("BusinessName"), jo.getString("FHRSID"), jo.getString("AddressLine1"), jo.getString("RatingValue")));
  247. }
  248. namesAdapter.notifyDataSetChanged();
  249.  
  250. }
  251.  
  252. public void populateList2(JSONArray data) throws JSONException {
  253.  
  254.  
  255. for (int i = 0; i < data.length(); i++) {
  256. JSONObject jo = data.getJSONObject(i);
  257. Region region = new Region(jo.getInt("id"), jo.getString("name"), jo.getString("nameKey"), jo.getString("code"));
  258. names1.add(region);
  259. }
  260. namesAdapter1.notifyDataSetChanged();
  261.  
  262. }
  263.  
  264.  
  265. public void useLocation(View view) {
  266. CheckBox checkBox = findViewById(R.id.location_checkbox);
  267. if (checkBox.isChecked()) {
  268. String longitude = String.format("%.2f", currentLocation.getLongitude());
  269. String latitude = String.format("%.2f", currentLocation.getLatitude());
  270.  
  271. locationExtra = "&longitude=" + longitude + "&latitude=" + latitude + "&maxDistanceLimit=2";
  272.  
  273. } else {
  274. locationExtra = "";
  275. }
  276. }
  277.  
  278. public String checkSpinner() {
  279. String choice = "name=";
  280. Spinner spinner = findViewById(R.id.filterSpinner);
  281. switch (spinner.getSelectedItem().toString()) {
  282. case "name":
  283. choice = "name=";
  284. break;
  285. case "type of business":
  286. choice = "BusinessType=";
  287. break;
  288. case "rating":
  289. choice = "ratingKey=";
  290. break;
  291. case "region and local authority":
  292. choice = "LocalAuthorityName=";
  293. break;
  294.  
  295.  
  296. }
  297. return choice;
  298. }
  299.  
  300. public void regionSpinner() {
  301. RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
  302.  
  303. String urlExtra1 = "http://api.ratings.food.gov.uk/Regions/?";
  304.  
  305.  
  306. // Request a string response from the provided URL.
  307. JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, urlExtra1, null,
  308. new Response.Listener<JSONObject>() {
  309. @Override
  310. public void onResponse(JSONObject response) {
  311. try {
  312. // pb.setVisibility(View.VISIBLE);
  313. // pb.setProgress(0);
  314. JSONArray data = response.getJSONArray("regions");
  315. if (data != null) {
  316. populateList2(data);
  317. System.out.println(response.toString());
  318.  
  319. }
  320. } catch (JSONException e) {
  321. e.printStackTrace();
  322. } finally {
  323. // pb.setProgress(100);
  324. }
  325.  
  326. }
  327.  
  328.  
  329. }, new Response.ErrorListener() {
  330. @Override
  331. public void onErrorResponse(VolleyError error) {
  332. // textView.setText("That didn't work!");
  333. System.err.println(error.toString());
  334. }
  335.  
  336.  
  337. }){
  338. @Override
  339. public Map<String, String> getHeaders() {
  340. Map<String, String> params = new HashMap<String, String>();
  341. params.put("x-api-version", "2");
  342. params.put("Accept-Language", "cy-GB");
  343. params.put("Content-Type", "application/json");
  344.  
  345. return params;
  346. }
  347.  
  348. };
  349.  
  350. requestQueue.add(objectRequest);
  351.  
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement