|
|
@@ -1,47 +1,330 @@ |
|
|
|
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 View onCreateView(@NonNull LayoutInflater inflater, |
|
|
|
ViewGroup container, Bundle savedInstanceState) { |
|
|
|
notificationsViewModel = ViewModelProviders.of(this).get(NotificationsViewModel.class); |
|
|
|
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); |
|
|
|
final TextView textView = root.findViewById(R.id.text_tasks); |
|
|
|
notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() { |
|
|
|
@Override |
|
|
|
public void onChanged(@Nullable String s) { |
|
|
|
textView.setText(s); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
FloatingActionButton fab = root.findViewById(R.id.fab); |
|
|
|
//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) { |
|
|
|
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) |
|
|
|
.setAction("Action", null).show(); |
|
|
|
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(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |