[android] setSound(null, null) for the Downloader notification channel.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako
2025-08-09 17:22:21 -03:00
committed by Konstantin Pastbin
parent 0f5125c61c
commit c6040d8ce6
2 changed files with 31 additions and 22 deletions

View File

@@ -20,6 +20,7 @@ import app.organicmaps.R;
import app.organicmaps.sdk.downloader.MapManager; import app.organicmaps.sdk.downloader.MapManager;
import app.organicmaps.sdk.util.StringUtils; import app.organicmaps.sdk.util.StringUtils;
import app.organicmaps.sdk.util.log.Logger; import app.organicmaps.sdk.util.log.Logger;
import java.util.Objects;
public class DownloaderNotifier public class DownloaderNotifier
{ {
@@ -29,7 +30,8 @@ public class DownloaderNotifier
public static final int NOTIFICATION_ID = 1; public static final int NOTIFICATION_ID = 1;
private final Context mContext; private final Context mContext;
private NotificationCompat.Builder mProgressNotificationBuilder; private NotificationCompat.Builder mProgressNotificationBuilder = null;
private String mNotificationCountryId = null;
public DownloaderNotifier(Context context) public DownloaderNotifier(Context context)
{ {
@@ -45,6 +47,7 @@ public class DownloaderNotifier
.setShowBadge(true) .setShowBadge(true)
.setVibrationEnabled(false) .setVibrationEnabled(false)
.setLightsEnabled(false) .setLightsEnabled(false)
.setSound(null, null)
.build(); .build();
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
} }
@@ -62,8 +65,6 @@ public class DownloaderNotifier
final String countryName = MapManager.nativeGetName(countryId); final String countryName = MapManager.nativeGetName(countryId);
final String content = mContext.getString(R.string.download_country_failed, countryName); final String content = mContext.getString(R.string.download_country_failed, countryName);
var contentPendingIntent = getNotificationPendingIntent(countryId);
final Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID) final Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
.setAutoCancel(true) .setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_ERROR) .setCategory(NotificationCompat.CATEGORY_ERROR)
@@ -74,7 +75,7 @@ public class DownloaderNotifier
.setContentText(content) .setContentText(content)
.setShowWhen(true) .setShowWhen(true)
.setTicker(getTicker(mContext, title, content)) .setTicker(getTicker(mContext, title, content))
.setContentIntent(contentPendingIntent) .setContentIntent(getNotificationPendingIntent(countryId))
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.build(); .build();
@@ -110,32 +111,41 @@ public class DownloaderNotifier
@NonNull @NonNull
public Notification buildProgressNotification(@Nullable String countryId, int maxProgress, int progress) public Notification buildProgressNotification(@Nullable String countryId, int maxProgress, int progress)
{ {
var builder = startNotification(countryId); var builder = getNotificationBuilder(countryId);
/// @todo Doesn't work properly .. Bad input sizes?
builder.setProgress(maxProgress, progress, maxProgress == 0); // builder.setProgress(maxProgress, progress, maxProgress == 0);
builder.setContentText("Download in progress"); builder.setProgress(maxProgress, progress, true);
return builder.build(); return builder.build();
} }
@NonNull @NonNull
private NotificationCompat.Builder startNotification(@Nullable String countryId) private NotificationCompat.Builder getNotificationBuilder(@Nullable String countryId)
{ {
final String title = mContext.getString(R.string.app_name); if (mProgressNotificationBuilder == null || !Objects.equals(countryId, mNotificationCountryId))
{
mNotificationCountryId = countryId;
final String countryName = countryId != null ? MapManager.nativeGetName(countryId) : "";
return new NotificationCompat.Builder(mContext, CHANNEL_ID) mProgressNotificationBuilder =
.setAutoCancel(true) new NotificationCompat.Builder(mContext, CHANNEL_ID)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setAutoCancel(true)
.setSmallIcon(R.drawable.ic_logo_small) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setColor(ContextCompat.getColor(mContext, R.color.notification)) .setSmallIcon(R.drawable.ic_logo_small)
.setShowWhen(true) .setColor(ContextCompat.getColor(mContext, R.color.notification))
.setContentTitle(title) .setShowWhen(true)
.setContentIntent(getNotificationPendingIntent(countryId)); .setContentTitle(mContext.getString(R.string.app_name))
.setContentIntent(getNotificationPendingIntent(countryId))
.setContentText(mContext.getString(R.string.downloader_downloading) + " " + countryName)
.setSound(null);
}
return mProgressNotificationBuilder;
} }
@NonNull @NonNull
private PendingIntent getNotificationPendingIntent(@Nullable String countryId) private PendingIntent getNotificationPendingIntent(@Nullable String countryId)
{ {
/// @todo Zooming to the countryId when tapping on the notification?
/// Shows very low zoom level, need z=9/10, I suppose ...
final int FLAG_IMMUTABLE = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PendingIntent.FLAG_IMMUTABLE; final int FLAG_IMMUTABLE = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PendingIntent.FLAG_IMMUTABLE;
final Intent contentIntent = MwmActivity.createShowMapIntent(mContext, countryId); final Intent contentIntent = MwmActivity.createShowMapIntent(mContext, countryId);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

View File

@@ -97,7 +97,7 @@ public class DownloaderService extends Service implements MapManager.StorageCall
} }
@Override @Override
public void onProgress(String countryId, long localSize, long remoteSize) public void onProgress(String countryId, long bytesDownloaded, long bytesTotal)
{ {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
&& ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PERMISSION_GRANTED) && ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PERMISSION_GRANTED)
@@ -106,8 +106,7 @@ public class DownloaderService extends Service implements MapManager.StorageCall
return; return;
} }
// TODO: How to calculate progress? mNotifier.notifyProgress(countryId, (int) bytesTotal, (int) bytesDownloaded);
mNotifier.notifyProgress();
} }
@Override @Override