[AA] Adjusted AA - NOTE: Not fully working still

This commit is contained in:
Alexander Dörflinger
2025-04-08 12:38:07 +02:00
parent 0d0a3f3883
commit 2a02a22001
4 changed files with 92 additions and 29 deletions

View File

@@ -1,20 +1,25 @@
package com.aldo.apps.familyhelpers.auto; package com.aldo.apps.familyhelpers.auto;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.car.app.CarAppService; import androidx.car.app.CarAppService;
import androidx.car.app.Session; import androidx.car.app.Session;
import androidx.car.app.validation.HostValidator; import androidx.car.app.validation.HostValidator;
public class ShareLocationCarAppService extends CarAppService { public class ShareLocationCarAppService extends CarAppService {
private static final String TAG = "ShareLocationCarAppServ";
@NonNull @NonNull
@Override @Override
public HostValidator createHostValidator() { public HostValidator createHostValidator() {
Log.d(TAG, "createHostValidator() called");
return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR; return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR;
} }
@NonNull @NonNull
@Override @Override
public Session onCreateSession() { public Session onCreateSession() {
Log.d(TAG, "onCreateSession: ");
return new ShareLocationSession(); return new ShareLocationSession();
} }
} }

View File

@@ -1,5 +1,10 @@
package com.aldo.apps.familyhelpers.auto; package com.aldo.apps.familyhelpers.auto;
import static androidx.core.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION;
import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
@@ -10,11 +15,15 @@ import androidx.car.app.CarContext;
import androidx.car.app.CarToast; import androidx.car.app.CarToast;
import androidx.car.app.Screen; import androidx.car.app.Screen;
import androidx.car.app.annotations.ExperimentalCarApi; import androidx.car.app.annotations.ExperimentalCarApi;
import androidx.car.app.model.Action;
import androidx.car.app.model.CarIcon; import androidx.car.app.model.CarIcon;
import androidx.car.app.model.GridTemplate; import androidx.car.app.model.GridItem;
import androidx.car.app.model.OnClickListener; import androidx.car.app.model.ItemList;
import androidx.car.app.model.ListTemplate;
import androidx.car.app.model.Template; import androidx.car.app.model.Template;
import androidx.car.app.notification.CarAppExtender;
import androidx.car.app.notification.CarNotificationManager;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.graphics.drawable.IconCompat; import androidx.core.graphics.drawable.IconCompat;
import com.aldo.apps.familyhelpers.R; import com.aldo.apps.familyhelpers.R;
@@ -23,6 +32,10 @@ import com.aldo.apps.familyhelpers.workers.ShareLocationBackgroundWorker;
public class ShareLocationScreen extends Screen { public class ShareLocationScreen extends Screen {
private static final String AA_NOTIFICATION_CHANNEL = "ShareLocation_AA";
private static final int AA_NOTIFICATION_ID = 1;
private static final String TAG = "ShareLocationScreen"; private static final String TAG = "ShareLocationScreen";
private LocationHelper mLocationHelper; private LocationHelper mLocationHelper;
@@ -30,6 +43,7 @@ public class ShareLocationScreen extends Screen {
// Constructor for ShareLocationScreen // Constructor for ShareLocationScreen
protected ShareLocationScreen(@NonNull CarContext carContext) { protected ShareLocationScreen(@NonNull CarContext carContext) {
super(carContext); super(carContext);
Log.d(TAG, "ShareLocationScreen() called with: carContext = [" + carContext + "]");
mLocationHelper = LocationHelper.getInstance(getCarContext()); mLocationHelper = LocationHelper.getInstance(getCarContext());
} }
@@ -38,36 +52,72 @@ public class ShareLocationScreen extends Screen {
@NonNull @NonNull
@Override @Override
public Template onGetTemplate() { public Template onGetTemplate() {
final Action startSharing = createGridItem( Log.d(TAG, "onGetTemplate() called");
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() final GridItem startItem = new GridItem.Builder()
.setTitle(getString(R.string.android_auto_share_location_start))
.setText(getString(R.string.android_auto_share_location_start_desc))
.setImage(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_start_sharing))
.build(), GridItem.IMAGE_TYPE_LARGE)
.setOnClickListener(this::startLocationShareService)
.build();
final GridItem stopItem = new GridItem.Builder()
.setTitle(getString(R.string.android_auto_share_location_stop))
.setText(getString(R.string.android_auto_share_location_stop_desc))
.setImage(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_stop_sharing))
.build(), GridItem.IMAGE_TYPE_LARGE)
.setOnClickListener(this::stopLocationSharingService)
.build();
final ItemList itemList = new ItemList.Builder()
.setNoItemsMessage("No Items yet, something went wrong I guess")
.addItem(startItem)
.addItem(stopItem)
.build();
mLocationHelper.getSharingStateSubject().subscribe(this::createNotification, this::handleError);
return new ListTemplate.Builder()
.setLoading(false)
.setTitle("Share Location") .setTitle("Share Location")
.addAction(startSharing) .setSingleList(itemList)
.addAction(stopSharing)
.setHeaderAction(Action.BACK)
.build(); .build();
} }
private Action createGridItem(final String title, private void createNotification(final Boolean isSharing) {
@DrawableRes final int icon, Log.d(TAG, "createNotification() called with: isSharing = [" + isSharing + "]");
final OnClickListener listener) { CarNotificationManager notificationManager = CarNotificationManager.from(getCarContext());
return new Action.Builder() notificationManager.createNotificationChannel(
.setTitle(title) new NotificationChannelCompat.Builder(AA_NOTIFICATION_CHANNEL, IMPORTANCE_DEFAULT)
.setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), icon)) .build());
.build()) final PendingIntent serviceIntent;
.setOnClickListener(listener) @DrawableRes final int iconId;
.build(); final String title;
} if (isSharing) {
// Create an intent to start the ShareLocationBackgroundWorker service
final Intent stopService = new Intent(getCarContext(), ShareLocationBackgroundWorker.class);
stopService.setAction(SHARE_LOCATION_CANCEL_ACTION);
serviceIntent = PendingIntent.getService(getCarContext(), 0, stopService, PendingIntent.FLAG_IMMUTABLE);
title = getString(R.string.android_auto_share_location_stop);
iconId = R.drawable.ic_stop_sharing;
} else {
// Create an intent to start the ShareLocationBackgroundWorker service
final Intent startServiceIntent = new Intent(getCarContext(), ShareLocationBackgroundWorker.class);
serviceIntent = PendingIntent.getService(getCarContext(), 0, startServiceIntent, PendingIntent.FLAG_IMMUTABLE);
title = getString(R.string.android_auto_share_location_start);
iconId = R.drawable.ic_start_sharing;
}
final NotificationCompat.Builder carNotification = new NotificationCompat.Builder(getCarContext(), AA_NOTIFICATION_CHANNEL)
.setContentTitle("Share Location")
.addAction(iconId, title, serviceIntent)
.extend(new CarAppExtender.Builder()
.build());
notificationManager.notify(AA_NOTIFICATION_ID, carNotification);
}
private void startLocationShareService() { private void startLocationShareService() {
Log.d(TAG, "startLocationShareService: "); Log.d(TAG, "startLocationShareService: ");
if (mLocationHelper != null && mLocationHelper.isCurrentlySharing()) { if (mLocationHelper != null && mLocationHelper.isCurrentlySharing()) {
@@ -95,4 +145,8 @@ public class ShareLocationScreen extends Screen {
private String getString(@StringRes final int stringId) { private String getString(@StringRes final int stringId) {
return getCarContext().getString(stringId); return getCarContext().getString(stringId);
} }
private void handleError(final Throwable error) {
Log.e(TAG, "handleErrot: ", error);
}
} }

View File

@@ -1,15 +1,19 @@
package com.aldo.apps.familyhelpers.auto; package com.aldo.apps.familyhelpers.auto;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.car.app.Screen;
import androidx.car.app.Session; import androidx.car.app.Session;
public class ShareLocationSession extends Session { public class ShareLocationSession extends Session {
private static final String TAG = "ShareLocationSession";
@NonNull @NonNull
@Override @Override
public androidx.car.app.Screen onCreateScreen(@NonNull Intent intent) { public Screen onCreateScreen(@NonNull Intent intent) {
Log.d(TAG, "onCreateScreen() called with: intent = [" + intent + "]");
return new ShareLocationScreen(getCarContext()); return new ShareLocationScreen(getCarContext());
} }
} }

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<automotiveApp> <automotiveApp>
<uses name="template" /> <uses name="notification" />
</automotiveApp> </automotiveApp>