diff --git a/app/build.gradle b/app/build.gradle
index 8422032..77431a5 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -55,8 +55,8 @@ dependencies {
implementation platform('com.google.firebase:firebase-bom:33.10.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
- implementation("com.google.firebase:firebase-firestore")
- implementation("com.google.firebase:firebase-crashlytics")
+ implementation 'com.google.firebase:firebase-firestore'
+ implementation 'com.google.firebase:firebase-crashlytics'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
diff --git a/app/src/main/java/com/aldo/apps/familyhelpers/auto/ShareLocationScreen.java b/app/src/main/java/com/aldo/apps/familyhelpers/auto/ShareLocationScreen.java
index 187aa8d..f0c15a4 100644
--- a/app/src/main/java/com/aldo/apps/familyhelpers/auto/ShareLocationScreen.java
+++ b/app/src/main/java/com/aldo/apps/familyhelpers/auto/ShareLocationScreen.java
@@ -3,50 +3,96 @@ package com.aldo.apps.familyhelpers.auto;
import android.content.Intent;
import android.util.Log;
+import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
+import androidx.annotation.StringRes;
import androidx.car.app.CarContext;
+import androidx.car.app.CarToast;
import androidx.car.app.Screen;
+import androidx.car.app.annotations.ExperimentalCarApi;
+import androidx.car.app.model.Action;
import androidx.car.app.model.CarIcon;
import androidx.car.app.model.GridTemplate;
-import androidx.car.app.model.ItemList;
-import androidx.car.app.model.Row;
+import androidx.car.app.model.OnClickListener;
import androidx.car.app.model.Template;
import androidx.core.graphics.drawable.IconCompat;
import com.aldo.apps.familyhelpers.R;
+import com.aldo.apps.familyhelpers.workers.LocationHelper;
import com.aldo.apps.familyhelpers.workers.ShareLocationBackgroundWorker;
public class ShareLocationScreen extends Screen {
+ private static final String TAG = "ShareLocationScreen";
+
+ private LocationHelper mLocationHelper;
+
// Constructor for ShareLocationScreen
protected ShareLocationScreen(@NonNull CarContext carContext) {
super(carContext);
+ mLocationHelper = LocationHelper.getInstance(getCarContext());
}
+ @ExperimentalCarApi
@NonNull
@Override
public Template onGetTemplate() {
- // Create an ItemList to hold the rows
- final ItemList itemList = new ItemList.Builder()
- .addItem(new Row.Builder()
- .setTitle("Start Sharing Location") // Set the title of the row
- .setOnClickListener(() -> {
- // Create an intent to start the ShareLocationBackgroundWorker service
- final Intent startServiceIntent = new Intent(getCarContext(), ShareLocationBackgroundWorker.class);
- // Start the service using the car context
- getCarContext().startForegroundService(startServiceIntent);
- Log.d("ShareLocationScreen", "Starting ShareLocationBackgroundWorker Service");
- })
- .setImage(new CarIcon.Builder(
- IconCompat.createWithResource(getCarContext(),
- R.drawable.ic_location_helper)).build())
- .build())
- .build();
- // Return a GridTemplate with the ItemList
+ final Action startSharing = createGridItem(
+ getString(R.string.android_auto_share_location_start),
+ R.drawable.ic_start_sharing,
+ this::startLocationShareService
+ );
+ final Action stopSharing = createGridItem(
+ getString(R.string.android_auto_share_location_stop),
+ R.drawable.ic_stop_sharing,
+ this::stopLocationSharingService
+ );
+
return new GridTemplate.Builder()
- .setSingleList(itemList)
.setTitle("Share Location")
+ .addAction(startSharing)
+ .addAction(stopSharing)
+ .setHeaderAction(Action.BACK)
.build();
}
+
+ private Action createGridItem(final String title,
+ @DrawableRes final int icon,
+ final OnClickListener listener) {
+ return new Action.Builder()
+ .setTitle(title)
+ .setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), icon))
+ .build())
+ .setOnClickListener(listener)
+ .build();
+ }
+
+ private void startLocationShareService() {
+ Log.d(TAG, "startLocationShareService: ");
+ if (mLocationHelper != null && mLocationHelper.isCurrentlySharing()) {
+ CarToast.makeText(getCarContext(), "You already are sharing", CarToast.LENGTH_SHORT).show();
+ return;
+ }
+ // Create an intent to start the ShareLocationBackgroundWorker service
+ final Intent startServiceIntent = new Intent(getCarContext(), ShareLocationBackgroundWorker.class);
+ // Start the service using the car context
+ getCarContext().startForegroundService(startServiceIntent);
+ }
+
+ private void stopLocationSharingService() {
+ Log.d(TAG, "stopLocationSharingService: ");
+ if (mLocationHelper != null && !mLocationHelper.isCurrentlySharing()) {
+ CarToast.makeText(getCarContext(), "You are not sharing", CarToast.LENGTH_SHORT).show();
+ return;
+ }
+ // Create an intent to start the ShareLocationBackgroundWorker service
+ final Intent startServiceIntent = new Intent(getCarContext(), ShareLocationBackgroundWorker.class);
+ // Start the service using the car context
+ getCarContext().stopService(startServiceIntent);
+ }
+
+ private String getString(@StringRes final int stringId) {
+ return getCarContext().getString(stringId);
+ }
}
diff --git a/app/src/main/res/drawable/ic_start_sharing.xml b/app/src/main/res/drawable/ic_start_sharing.xml
new file mode 100644
index 0000000..b1e9e5d
--- /dev/null
+++ b/app/src/main/res/drawable/ic_start_sharing.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
diff --git a/app/src/main/res/drawable/ic_stop_sharing.xml b/app/src/main/res/drawable/ic_stop_sharing.xml
new file mode 100644
index 0000000..fc1c269
--- /dev/null
+++ b/app/src/main/res/drawable/ic_stop_sharing.xml
@@ -0,0 +1,8 @@
+
+
+
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index df2fb58..f397c8d 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -73,5 +73,9 @@
Android Auto start has been detected, do you wanna share your location?
Yes, start to share
Yes, stop sharing
+ Start
+ Start to share location
+ Stop
+ Stop to share the location
\ No newline at end of file
diff --git a/app/src/main/res/xml/automotive_app_desc.xml b/app/src/main/res/xml/automotive_app_desc.xml
index 66dd335..0fb852c 100644
--- a/app/src/main/res/xml/automotive_app_desc.xml
+++ b/app/src/main/res/xml/automotive_app_desc.xml
@@ -1,4 +1,4 @@
-
+