ScheduleLayout.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package com.ivalentin.margolariak;
  2. import java.text.DateFormat;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.Locale;
  8. import com.google.android.gms.maps.CameraUpdate;
  9. import com.google.android.gms.maps.CameraUpdateFactory;
  10. import com.google.android.gms.maps.GoogleMap;
  11. import com.google.android.gms.maps.MapView;
  12. import com.google.android.gms.maps.MapsInitializer;
  13. import com.google.android.gms.maps.OnMapReadyCallback;
  14. import com.google.android.gms.maps.model.LatLng;
  15. import com.google.android.gms.maps.model.MarkerOptions;
  16. import android.annotation.SuppressLint;
  17. import android.app.Dialog;
  18. import android.content.DialogInterface;
  19. import android.content.DialogInterface.OnCancelListener;
  20. import android.content.DialogInterface.OnShowListener;
  21. import android.content.pm.PackageManager;
  22. import android.database.Cursor;
  23. import android.database.sqlite.SQLiteDatabase;
  24. import android.os.Bundle;
  25. import android.app.Fragment;
  26. import android.support.v4.app.ActivityCompat;
  27. import android.text.Editable;
  28. import android.text.TextWatcher;
  29. import android.util.Log;
  30. import android.view.Gravity;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.Window;
  34. import android.view.View.OnClickListener;
  35. import android.view.ViewGroup;
  36. import android.view.WindowManager;
  37. import android.widget.Button;
  38. import android.widget.EditText;
  39. import android.widget.ImageView;
  40. import android.widget.LinearLayout;
  41. import android.widget.ScrollView;
  42. import android.widget.TextView;
  43. /**
  44. * Fragment to be inflated showing the festivals schedule.
  45. * Contains a date selector and a ScrollView with all the activities for the day.
  46. *
  47. * @author Inigo Valentin
  48. *
  49. */
  50. public class ScheduleLayout extends Fragment implements OnMapReadyCallback{
  51. private Bundle bund;
  52. private final String dates[] = new String[20];
  53. private int dateCount = 0;
  54. private int selected = 0;
  55. //Indicates official schedule
  56. private int schedule;
  57. //Map stuff for the dialog
  58. private MapView mapView;
  59. private GoogleMap map;
  60. private LatLng location;
  61. private View view;
  62. private String markerName = "";
  63. /**
  64. * Run when the fragment is inflated.
  65. * Assigns views, gets the date and does the first call to the populateSchedule function.
  66. *
  67. * @param inflater A LayoutInflater to manage views
  68. * @param container The container View
  69. * @param savedInstanceState Bundle containing the state
  70. *
  71. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  72. */
  73. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  74. @Override
  75. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  76. Calendar calendar = Calendar.getInstance();
  77. String year = Integer.toString(calendar.get(Calendar.YEAR));
  78. //Set bundle for the map
  79. bund = savedInstanceState;
  80. //Load the layout
  81. view = inflater.inflate(R.layout.fragment_layout_schedule, null);
  82. view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
  83. @Override
  84. public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
  85. CustomScrollView sv = (CustomScrollView) v.findViewById(R.id.sv_schedule);
  86. Log.d("SV", "INVALIDATED");
  87. sv.invalidate();
  88. }
  89. });
  90. //Get schedule type
  91. Bundle bundle = this.getArguments();
  92. schedule = bundle.getInt(GM.SCHEDULE.KEY, GM.SCHEDULE.CITY);
  93. //Set the title
  94. if (schedule == GM.SCHEDULE.CITY)
  95. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_lablanca_schedule));
  96. else
  97. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_lablanca_gm_schedule));
  98. //Populate the dates array
  99. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  100. Cursor cursor;
  101. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  102. cursor = db.rawQuery("SELECT DISTINCT date(start) FROM festival_event WHERE strftime('%Y', start) = '" + year + "' AND strftime('%H', start) > '06' AND gm = 1;", null);
  103. }
  104. else{
  105. cursor = db.rawQuery("SELECT DISTINCT date(start) FROM festival_event WHERE strftime('%Y', start) = '" + year + "' AND strftime('%H', start) > '06' AND gm = 0;", null);
  106. }
  107. //Get current date in the same format as retrieved from the database
  108. DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
  109. Date today = Calendar.getInstance().getTime();
  110. String curDate = df.format(today);
  111. //Log.e("Cur Date", curDate);
  112. while (cursor.moveToNext()){
  113. dates[dateCount] = cursor.getString(0);
  114. //TODO: Check if some date is of today, and set selected
  115. if (curDate.equals(cursor.getString(0))){
  116. selected = dateCount;
  117. }
  118. dateCount ++;
  119. }
  120. cursor.close();
  121. db.close();
  122. //Assign buttons
  123. Button btL = (Button) view.findViewById(R.id.bt_schedule_l);
  124. Button btR = (Button) view.findViewById(R.id.bt_schedule_r);
  125. btL.setOnClickListener(new OnClickListener() {
  126. @Override
  127. public void onClick(View v) {
  128. selected --;
  129. populateSchedule();
  130. }
  131. });
  132. btR.setOnClickListener(new OnClickListener() {
  133. @Override
  134. public void onClick(View v) {
  135. selected ++;
  136. populateSchedule();
  137. }
  138. });
  139. //Assign the filter text
  140. EditText etFilter = (EditText) view.findViewById(R.id.et_schedule_filter);
  141. etFilter.addTextChangedListener(new TextWatcher() {
  142. @Override
  143. public void afterTextChanged(Editable s) { }
  144. @Override
  145. public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
  146. @Override
  147. public void onTextChanged(CharSequence s, int start, int before, int count) {
  148. //Repopulate when the text is changed
  149. populateSchedule();
  150. }
  151. });
  152. //Populate the activity list
  153. populateSchedule();
  154. //Return the fragment view
  155. return view;
  156. }
  157. /**
  158. * Populates the list of activities with the ones n the selected day.
  159. *
  160. */
  161. @SuppressLint({"InflateParams", "SwitchIntDef"})
  162. //Views are added from a loop: I can't specify the parent when inflating.
  163. private int populateSchedule() {
  164. int eventCount = 0;
  165. //Hide or show buttons
  166. Button btNext = (Button) view.findViewById(R.id.bt_schedule_r);
  167. Button btPrevious = (Button) view.findViewById(R.id.bt_schedule_l);
  168. if (selected <= 0) {
  169. btPrevious.setVisibility(View.INVISIBLE);
  170. } else {
  171. btPrevious.setVisibility(View.VISIBLE);
  172. }
  173. if (selected >= dateCount - 1) {
  174. btNext.setVisibility(View.INVISIBLE);
  175. } else {
  176. btNext.setVisibility(View.VISIBLE);
  177. }
  178. //If there are no events,return now
  179. if (selected < 0 || selected >= dateCount) {
  180. return eventCount;
  181. }
  182. //Search filter
  183. String filter = ((EditText) view.findViewById(R.id.et_schedule_filter)).getText().toString().toLowerCase(Locale.US);
  184. Calendar calendar = Calendar.getInstance();
  185. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
  186. try {
  187. calendar.setTime(dateFormat.parse(dates[selected] + " 06:00:00"));
  188. } catch (Exception ex) {
  189. Log.e("Datetime error", ex.toString());
  190. }
  191. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  192. String lang = GM.getLang();
  193. //A layout to be populated with one activity
  194. LinearLayout entry, list;
  195. //An inflater
  196. LayoutInflater factory = LayoutInflater.from(getActivity());
  197. //Views in each row
  198. TextView tvRowTitle, tvRowTime, tvRowDesc, tvRowPlace, tvRowId, tvRowAddress;
  199. //Set date selector texts
  200. TextView tvDayNumber = (TextView) view.findViewById(R.id.tv_schedule_day_number);
  201. TextView tvDayMonth = (TextView) view.findViewById(R.id.tv_schedule_day_month);
  202. TextView tvDayTitle = (TextView) view.findViewById(R.id.tv_schedule_day_title);
  203. tvDayNumber.setText(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
  204. switch (calendar.get(Calendar.MONTH)) {
  205. case 6:
  206. tvDayMonth.setText(getString(R.string.schedule_month_july));
  207. break;
  208. case 7:
  209. tvDayMonth.setText(getString(R.string.schedule_month_august));
  210. break;
  211. }
  212. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  213. Cursor cursorDay = db.rawQuery("SELECT name_" + lang + " FROM festival_day WHERE date(date) = '" + dates[selected] + "';", null);
  214. if (cursorDay.getCount() > 0) {
  215. cursorDay.moveToFirst();
  216. tvDayTitle.setText(cursorDay.getString(0));
  217. } else {
  218. tvDayTitle.setText("");
  219. }
  220. cursorDay.close();
  221. }
  222. //Get day end date
  223. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
  224. Calendar c = Calendar.getInstance();
  225. try {
  226. c.setTime(sdf.parse(dates[selected]));
  227. } catch (Exception ex) {
  228. Log.e("Date parsing error", ex.toString());
  229. return eventCount;
  230. }
  231. c.add(Calendar.DATE, 1); // number of days to add
  232. String endDate = sdf.format(c.getTime());
  233. String query = "SELECT festival_event.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon FROM festival_event, place WHERE place = place.id AND start >= '" + dates[selected] + " 06:00:00' AND start < '" + endDate + " 05:59:59' AND ";
  234. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  235. query = query + "gm = 1 ";
  236. } else {
  237. query = query + "gm = 0 ";
  238. }
  239. if (filter.length() > 0) {
  240. query = query + "AND (title_" + lang + " like '%" + filter + "%' OR description_" + lang + " like '%" + filter + "%' OR name_" + lang + " like '%" + filter + "%' OR address_" + lang + " like '%" + filter + "%') ";
  241. }
  242. query = query + " ORDER BY start;";
  243. Cursor cursor = db.rawQuery(query, null);
  244. //Clear the list
  245. list = (LinearLayout) view.findViewById(R.id.ll_schedule_list);
  246. list.removeAllViews();
  247. while (cursor.moveToNext()) {
  248. eventCount++;
  249. entry = (LinearLayout) factory.inflate(R.layout.row_schedule, null);
  250. //Set id
  251. tvRowId = (TextView) entry.findViewById(R.id.tv_row_schedule_id);
  252. tvRowId.setText(cursor.getString(0));
  253. //Set title
  254. tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_schedule_title);
  255. tvRowTitle.setText(cursor.getString(1));
  256. //Set description
  257. tvRowDesc = (TextView) entry.findViewById(R.id.tv_row_schedule_description);
  258. if (cursor.getString(2).length() <= 0 || cursor.getString(2).equals(cursor.getString(1))) {
  259. tvRowDesc.setVisibility(View.GONE);
  260. } else {
  261. tvRowDesc.setText(cursor.getString(2));
  262. }
  263. //Set place
  264. tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_schedule_place);
  265. tvRowPlace.setText(cursor.getString(6));
  266. //Set address
  267. tvRowAddress = (TextView) entry.findViewById(R.id.tv_row_schedule_address);
  268. if (cursor.getString(7).length() <= 0 || cursor.getString(7).equals(cursor.getString(6))) {
  269. tvRowAddress.setVisibility(View.GONE);
  270. } else {
  271. tvRowAddress.setText(cursor.getString(7));
  272. }
  273. //Set icon
  274. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  275. ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_schedule_pinpoint);
  276. pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.margolariak:drawable/pinpoint_gm", null, null));
  277. }
  278. //Set time
  279. tvRowTime = (TextView) entry.findViewById(R.id.tv_row_schedule_time);
  280. String tm = cursor.getString(4).substring(cursor.getString(4).length() - 8, cursor.getString(4).length() - 3);
  281. tvRowTime.setText(tm);
  282. //Set clisk listener
  283. entry.setOnClickListener(new OnClickListener() {
  284. @Override
  285. public void onClick(View v) {
  286. TextView tvId = (TextView) v.findViewById(R.id.tv_row_schedule_id);
  287. int id = Integer.parseInt(tvId.getText().toString());
  288. showDialog(id);
  289. }
  290. });
  291. //Add the view
  292. list.addView(entry);
  293. }
  294. cursor.close();
  295. db.close();
  296. //Scroll to top
  297. try {
  298. ScrollView sw = (ScrollView) list.getParent().getParent();
  299. sw.scrollTo(0, 0);
  300. }
  301. catch(Exception ex){
  302. Log.e("Schedule scroll", "Culdnt get scrollview to scroll: " + ex.toString());
  303. }
  304. return eventCount;
  305. }
  306. /**
  307. * Shows a dialog with info about the selected event.
  308. *
  309. * @param id The event id
  310. */
  311. @SuppressWarnings("ConstantConditions")
  312. private void showDialog(final int id){
  313. //Create the dialog
  314. final Dialog dialog = new Dialog(getActivity());
  315. //Set up dialog window
  316. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  317. dialog.setContentView(R.layout.dialog_schedule);
  318. //Date formatters
  319. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
  320. SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd-", Locale.US);
  321. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.US);
  322. //Set the custom dialog components - text, image and button
  323. TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_title);
  324. TextView tvDescription = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_description);
  325. TextView tvHost = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_host);
  326. TextView tvDate = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_date);
  327. TextView tvTime = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_time);
  328. TextView tvPlace = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_place);
  329. TextView tvAddress = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_address);
  330. Button btClose = (Button) dialog.findViewById(R.id.bt_schedule_close);
  331. //Get info about the event
  332. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  333. String lang = GM.getLang();
  334. Cursor cursor = db.rawQuery("SELECT festival_event.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon, host FROM festival_event, place WHERE place = place.id AND festival_event.id = " + id + ";", null);
  335. if (cursor.getCount() > 0){
  336. cursor.moveToNext();
  337. //Set title
  338. tvTitle.setText(cursor.getString(1));
  339. markerName = cursor.getString(1);
  340. //Set description
  341. if (cursor.getString(2).length() > 0) {
  342. tvDescription.setText(cursor.getString(2));
  343. }
  344. else{
  345. tvDescription.setVisibility(View.GONE);
  346. }
  347. //Set host
  348. if (cursor.getString(10) != null){
  349. Cursor hostCursor = db.rawQuery("SELECT name_" + lang + " FROM people WHERE id = " + cursor.getString(10) + ";", null);
  350. if (hostCursor.moveToNext()){
  351. tvHost.setVisibility(View.VISIBLE);
  352. tvHost.setText(String.format(getString(R.string.schedule_host), hostCursor.getString(0)));
  353. }
  354. hostCursor.close();
  355. }
  356. else{
  357. tvHost.setVisibility(View.GONE);
  358. }
  359. //Set date
  360. try{
  361. Date day = dateFormat.parse(cursor.getString(4));
  362. Date date = new Date();
  363. //If the event is today, show "Today" instead of the date
  364. if (dayFormat.format(day).equals(dayFormat.format(date)))
  365. tvDate.setText(dialog.getContext().getString(R.string.today));
  366. else{
  367. Calendar cal = Calendar.getInstance();
  368. cal.setTime(date);
  369. cal.add(Calendar.HOUR_OF_DAY, 24);
  370. //If the event is tomorrow, show "Tomorrow" instead of the date
  371. if (dayFormat.format(cal.getTime()).equals(dayFormat.format(date))){
  372. tvDate.setText(dialog.getContext().getString(R.string.tomorrow));
  373. }
  374. //Else, show the date
  375. else{
  376. SimpleDateFormat printFormat;
  377. switch (lang){
  378. case "en":
  379. printFormat = new SimpleDateFormat("MMMM dd", Locale.US);
  380. tvDate.setText(printFormat.format(day));
  381. break;
  382. case "eu":
  383. printFormat = new SimpleDateFormat("MMMM dd", new Locale("eu", "ES"));
  384. tvDate.setText(printFormat.format(day));
  385. break;
  386. default:
  387. printFormat = new SimpleDateFormat("dd", new Locale("es", "ES"));
  388. String str = printFormat.format(day) + " de ";
  389. printFormat = new SimpleDateFormat("MMMM", new Locale("es", "ES"));
  390. str = str + printFormat.format(day);
  391. tvDate.setText(str);
  392. }
  393. }
  394. }
  395. }
  396. catch (Exception ex){
  397. Log.e("Error parsing date", ex.toString());
  398. }
  399. //Set time
  400. try{
  401. if (cursor.getString(5).length() == 0) {
  402. tvTime.setText(timeFormat.format(dateFormat.parse(cursor.getString(4))));
  403. }
  404. else {
  405. String time = timeFormat.format(dateFormat.parse(cursor.getString(4))) + " - " + timeFormat.format(dateFormat.parse(cursor.getString(5)));
  406. tvTime.setText(time);
  407. }
  408. }
  409. catch (ParseException ex){
  410. Log.e("Error parsing time", ex.toString());
  411. }
  412. //Set the place
  413. tvPlace.setText(cursor.getString(6));
  414. tvAddress.setText(cursor.getString(7));
  415. //Set up map
  416. location = new LatLng(Double.parseDouble(cursor.getString(8)), Double.parseDouble(cursor.getString(9)));
  417. mapView = (MapView) dialog.findViewById(R.id.mv_dialog_schedule_map);
  418. mapView.onCreate(bund);
  419. //Close the db connection
  420. cursor.close();
  421. db.close();
  422. //Set close button
  423. btClose.setOnClickListener(new OnClickListener() {
  424. @Override
  425. public void onClick(View v) {
  426. dialog.dismiss();
  427. }
  428. });
  429. //Actions to take when the dialog is cancelled
  430. dialog.setOnCancelListener(new OnCancelListener(){
  431. @Override
  432. public void onCancel(DialogInterface dialog) {
  433. if (map != null) {
  434. if (!(ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  435. map.setMyLocationEnabled(false);
  436. }
  437. }
  438. if (mapView != null){
  439. mapView.onResume();
  440. mapView.onDestroy();
  441. }
  442. }
  443. });
  444. //Show the dialog
  445. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  446. lp.copyFrom(dialog.getWindow().getAttributes());
  447. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  448. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  449. lp.gravity = Gravity.CENTER;
  450. lp.dimAmount = 0.0f;
  451. dialog.getWindow().setAttributes(lp);
  452. //Show dialog
  453. dialog.show();
  454. //Start the map
  455. startMap();
  456. mapView.onResume();
  457. dialog.setOnShowListener(new OnShowListener(){
  458. @Override
  459. public void onShow(DialogInterface dialog) {
  460. // Gets to GoogleMap from the MapView and does initialization stuff
  461. startMap();
  462. }
  463. });
  464. }
  465. }
  466. /**
  467. * Starts the map in the dialog.
  468. */
  469. private void startMap(){
  470. mapView.getMapAsync(this);
  471. }
  472. /**
  473. * Called when the fragment is brought back into the foreground.
  474. * Resumes the map and the location manager.
  475. *
  476. * @see android.app.Fragment#onResume()
  477. */
  478. @Override
  479. public void onResume() {
  480. if (mapView != null)
  481. mapView.onResume();
  482. if (map != null) {
  483. if (!(ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  484. map.setMyLocationEnabled(true);
  485. }
  486. }
  487. super.onResume();
  488. }
  489. /**
  490. * Called when the fragment is destroyed.
  491. * Finishes the map.
  492. *
  493. * @see android.app.Fragment#onDestroy()
  494. */
  495. @Override
  496. public void onDestroy() {
  497. super.onDestroy();
  498. if (map != null) {
  499. if (!(ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  500. map.setMyLocationEnabled(false);
  501. }
  502. }
  503. if (mapView != null){
  504. mapView.onResume();
  505. mapView.onDestroy();
  506. }
  507. }
  508. /**
  509. * Called when the fragment is paused.
  510. * Finishes the map.
  511. *
  512. * @see android.app.Fragment#onPause()
  513. */
  514. @Override
  515. public void onPause() {
  516. super.onDestroy();
  517. if (map != null) {
  518. if (!(ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  519. map.setMyLocationEnabled(false);
  520. }
  521. }
  522. if (mapView != null){
  523. mapView.onPause();
  524. }
  525. }
  526. /**
  527. * Called in a situation of low memory.
  528. * Lets the map handle this situation.
  529. *
  530. * @see android.app.Fragment#onLowMemory()
  531. */
  532. @Override
  533. public void onLowMemory() {
  534. super.onLowMemory();
  535. if (mapView != null){
  536. mapView.onResume();
  537. mapView.onLowMemory();
  538. }
  539. }
  540. /**
  541. * Called when the map is ready to be displayed.
  542. * Sets the map options and a marker for the map.
  543. *
  544. * @param googleMap The map to be shown
  545. *
  546. * @see com.google.android.gms.maps.OnMapReadyCallback#onMapReady(com.google.android.gms.maps.GoogleMap)
  547. */
  548. @Override
  549. public void onMapReady(GoogleMap googleMap) {
  550. this.map = googleMap;
  551. map.getUiSettings().setMyLocationButtonEnabled(false);
  552. if (!(ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  553. map.setMyLocationEnabled(true);
  554. }
  555. // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
  556. try {
  557. MapsInitializer.initialize(this.getActivity());
  558. CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 14);
  559. map.animateCamera(cameraUpdate);
  560. } catch (Exception e) {
  561. Log.e("Error initializing maps", e.toString());
  562. }
  563. //Set GM marker
  564. MarkerOptions mo = new MarkerOptions();
  565. mo.title(markerName);
  566. mo.position(location);
  567. map.addMarker(mo);
  568. }
  569. }