HomeLayout.java 40 KB

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