You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TasksFragment.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.example.meinwald.ui.task;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.media.Image;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.util.Log;
  10. import android.view.Gravity;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import androidx.annotation.NonNull;
  18. import androidx.annotation.Nullable;
  19. import androidx.core.app.ActivityCompat;
  20. import androidx.core.content.ContextCompat;
  21. import androidx.fragment.app.Fragment;
  22. import androidx.lifecycle.Observer;
  23. import androidx.lifecycle.ViewModelProviders;
  24. import com.example.meinwald.BuildConfig;
  25. import com.example.meinwald.R;
  26. import com.google.android.gms.location.FusedLocationProviderClient;
  27. import com.google.android.gms.location.LocationServices;
  28. import com.google.android.gms.maps.CameraUpdateFactory;
  29. import com.google.android.gms.tasks.OnCompleteListener;
  30. import com.google.android.gms.tasks.Task;
  31. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  32. import com.google.android.material.snackbar.Snackbar;
  33. import org.json.JSONException;
  34. import org.json.JSONObject;
  35. public class TasksFragment extends Fragment {
  36. private static final String TAG = TasksFragment.class.getSimpleName();
  37. /**
  38. * ______________________________________________________________________________________________
  39. * LOCATION
  40. */
  41. private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
  42. private static final String KEY_LOCATION = "location";
  43. /**
  44. * The entry point to the Fused Location Provider.
  45. */
  46. private FusedLocationProviderClient mFusedLocationProviderClient;
  47. private boolean mLocationPermissionGranted;
  48. /**
  49. * The geographical location where the device is currently located.
  50. * That is the last-known location retrieved by the Fused Location Provider.
  51. */
  52. private Location mLastKnownLocation;
  53. private NotificationsViewModel notificationsViewModel;
  54. public class OwnTask
  55. {
  56. private String title;
  57. private String notice;
  58. private Location location;
  59. private Image image;
  60. public OwnTask()
  61. {
  62. this.title = null;
  63. this.notice = null;
  64. this.location = null;
  65. this.image = null;
  66. }
  67. public OwnTask(String title, String notice, Location location, Image image)
  68. {
  69. this.title = title;
  70. this.notice = notice;
  71. this.location = location;
  72. this.image = image;
  73. }
  74. public String getTitle() {
  75. return title;
  76. }
  77. public String getNotice() {
  78. return notice;
  79. }
  80. public Location getLocation() {
  81. return location;
  82. }
  83. public Image getImage() {
  84. return image;
  85. }
  86. public void setTitle(String title) {
  87. this.title = title;
  88. }
  89. public void setNotice(String notice) {
  90. this.notice = notice;
  91. }
  92. public void setLocation(Location location) {
  93. this.location = location;
  94. }
  95. public void setImage(Image image) {
  96. this.image = image;
  97. }
  98. @Override
  99. public String toString() {
  100. return "OwnTask{" +
  101. "title='" + title + '\'' +
  102. ", notice='" + notice + '\'' +
  103. ", location=" + location +
  104. '}';
  105. }
  106. }
  107. OwnTask newTask = new OwnTask();
  108. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  109. View root = inflater.inflate(R.layout.fragment_tasks, container, false);
  110. //instance add button
  111. FloatingActionButton fab = root.findViewById(R.id.fab_tasks);
  112. //construct a FusedLocationProviderClient.
  113. mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
  114. //get location permission
  115. getLocationPermission();
  116. fab.setOnClickListener(new View.OnClickListener() {
  117. @Override
  118. public void onClick(View view) {
  119. AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
  120. getDeviceLocation();
  121. final View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.task_input, (ViewGroup) getView(), false);
  122. builder.setView(viewInflated);
  123. //set location
  124. final Button buttonSetLocation = viewInflated.findViewById(R.id.taskSetLocation);
  125. final TextView locationText = viewInflated.findViewById(R.id.taskLocation);
  126. buttonSetLocation.setOnClickListener(new View.OnClickListener() {
  127. public void onClick(View v) {
  128. getLocationPermission();
  129. getDeviceLocation();
  130. do {
  131. final Handler handler = new Handler();
  132. handler.postDelayed(new Runnable() {
  133. @Override
  134. public void run() {
  135. //wait if there is no known location
  136. }
  137. }, 100);
  138. }while (mLastKnownLocation == null);
  139. //save and display location
  140. newTask.setLocation(mLastKnownLocation);
  141. locationText.setText("Lat: " +String.valueOf(mLastKnownLocation.getLatitude()) + " Lng: " + String.valueOf(mLastKnownLocation.getLongitude()));
  142. }
  143. });
  144. //add picture
  145. final Button buttonAddPicture = viewInflated.findViewById(R.id.taskAddPicture);
  146. buttonAddPicture.setOnClickListener(new View.OnClickListener() {
  147. public void onClick(View v) {
  148. // Code here executes on main thread after user presses button
  149. }
  150. });
  151. builder.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
  152. @Override
  153. public void onClick(DialogInterface dialog, int which) {
  154. final TextView titleView = viewInflated.findViewById(R.id.taskTitleUserInput);
  155. final TextView noticeView = viewInflated.findViewById(R.id.taskNoticeUserInput);
  156. if (titleView.getText()!=null)
  157. {
  158. //save user input
  159. newTask.setTitle(String.valueOf(titleView.getText()));
  160. newTask.setNotice(String.valueOf(noticeView.getText()));
  161. if (BuildConfig.DEBUG)
  162. {
  163. Log.d(TAG, "new Task: " + newTask.toString());
  164. }
  165. dialog.dismiss();
  166. //confirm added task
  167. Toast toast = Toast.makeText(getActivity(), "Aufgabe hinzugefügt!", Toast.LENGTH_SHORT);
  168. TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
  169. if( v != null)
  170. {
  171. //text align center
  172. v.setGravity(Gravity.CENTER);
  173. }
  174. toast.show();
  175. }
  176. else
  177. {
  178. //title needed
  179. Toast toast = Toast.makeText(getActivity(), "Aufgabentitel fehlt!", Toast.LENGTH_SHORT);
  180. TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
  181. if( v != null)
  182. {
  183. //text align center
  184. v.setGravity(Gravity.CENTER);
  185. }
  186. toast.show();
  187. }
  188. }
  189. });
  190. builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
  191. @Override
  192. public void onClick(DialogInterface dialog, int which) {
  193. dialog.cancel();
  194. }
  195. });
  196. builder.show();
  197. }
  198. });
  199. return root;
  200. }
  201. /**
  202. * Request location permission, so that we can get the location of the device.
  203. * The result of the permission request is handled by a callback,
  204. * onRequestPermissionsResult.
  205. *
  206. */
  207. private void getLocationPermission() {
  208. if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
  209. android.Manifest.permission.ACCESS_FINE_LOCATION)
  210. == PackageManager.PERMISSION_GRANTED) {
  211. mLocationPermissionGranted = true;
  212. } else {
  213. ActivityCompat.requestPermissions(getActivity(),
  214. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  215. PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
  216. }
  217. }
  218. /**
  219. * DOCU ME!
  220. * TOOD: What are grant results etc for?
  221. * @param requestCode
  222. * @param permissions
  223. * @param grantResults
  224. */
  225. @Override
  226. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
  227. {
  228. mLocationPermissionGranted = false;
  229. switch (requestCode) {
  230. case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
  231. // If request is cancelled, the result arrays are empty.
  232. if (grantResults.length > 0
  233. && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  234. {
  235. mLocationPermissionGranted = true;
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Get the best and most recent location of the device, which may be null in rare
  242. * cases when a location is not available.
  243. *
  244. */
  245. private void getDeviceLocation() {
  246. try {
  247. if (mLocationPermissionGranted) {
  248. Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
  249. locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
  250. @Override
  251. public void onComplete(@NonNull Task<Location> task) {
  252. if (task.isSuccessful()) {
  253. // Set the last known position to the current location of the device.
  254. mLastKnownLocation = task.getResult();
  255. if (BuildConfig.DEBUG) {
  256. if (task.getResult() != null)
  257. {
  258. Log.d(TAG, "getDeviceLocation " + task.getResult().toString());
  259. }
  260. }
  261. } else {
  262. if (BuildConfig.DEBUG) {
  263. Log.d(TAG, "Current location is null. Using defaults.");
  264. Log.e(TAG, "Exception: %s", task.getException());
  265. }
  266. if (BuildConfig.DEBUG) {
  267. Log.d(TAG, "getDeviceLocation: task not successful");
  268. }
  269. }
  270. }
  271. });
  272. }
  273. } catch (SecurityException e) {
  274. if (BuildConfig.DEBUG) {
  275. Log.e(TAG, "Exception occurred: ");
  276. e.printStackTrace();
  277. }
  278. }
  279. }
  280. }