ScheduleLayout.java 21 KB

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