123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- package com.example.meinwald.ui.task;
-
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.media.Image;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import androidx.fragment.app.Fragment;
- import androidx.lifecycle.Observer;
- import androidx.lifecycle.ViewModelProviders;
-
- import com.example.meinwald.BuildConfig;
- import com.example.meinwald.R;
- import com.google.android.gms.location.FusedLocationProviderClient;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import com.google.android.material.snackbar.Snackbar;
-
- import org.json.JSONException;
- import org.json.JSONObject;
-
- public class TasksFragment extends Fragment {
-
- private static final String TAG = TasksFragment.class.getSimpleName();
-
- /**
- * ______________________________________________________________________________________________
- * LOCATION
- */
- private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
- private static final String KEY_LOCATION = "location";
- /**
- * The entry point to the Fused Location Provider.
- */
- private FusedLocationProviderClient mFusedLocationProviderClient;
- private boolean mLocationPermissionGranted;
- /**
- * The geographical location where the device is currently located.
- * That is the last-known location retrieved by the Fused Location Provider.
- */
- private Location mLastKnownLocation;
-
- private NotificationsViewModel notificationsViewModel;
-
- public class OwnTask
- {
- private String title;
- private String notice;
- private Location location;
- private Image image;
-
- public OwnTask()
- {
- this.title = null;
- this.notice = null;
- this.location = null;
- this.image = null;
- }
-
- public OwnTask(String title, String notice, Location location, Image image)
- {
- this.title = title;
- this.notice = notice;
- this.location = location;
- this.image = image;
- }
-
- public String getTitle() {
- return title;
- }
-
- public String getNotice() {
- return notice;
- }
-
- public Location getLocation() {
- return location;
- }
-
- public Image getImage() {
- return image;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public void setNotice(String notice) {
- this.notice = notice;
- }
-
- public void setLocation(Location location) {
- this.location = location;
- }
-
- public void setImage(Image image) {
- this.image = image;
- }
-
- @Override
- public String toString() {
- return "OwnTask{" +
- "title='" + title + '\'' +
- ", notice='" + notice + '\'' +
- ", location=" + location +
- '}';
- }
- }
-
- OwnTask newTask = new OwnTask();
-
- public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-
- View root = inflater.inflate(R.layout.fragment_tasks, container, false);
-
- //instance add button
- FloatingActionButton fab = root.findViewById(R.id.fab_tasks);
-
- //construct a FusedLocationProviderClient.
- mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
-
- //get location permission
- getLocationPermission();
-
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
-
- getDeviceLocation();
-
- final View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.task_input, (ViewGroup) getView(), false);
- builder.setView(viewInflated);
-
- //set location
- final Button buttonSetLocation = viewInflated.findViewById(R.id.taskSetLocation);
- final TextView locationText = viewInflated.findViewById(R.id.taskLocation);
-
- buttonSetLocation.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
-
- getLocationPermission();
- getDeviceLocation();
-
- do {
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- //wait if there is no known location
- }
- }, 100);
- }while (mLastKnownLocation == null);
-
- //save and display location
- newTask.setLocation(mLastKnownLocation);
- locationText.setText("Lat: " +String.valueOf(mLastKnownLocation.getLatitude()) + " Lng: " + String.valueOf(mLastKnownLocation.getLongitude()));
- }
- });
-
- //add picture
- final Button buttonAddPicture = viewInflated.findViewById(R.id.taskAddPicture);
-
- buttonAddPicture.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // Code here executes on main thread after user presses button
- }
- });
-
- builder.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- final TextView titleView = viewInflated.findViewById(R.id.taskTitleUserInput);
- final TextView noticeView = viewInflated.findViewById(R.id.taskNoticeUserInput);
-
- if (titleView.getText()!=null)
- {
- //save user input
- newTask.setTitle(String.valueOf(titleView.getText()));
- newTask.setNotice(String.valueOf(noticeView.getText()));
-
- if (BuildConfig.DEBUG)
- {
- Log.d(TAG, "new Task: " + newTask.toString());
- }
-
- dialog.dismiss();
-
- //confirm added task
- Toast toast = Toast.makeText(getActivity(), "Aufgabe hinzugefügt!", Toast.LENGTH_SHORT);
- TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
- if( v != null)
- {
- //text align center
- v.setGravity(Gravity.CENTER);
- }
- toast.show();
- }
- else
- {
- //title needed
- Toast toast = Toast.makeText(getActivity(), "Aufgabentitel fehlt!", Toast.LENGTH_SHORT);
- TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
- if( v != null)
- {
- //text align center
- v.setGravity(Gravity.CENTER);
- }
- toast.show();
- }
- }
- });
-
- builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
-
- builder.show();
- }
- });
-
- return root;
- }
-
- /**
- * Request location permission, so that we can get the location of the device.
- * The result of the permission request is handled by a callback,
- * onRequestPermissionsResult.
- *
- */
- private void getLocationPermission() {
- if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
- android.Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- mLocationPermissionGranted = true;
- } else {
- ActivityCompat.requestPermissions(getActivity(),
- new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
- PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
- }
- }
-
- /**
- * DOCU ME!
- * TOOD: What are grant results etc for?
- * @param requestCode
- * @param permissions
- * @param grantResults
- */
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
- {
- mLocationPermissionGranted = false;
- switch (requestCode) {
- case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED)
- {
- mLocationPermissionGranted = true;
- }
- }
- }
- }
-
- /**
- * Get the best and most recent location of the device, which may be null in rare
- * cases when a location is not available.
- *
- */
- private void getDeviceLocation() {
- try {
- if (mLocationPermissionGranted) {
- Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
- locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
- @Override
- public void onComplete(@NonNull Task<Location> task) {
- if (task.isSuccessful()) {
- // Set the last known position to the current location of the device.
- mLastKnownLocation = task.getResult();
- if (BuildConfig.DEBUG) {
-
- if (task.getResult() != null)
- {
- Log.d(TAG, "getDeviceLocation " + task.getResult().toString());
- }
- }
- } else {
- if (BuildConfig.DEBUG) {
- Log.d(TAG, "Current location is null. Using defaults.");
- Log.e(TAG, "Exception: %s", task.getException());
- }
-
- if (BuildConfig.DEBUG) {
- Log.d(TAG, "getDeviceLocation: task not successful");
- }
- }
- }
- });
- }
- } catch (SecurityException e) {
- if (BuildConfig.DEBUG) {
- Log.e(TAG, "Exception occurred: ");
- e.printStackTrace();
- }
- }
- }
- }
|