Guest User

Untitled

a guest
Oct 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
  3. public static final String EXTRA_MESSAGE = "message";
  4. public static final String PROPERTY_REG_ID = "registration_id";
  5. private static final String PROPERTY_APP_VERSION = "appVersion";
  6.  
  7. GoogleCloudMessaging gcm;
  8. AtomicInteger msgId = new AtomicInteger();
  9. String regid;
  10.  
  11. ImageButton btnVoyMap;
  12.  
  13. private static final String TAG = "MainActivity";
  14.  
  15. private BroadcastReceiver mRegistrationBroadcastReceiver;
  16.  
  17. private TextView mInformationTextView;
  18.  
  19. // Progress Dialog
  20. private ProgressDialog pDialog;
  21.  
  22. // Creating JSON Parser object
  23. JSONParser jParser = new JSONParser();
  24.  
  25. ArrayList<HashMap<String, String>> emergenciaList;
  26.  
  27. // url to get all products list
  28. private static String url_all_emergencias = "http://bomberosemergencias.pe.hu/get_all_emergencias.php";
  29.  
  30. // JSON Node names
  31. private static final String TAG_SUCCESS = "success";
  32. private static final String TAG_PRODUCTS = "emergencia";
  33.  
  34. private static final String TAG_HORA = "hora";
  35. private static final String TAG_UBICACION = "ubicacion";
  36. private static final String TAG_TIPO = "tipo";
  37. private static final String TAG_ESTADO = "estado";
  38. private static final String TAG_MAQUINAS = "maquinas";
  39.  
  40.  
  41. JSONArray products = null;
  42.  
  43. ListView lista;
  44.  
  45.  
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50.  
  51. btnVoyMap = (ImageButton) findViewById(R.id.btnmap);
  52.  
  53. // Hashmap para el ListView
  54. emergenciaList = new ArrayList<>();
  55.  
  56. // Cargar los productos en el Background Thread
  57. new LoadAllProducts().execute();
  58. lista = (ListView) findViewById(R.id.listAllProducts);
  59.  
  60. ActionBar actionBar = getSupportActionBar();
  61. assert actionBar != null;
  62. actionBar.setDisplayHomeAsUpEnabled(true);
  63.  
  64.  
  65. // Check device for Play Services APK.
  66. if (checkPlayServices()) {
  67. gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
  68. regid = getRegistrationId(getApplicationContext());
  69.  
  70. if (regid.isEmpty()) {
  71.  
  72. new RegisterApp(getApplicationContext(), gcm, getAppVersion(getApplicationContext())).execute();
  73. }
  74.  
  75. } else {
  76. Log.i(TAG, "No valid Google Play Services APK found.");
  77. }
  78.  
  79.  
  80. }//fin onCreate
  81.  
  82. class LoadAllProducts extends AsyncTask<String, String, String> {
  83.  
  84. /**
  85. * Antes de empezar el background thread Show Progress Dialog
  86. * */
  87. @Override
  88. protected void onPreExecute() {
  89. super.onPreExecute();
  90. pDialog = new ProgressDialog(MainActivity.this);
  91. pDialog.setMessage("Cargando...Por favor espere...");
  92. pDialog.setIcon(R.drawable.ic_launcher);
  93. pDialog.setIndeterminate(false);
  94. pDialog.setCancelable(false);
  95. pDialog.show();
  96. }
  97.  
  98. /**
  99. * obteniendo todos los productos
  100. * */
  101. protected String doInBackground(String... args) {
  102. // Building Parameters
  103. List params = new ArrayList();
  104. // getting JSON string from URL
  105. JSONObject json = jParser.makeHttpRequest(url_all_emergencias, "GET", params);
  106.  
  107. // Check your log cat for JSON reponse
  108. Log.d("All Products: ", json.toString());
  109.  
  110. try {
  111. // Checking for SUCCESS TAG
  112. int success = json.getInt(TAG_SUCCESS);
  113.  
  114. if (success == 1) {
  115. // products found
  116. // Getting Array of Products
  117. products = json.getJSONArray(TAG_PRODUCTS);
  118.  
  119. // looping through All Products
  120. //Log.i("ramiro", "produtos.length" + products.length());
  121. for (int i = 0; i < products.length(); i++) {
  122. JSONObject c = products.getJSONObject(i);
  123.  
  124. // Storing each json item in variable
  125.  
  126. String hora = c.getString(TAG_HORA);
  127. String ubicacion = c.getString(TAG_UBICACION);
  128. String tipo = c.getString(TAG_TIPO);
  129. String estado = c.getString(TAG_ESTADO);
  130. String maquinas = c.getString(TAG_MAQUINAS);
  131.  
  132.  
  133. // creating new HashMap
  134. HashMap<String, String> map = new HashMap<>();
  135.  
  136. // adding each child node to HashMap key => value
  137.  
  138. map.put(TAG_HORA, hora);
  139. map.put(TAG_UBICACION, ubicacion);
  140. map.put(TAG_TIPO, tipo);
  141. map.put(TAG_ESTADO, estado);
  142. map.put(TAG_MAQUINAS, maquinas);
  143.  
  144. emergenciaList.add(map);
  145. }
  146.  
  147. }
  148.  
  149. } catch (JSONException e) {
  150. e.printStackTrace();
  151.  
  152.  
  153. }
  154. return null;
  155. }
  156.  
  157. /**
  158. * After completing background task Dismiss the progress dialog
  159. * **/
  160. protected void onPostExecute(String file_url) {
  161. // dismiss the dialog after getting all products
  162. pDialog.dismiss();
  163. // updating UI from Background Thread
  164. runOnUiThread(new Runnable() {
  165. public void run() {
  166. /**
  167. * Updating parsed JSON data into ListView
  168. * */
  169. ListAdapter adapter = new SimpleAdapter(
  170. MainActivity.this,
  171. emergenciaList,
  172. R.layout.single_post,
  173.  
  174.  
  175. new String[] {
  176.  
  177. TAG_HORA,
  178. TAG_UBICACION,
  179. TAG_TIPO,
  180. TAG_ESTADO,
  181. TAG_MAQUINAS,
  182. },
  183. new int[] {
  184.  
  185. R.id.single_post_tv_hora,
  186. R.id.single_post_tv_ubicacion,
  187. R.id.single_post_tv_tipo,
  188. R.id.single_post_tv_estado,
  189. R.id.single_post_tv_maquinas,
  190.  
  191. });
  192. // updating listview
  193. //setListAdapter(adapter);
  194. lista.setAdapter(adapter);
  195. Toast.makeText(getBaseContext(), "EMERGENCIAS ACTUALIZADAS",
  196. Toast.LENGTH_SHORT).show();
  197.  
  198. }
  199. });
  200.  
  201.  
  202. }
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. private void refresh() {
  211. finish();
  212. Intent myIntent = new Intent(this, MainActivity.class);
  213. startActivity(myIntent);
  214.  
  215. }
  216.  
  217. @Override
  218. public boolean onCreateOptionsMenu(Menu menu) {
  219. // Inflate the menu; this adds items to the action bar if it is present.
  220. getMenuInflater().inflate(R.menu.menu_main, menu);
  221. return true;
  222. }
  223.  
  224.  
  225. private boolean checkPlayServices() {
  226. int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  227. if (resultCode != ConnectionResult.SUCCESS) {
  228. if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  229. GooglePlayServicesUtil.getErrorDialog(resultCode, this,
  230. PLAY_SERVICES_RESOLUTION_REQUEST).show();
  231. } else {
  232. Log.i(TAG, "This device is not supported.");
  233. finish();
  234. }
  235. return false;
  236. }
  237. return true;
  238. }
  239.  
  240. /**
  241. * Gets the current registration ID for application on GCM service.
  242. * <p>
  243. * If result is empty, the app needs to register.
  244. *
  245. * @return registration ID, or empty string if there is no existing
  246. * registration ID.
  247. */
  248. private String getRegistrationId(Context context) {
  249. final SharedPreferences prefs = getGCMPreferences(context);
  250. String registrationId = prefs.getString(PROPERTY_REG_ID, "");
  251. if (registrationId.isEmpty()) {
  252. Log.i(TAG, "Registration not found.");
  253. return "";
  254. }
  255. // Check if app was updated; if so, it must clear the registration ID
  256. // since the existing regID is not guaranteed to work with the new
  257. // app version.
  258. int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
  259. int currentVersion = getAppVersion(getApplicationContext());
  260. if (registeredVersion != currentVersion) {
  261. Log.i(TAG, "App version changed.");
  262. return "";
  263. }
  264. return registrationId;
  265. }
  266.  
  267. /**
  268. * @return Application's {@code SharedPreferences}.
  269. */
  270. private SharedPreferences getGCMPreferences(Context context) {
  271. // This sample app persists the registration ID in shared preferences, but
  272. // how you store the regID in your app is up to you.
  273. return getSharedPreferences(MainActivity.class.getSimpleName(),
  274. Context.MODE_PRIVATE);
  275. }
  276.  
  277. /**
  278. * @return Application's version code from the {@code PackageManager}.
  279. */
  280. private static int getAppVersion(Context context) {
  281. try {
  282. PackageInfo packageInfo = context.getPackageManager()
  283. .getPackageInfo(context.getPackageName(), 0);
  284. return packageInfo.versionCode;
  285. } catch (PackageManager.NameNotFoundException e) {
  286. // should never happen
  287. throw new RuntimeException("Could not get package name: " + e);
  288. }
  289. }
  290.  
  291. public void onClickMapa (View view){
  292.  
  293. Intent i = new Intent(this, MapsActivity.class);
  294. startActivity(i);
  295.  
  296. }
  297.  
  298. @Override
  299. public boolean onOptionsItemSelected(MenuItem item){
  300. switch (item.getItemId()){
  301. case R.id.AcercaDe:
  302. Toast.makeText(this, "DESARROLLADO POR ARQMAS-JMQC-2016",
  303. Toast.LENGTH_LONG).show();
  304. break;
  305.  
  306.  
  307. case R.id.actualizar:
  308. refresh();
  309.  
  310. case R.id.Salir:
  311. finish();
  312.  
  313. }
  314. return false;
  315. }
  316. }
  317.  
  318. public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
  319.  
  320. String tipo, hora, ubicacion, estado;
  321.  
  322. private static final String TAG = "CustomInfoWindowAdapter";
  323. private LayoutInflater inflater;
  324.  
  325.  
  326.  
  327. public CustomInfoWindowAdapter(LayoutInflater inflater){
  328. this.inflater = inflater;
  329. }
  330.  
  331. public View getInfoWindow(Marker arg0) {
  332. return null;
  333.  
  334. }
  335.  
  336.  
  337.  
  338. @Override
  339. public View getInfoContents(final Marker m) {
  340.  
  341. //Carga layout personalizado.
  342. View v = inflater.inflate(R.layout.infowindow_layout, null);
  343.  
  344. String[] info = m.getTitle().split("&");
  345. String url = m.getSnippet();
  346.  
  347.  
  348. ((TextView)v.findViewById(R.id.tipo)).setText("Accidente vehicular");
  349. ((TextView)v.findViewById(R.id.hora)).setText("2017-10-14 04:16:38");
  350. ((TextView)v.findViewById(R.id.ubicacion)).setText("Arevalo");
  351. ((TextView)v.findViewById(R.id.estado)).setText("atendiendo");
  352. return v;
  353. }
  354.  
  355.  
  356.  
  357.  
  358. }
Add Comment
Please, Sign In to add comment