Search in sources :

Example 1 with BigTextStyle

use of androidx.core.app.NotificationCompat.BigTextStyle in project Osmand by osmandapp.

the class NavigationNotification method buildNotification.

@Override
public Builder buildNotification(boolean wearable) {
    if (!isEnabled()) {
        return null;
    }
    NavigationService service = app.getNavigationService();
    String notificationTitle;
    StringBuilder notificationText = new StringBuilder();
    color = 0;
    icon = R.drawable.ic_notification_start_navigation;
    Bitmap turnBitmap = null;
    ongoing = true;
    RoutingHelper routingHelper = app.getRoutingHelper();
    if (service != null && (service.getUsedBy() & USED_BY_NAVIGATION) != 0) {
        color = app.getResources().getColor(R.color.osmand_orange);
        String distanceStr = OsmAndFormatter.getFormattedDistance(routingHelper.getLeftDistance(), app);
        String timeStr = OsmAndFormatter.getFormattedDuration(routingHelper.getLeftTime(), app);
        String etaStr = SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(System.currentTimeMillis() + routingHelper.getLeftTime() * 1000L));
        String speedStr = null;
        Location location = getLastKnownLocation();
        if (location != null && location.hasSpeed()) {
            speedStr = OsmAndFormatter.getFormattedSpeed(location.getSpeed(), app);
        }
        TurnType turnType = null;
        boolean deviatedFromRoute;
        int turnImminent = 0;
        int nextTurnDistance = 0;
        int nextNextTurnDistance = 0;
        RouteDirectionInfo ri = null;
        if (routingHelper.isRouteCalculated() && routingHelper.isFollowingMode()) {
            deviatedFromRoute = routingHelper.isDeviatedFromRoute();
            if (deviatedFromRoute) {
                turnImminent = 0;
                turnType = TurnType.valueOf(TurnType.OFFR, leftSide);
                nextTurnDistance = (int) routingHelper.getRouteDeviation();
            } else {
                NextDirectionInfo calc1 = new NextDirectionInfo();
                NextDirectionInfo r = routingHelper.getNextRouteDirectionInfo(calc1, true);
                if (r != null && r.distanceTo > 0 && r.directionInfo != null) {
                    ri = r.directionInfo;
                    turnType = r.directionInfo.getTurnType();
                    nextTurnDistance = r.distanceTo;
                    turnImminent = r.imminent;
                    NextDirectionInfo next = routingHelper.getNextRouteDirectionInfoAfter(r, calc1, true);
                    nextNextTurnDistance = next.distanceTo;
                }
            }
            if (turnType != null) {
                TurnDrawable drawable = new TurnDrawable(app, false);
                int height = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_height);
                int width = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_width);
                drawable.setBounds(0, 0, width, height);
                drawable.setTurnType(turnType);
                drawable.setTurnImminent(turnImminent, deviatedFromRoute);
                turnBitmap = drawableToBitmap(drawable);
            }
            notificationTitle = OsmAndFormatter.getFormattedDistance(nextTurnDistance, app) + (turnType != null ? " • " + RouteCalculationResult.toString(turnType, app, true) : "");
            if (ri != null && !Algorithms.isEmpty(ri.getDescriptionRoutePart())) {
                notificationText.append(ri.getDescriptionRoutePart());
                if (nextNextTurnDistance > 0) {
                    notificationText.append(" ").append(OsmAndFormatter.getFormattedDistance(nextNextTurnDistance, app));
                }
                notificationText.append("\n");
            }
            int distanceToNextIntermediate = routingHelper.getLeftDistanceNextIntermediate();
            if (distanceToNextIntermediate > 0) {
                int nextIntermediateIndex = routingHelper.getRoute().getNextIntermediate();
                List<TargetPoint> intermediatePoints = app.getTargetPointsHelper().getIntermediatePoints();
                if (nextIntermediateIndex < intermediatePoints.size()) {
                    TargetPoint nextIntermediate = intermediatePoints.get(nextIntermediateIndex);
                    notificationText.append(OsmAndFormatter.getFormattedDistance(distanceToNextIntermediate, app)).append(" • ").append(nextIntermediate.getOnlyName());
                    notificationText.append("\n");
                }
            }
            notificationText.append(distanceStr).append(" • ").append(timeStr).append(" • ").append(etaStr);
            if (speedStr != null) {
                notificationText.append(" • ").append(speedStr);
            }
        } else {
            notificationTitle = app.getString(R.string.shared_string_navigation);
            String error = routingHelper.getLastRouteCalcErrorShort();
            if (Algorithms.isEmpty(error)) {
                notificationText.append(app.getString(R.string.route_calculation)).append("...");
            } else {
                notificationText.append(error);
            }
        }
    } else if (routingHelper.isRoutePlanningMode() && routingHelper.isPauseNavigation()) {
        ongoing = false;
        notificationTitle = app.getString(R.string.shared_string_navigation);
        notificationText.append(app.getString(R.string.shared_string_paused));
    } else {
        return null;
    }
    final Builder notificationBuilder = createBuilder(wearable).setContentTitle(notificationTitle).setCategory(NotificationCompat.CATEGORY_NAVIGATION).setStyle(new BigTextStyle().bigText(notificationText)).setLargeIcon(turnBitmap);
    NavigationSession carNavigationSession = app.getCarNavigationSession();
    if (carNavigationSession != null) {
        Intent intent = new Intent(Intent.ACTION_VIEW).setComponent(new ComponentName(app, NavigationCarAppService.class)).setData(NavigationCarAppService.createDeepLinkUri(DEEP_LINK_ACTION_OPEN_ROOT_SCREEN));
        notificationBuilder.extend(new CarAppExtender.Builder().setContentIntent(CarPendingIntent.getCarApp(app, intent.hashCode(), intent, 0)).build());
    }
    Intent stopIntent = new Intent(OSMAND_STOP_NAVIGATION_SERVICE_ACTION);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.addAction(R.drawable.ic_notification_remove, app.getString(R.string.shared_string_control_stop), stopPendingIntent);
    if (routingHelper.isRouteCalculated() && routingHelper.isFollowingMode()) {
        Intent pauseIntent = new Intent(OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION);
        PendingIntent pausePendingIntent = PendingIntent.getBroadcast(app, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.ic_notification_pause, app.getString(R.string.shared_string_pause), pausePendingIntent);
    } else if (routingHelper.isRouteCalculated() && routingHelper.isPauseNavigation()) {
        Intent resumeIntent = new Intent(OSMAND_RESUME_NAVIGATION_SERVICE_ACTION);
        PendingIntent resumePendingIntent = PendingIntent.getBroadcast(app, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.ic_notification_play, app.getString(R.string.shared_string_resume), resumePendingIntent);
    }
    return notificationBuilder;
}
Also used : Builder(androidx.core.app.NotificationCompat.Builder) CarPendingIntent(androidx.car.app.notification.CarPendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RoutingHelper(net.osmand.plus.routing.RoutingHelper) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) TurnType(net.osmand.router.TurnType) Date(java.util.Date) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) NextDirectionInfo(net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo) Bitmap(android.graphics.Bitmap) NavigationService(net.osmand.plus.NavigationService) BigTextStyle(androidx.core.app.NotificationCompat.BigTextStyle) CarAppExtender(androidx.car.app.notification.CarAppExtender) RouteDirectionInfo(net.osmand.plus.routing.RouteDirectionInfo) TurnDrawable(net.osmand.plus.views.mapwidgets.TurnDrawable) ComponentName(android.content.ComponentName) CarPendingIntent(androidx.car.app.notification.CarPendingIntent) PendingIntent(android.app.PendingIntent) NavigationSession(net.osmand.plus.auto.NavigationSession) Location(net.osmand.Location)

