ScheduleLayout.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package com.ivalentin.margolariak;
  2. import java.text.DateFormat;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. import android.annotation.SuppressLint;
  10. import android.app.Dialog;
  11. import android.content.DialogInterface;
  12. import android.content.DialogInterface.OnCancelListener;
  13. import android.content.DialogInterface.OnShowListener;
  14. import android.content.Intent;
  15. import android.content.pm.PackageManager;
  16. import android.database.Cursor;
  17. import android.database.sqlite.SQLiteDatabase;
  18. import android.graphics.Color;
  19. import android.graphics.drawable.Drawable;
  20. import android.net.Uri;
  21. import android.os.Bundle;
  22. import android.app.Fragment;
  23. import android.preference.PreferenceManager;
  24. import android.text.Editable;
  25. import android.text.TextWatcher;
  26. import android.util.Log;
  27. import android.view.Gravity;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.Window;
  31. import android.view.View.OnClickListener;
  32. import android.view.ViewGroup;
  33. import android.view.WindowManager;
  34. import android.widget.Button;
  35. import android.widget.EditText;
  36. import android.widget.ImageView;
  37. import android.widget.LinearLayout;
  38. import android.widget.ScrollView;
  39. import android.widget.TextView;
  40. import org.osmdroid.api.IMapController;
  41. import org.osmdroid.config.Configuration;
  42. import org.osmdroid.util.GeoPoint;
  43. import org.osmdroid.views.MapView;
  44. import org.osmdroid.views.overlay.ItemizedIconOverlay;
  45. import org.osmdroid.views.overlay.OverlayItem;
  46. import org.osmdroid.views.overlay.Polyline;
  47. /**
  48. * Fragment to be inflated showing the festivals schedule.
  49. * Contains a date selector and a ScrollView with all the activities for the day.
  50. *
  51. * @author Inigo Valentin
  52. *
  53. */
  54. public class ScheduleLayout extends Fragment{
  55. private Bundle bund;
  56. private final String dates[] = new String[40];
  57. private int dateCount = 0;
  58. private int selected = 0;
  59. private View view;
  60. /**
  61. * Run when the fragment is inflated.
  62. * Assigns views, gets the date and does the first call to the populateSchedule function.
  63. *
  64. * @param inflater A LayoutInflater to manage views
  65. * @param container The container View
  66. * @param savedInstanceState Bundle containing the state
  67. *
  68. * @see Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  69. */
  70. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  71. @Override
  72. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  73. Calendar calendar = Calendar.getInstance();
  74. String year = Integer.toString(calendar.get(Calendar.YEAR));
  75. //Load the layout
  76. view = inflater.inflate(R.layout.fragment_layout_schedule, null);
  77. view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
  78. @Override
  79. public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
  80. CustomScrollView sv = (CustomScrollView) v.findViewById(R.id.sv_schedule);
  81. sv.invalidate();
  82. }
  83. });
  84. //Get schedule type
  85. TextView title = (TextView) view.findViewById(R.id.tv_schedule_type);
  86. Bundle bundle = this.getArguments();
  87. final int schedule = bundle.getInt(GM.SCHEDULE.KEY, GM.SCHEDULE.CITY);
  88. //Set the title
  89. if (schedule == GM.SCHEDULE.CITY) {
  90. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_lablanca_schedule));
  91. title.setText(R.string.menu_lablanca_schedule);
  92. }
  93. else{
  94. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_lablanca_gm_schedule));
  95. title.setText(R.string.menu_lablanca_gm_schedule);
  96. }
  97. ((MainActivity) getActivity()).setShareLink(getString(R.string.share_lablanca), GM.SHARE.LABLANCA);
  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) AS daydate FROM festival_event_gm WHERE strftime('%Y', start) = '" + year + "' AND strftime('%H', start) > '06' ORDER BY daydate;", null);
  103. }
  104. else{
  105. cursor = db.rawQuery("SELECT DISTINCT date(start) AS daydate FROM festival_event_vity WHERE strftime('%Y', start) = '" + year + "' AND strftime('%H', start) > '06' ORDER BY daydate;", 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. while (cursor.moveToNext()){
  112. if (dateCount < 40) {
  113. dates[dateCount] = cursor.getString(0);
  114. if (curDate.equals(cursor.getString(0))) {
  115. selected = dateCount;
  116. }
  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(schedule);
  130. }
  131. });
  132. btR.setOnClickListener(new OnClickListener() {
  133. @Override
  134. public void onClick(View v) {
  135. selected ++;
  136. populateSchedule(schedule);
  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(schedule);
  150. }
  151. });
  152. //Populate the activity list
  153. populateSchedule(schedule);
  154. //Return the fragment view
  155. return view;
  156. }
  157. /**
  158. * Populates the list of activities with the ones in the selected day.
  159. *
  160. * @param schedule GM.SCHEDULE.GM or GM.SCHEDULE.CITY
  161. */
  162. @SuppressLint({"InflateParams", "SwitchIntDef"})
  163. //Views are added from a loop: I can't specify the parent when inflating.
  164. private int populateSchedule(final int schedule) {
  165. int eventCount = 0;
  166. //Hide or show buttons
  167. Button btNext = (Button) view.findViewById(R.id.bt_schedule_r);
  168. Button btPrevious = (Button) view.findViewById(R.id.bt_schedule_l);
  169. if (selected <= 0) {
  170. btPrevious.setVisibility(View.INVISIBLE);
  171. } else {
  172. btPrevious.setVisibility(View.VISIBLE);
  173. }
  174. if (selected >= dateCount - 1) {
  175. btNext.setVisibility(View.INVISIBLE);
  176. } else {
  177. btNext.setVisibility(View.VISIBLE);
  178. }
  179. //If there are no events,return now
  180. if (selected < 0 || selected >= dateCount) {
  181. return eventCount;
  182. }
  183. //Search filter
  184. String filter = ((EditText) view.findViewById(R.id.et_schedule_filter)).getText().toString().toLowerCase(Locale.US);
  185. Calendar calendar = Calendar.getInstance();
  186. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
  187. try {
  188. calendar.setTime(dateFormat.parse(dates[selected] + " 06:00:00"));
  189. } catch (Exception ex) {
  190. Log.e("Datetime error", ex.toString());
  191. }
  192. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  193. String lang = GM.getLang();
  194. //A layout to be populated with one activity
  195. LinearLayout entry, list;
  196. //An inflater
  197. LayoutInflater factory = LayoutInflater.from(getActivity());
  198. //Views in each row
  199. TextView tvRowTitle, tvRowTime, tvRowDesc, tvRowPlace, tvRowId, tvRowAddress;
  200. //Set date selector texts
  201. TextView tvDayNumber = (TextView) view.findViewById(R.id.tv_schedule_day_number);
  202. TextView tvDayMonth = (TextView) view.findViewById(R.id.tv_schedule_day_month);
  203. TextView tvDayTitle = (TextView) view.findViewById(R.id.tv_schedule_day_title);
  204. tvDayNumber.setText(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
  205. switch (calendar.get(Calendar.MONTH)) {
  206. case 6:
  207. tvDayMonth.setText(getString(R.string.schedule_month_july));
  208. break;
  209. case 7:
  210. tvDayMonth.setText(getString(R.string.schedule_month_august));
  211. break;
  212. }
  213. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  214. Cursor cursorDay = db.rawQuery("SELECT name_" + lang + " FROM festival_day WHERE date(date) = '" + dates[selected] + "';", null);
  215. if (cursorDay.getCount() > 0) {
  216. cursorDay.moveToFirst();
  217. tvDayTitle.setText(cursorDay.getString(0));
  218. } else {
  219. tvDayTitle.setText("");
  220. }
  221. cursorDay.close();
  222. }
  223. //Get day end date
  224. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
  225. Calendar c = Calendar.getInstance();
  226. try {
  227. c.setTime(sdf.parse(dates[selected]));
  228. } catch (Exception ex) {
  229. Log.e("Date parsing error", ex.toString());
  230. return eventCount;
  231. }
  232. c.add(Calendar.DATE, 1); // number of days to add
  233. String endDate = sdf.format(c.getTime());
  234. String query = "";;
  235. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  236. query = "SELECT festival_event_gm.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon FROM festival_event_gm, place WHERE place = place.id AND start >= '" + dates[selected] + " 06:00:00' AND start < '" + endDate + " 05:59:59'";
  237. }
  238. else {
  239. query = "SELECT festival_event_city.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon FROM festival_event_city, place WHERE place = place.id AND start >= '" + dates[selected] + " 06:00:00' AND start < '" + endDate + " 05:59:59'";
  240. }
  241. if (filter.length() > 0) {
  242. query = query + "AND (title_" + lang + " like '%" + filter + "%' OR description_" + lang + " like '%" + filter + "%' OR name_" + lang + " like '%" + filter + "%' OR address_" + lang + " like '%" + filter + "%') ";
  243. }
  244. query = query + " ORDER BY start;";
  245. Cursor cursor = db.rawQuery(query, null);
  246. //Clear the list
  247. list = (LinearLayout) view.findViewById(R.id.ll_schedule_list);
  248. list.removeAllViews();
  249. while (cursor.moveToNext()) {
  250. eventCount++;
  251. entry = (LinearLayout) factory.inflate(R.layout.row_schedule, null);
  252. //Set id
  253. tvRowId = (TextView) entry.findViewById(R.id.tv_row_schedule_id);
  254. tvRowId.setText(cursor.getString(0));
  255. //Set title
  256. tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_schedule_title);
  257. tvRowTitle.setText(cursor.getString(1));
  258. //Set description
  259. tvRowDesc = (TextView) entry.findViewById(R.id.tv_row_schedule_description);
  260. if (cursor.getString(2) == null || cursor.getString(2).length() <= 0 || cursor.getString(2).equals(cursor.getString(1))) {
  261. tvRowDesc.setVisibility(View.GONE);
  262. } else {
  263. tvRowDesc.setText(cursor.getString(2));
  264. }
  265. //Set place
  266. tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_schedule_place);
  267. tvRowPlace.setText(cursor.getString(6));
  268. //Set address
  269. tvRowAddress = (TextView) entry.findViewById(R.id.tv_row_schedule_address);
  270. if (cursor.getString(7) == null || cursor.getString(7).length() <= 0 || cursor.getString(7).equals(cursor.getString(6))) {
  271. tvRowAddress.setVisibility(View.GONE);
  272. } else {
  273. tvRowAddress.setText(cursor.getString(7));
  274. }
  275. //Set icon
  276. if (schedule == GM.SCHEDULE.MARGOLARIAK) {
  277. ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_schedule_pinpoint);
  278. pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.margolariak:drawable/pinpoint_gm", null, null));
  279. }
  280. //Set time
  281. tvRowTime = (TextView) entry.findViewById(R.id.tv_row_schedule_time);
  282. String tm = cursor.getString(4).substring(cursor.getString(4).length() - 8, cursor.getString(4).length() - 3);
  283. tvRowTime.setText(tm);
  284. //Set clisk listener
  285. entry.setOnClickListener(new OnClickListener() {
  286. @Override
  287. public void onClick(View v) {
  288. TextView tvId = (TextView) v.findViewById(R.id.tv_row_schedule_id);
  289. int id = Integer.parseInt(tvId.getText().toString());
  290. showDialog(id, schedule);
  291. }
  292. });
  293. //Add the view
  294. list.addView(entry);
  295. }
  296. cursor.close();
  297. db.close();
  298. //Scroll to top
  299. try {
  300. ScrollView sw = (ScrollView) list.getParent().getParent();
  301. sw.scrollTo(0, 0);
  302. }
  303. catch(Exception ex){
  304. Log.e("Schedule scroll", "Culdnt get scrollview to scroll: " + ex.toString());
  305. }
  306. return eventCount;
  307. }
  308. /**
  309. * Shows a dialog with info about the selected event.
  310. *
  311. * @param id The event id
  312. * @param schedule GM.SCHEDULE.GM or GM.SCHEDULE.CITY
  313. */
  314. @SuppressWarnings("ConstantConditions")
  315. private void showDialog(final int id, final int schedule){
  316. //Create the dialog
  317. final Dialog dialog = new Dialog(getActivity());
  318. //Set up dialog window
  319. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  320. dialog.setContentView(R.layout.dialog_schedule);
  321. //Date formatters
  322. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
  323. SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd-", Locale.US);
  324. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.US);
  325. //Set the custom dialog components - text, image and button
  326. TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_title);
  327. TextView tvDescription = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_description);
  328. TextView tvHost = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_host);
  329. TextView tvSponsor = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_sponsor);
  330. TextView tvDate = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_date);
  331. TextView tvTime = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_time);
  332. LinearLayout llPlace = (LinearLayout) dialog.findViewById(R.id.ll_dialog_schedule_place);
  333. TextView tvPlace = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_place);
  334. TextView tvAddress = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_address);
  335. Button btClose = (Button) dialog.findViewById(R.id.bt_schedule_close);
  336. TextView tvOsm = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_osm);
  337. MapView mapView = (MapView) dialog.findViewById(R.id.mv_dialog_schedule_map);
  338. // Basic map configuration
  339. mapView.setMultiTouchControls(true);
  340. IMapController mapController = mapView.getController();
  341. //Get info about the event
  342. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  343. String lang = GM.getLang();
  344. Cursor cursor;
  345. if (schedule == GM.SCHEDULE.MARGOLARIAK){
  346. //cursor = db.rawQuery("SELECT festival_event_gm.id, title_" + lang + ", description_" + lang + ", place, route, start, end, name_" + lang + ", address_" + lang + ", lat, lon, host, sponsor FROM festival_event_gm, place WHERE place = place.id AND festival_event_gm.id = " + id + ";", null);
  347. cursor = db.rawQuery("SELECT id, title_" + lang + ", description_" + lang + ", place, route, start, end, host, sponsor FROM festival_event_gm WHERE id = " + id + ";", null);
  348. }
  349. else{
  350. //cursor = db.rawQuery("SELECT festival_event_city.id, title_" + lang + ", description_" + lang + ", place, route, start, end, name_" + lang + ", address_" + lang + ", lat, lon, host, sponsor FROM, sponsor festival_event_city, place WHERE place = place.id AND festival_event_city.id = " + id + ";", null);
  351. cursor = db.rawQuery("SELECT id, title_" + lang + ", description_" + lang + ", place, route, start, end, host, sponsor FROM festival_event_city WHERE id = " + id + ";", null);
  352. }
  353. if (cursor.getCount() > 0){
  354. cursor.moveToNext();
  355. String title = cursor.getString(1);
  356. String description = "";
  357. if (!cursor.isNull(2)){
  358. description = cursor.getString(2);
  359. }
  360. String strStart = cursor.getString(5);
  361. String strEnd = "";
  362. if (!cursor.isNull(6)){
  363. strEnd = cursor.getString(6);
  364. }
  365. int placeId = 0;
  366. if (!cursor.isNull(3)){
  367. placeId = cursor.getInt(3);
  368. }
  369. int routeId = 0;
  370. if (!cursor.isNull(4)){
  371. routeId = cursor.getInt(4);
  372. }
  373. int hostId = 0;
  374. if (!cursor.isNull(7)){
  375. hostId = cursor.getInt(7);
  376. }
  377. int sponsorId = 0;
  378. if (!cursor.isNull(8)){
  379. sponsorId = cursor.getInt(8);
  380. }
  381. //Set title
  382. tvTitle.setText(title);
  383. //Set description
  384. if (description.length() > 0) {
  385. tvDescription.setText(description);
  386. }
  387. else{
  388. tvDescription.setVisibility(View.GONE);
  389. }
  390. //Set host
  391. if (hostId != 0){
  392. Cursor hostCursor = db.rawQuery("SELECT name_" + lang + " FROM people WHERE id = " + hostId + ";", null);
  393. if (hostCursor.moveToNext()){
  394. tvHost.setVisibility(View.VISIBLE);
  395. tvHost.setText(String.format(getString(R.string.schedule_host), hostCursor.getString(0)));
  396. }
  397. hostCursor.close();
  398. }
  399. else{
  400. tvHost.setVisibility(View.GONE);
  401. }
  402. //Set sponsor
  403. if (sponsorId != 0){
  404. Cursor sponsorCursor = db.rawQuery("SELECT name_" + lang + " FROM people WHERE id = " + sponsorId + ";", null);
  405. if (sponsorCursor.moveToNext()){
  406. tvSponsor.setVisibility(View.VISIBLE);
  407. tvSponsor.setText(String.format(getString(R.string.schedule_sponsor), sponsorCursor.getString(0)));
  408. }
  409. sponsorCursor.close();
  410. }
  411. else{
  412. tvSponsor.setVisibility(View.GONE);
  413. }
  414. //Set date
  415. try{
  416. Date day = dateFormat.parse(strStart);
  417. Date date = new Date();
  418. //If the event is today, show "Today" instead of the date
  419. if (dayFormat.format(day).equals(dayFormat.format(date)))
  420. tvDate.setText(dialog.getContext().getString(R.string.today));
  421. else{
  422. Calendar cal = Calendar.getInstance();
  423. cal.setTime(date);
  424. cal.add(Calendar.HOUR_OF_DAY, 24);
  425. //If the event is tomorrow, show "Tomorrow" instead of the date
  426. if (dayFormat.format(cal.getTime()).equals(dayFormat.format(date))){
  427. tvDate.setText(dialog.getContext().getString(R.string.tomorrow));
  428. }
  429. //Else, show the date
  430. else{
  431. SimpleDateFormat printFormat;
  432. switch (lang){
  433. case "en":
  434. printFormat = new SimpleDateFormat("MMMM dd", Locale.US);
  435. tvDate.setText(printFormat.format(day));
  436. break;
  437. case "eu":
  438. printFormat = new SimpleDateFormat("MMMM dd", new Locale("eu", "ES"));
  439. tvDate.setText(printFormat.format(day));
  440. break;
  441. default:
  442. printFormat = new SimpleDateFormat("dd", new Locale("es", "ES"));
  443. String str = printFormat.format(day) + " de ";
  444. printFormat = new SimpleDateFormat("MMMM", new Locale("es", "ES"));
  445. str = str + printFormat.format(day);
  446. tvDate.setText(str);
  447. }
  448. }
  449. }
  450. }
  451. catch (Exception ex){
  452. Log.e("Error parsing date", ex.toString());
  453. }
  454. //Set time
  455. try{
  456. if (strEnd.length() == 0) {
  457. tvTime.setText(timeFormat.format(dateFormat.parse(strStart)));
  458. }
  459. else {
  460. String time = timeFormat.format(dateFormat.parse(strStart)) + " - " + timeFormat.format(dateFormat.parse(strEnd));
  461. tvTime.setText(time);
  462. }
  463. }
  464. catch (ParseException ex){
  465. Log.e("Error parsing time", ex.toString());
  466. }
  467. //Set the place
  468. if (routeId != 0){
  469. llPlace.setVisibility(View.GONE);
  470. Cursor routeCursor = db.rawQuery("SELECT id, c_lat, c_lon, zoom FROM route WHERE id = " + routeId + ";", null);
  471. if (routeCursor.moveToNext()) {
  472. // Configure map route
  473. mapController.setZoom(routeCursor.getInt(3));
  474. GeoPoint center = new GeoPoint(routeCursor.getDouble(1), routeCursor.getDouble(2));
  475. mapController.setCenter(center);
  476. // Read from route_point and draw the route on the map.
  477. Cursor pointCursor = db.rawQuery("SELECT lat_o, lon_o, lat_d, lon_d FROM route_point WHERE route = " + routeId + " ORDER BY part;", null);
  478. final ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
  479. GeoPoint lastPoint = new GeoPoint(0.0, 0.0);
  480. while (pointCursor.moveToNext()){
  481. points.add(new GeoPoint(pointCursor.getDouble(0), pointCursor.getDouble(1)));
  482. lastPoint = new GeoPoint(pointCursor.getDouble(2), pointCursor.getDouble(3));
  483. }
  484. // For the last one, also take destination
  485. points.add(lastPoint);
  486. pointCursor.close();
  487. //Create path
  488. Polyline line = new Polyline();
  489. line.setPoints(points);
  490. line.setColor(getResources().getColor(R.color.background_menu));
  491. mapView.getOverlays().add(line);
  492. }
  493. }
  494. else{
  495. Cursor placeCursor = db.rawQuery("SELECT name_" + lang + ", address_" + lang + ", lat, lon FROM place WHERE id = " + placeId + ";", null);
  496. if (placeCursor.moveToNext()){
  497. tvPlace.setText(placeCursor.getString(0));
  498. tvAddress.setText(placeCursor.getString(1));
  499. // Configure map marker
  500. mapController.setZoom(17);
  501. GeoPoint center = new GeoPoint(placeCursor.getDouble(2), placeCursor.getDouble(3));
  502. mapController.setCenter(center);
  503. OverlayItem locationOverlayItem = new OverlayItem(title, placeCursor.getString(0), center);
  504. Drawable locationMarker = this.getResources().getDrawable(R.drawable.pinpoint);
  505. locationOverlayItem.setMarker(locationMarker);
  506. final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
  507. items.add(locationOverlayItem);
  508. ItemizedIconOverlay locationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
  509. new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
  510. public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
  511. return true;
  512. }
  513. public boolean onItemLongPress(final int index, final OverlayItem item) {
  514. return true;
  515. }
  516. }, getContext());
  517. mapView.getOverlays().add(locationOverlay);
  518. }
  519. placeCursor.close();
  520. }
  521. //Set up OpenStreetMap attribution
  522. tvOsm.setOnClickListener(new OnClickListener() {
  523. @Override
  524. public void onClick(View view) {
  525. Intent i = new Intent(Intent.ACTION_VIEW);
  526. i.setData(Uri.parse(GM.URL.OSM_COPYRIGHT));
  527. startActivity(i);
  528. }
  529. });
  530. //Close the db connection
  531. cursor.close();
  532. db.close();
  533. //Set close button
  534. btClose.setOnClickListener(new OnClickListener() {
  535. @Override
  536. public void onClick(View v) {
  537. dialog.dismiss();
  538. }
  539. });
  540. //Actions to take when the dialog is cancelled
  541. dialog.setOnCancelListener(new OnCancelListener(){
  542. @Override
  543. public void onCancel(DialogInterface dialog) {
  544. }
  545. });
  546. //Show the dialog
  547. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  548. lp.copyFrom(dialog.getWindow().getAttributes());
  549. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  550. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  551. lp.gravity = Gravity.CENTER;
  552. lp.dimAmount = 0.0f;
  553. dialog.getWindow().setAttributes(lp);
  554. //Show dialog
  555. dialog.show();
  556. }
  557. }
  558. /**
  559. * Called when the activity is resumed.
  560. * Lets the map handle this situation.
  561. *
  562. * @see android.app.Fragment#onResume()
  563. */
  564. @Override
  565. public void onResume() {
  566. Configuration.getInstance().load(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext()));
  567. super.onResume();
  568. }
  569. /**
  570. * Checks app permission to access the user location.
  571. * @return true if the permission has been granted, false otherwise.
  572. */
  573. private boolean checkLocationPermission(){
  574. return getContext().checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && getContext().checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
  575. }
  576. }