From 5089e8342fae00c0cb9bf6a1adf1c7c5d1e6fb3e Mon Sep 17 00:00:00 2001 From: Muhammad Rifqi Priyo Susanto Date: Sun, 14 Jan 2024 02:01:35 +0700 Subject: [PATCH] Android: Use the correct value for notification (#14209) The notification channel creation is moved into MainActivity. The notification channel ID string is stored into a static variable. The name and description of the notification channel are stored into the strings resource file. Co-authored-by: sfan5 --- .../net/minetest/minetest/MainActivity.java | 34 +++++++++++++++++++ .../net/minetest/minetest/UnzipService.java | 28 ++++----------- android/app/src/main/res/values/strings.xml | 6 ++-- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/android/app/src/main/java/net/minetest/minetest/MainActivity.java b/android/app/src/main/java/net/minetest/minetest/MainActivity.java index d942e93e3..9eaa7316d 100644 --- a/android/app/src/main/java/net/minetest/minetest/MainActivity.java +++ b/android/app/src/main/java/net/minetest/minetest/MainActivity.java @@ -20,23 +20,29 @@ with this program; if not, write to the Free Software Foundation, Inc., package net.minetest.minetest; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; import androidx.appcompat.app.AppCompatActivity; import static net.minetest.minetest.UnzipService.*; public class MainActivity extends AppCompatActivity { + public static final String NOTIFICATION_CHANNEL_ID = "Minetest channel"; + private final static int versionCode = BuildConfig.VERSION_CODE; private static final String SETTINGS = "MinetestSettings"; private static final String TAG_VERSION_CODE = "versionCode"; @@ -81,12 +87,18 @@ public class MainActivity extends AppCompatActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + IntentFilter filter = new IntentFilter(ACTION_UPDATE); registerReceiver(myReceiver, filter); + mProgressBar = findViewById(R.id.progressBar); mTextView = findViewById(R.id.textView); sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE); + checkAppVersion(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + createNotificationChannel(); } private void checkAppVersion() { @@ -114,6 +126,28 @@ public class MainActivity extends AppCompatActivity { startActivity(intent); } + @RequiresApi(Build.VERSION_CODES.O) + private void createNotificationChannel() { + NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notifyManager == null) + return; + + NotificationChannel notifyChannel = new NotificationChannel( + NOTIFICATION_CHANNEL_ID, + getString(R.string.notification_channel_name), + NotificationManager.IMPORTANCE_LOW + ); + notifyChannel.setDescription(getString(R.string.notification_channel_description)); + // Configure the notification channel without sound set + notifyChannel.setSound(null, null); + notifyChannel.enableLights(false); + notifyChannel.enableVibration(false); + + // It is fine to always create the notification channel because creating a channel + // with the same ID is the same as overriding it (only its name and description). + notifyManager.createNotificationChannel(notifyChannel); + } + @Override public void onBackPressed() { // Prevent abrupt interruption when copy game files from assets diff --git a/android/app/src/main/java/net/minetest/minetest/UnzipService.java b/android/app/src/main/java/net/minetest/minetest/UnzipService.java index 8c293fa89..5dccc4563 100644 --- a/android/app/src/main/java/net/minetest/minetest/UnzipService.java +++ b/android/app/src/main/java/net/minetest/minetest/UnzipService.java @@ -22,7 +22,6 @@ package net.minetest.minetest; import android.app.IntentService; import android.app.Notification; -import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -58,9 +57,11 @@ public class UnzipService extends IntentService { private String failureMessage; private static boolean isRunning = false; + public static synchronized boolean getIsRunning() { return isRunning; } + private static synchronized void setIsRunning(boolean v) { isRunning = v; } @@ -99,28 +100,13 @@ public class UnzipService extends IntentService { } } + @NonNull private Notification.Builder createNotification() { - String name = "net.minetest.minetest"; - String channelId = "Minetest channel"; - String description = "notifications from Minetest"; Notification.Builder builder; if (mNotifyManager == null) mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - int importance = NotificationManager.IMPORTANCE_LOW; - NotificationChannel mChannel = null; - if (mNotifyManager != null) - mChannel = mNotifyManager.getNotificationChannel(channelId); - if (mChannel == null) { - mChannel = new NotificationChannel(channelId, name, importance); - mChannel.setDescription(description); - // Configure the notification channel, NO SOUND - mChannel.setSound(null, null); - mChannel.enableLights(false); - mChannel.enableVibration(false); - mNotifyManager.createNotificationChannel(mChannel); - } - builder = new Notification.Builder(this, channelId); + builder = new Notification.Builder(this, MainActivity.NOTIFICATION_CHANNEL_ID); } else { builder = new Notification.Builder(this); } @@ -135,9 +121,9 @@ public class UnzipService extends IntentService { PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, pendingIntentFlag); - builder.setContentTitle(getString(R.string.notification_title)) + builder.setContentTitle(getString(R.string.unzip_notification_title)) .setSmallIcon(R.mipmap.ic_launcher) - .setContentText(getString(R.string.notification_description)) + .setContentText(getString(R.string.unzip_notification_description)) .setContentIntent(intent) .setOngoing(true) .setProgress(0, 0, true); @@ -198,7 +184,7 @@ public class UnzipService extends IntentService { } } - private void publishProgress(@Nullable Notification.Builder notificationBuilder, @StringRes int message, int progress) { + private void publishProgress(@Nullable Notification.Builder notificationBuilder, @StringRes int message, int progress) { Intent intentUpdate = new Intent(ACTION_UPDATE); intentUpdate.putExtra(ACTION_PROGRESS, progress); intentUpdate.putExtra(ACTION_PROGRESS_MESSAGE, message); diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index d8bb7ac68..9eb1d3ca1 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -2,7 +2,9 @@ Minetest Loading… - Loading Minetest - Less than 1 minute… + General notification + Notifications from Minetest + Loading Minetest + Less than 1 minute… Done