Example 2 with BigTextStyle

use of androidx.core.app.NotificationCompat.BigTextStyle in project Osmand by osmandapp.

the class GpxNotification method buildNotification.

@Override
public Builder buildNotification(boolean wearable) {
    if (!isEnabled()) {
        return null;
    }
    String notificationTitle;
    String notificationText;
    color = 0;
    icon = R.drawable.ic_notification_track;
    boolean isGpxRecording = app.getSavingTrackHelper().getIsRecording();
    float recordedDistance = app.getSavingTrackHelper().getDistance();
    ongoing = true;
    lastBuiltNoData = false;
    if (isGpxRecording) {
        color = app.getResources().getColor(R.color.osmand_orange);
        notificationTitle = app.getString(R.string.shared_string_trip) + " • " + Algorithms.formatDuration((int) (app.getSavingTrackHelper().getDuration() / 1000), true) + " • " + OsmAndFormatter.getFormattedDistance(recordedDistance, app);
        notificationText = app.getString(R.string.shared_string_recorded) + ": " + OsmAndFormatter.getFormattedDistance(recordedDistance, app);
    } else {
        if (app.getSavingTrackHelper().getTrkPoints() > 0) {
            notificationTitle = app.getString(R.string.shared_string_paused) + " • " + Algorithms.formatDuration((int) (app.getSavingTrackHelper().getDuration() / 1000), true) + " • " + OsmAndFormatter.getFormattedDistance(recordedDistance, app);
            notificationText = app.getString(R.string.shared_string_recorded) + ": " + OsmAndFormatter.getFormattedDistance(recordedDistance, app);
        } else {
            ongoing = false;
            notificationTitle = app.getString(R.string.shared_string_trip_recording);
            notificationText = app.getString(R.string.gpx_logging_no_data);
            lastBuiltNoData = true;
        }
    }
    notificationText = notificationText + "  (" + Integer.toString(app.getSavingTrackHelper().getTrkPoints()) + ")";
    if ((wasNoDataDismissed || !app.getSettings().SHOW_TRIP_REC_NOTIFICATION.get()) && !ongoing) {
        return null;
    }
    final Builder notificationBuilder = createBuilder(wearable).setContentTitle(notificationTitle).setStyle(new BigTextStyle().bigText(notificationText));
    Intent saveIntent = new Intent(OSMAND_SAVE_GPX_SERVICE_ACTION);
    PendingIntent savePendingIntent = PendingIntent.getBroadcast(app, 0, saveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (isGpxRecording) {
        Intent stopIntent = new Intent(OSMAND_STOP_GPX_SERVICE_ACTION);
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (app.getSavingTrackHelper().getTrkPoints() > 0) {
            notificationBuilder.addAction(R.drawable.ic_notification_pause, app.getString(R.string.shared_string_pause), stopPendingIntent);
            notificationBuilder.addAction(R.drawable.ic_notification_save, app.getString(R.string.shared_string_save), savePendingIntent);
        } else {
            notificationBuilder.addAction(R.drawable.ic_notification_rec_stop, app.getString(R.string.shared_string_control_stop), stopPendingIntent);
        }
    } else {
        Intent startIntent = new Intent(OSMAND_START_GPX_SERVICE_ACTION);
        PendingIntent startPendingIntent = PendingIntent.getBroadcast(app, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (app.getSavingTrackHelper().getTrkPoints() > 0) {
            notificationBuilder.addAction(R.drawable.ic_notification_rec_start, app.getString(R.string.shared_string_resume), startPendingIntent);
            notificationBuilder.addAction(R.drawable.ic_notification_save, app.getString(R.string.shared_string_save), savePendingIntent);
        } else {
            notificationBuilder.addAction(R.drawable.ic_notification_rec_start, app.getString(R.string.shared_string_record), startPendingIntent);
        }
    }
    return notificationBuilder;
}
Also used : BigTextStyle(androidx.core.app.NotificationCompat.BigTextStyle) Builder(androidx.core.app.NotificationCompat.Builder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Aggregations

PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 BigTextStyle (androidx.core.app.NotificationCompat.BigTextStyle)2 Builder (androidx.core.app.NotificationCompat.Builder)2 ComponentName (android.content.ComponentName)1 Bitmap (android.graphics.Bitmap)1 CarAppExtender (androidx.car.app.notification.CarAppExtender)1 CarPendingIntent (androidx.car.app.notification.CarPendingIntent)1 Date (java.util.Date)1 Location (net.osmand.Location)1 NavigationService (net.osmand.plus.NavigationService)1 NavigationSession (net.osmand.plus.auto.NavigationSession)1 TargetPoint (net.osmand.plus.helpers.TargetPointsHelper.TargetPoint)1 NextDirectionInfo (net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo)1 RouteDirectionInfo (net.osmand.plus.routing.RouteDirectionInfo)1 RoutingHelper (net.osmand.plus.routing.RoutingHelper)1 TurnDrawable (net.osmand.plus.views.mapwidgets.TurnDrawable)1 TurnType (net.osmand.router.TurnType)1