Skip to content

Commit

Permalink
Make it possible to locate specific device on map
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp0002 committed Oct 14, 2023
1 parent 74cc06d commit 6d52a1e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class DeviceInfoSheet extends BottomSheetDialogFragment implements Device
private TextView deviceIsStopped;
private TextView deviceSpeed;
private TextView deviceDistance;
private Button deviceShowOnMapButton;

public DeviceInfoSheet() {
}
Expand Down Expand Up @@ -95,6 +97,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
deviceIsStopped = view.findViewById(R.id.deviceStopped);
deviceSpeed = view.findViewById(R.id.deviceSpeed);
deviceDistance = view.findViewById(R.id.deviceDistance);
deviceShowOnMapButton = view.findViewById(R.id.deviceShowOnMap);

deviceShowOnMapButton.setVisibility(View.GONE);
deviceShowOnMapButton.setOnClickListener(v -> showDeviceOnMap());
}

private void updateView(Device device) {
Expand All @@ -120,6 +126,7 @@ private void updateView(Device device) {
deviceIsStopped.setText("1".equals(device.isStop) ? R.string.yes : R.string.no);
deviceSpeed.setText(device.speed + " km/h");
deviceDistance.setText(device.distance);
deviceShowOnMapButton.setVisibility(View.VISIBLE);
}

@Override
Expand Down Expand Up @@ -149,4 +156,9 @@ public void onDeviceListUpdate(ArrayList<Device> deviceList) {
}
}
}

public void showDeviceOnMap() {
((MainActivity)getActivity()).showDeviceOnMap(id);
this.dismiss();
}
}
16 changes: 15 additions & 1 deletion app/src/main/java/de/raffaelhahn/xadgps_client/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ private boolean openFragmentById(int itemId) {
}

private void openFragment(Class clazz) {
openFragment(clazz, null);
}

private void openFragment(Class clazz, Bundle args) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.replace(R.id.fragment_container_view, clazz, null)
.replace(R.id.fragment_container_view, clazz, args)
.commit();
}

Expand Down Expand Up @@ -108,5 +112,15 @@ protected void onDestroy() {
}
}

public void showDeviceOnMap(String deviceId) {
Bundle args = new Bundle();
args.putString(TrackingFragment.ARG_PARAM_SHOW_DEVICE_ID, deviceId);

openFragment(TrackingFragment.class, args);
//bottomNavigationView.setSelectedItemId(R.id.item_tracking);
//bottomNavigationView.getMenu().getItem(0).setChecked(true);
bottomNavigationView.getMenu().findItem(R.id.item_tracking).setChecked(true);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@
public class TrackingFragment extends Fragment implements DeviceListService.DeviceListUpdateListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String ARG_PARAM_SHOW_DEVICE_ID = "paramShowDeviceId";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private String paramShowDeviceId;

private MapView map = null;
private MaterialCardView trackingLoadingView = null;
Expand All @@ -62,30 +60,11 @@ public TrackingFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment TrackingFragment.
*/
// TODO: Rename and change types and number of parameters
public static TrackingFragment newInstance(String param1, String param2) {
TrackingFragment fragment = new TrackingFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
paramShowDeviceId = getArguments().getString(ARG_PARAM_SHOW_DEVICE_ID);
}
}

Expand Down Expand Up @@ -126,8 +105,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
map.setFlingEnabled(true);
map.setVerticalMapRepetitionEnabled(false);
map.setScrollableAreaLimitLatitude(MapView.getTileSystem().getMaxLatitude(), MapView.getTileSystem().getMinLatitude(), 0);

if(preferences.contains("myLatestLat") && preferences.contains("myLatestLon")){
if(preferences.contains("myLatestLat") && preferences.contains("myLatestLon") && paramShowDeviceId == null){
mapController.setZoom(20.0);
mapController.setCenter(new GeoPoint(preferences.getFloat("myLatestLat", 0), preferences.getFloat("myLatestLon", 0)));
}
Expand All @@ -138,8 +116,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mLocationOverlay.runOnFirstFix(() -> {
if(getActivity() != null) {
getActivity().runOnUiThread(() -> {
mapController.setZoom(20.0);
mapController.setCenter(mLocationOverlay.getMyLocation());
if(paramShowDeviceId == null) {
mapController.setZoom(20.0);
mapController.setCenter(mLocationOverlay.getMyLocation());
}
editor.putFloat("myLatestLat", (float) mLocationOverlay.getMyLocation().getLatitude());
editor.putFloat("myLatestLon", (float) mLocationOverlay.getMyLocation().getLongitude());
editor.apply();
Expand Down Expand Up @@ -225,6 +205,11 @@ public void onDeviceListUpdate(ArrayList<Device> deviceList) {
//marker.setIcon(getResources().getDrawable(R.drawable.router));
map.getOverlays().add(marker);
map.invalidate();
if(paramShowDeviceId != null && paramShowDeviceId.equals(device.id)) {
IMapController mapController = map.getController();
mapController.setZoom(20.0);
mapController.setCenter(new GeoPoint(lat, lon));
}
}
});
}
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/res/layout/device_info_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="TextView"
android:text="DeviceName"
android:textAppearance="?attr/textAppearanceHeadlineLarge"
android:textColor="?attr/colorOnSurface" />

Expand All @@ -38,7 +38,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:text="DeviceGroup"
android:textAppearance="?attr/textAppearanceHeadlineSmall"
android:textColor="?attr/colorOnSurface" />

Expand Down Expand Up @@ -272,6 +272,18 @@

</TableLayout>

<com.google.android.material.button.MaterialButton
style="@style/Widget.Material3.Button.Icon"
android:id="@+id/deviceShowOnMap"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
app:icon="@drawable/location"
android:text="@string/action_show_on_map"
/>

</LinearLayout>

</androidx.core.widget.NestedScrollView>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<string name="device_parked">Geparkt</string>
<string name="device_moving">Fährt</string>

<string name="action_show_on_map">Auf Karte anzeigen</string>

<string name="tracking_loading_header">Tracking lädt...</string>
<string name="tracking_loading_body">Die Position deiner Tracker wird ermittelt.</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<string name="device_parked">Parked</string>
<string name="device_moving">Moving</string>

<string name="action_show_on_map">Show on map</string>

<string name="tracking_loading_header">Loading tracking...</string>
<string name="tracking_loading_body">The location of your trackers is being determined.</string>
</resources>

0 comments on commit 6d52a1e

Please sign in to comment.