|
|
|
|
@@ -1,5 +1,10 @@
|
|
|
|
|
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.util.Log;
|
|
|
|
|
|
|
|
|
|
@@ -10,11 +15,15 @@ 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.OnClickListener;
|
|
|
|
|
import androidx.car.app.model.GridItem;
|
|
|
|
|
import androidx.car.app.model.ItemList;
|
|
|
|
|
import androidx.car.app.model.ListTemplate;
|
|
|
|
|
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 com.aldo.apps.familyhelpers.R;
|
|
|
|
|
@@ -23,6 +32,10 @@ import com.aldo.apps.familyhelpers.workers.ShareLocationBackgroundWorker;
|
|
|
|
|
|
|
|
|
|
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 LocationHelper mLocationHelper;
|
|
|
|
|
@@ -30,6 +43,7 @@ public class ShareLocationScreen extends Screen {
|
|
|
|
|
// Constructor for ShareLocationScreen
|
|
|
|
|
protected ShareLocationScreen(@NonNull CarContext carContext) {
|
|
|
|
|
super(carContext);
|
|
|
|
|
Log.d(TAG, "ShareLocationScreen() called with: carContext = [" + carContext + "]");
|
|
|
|
|
mLocationHelper = LocationHelper.getInstance(getCarContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -38,36 +52,72 @@ public class ShareLocationScreen extends Screen {
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public Template onGetTemplate() {
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
Log.d(TAG, "onGetTemplate() called");
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
.addAction(startSharing)
|
|
|
|
|
.addAction(stopSharing)
|
|
|
|
|
.setHeaderAction(Action.BACK)
|
|
|
|
|
.setSingleList(itemList)
|
|
|
|
|
.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 createNotification(final Boolean isSharing) {
|
|
|
|
|
Log.d(TAG, "createNotification() called with: isSharing = [" + isSharing + "]");
|
|
|
|
|
CarNotificationManager notificationManager = CarNotificationManager.from(getCarContext());
|
|
|
|
|
notificationManager.createNotificationChannel(
|
|
|
|
|
new NotificationChannelCompat.Builder(AA_NOTIFICATION_CHANNEL, IMPORTANCE_DEFAULT)
|
|
|
|
|
.build());
|
|
|
|
|
final PendingIntent serviceIntent;
|
|
|
|
|
@DrawableRes final int iconId;
|
|
|
|
|
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() {
|
|
|
|
|
Log.d(TAG, "startLocationShareService: ");
|
|
|
|
|
if (mLocationHelper != null && mLocationHelper.isCurrentlySharing()) {
|
|
|
|
|
@@ -95,4 +145,8 @@ public class ShareLocationScreen extends Screen {
|
|
|
|
|
private String getString(@StringRes final int stringId) {
|
|
|
|
|
return getCarContext().getString(stringId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleError(final Throwable error) {
|
|
|
|
|
Log.e(TAG, "handleErrot: ", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|