From 65ef7879d29a624ba1f59a649b9cf4e0af2eccad Mon Sep 17 00:00:00 2001 From: diemarfe64635 Date: Thu, 10 Sep 2020 16:59:22 +0200 Subject: [PATCH] initial area management --- .../example/meinwald/ui/area/AreaAdapter.java | 96 ++++++++-- .../meinwald/ui/area/AreaFragment.java | 135 ++++++++++---- .../com/example/meinwald/ui/area/OwnArea.java | 55 +++--- .../example/meinwald/ui/map/MapsFragment.java | 173 +++++++++++++++++- .../meinwald/ui/task/TasksFragment.java | 6 + .../app/src/main/res/layout/area_element.xml | 38 +++- .../app/src/main/res/layout/area_input.xml | 6 +- .../src/main/res/layout/area_management.xml | 145 +++++++++++++++ .../res/layout/area_management_notice.xml | 35 ++++ .../app/src/main/res/layout/fragment_map.xml | 2 +- .../app/src/main/res/layout/image_large.xml | 2 +- .../app/src/main/res/layout/task_input.xml | 4 +- .../main/res/navigation/mobile_navigation.xml | 2 +- 13 files changed, 608 insertions(+), 91 deletions(-) create mode 100644 Code Android/meinWald/app/src/main/res/layout/area_management.xml create mode 100644 Code Android/meinWald/app/src/main/res/layout/area_management_notice.xml diff --git a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaAdapter.java b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaAdapter.java index c68c8c3..6129de5 100644 --- a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaAdapter.java +++ b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaAdapter.java @@ -5,12 +5,14 @@ import android.content.Context; import android.content.DialogInterface; import android.graphics.Rect; import android.util.Log; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import com.example.meinwald.BuildConfig; import com.example.meinwald.R; @@ -50,15 +52,15 @@ public class AreaAdapter extends BaseAdapter public View getView(final int i, View view, final ViewGroup viewGroup) { view = inflater.inflate(R.layout.area_element, null); - final ConstraintLayout elementView = view.findViewById(R.id.areaInfoView); + final ConstraintLayout elementView = view.findViewById(R.id.areaElement); final ConstraintLayout infovView = view.findViewById(R.id.areaInfoView); final TextView title = (TextView) view.findViewById(R.id.areaInfoTitle); final TextView notice = (TextView) view.findViewById(R.id.areaInfoNotice); final ImageView icon = (ImageView) view.findViewById(R.id.areaIcon); + final ImageView manageArea = (ImageView) view.findViewById(R.id.areaManageArea); + context = view.getContext(); - - if (BuildConfig.DEBUG) { Log.d("AreaAdapter",areaList.get(i).toString()); @@ -70,21 +72,84 @@ public class AreaAdapter extends BaseAdapter notice.setText(areaList.get(i).getNotice()); icon.setImageBitmap(areaList.get(i).getImage()); - title.setOnClickListener(new View.OnClickListener() { + manageArea.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + final AlertDialog.Builder builder = new AlertDialog.Builder(v.getRootView().getContext()); + final View viewInflated = LayoutInflater.from(v.getRootView().getContext()).inflate(R.layout.area_management, viewGroup, false); + + builder.setView(viewInflated); + + //get elements + TextView title = viewInflated.findViewById(R.id.areaManageTitle); + final TextView notice = viewInflated.findViewById(R.id.areaManageNotice); + TextView gpscount = viewInflated.findViewById(R.id.areaManageGPSPoints); + ImageView image = (ImageView) viewInflated.findViewById(R.id.areaManageImage); + + ImageView newNotice = (ImageView) viewInflated.findViewById(R.id.areaManageNewDescription); + ImageView newGPS = (ImageView) viewInflated.findViewById(R.id.areaManageNewGPS); + ImageView newImage = (ImageView) viewInflated.findViewById(R.id.areaManageNewImage); + ImageView deleteArea = (ImageView) viewInflated.findViewById(R.id.areaManageDeleteArea); + + //set current vlues + title.setText(areaList.get(i).getTitle()); + notice.setText(areaList.get(i).getNotice()); + gpscount.setText(String.valueOf(areaList.get(i).getLocations().size())); + + if (areaList.get(i).getImage() != null) + { + image.setImageBitmap(areaList.get(i).getImage()); + } + + newNotice.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) + { + final AlertDialog.Builder newbuilder = new AlertDialog.Builder(v.getRootView().getContext()); + final View viewInflated = LayoutInflater.from(v.getRootView().getContext()).inflate(R.layout.area_management_notice, viewGroup, false); + + newbuilder.setView(viewInflated); + + final TextView noticeView = viewInflated.findViewById(R.id.areaManagementNoticeUserInput); + + newbuilder.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + areaList.get(i).setNotice(noticeView.getText().toString()); + notice.setText(areaList.get(i).getNotice()); + dialog.dismiss(); + } + }); + + newbuilder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + newbuilder.show(); + }}); - } - }); + builder.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { - notice.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) - { + dialog.dismiss(); + } + }); + builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + builder.show(); } }); @@ -114,10 +179,17 @@ public class AreaAdapter extends BaseAdapter } }); - view.setOnLongClickListener(new View.OnLongClickListener() { + elementView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - // TODO Auto-generated method stub + + Toast toast = Toast.makeText(context, "on long click!", Toast.LENGTH_SHORT); + TextView tv = (TextView) toast.getView().findViewById(android.R.id.message); + if( v != null) + { + //text align center + tv.setGravity(Gravity.CENTER); + } return true; } }); diff --git a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaFragment.java b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaFragment.java index 52967ae..7156e30 100644 --- a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaFragment.java +++ b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/area/AreaFragment.java @@ -3,13 +3,9 @@ package com.example.meinwald.ui.area; import android.Manifest; import android.annotation.SuppressLint; import android.app.AlertDialog; -import android.content.ComponentName; import android.content.DialogInterface; -import android.content.ServiceConnection; import android.content.pm.PackageManager; -import android.location.Location; import android.os.Bundle; -import android.os.IBinder; import android.os.PowerManager; import android.preference.PreferenceManager; import android.util.Log; @@ -24,27 +20,20 @@ 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 com.example.meinwald.BuildConfig; import com.example.meinwald.R; -import com.example.meinwald.ui.task.TaskAdapter; -import com.example.meinwald.ui.task.TasksFragment; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.model.LatLng; -import com.google.android.gms.tasks.OnCompleteListener; -import com.google.android.gms.tasks.Task; import com.google.android.material.floatingactionbutton.FloatingActionButton; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.json.JSONStringer; import java.io.BufferedReader; import java.io.File; @@ -52,8 +41,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -62,7 +49,7 @@ import static android.content.Context.POWER_SERVICE; public class AreaFragment extends Fragment { - private static final String TAG = TasksFragment.class.getSimpleName(); + private static final String TAG = AreaFragment.class.getSimpleName(); /** * ______________________________________________________________________________________________ @@ -109,6 +96,15 @@ public class AreaFragment extends Fragment { PowerManager.WakeLock wakeLock; LocationService gps; + @Override + public void onCreate (Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + areaReferences = new ArrayList<>(); + areaReferences = readAreaReferencesFromPreferences(); + } + @SuppressLint("InvalidWakeLockTag") public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -125,7 +121,10 @@ public class AreaFragment extends Fragment { //instantiate area lists areaList = (ListView) root.findViewById(R.id.areaListView); allAreas = new ArrayList<>(); - areaReferences = new ArrayList<>(); + + //get saved areas + allAreas = loadAreasFromPreferences(); + instantiateAreasList(); PowerManager powerManager = (PowerManager) getContext().getSystemService(POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); @@ -142,11 +141,6 @@ public class AreaFragment extends Fragment { getLocationPermission(); getWriteStoragePPermission(); - //get saved areas - areaReferences = readAreaReferencesFromPreferences(); - allAreas = loadAreasFromPreferences(); - instantiateAreasList(); - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); newArea = new OwnArea(); @@ -195,7 +189,7 @@ public class AreaFragment extends Fragment { public void onClick(DialogInterface dialog, int which) { final TextView titleView = viewInflated.findViewById(R.id.areaTitleUserInput); - final TextView noticeView = viewInflated.findViewById(R.id.areaNoticeUserInput); + final TextView noticeView = viewInflated.findViewById(R.id.areaManagementNoticeUserInput); if (BuildConfig.DEBUG) { Log.d(TAG, "new title: " + titleView.getText()); @@ -215,12 +209,9 @@ public class AreaFragment extends Fragment { Log.d(TAG, "add new task result: " + result); } - //stop tracking - //wakeLock.release(); + emptyPreferencesAndSaveAreaReferencesToPreferences(); - //save new area - allAreas.add(newArea); - writeAreaToExternalStorage(newArea); + areaReferences.add(new AreaReference(newArea.getId(), writeAreaToExternalStorage(newArea))); //actuate list instantiateAreasList(); @@ -291,10 +282,10 @@ public class AreaFragment extends Fragment { @Override public void onPause() { - //save area references to shared preferences - emptyPreferencesAndSaveEpiColorsToPreferences(); - super.onPause(); + + //save area references to shared preferences + emptyPreferencesAndSaveAreaReferencesToPreferences(); } /** @@ -303,13 +294,26 @@ public class AreaFragment extends Fragment { */ private List loadAreasFromPreferences() { + if (BuildConfig.DEBUG) + { + Log.d(TAG, "loadAreasFromPreferences() reference size: " + areaReferences.size()); + } + List areas = new ArrayList<>(); + Integer i = 0; for (AreaReference reference: areaReferences) { String jsonString = readAreaFromExternalStorage(reference.getPath()); areas.add(new OwnArea(jsonString)); + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "load area from preferences: " + areas.get(i).toString()); + } + + i++; } return areas; @@ -323,13 +327,22 @@ public class AreaFragment extends Fragment { */ private String writeAreaToExternalStorage(OwnArea newArea){ + if (BuildConfig.DEBUG) + { + Log.d(TAG, "writeAreaToExternalStorage()"); + } + // Find the root of the external storage. File root = android.os.Environment.getExternalStorageDirectory(); //create file if it does not exist - File path = new File (root.getAbsolutePath() + "meinWald" + File.pathSeparator + "areas"); + File path = new File (getContext().getFilesDir(), "meinWald" + File.pathSeparator + "areas"); if(!path.exists()){ path.mkdirs(); + if (BuildConfig.DEBUG) + { + Log.d(TAG, "Create new text file: " + path); + } } File file = new File(path, newArea.getId() + ".txt"); @@ -341,9 +354,15 @@ public class AreaFragment extends Fragment { pw.close(); f.close(); } catch (FileNotFoundException e) { - e.printStackTrace(); + if (BuildConfig.DEBUG) + { + Log.e(TAG, "Failed write to external storage: " + e.toString()); + } } catch (IOException e) { - e.printStackTrace(); + if (BuildConfig.DEBUG) + { + Log.e(TAG, "Failed write to external storage: " + e.toString()); + } } return file.getPath(); @@ -357,6 +376,11 @@ public class AreaFragment extends Fragment { */ private String readAreaFromExternalStorage(String path) { + if (BuildConfig.DEBUG) + { + Log.d(TAG, "readAreaFromExternalStorage()"); + } + //Get the text file File file = new File(path); @@ -373,7 +397,10 @@ public class AreaFragment extends Fragment { br.close(); } catch (IOException e) { - e.printStackTrace(); + if (BuildConfig.DEBUG) + { + Log.e(TAG, "Failed read from external storage: " + e.toString()); + } } return text.toString(); @@ -396,9 +423,15 @@ public class AreaFragment extends Fragment { /** * Reads areaReferences saved in preferences as jsonStrings * - * @return list of epiColorDtos + * @return list of AreaReferences */ private List readAreaReferencesFromPreferences() { + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "readAreaReferencesFromPreferences()"); + } + List areas = new ArrayList<>(); try { @@ -406,24 +439,44 @@ public class AreaFragment extends Fragment { if(!"error".equalsIgnoreCase(jsonString)) { JSONArray jsonObjects = new JSONArray(jsonString); for (int i = 0; i(); + + for (int i = 0; i allAreas; + private static final String KEY_AREA_REFERENCES = "areaReferences"; + private List areaReferences; + + @Override + public void onCreate (Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + areaReferences = new ArrayList<>(); + areaReferences = readAreaReferencesFromPreferences(); + } + /** * Is called when the MapsFragment view is created. Initializes all service connections, the location client and map itself. * @param inflater Calling LayoutInflater @@ -112,6 +136,13 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback { //get tasks allTasks = readTasksFromPreferences(); + //get areas + //instantiate area lists + allAreas = new ArrayList<>(); + + //get saved areas + allAreas = loadAreasFromPreferences(); + //display map fragment SupportMapFragment mapFragment = new SupportMapFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); @@ -163,6 +194,144 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback { super.onPause(); } + private void drawAreas() + { + for (OwnArea area: allAreas) + { + + PolylineOptions options = new PolylineOptions().clickable(true); + + for (LatLng location: area.getLocations()) + { + options.add(location); + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "Position: " + location.toString()); + } + } + + Polyline polyline1 = mMap.addPolyline(options); + + } + } + + /** + * Reads areaReferences saved in preferences as jsonStrings + * + * @return list of AreaReferences + */ + private List readAreaReferencesFromPreferences() { + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "readAreaReferencesFromPreferences()"); + } + + List areas = new ArrayList<>(); + + try { + String jsonString = PreferenceManager.getDefaultSharedPreferences(getContext()).getString(KEY_AREA_REFERENCES, "error"); + if(!"error".equalsIgnoreCase(jsonString)) { + JSONArray jsonObjects = new JSONArray(jsonString); + for (int i = 0; i loadAreasFromPreferences() + { + if (BuildConfig.DEBUG) + { + Log.d(TAG, "loadAreasFromPreferences() reference size: " + areaReferences.size()); + } + + List areas = new ArrayList<>(); + Integer i = 0; + + for (AreaReference reference: areaReferences) + { + String jsonString = readAreaFromExternalStorage(reference.getPath()); + + areas.add(new OwnArea(jsonString)); + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "load area from preferences: " + areas.get(i).toString()); + } + + i++; + } + + return areas; + } + + /** + * Read OwnArea from file in external storage and return as a string. + * + * @param path File path where the OwnArea is saved. + * @return + */ + private String readAreaFromExternalStorage(String path) + { + if (BuildConfig.DEBUG) + { + Log.d(TAG, "readAreaFromExternalStorage()"); + } + + //Get the text file + File file = new File(path); + + //Read text from file + StringBuilder text = new StringBuilder(); + + try { + BufferedReader br = new BufferedReader(new FileReader(file)); + String line; + + while ((line = br.readLine()) != null) { + text.append(line); + } + br.close(); + } + catch (IOException e) { + if (BuildConfig.DEBUG) + { + Log.e(TAG, "Failed read from external storage: " + e.toString()); + } + } + + return text.toString(); + } + @Override public void onResume() { super.onResume(); @@ -330,6 +499,8 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback { mMap.addMarker(marker); } + + drawAreas(); } /** diff --git a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/task/TasksFragment.java b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/task/TasksFragment.java index 18f52ff..b29405a 100644 --- a/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/task/TasksFragment.java +++ b/Code Android/meinWald/app/src/main/java/com/example/meinwald/ui/task/TasksFragment.java @@ -299,6 +299,12 @@ public class TasksFragment extends Fragment { * Saves tasks to preferences */ private void emptyPreferencesAndSaveTasksToPreferences() { + + if (BuildConfig.DEBUG) + { + Log.d(TAG, "emptyPreferencesAndSaveTasksToPreferences()"); + } + if (!allTasks.isEmpty()) { if (BuildConfig.DEBUG) { Log.d(TAG, "allTasks not empty!"); diff --git a/Code Android/meinWald/app/src/main/res/layout/area_element.xml b/Code Android/meinWald/app/src/main/res/layout/area_element.xml index 28a9423..be34a05 100644 --- a/Code Android/meinWald/app/src/main/res/layout/area_element.xml +++ b/Code Android/meinWald/app/src/main/res/layout/area_element.xml @@ -13,23 +13,40 @@ + + + tools:visibility="visible" + android:duplicateParentState="true"> + app:srcCompat="@android:drawable/ic_menu_camera" + android:duplicateParentState="true"/> + app:layout_constraintTop_toTopOf="parent" + android:duplicateParentState="true"/> diff --git a/Code Android/meinWald/app/src/main/res/layout/area_input.xml b/Code Android/meinWald/app/src/main/res/layout/area_input.xml index e0a10eb..e8df1b5 100644 --- a/Code Android/meinWald/app/src/main/res/layout/area_input.xml +++ b/Code Android/meinWald/app/src/main/res/layout/area_input.xml @@ -25,16 +25,16 @@ diff --git a/Code Android/meinWald/app/src/main/res/layout/area_management.xml b/Code Android/meinWald/app/src/main/res/layout/area_management.xml new file mode 100644 index 0000000..7c807b1 --- /dev/null +++ b/Code Android/meinWald/app/src/main/res/layout/area_management.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Code Android/meinWald/app/src/main/res/layout/area_management_notice.xml b/Code Android/meinWald/app/src/main/res/layout/area_management_notice.xml new file mode 100644 index 0000000..e73a7f6 --- /dev/null +++ b/Code Android/meinWald/app/src/main/res/layout/area_management_notice.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Code Android/meinWald/app/src/main/res/layout/fragment_map.xml b/Code Android/meinWald/app/src/main/res/layout/fragment_map.xml index 6c53af3..88388cc 100644 --- a/Code Android/meinWald/app/src/main/res/layout/fragment_map.xml +++ b/Code Android/meinWald/app/src/main/res/layout/fragment_map.xml @@ -8,7 +8,7 @@ app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" - tools:context=".ui.home.MapsFragment"> + tools:context=".ui.map.MapsFragment">