HomeLayout.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. package com.ivalentin.margolariak;
  2. import java.io.File;
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Locale;
  7. import android.annotation.SuppressLint;
  8. import android.Manifest;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.SharedPreferences;
  13. import android.content.pm.PackageManager;
  14. import android.database.Cursor;
  15. import android.database.sqlite.SQLiteDatabase;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18. import android.app.Fragment;
  19. import android.location.Criteria;
  20. import android.location.Location;
  21. import android.location.LocationListener;
  22. import android.location.LocationManager;
  23. import android.net.Uri;
  24. import android.os.Bundle;
  25. import android.app.FragmentManager;
  26. import android.app.FragmentTransaction;
  27. import android.support.v4.app.ActivityCompat;
  28. import android.text.Html;
  29. import android.util.Log;
  30. import android.view.LayoutInflater;
  31. import android.view.MenuItem;
  32. import android.view.View;
  33. import android.view.View.OnClickListener;
  34. import android.view.ViewGroup;
  35. import android.widget.Button;
  36. import android.widget.ImageView;
  37. import android.widget.LinearLayout;
  38. import android.widget.TextView;
  39. import android.widget.Toast;
  40. /**
  41. * Section that will be seen when the app is started.
  42. * Contains info from almost every other section.
  43. *
  44. * @author Iñigo Valentin
  45. *
  46. * @see Fragment
  47. *
  48. */
  49. @SuppressWarnings("UnusedReturnValue")
  50. public class HomeLayout extends Fragment implements LocationListener {
  51. //The location manager
  52. private LocationManager locationManager;
  53. @Override
  54. public boolean onOptionsItemSelected(MenuItem item) {
  55. return false;
  56. }
  57. private View view;
  58. /**
  59. * Run when the fragment is inflated.
  60. * Assigns views, gets the date and does the first call to the {//@link populate function}.
  61. *
  62. * @param inflater A LayoutInflater to handle the views
  63. * @param container The parent View
  64. * @param savedInstanceState Bundle with the saved state
  65. *
  66. * @return The fragment view
  67. *
  68. * @see android.app.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, final ViewGroup container, Bundle savedInstanceState) {
  73. //Load the layout.
  74. view = inflater.inflate(R.layout.fragment_layout_home, null);
  75. //Request location permissions if not set
  76. 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) {
  77. ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, GM.PERMISSION.LOCATION);
  78. }
  79. //Set Location manager
  80. locationManager = (LocationManager) view.getContext().getSystemService(Context.LOCATION_SERVICE);
  81. if (!(ActivityCompat.checkSelfPermission(view.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  82. locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), GM.LOCATION.ACCURACY.TIME, GM.LOCATION.ACCURACY.SPACE, this);
  83. onLocationChanged(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
  84. }
  85. //Set the title
  86. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_home));
  87. setUpLablanca(view);
  88. //Set up the blog section
  89. setUpBlog(view);
  90. //Set up activities sections
  91. if (setUpFutureActivities(view) == 0) {
  92. setUpPastActivities(view);
  93. }
  94. //Set up the gallery section
  95. setUpGallery(view);
  96. //Asynchronously set up location section
  97. //This will trigger onLocationChanged, that will trigger setUpLocation
  98. new HomeSectionLocation(view).execute();
  99. //Set up schedule sections
  100. setUpSchedule(1, view);
  101. setUpSchedule(0, view);
  102. //Setup the social section
  103. setUpSocial(view);
  104. //Return the view itself.
  105. return view;
  106. }
  107. /**
  108. * Populates the schedule sections of the home screen.
  109. *
  110. * @return The number of entries shown.
  111. */
  112. @SuppressLint("InflateParams")
  113. private int setUpSchedule(int gm, View view) {
  114. int count = 0;
  115. SharedPreferences data = view.getContext().getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
  116. if (!data.getBoolean(GM.DATA.KEY.LABLANCA, GM.DATA.DEFAULT.LABLANCA)) {
  117. return count;
  118. }
  119. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  120. Calendar calendar = Calendar.getInstance();
  121. Calendar calendarEnd;
  122. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
  123. String nowString = dateFormat.format(calendar.getTime());
  124. String endString;
  125. String query;
  126. //An inflater
  127. LayoutInflater factory = LayoutInflater.from(getActivity());
  128. //Views in each row
  129. TextView tvRowTitle, tvRowTime, tvRowPlace, tvRowId;
  130. Button btSchedule;
  131. //Assign layouts
  132. LinearLayout section, listNow, listNext, now, next, entry;
  133. if (gm == 1) {
  134. section = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule);
  135. listNow = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_now_list);
  136. listNext = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_later_list);
  137. now = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_now_content);
  138. next = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_later_content);
  139. btSchedule = (Button) view.findViewById(R.id.bt_home_gm);
  140. btSchedule.setOnClickListener(new OnClickListener() {
  141. @Override
  142. public void onClick(View v) {
  143. ((MainActivity) getActivity()).loadSection(GM.SECTION.GM_SCHEDULE);
  144. }
  145. });
  146. } else {
  147. section = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule);
  148. listNow = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_now_list);
  149. listNext = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_later_list);
  150. now = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_now_content);
  151. next = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_later_content);
  152. }
  153. //Set query for current events
  154. String lang = GM.getLang();
  155. calendarEnd = calendar;
  156. calendarEnd.add(Calendar.MINUTE, -40);
  157. endString = dateFormat.format(calendarEnd.getTime());
  158. if (gm == 1) {
  159. 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 <= '" + nowString + "' AND end > '" + nowString + "') OR (start > '" + endString + "' AND start < '" + nowString + "')) ORDER BY start LIMIT 2";
  160. }
  161. else{
  162. 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 <= '" + nowString + "' AND end > '" + nowString + "') OR (start > '" + endString + "' AND start < '" + nowString + "')) ORDER BY start LIMIT 2";
  163. }
  164. //Execute query and loop
  165. Cursor cursorNow = db.rawQuery(query, null);
  166. if (cursorNow.getCount() == 0) {
  167. now.setVisibility(View.GONE);
  168. }
  169. while (cursorNow.moveToNext()) {
  170. count++;
  171. entry = (LinearLayout) factory.inflate(R.layout.row_home_schedule, null);
  172. //Set id
  173. tvRowId = (TextView) entry.findViewById(R.id.tv_row_home_schedule_id);
  174. tvRowId.setText(cursorNow.getString(0));
  175. //Set title
  176. tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_home_schedule_title);
  177. tvRowTitle.setText(cursorNow.getString(1));
  178. //Set place
  179. tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_home_schedule_place);
  180. tvRowPlace.setText(cursorNow.getString(6));
  181. //Set icon
  182. if (gm == GM.SECTION.GM_SCHEDULE) {
  183. ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_home_schedule_pinpoint);
  184. pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.gm:drawable/pinpoint_gm", null, null));
  185. }
  186. //Set time
  187. tvRowTime = (TextView) entry.findViewById(R.id.tv_row_home_schedule_time);
  188. tvRowTime.setVisibility(View.GONE);
  189. //Add the view
  190. listNow.addView(entry);
  191. }
  192. cursorNow.close();
  193. //Prepare query for upcoming events
  194. calendarEnd = calendar;
  195. calendarEnd.add(Calendar.MINUTE, 320);
  196. endString = dateFormat.format(calendarEnd.getTime());
  197. if (gm == 1) {
  198. 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 > '" + nowString + "' AND start < '" + endString + "' ORDER BY start LIMIT 2";
  199. }
  200. else{
  201. 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 > '" + nowString + "' AND start < '" + endString + "' ORDER BY start LIMIT 2";
  202. }
  203. //Execute query and loop
  204. Cursor cursorNext = db.rawQuery(query, null);
  205. if (cursorNext.getCount() == 0) {
  206. next.setVisibility(View.GONE);
  207. }
  208. while (cursorNext.moveToNext()) {
  209. count++;
  210. entry = (LinearLayout) factory.inflate(R.layout.row_home_schedule, null);
  211. //Set id
  212. tvRowId = (TextView) entry.findViewById(R.id.tv_row_home_schedule_id);
  213. tvRowId.setText(cursorNext.getString(0));
  214. //Set title
  215. tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_home_schedule_title);
  216. tvRowTitle.setText(cursorNext.getString(1));
  217. //Set place
  218. tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_home_schedule_place);
  219. tvRowPlace.setText(cursorNext.getString(6));
  220. //Set icon
  221. if (gm == GM.SECTION.GM_SCHEDULE) {
  222. ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_home_schedule_pinpoint);
  223. pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.gm:drawable/pinpoint_gm", null, null));
  224. }
  225. //Set time
  226. tvRowTime = (TextView) entry.findViewById(R.id.tv_row_home_schedule_time);
  227. String tm = cursorNext.getString(4).substring(cursorNext.getString(4).length() - 8, cursorNext.getString(4).length() - 3);
  228. tvRowTime.setText(tm);
  229. //Add the view
  230. listNext.addView(entry);
  231. }
  232. cursorNext.close();
  233. if (count > 0) {
  234. section.setVisibility(View.VISIBLE);
  235. if (gm == 1) {
  236. section.setOnClickListener(new OnClickListener() {
  237. @Override
  238. public void onClick(View v) {
  239. ((MainActivity) getActivity()).loadSection(GM.SECTION.GM_SCHEDULE);
  240. }
  241. });
  242. } else {
  243. section.setOnClickListener(new OnClickListener() {
  244. @Override
  245. public void onClick(View v) {
  246. ((MainActivity) getActivity()).loadSection(GM.SECTION.SCHEDULE);
  247. }
  248. });
  249. }
  250. }
  251. db.close();
  252. return count;
  253. }
  254. /**
  255. * Populates the Social section of the home screen.
  256. */
  257. private void setUpSocial(View view) {
  258. ImageView phone = (ImageView) view.findViewById(R.id.iv_home_section_social_phone);
  259. ImageView mail = (ImageView) view.findViewById(R.id.iv_home_section_social_mail);
  260. ImageView whatsapp = (ImageView) view.findViewById(R.id.iv_home_section_social_whatsapp);
  261. ImageView facebook = (ImageView) view.findViewById(R.id.iv_home_section_social_facebook);
  262. ImageView twitter = (ImageView) view.findViewById(R.id.iv_home_section_social_twitter);
  263. ImageView googleplus = (ImageView) view.findViewById(R.id.iv_home_section_social_googleplus);
  264. ImageView youtube = (ImageView) view.findViewById(R.id.iv_home_section_social_youtube);
  265. ImageView instagram = (ImageView) view.findViewById(R.id.iv_home_section_social_instagram);
  266. phone.setOnClickListener(new View.OnClickListener() {
  267. @Override
  268. public void onClick(View v) {
  269. String url = v.getContext().getString(R.string.social_phone_link);
  270. Intent intent = new Intent(Intent.ACTION_DIAL);
  271. intent.setData(Uri.parse(url));
  272. startActivity(intent);
  273. }
  274. });
  275. mail.setOnClickListener(new View.OnClickListener() {
  276. @Override
  277. public void onClick(View v) {
  278. String url = v.getContext().getString(R.string.social_mail_link);
  279. Intent i = new Intent(Intent.ACTION_VIEW);
  280. i.setData(Uri.parse(url));
  281. startActivity(i);
  282. }
  283. });
  284. whatsapp.setOnClickListener(new View.OnClickListener() {
  285. @Override
  286. public void onClick(View v) {
  287. try {
  288. Uri mUri = Uri.parse(getString(R.string.social_whatsapp_link));
  289. Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
  290. mIntent.setPackage(getString(R.string.social_whatsapp_package));
  291. mIntent.putExtra(getString(R.string.social_whatsapp_chat), true);
  292. startActivity(mIntent);
  293. } catch (Exception e) {
  294. Toast.makeText(v.getContext(), getString(R.string.social_whatsapp_error), Toast.LENGTH_LONG).show();
  295. }
  296. }
  297. });
  298. facebook.setOnClickListener(new View.OnClickListener() {
  299. @Override
  300. public void onClick(View v) {
  301. String url = v.getContext().getString(R.string.social_facebook_link);
  302. Intent i = new Intent(Intent.ACTION_VIEW);
  303. i.setData(Uri.parse(url));
  304. startActivity(i);
  305. }
  306. });
  307. twitter.setOnClickListener(new View.OnClickListener() {
  308. @Override
  309. public void onClick(View v) {
  310. String url = v.getContext().getString(R.string.social_twitter_link);
  311. Intent i = new Intent(Intent.ACTION_VIEW);
  312. i.setData(Uri.parse(url));
  313. startActivity(i);
  314. }
  315. });
  316. googleplus.setOnClickListener(new View.OnClickListener() {
  317. @Override
  318. public void onClick(View v) {
  319. String url = v.getContext().getString(R.string.social_googleplus_link);
  320. Intent i = new Intent(Intent.ACTION_VIEW);
  321. i.setData(Uri.parse(url));
  322. startActivity(i);
  323. }
  324. });
  325. youtube.setOnClickListener(new View.OnClickListener() {
  326. @Override
  327. public void onClick(View v) {
  328. String url = v.getContext().getString(R.string.social_youtube_link);
  329. Intent i = new Intent(Intent.ACTION_VIEW);
  330. i.setData(Uri.parse(url));
  331. startActivity(i);
  332. }
  333. });
  334. instagram.setOnClickListener(new View.OnClickListener() {
  335. @Override
  336. public void onClick(View v) {
  337. String url = v.getContext().getString(R.string.social_instagram_link);
  338. Intent i = new Intent(Intent.ACTION_VIEW);
  339. i.setData(Uri.parse(url));
  340. startActivity(i);
  341. }
  342. });
  343. }
  344. /**
  345. * Populates the La Blanca section of the home screen.
  346. *
  347. * @return True if the section has been shown, false otherwise.
  348. */
  349. @SuppressWarnings("ResultOfMethodCallIgnored")
  350. private boolean setUpLablanca(View view) {
  351. SharedPreferences preferences = view.getContext().getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
  352. Boolean set = false;
  353. if (preferences.getBoolean(GM.DATA.KEY.LABLANCA, GM.DATA.DEFAULT.LABLANCA)) {
  354. LinearLayout llSection = (LinearLayout) view.findViewById(R.id.ll_home_section_lablanca);
  355. TextView tvText = (TextView) view.findViewById(R.id.tv_home_section_lablanca_text);
  356. ImageView ivImage = (ImageView) view.findViewById(R.id.iv_home_section_lablanca_image);
  357. //Set click listener
  358. llSection.setOnClickListener(new OnClickListener() {
  359. @Override
  360. public void onClick(View v) {
  361. ((MainActivity) getActivity()).loadSection(GM.SECTION.LABLANCA);
  362. }
  363. });
  364. //Get data from the database of the future activities
  365. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  366. int year = Calendar.getInstance().get(Calendar.YEAR);
  367. String lang = GM.getLang();
  368. Cursor cursor = db.rawQuery("SELECT text_" + lang + ", img FROM festival WHERE year = " + year + ";", null);
  369. if (cursor.getCount() > 0) {
  370. cursor.moveToFirst();
  371. llSection.setVisibility(View.VISIBLE);
  372. //Enable menu entries
  373. //this.getActivity().findViewById(R.id.menu_lablanca_gm_schedule).setVisibility(View.VISIBLE);
  374. //this.getActivity().findViewById(R.id.menu_lablanca_schedule).setVisibility(View.VISIBLE);
  375. //Set text
  376. String text = Html.fromHtml(cursor.getString(0)).toString();
  377. String shortText = text.substring(0, 80);
  378. if (text.equals(shortText)) {
  379. tvText.setText(text);
  380. }
  381. else{
  382. shortText = shortText + "...";
  383. tvText.setText(shortText);
  384. }
  385. set = true;
  386. //Set image
  387. String image = cursor.getString(1);
  388. if (image != null && image.length() > 0){
  389. //Check if image exists
  390. File f;
  391. f = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
  392. if (f.exists()) {
  393. //If the image exists, set it.
  394. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
  395. ivImage.setImageBitmap(myBitmap);
  396. } else {
  397. //If not, create directories and download asynchronously
  398. File fpath;
  399. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/");
  400. fpath.mkdirs();
  401. new DownloadImage(GM.API.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, ivImage, GM.IMG.SIZE.PREVIEW).execute();
  402. }
  403. }
  404. else {
  405. ivImage.setVisibility(View.GONE);
  406. }
  407. }
  408. cursor.close();
  409. db.close();
  410. }
  411. return set;
  412. }
  413. /**
  414. * Populates the Location section of the home screen.
  415. *
  416. * @return True if the section has been shown, false otherwise.
  417. */
  418. private boolean setUpLocation(Location location, View view) {
  419. try {
  420. Double lat, lon;
  421. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB.NAME, Activity.MODE_PRIVATE, null);
  422. Cursor cursor;
  423. cursor = db.rawQuery("SELECT lat, lon FROM location WHERE dtime >= Datetime('now', '-10 minutes') ORDER BY dtime DESC LIMIT 1;", null);
  424. if (cursor.getCount() == 0){
  425. cursor.close();
  426. db.close();
  427. return false;
  428. }
  429. else {
  430. cursor.moveToFirst();
  431. lat = cursor.getDouble(0);
  432. lon = cursor.getDouble(1);
  433. cursor.close();
  434. db.close();
  435. Double distance = Distance.calculateDistance(lat, lon, location.getLatitude(), location.getLongitude());
  436. TextView text = (TextView) view.findViewById(R.id.tv_home_section_location_distance);
  437. if (distance <= 2) {
  438. distance = 1000 * distance;
  439. text.setText(String.format(getString(R.string.home_section_location_text_short), distance.intValue(), (int) (0.012 * distance.intValue())));
  440. } else {
  441. text.setText(String.format(getString(R.string.home_section_location_text_long), String.format(Locale.US, "%.02f", distance)));
  442. }
  443. return true;
  444. }
  445. }
  446. catch (Exception ex){
  447. Log.e("Location", "Couldn't set up home location section: " + ex.toString());
  448. }
  449. return false;
  450. }
  451. /**
  452. * Populates the Gallery section of the home screen.
  453. * Loads four photos.
  454. * Tapping them takes to the respective album, tapping anywhere else, to the gallery section.
  455. *
  456. * @return The number of entries shown
  457. */
  458. @SuppressWarnings("ResultOfMethodCallIgnored")
  459. private int setUpGallery(View view) {
  460. int counter = 0;
  461. //Set onClick listener of section
  462. LinearLayout llGallery = (LinearLayout) view.findViewById(R.id.ll_home_section_gallery);
  463. //Set click listener
  464. llGallery.setOnClickListener(new OnClickListener() {
  465. @Override
  466. public void onClick(View v) {
  467. ((MainActivity) getActivity()).loadSection(GM.SECTION.GALLERY);
  468. }
  469. });
  470. //Assign views
  471. ImageView ivPhoto[] = new ImageView[4];
  472. ivPhoto[0] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_0);
  473. ivPhoto[1] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_1);
  474. ivPhoto[2] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_2);
  475. ivPhoto[3] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_3);
  476. //Get data from the database of the future activities
  477. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  478. Cursor cursor = db.rawQuery("SELECT id, album, file, uploaded FROM photo, photo_album WHERE id = photo ORDER BY uploaded DESC LIMIT 4;", null);
  479. while (cursor.moveToNext()) {
  480. //Set id
  481. ivPhoto[counter].setTag(R.string.key_0, cursor.getString(0));
  482. ivPhoto[counter].setTag(R.string.key_1, cursor.getString(1));
  483. //Set image
  484. String image = cursor.getString(2);
  485. //Check if image exists
  486. File f;
  487. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  488. if (f.exists()) {
  489. //If the image exists, set it.
  490. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  491. ivPhoto[counter].setImageBitmap(myBitmap);
  492. } else {
  493. //If not, create directories and download asynchronously
  494. File fpath;
  495. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  496. fpath.mkdirs();
  497. new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivPhoto[counter], GM.IMG.SIZE.PREVIEW).execute();
  498. }
  499. //Set listeners for images
  500. ivPhoto[counter].setOnClickListener(new View.OnClickListener() {
  501. @Override
  502. public void onClick(View v) {
  503. Fragment fragment = new AlbumLayout();
  504. Bundle bundle = new Bundle();
  505. int photoId = Integer.parseInt((String) v.getTag(R.string.key_0));
  506. int albumId = Integer.parseInt((String) v.getTag(R.string.key_1));
  507. bundle.putInt("album", albumId);
  508. bundle.putInt("photo", photoId);
  509. fragment.setArguments(bundle);
  510. FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
  511. FragmentTransaction ft = fm.beginTransaction();
  512. ft.replace(R.id.activity_main_content_fragment, fragment);
  513. ft.addToBackStack("album_" + albumId);
  514. ft.commit();
  515. }
  516. });
  517. counter++;
  518. }
  519. //Close db
  520. cursor.close();
  521. db.close();
  522. return counter;
  523. }
  524. /**
  525. * Populates the Past Activities section of the home screen.
  526. * Makes the section visible and adds two rows with the two last activities.
  527. * Tapping them takes to the post, tapping anywhere else, to the blog section.
  528. *
  529. * @return The number of entries shown
  530. */
  531. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  532. @SuppressWarnings("ResultOfMethodCallIgnored")
  533. private int setUpPastActivities(View view) {
  534. int counter = 0;
  535. String lang = GM.getLang();
  536. //Get data from the database of the future activities
  537. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  538. Cursor cursor = db.rawQuery("SELECT id, date, city, title_" + lang + " AS title, text_" + lang + " AS text, price, after_" + lang + " AS after FROM activity WHERE date < date('now') ORDER BY date DESC LIMIT 2;", null);
  539. //Show section
  540. LinearLayout llActivitiesPast = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_past);
  541. llActivitiesPast.setVisibility(View.VISIBLE);
  542. //Set click listener
  543. llActivitiesPast.setOnClickListener(new OnClickListener() {
  544. @Override
  545. public void onClick(View v) {
  546. ((MainActivity) getActivity()).loadSection(GM.SECTION.ACTIVITIES);
  547. }
  548. });
  549. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_past_content);
  550. LinearLayout entry;
  551. //An inflater
  552. LayoutInflater factory = LayoutInflater.from(getActivity());
  553. //Print rows
  554. while (cursor.moveToNext()) {
  555. //Create a new row
  556. entry = (LinearLayout) factory.inflate(R.layout.row_home_activities, null);
  557. //Set margins
  558. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  559. layoutParams.setMargins(GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN);
  560. entry.setLayoutParams(layoutParams);
  561. //Set title
  562. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_activity_title);
  563. tvTitle.setText(cursor.getString(3));
  564. //Set city
  565. TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_home_activity_city);
  566. tvCity.setText(cursor.getString(2));
  567. //Hide price
  568. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_home_activity_price);
  569. tvPrice.setVisibility(View.GONE);
  570. //tvPrice.setText(cursor.getString(5) + "€");
  571. //Set text
  572. String text;
  573. if (cursor.getString(6) == null || cursor.getString(6).length() < 1) {
  574. text = Html.fromHtml(cursor.getString(4)).toString();
  575. } else {
  576. text = Html.fromHtml(cursor.getString(6)).toString();
  577. }
  578. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_activity_text);
  579. tvText.setText(text);
  580. //Set date
  581. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_activity_date);
  582. tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, true, true, false));
  583. //Set hidden id
  584. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_activity_hidden);
  585. tvId.setText(cursor.getString(0));
  586. //Get image
  587. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_activity);
  588. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
  589. if (cursorImage.getCount() > 0) {
  590. cursorImage.moveToFirst();
  591. String image = cursorImage.getString(0);
  592. //Check if image exists
  593. File f;
  594. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  595. if (f.exists()) {
  596. //If the image exists, set it.
  597. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  598. iv.setImageBitmap(myBitmap);
  599. } else {
  600. //If not, create directories and download asynchronously
  601. File fpath;
  602. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  603. fpath.mkdirs();
  604. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
  605. }
  606. }
  607. cursorImage.close();
  608. //Set onCLickListener
  609. entry.setOnClickListener(new View.OnClickListener() {
  610. @Override
  611. public void onClick(View v) {
  612. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  613. Fragment fragment = new ActivityPastLayout();
  614. Bundle bundle = new Bundle();
  615. //Pass post id
  616. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_activity_hidden)).getText().toString());
  617. bundle.putInt("activity", id);
  618. fragment.setArguments(bundle);
  619. FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
  620. FragmentTransaction ft = fm.beginTransaction();
  621. ft.replace(R.id.activity_main_content_fragment, fragment);
  622. ft.addToBackStack("activity_" + id);
  623. ft.commit();
  624. }
  625. });
  626. //Add to the list
  627. llList.addView(entry);
  628. counter++;
  629. }
  630. //Close db
  631. cursor.close();
  632. db.close();
  633. return counter;
  634. }
  635. /**
  636. * Populates the Future activities section of the home screen.
  637. * If there are future activities, makes the section visible.
  638. * Adds two rows with the two next activities.
  639. * Tapping them takes to the post, tapping anywhere else, to the blog section.
  640. *
  641. * @return The number of entries shown
  642. */
  643. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  644. @SuppressWarnings("ResultOfMethodCallIgnored")
  645. private int setUpFutureActivities(View view) {
  646. int counter = 0;
  647. String lang = GM.getLang();
  648. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_future_content);
  649. LinearLayout entry;
  650. //An inflater
  651. LayoutInflater factory = LayoutInflater.from(getActivity());
  652. //Get data from the database of the future activities
  653. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  654. Cursor cursor = db.rawQuery("SELECT id, date, city, title_" + lang + " AS title, text_" + lang + " AS text, price FROM activity WHERE date >= date('now') ORDER BY date LIMIT 2;", null);
  655. //If there are future activities...
  656. if (cursor.getCount() > 0) {
  657. //Show section
  658. LinearLayout llActivitiesFuture = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_future);
  659. llActivitiesFuture.setVisibility(View.VISIBLE);
  660. //Set click listener
  661. llActivitiesFuture.setOnClickListener(new OnClickListener() {
  662. @Override
  663. public void onClick(View v) {
  664. ((MainActivity) getActivity()).loadSection(GM.SECTION.ACTIVITIES);
  665. }
  666. });
  667. //Print rows
  668. while (cursor.moveToNext()) {
  669. //Create a new row
  670. entry = (LinearLayout) factory.inflate(R.layout.row_home_activities, null);
  671. //Set margins
  672. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  673. layoutParams.setMargins(GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN);
  674. entry.setLayoutParams(layoutParams);
  675. //Set title
  676. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_activity_title);
  677. tvTitle.setText(cursor.getString(3));
  678. //Set city
  679. TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_home_activity_city);
  680. tvCity.setText(cursor.getString(2));
  681. //Set price
  682. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_home_activity_price);
  683. tvPrice.setText(String.format(getString(R.string.price), cursor.getInt(5)));
  684. //Set text
  685. String text = Html.fromHtml(cursor.getString(4)).toString();
  686. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_activity_text);
  687. tvText.setText(text);
  688. //Set date
  689. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_activity_date);
  690. tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, true, false, false));
  691. //Set hidden id
  692. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_activity_hidden);
  693. tvId.setText(cursor.getString(0));
  694. //Get image
  695. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_activity);
  696. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
  697. if (cursorImage.getCount() > 0) {
  698. cursorImage.moveToFirst();
  699. String image = cursorImage.getString(0);
  700. //Check if image exists
  701. File f;
  702. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  703. if (f.exists()) {
  704. //If the image exists, set it.
  705. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  706. iv.setImageBitmap(myBitmap);
  707. } else {
  708. //If not, create directories and download asynchronously
  709. File fpath;
  710. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  711. fpath.mkdirs();
  712. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
  713. }
  714. }
  715. cursorImage.close();
  716. //Set onCLickListener
  717. entry.setOnClickListener(new View.OnClickListener() {
  718. @Override
  719. public void onClick(View v) {
  720. Fragment fragment = new ActivityFutureLayout();
  721. Bundle bundle = new Bundle();
  722. //Pass post id
  723. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_activity_hidden)).getText().toString());
  724. bundle.putInt("activity", id);
  725. fragment.setArguments(bundle);
  726. FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
  727. FragmentTransaction ft = fm.beginTransaction();
  728. ft.replace(R.id.activity_main_content_fragment, fragment);
  729. ft.addToBackStack("activity_" + id);
  730. ft.commit();
  731. }
  732. });
  733. //Add to the list
  734. llList.addView(entry);
  735. counter++;
  736. }
  737. }
  738. //Close db
  739. cursor.close();
  740. db.close();
  741. return counter;
  742. }
  743. /**
  744. * Populates the Blog section of the home screen.
  745. * Adds two rows with the two latest posts. Tapping them takes to the post, tapping anywhere else, to the blog section.
  746. *
  747. * @return The number of entries shown
  748. */
  749. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  750. @SuppressWarnings("ResultOfMethodCallIgnored")
  751. private int setUpBlog(View view) {
  752. int counter = 0;
  753. String lang = GM.getLang();
  754. LinearLayout llBlog = (LinearLayout) view.findViewById(R.id.ll_home_section_blog);
  755. llBlog.setOnClickListener(new OnClickListener() {
  756. @Override
  757. public void onClick(View v) {
  758. ((MainActivity) getActivity()).loadSection(GM.SECTION.BLOG);
  759. }
  760. });
  761. //Assign elements
  762. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_blog_content);
  763. LinearLayout entry;
  764. //An inflater
  765. LayoutInflater factory = LayoutInflater.from(getActivity());
  766. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  767. final Cursor cursor;
  768. //Get data from the database
  769. cursor = db.rawQuery("SELECT id, title_" + lang + " AS title, text_" + lang + " AS text, dtime FROM post ORDER BY dtime DESC LIMIT 2;", null);
  770. //Loop
  771. while (cursor.moveToNext()) {
  772. //Create a new row
  773. entry = (LinearLayout) factory.inflate(R.layout.row_home_blog, null);
  774. //Set margins
  775. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  776. layoutParams.setMargins(GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN, GM.UI.ENTRY_MARGIN);
  777. entry.setLayoutParams(layoutParams);
  778. //Set title
  779. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_blog_title);
  780. tvTitle.setText(cursor.getString(1));
  781. //Set text
  782. String text = Html.fromHtml(cursor.getString(2)).toString();
  783. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_blog_text);
  784. tvText.setText(text);
  785. //Set date
  786. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_blog_date);
  787. tvDate.setText(GM.formatDate(cursor.getString(3), lang, true, true, false));
  788. //Set hidden id
  789. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_blog_hidden);
  790. tvId.setText(cursor.getString(0));
  791. //Get image
  792. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_blog_image);
  793. Cursor cursorImage = db.rawQuery("SELECT image FROM post_image WHERE post = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
  794. if (cursorImage.getCount() > 0) {
  795. cursorImage.moveToFirst();
  796. String image = cursorImage.getString(0);
  797. //Check if image exists
  798. File f;
  799. f = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
  800. if (f.exists()) {
  801. //If the image exists, set it.
  802. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
  803. iv.setImageBitmap(myBitmap);
  804. } else {
  805. //If not, create directories and download asynchronously
  806. File fpath;
  807. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/");
  808. fpath.mkdirs();
  809. new DownloadImage(GM.API.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
  810. }
  811. }
  812. cursorImage.close();
  813. //Set onCLickListener
  814. entry.setOnClickListener(new View.OnClickListener() {
  815. @Override
  816. public void onClick(View v) {
  817. Fragment fragment = new PostLayout();
  818. Bundle bundle = new Bundle();
  819. //Pass post id
  820. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_blog_hidden)).getText().toString());
  821. bundle.putInt("post", id);
  822. fragment.setArguments(bundle);
  823. FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
  824. FragmentTransaction ft = fm.beginTransaction();
  825. ft.replace(R.id.activity_main_content_fragment, fragment);
  826. ft.addToBackStack("post_" + id);
  827. ft.commit();
  828. }
  829. });
  830. //Add to the list
  831. llList.addView(entry);
  832. counter++;
  833. }
  834. //Close database
  835. cursor.close();
  836. db.close();
  837. return counter;
  838. }
  839. /**
  840. * Called when the user location changes.
  841. * Recalculates the list of around events and calls updateLocation() to
  842. * update the distance in the location section.
  843. *
  844. * @param location The new location
  845. *
  846. * @see android.location.LocationListener#onLocationChanged(android.location.Location)
  847. */
  848. @Override
  849. public void onLocationChanged(Location location) {
  850. setUpLocation(location, view);
  851. }
  852. /**
  853. * Called when the location provider changes it's state.
  854. * Recalculates the list of events in the around section.
  855. *
  856. * @param provider The name of the provider
  857. * @param status Status code of the provider
  858. * @param extras Extras passed
  859. *
  860. * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
  861. */
  862. @Override
  863. public void onStatusChanged(String provider, int status, Bundle extras) {
  864. //populateAround();
  865. }
  866. /**
  867. * Called when a location provider is enabled.
  868. * Recalculates the list of events.
  869. *
  870. * @param provider The name of the provider
  871. *
  872. * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
  873. */
  874. @Override
  875. public void onProviderEnabled(String provider) {
  876. //populateAround();
  877. }
  878. /**
  879. * Called when a location provider is disabled.
  880. * Recalculates the list of events.
  881. *
  882. * @param provider The name of the provider
  883. *
  884. * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
  885. */
  886. @Override
  887. public void onProviderDisabled(String provider) {
  888. //populateAround();
  889. }
  890. /**
  891. * Called when the fragment is paused.
  892. * Stops the location manager
  893. * @see android.app.Fragment#onPause()
  894. */
  895. @Override
  896. public void onPause() {
  897. 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)) {
  898. locationManager.removeUpdates(this);
  899. }
  900. super.onPause();
  901. }
  902. /**
  903. * Called when the fragment is destroyed.
  904. * Stops the location manager
  905. * @see android.app.Fragment#onDestroy()
  906. */
  907. @Override
  908. public void onDestroy() {
  909. try {
  910. 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)) {
  911. locationManager.removeUpdates(this);
  912. }
  913. }
  914. catch (Exception ex){
  915. Log.e("Location manager", "Unable to stop location manager on destroy: " + ex.toString());
  916. }
  917. super.onDestroy();
  918. }
  919. /**
  920. * Called when the fragment is brought back into the foreground.
  921. * Resumes the map and the location manager.
  922. *
  923. * @see android.app.Fragment#onResume()
  924. */
  925. @Override
  926. public void onResume(){
  927. 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)) {
  928. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
  929. }
  930. super.onResume();
  931. }
  932. }