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 <sfan5@live.de>
This commit is contained in:
Muhammad Rifqi Priyo Susanto 2024-01-14 02:01:35 +07:00 committed by grorp
parent 81e40a1172
commit 00f6bd0f08
3 changed files with 45 additions and 23 deletions

@ -20,23 +20,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
package net.minetest.minetest; package net.minetest.minetest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import static net.minetest.minetest.UnzipService.*; import static net.minetest.minetest.UnzipService.*;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "Minetest channel";
private final static int versionCode = BuildConfig.VERSION_CODE; private final static int versionCode = BuildConfig.VERSION_CODE;
private static final String SETTINGS = "MinetestSettings"; private static final String SETTINGS = "MinetestSettings";
private static final String TAG_VERSION_CODE = "versionCode"; private static final String TAG_VERSION_CODE = "versionCode";
@ -81,12 +87,18 @@ public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ACTION_UPDATE); IntentFilter filter = new IntentFilter(ACTION_UPDATE);
registerReceiver(myReceiver, filter); registerReceiver(myReceiver, filter);
mProgressBar = findViewById(R.id.progressBar); mProgressBar = findViewById(R.id.progressBar);
mTextView = findViewById(R.id.textView); mTextView = findViewById(R.id.textView);
sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE); sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
checkAppVersion(); checkAppVersion();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createNotificationChannel();
} }
private void checkAppVersion() { private void checkAppVersion() {
@ -114,6 +126,28 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent); 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 @Override
public void onBackPressed() { public void onBackPressed() {
// Prevent abrupt interruption when copy game files from assets // Prevent abrupt interruption when copy game files from assets

@ -22,7 +22,6 @@ package net.minetest.minetest;
import android.app.IntentService; import android.app.IntentService;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
@ -58,9 +57,11 @@ public class UnzipService extends IntentService {
private String failureMessage; private String failureMessage;
private static boolean isRunning = false; private static boolean isRunning = false;
public static synchronized boolean getIsRunning() { public static synchronized boolean getIsRunning() {
return isRunning; return isRunning;
} }
private static synchronized void setIsRunning(boolean v) { private static synchronized void setIsRunning(boolean v) {
isRunning = v; isRunning = v;
} }
@ -99,28 +100,13 @@ public class UnzipService extends IntentService {
} }
} }
@NonNull
private Notification.Builder createNotification() { private Notification.Builder createNotification() {
String name = "net.minetest.minetest";
String channelId = "Minetest channel";
String description = "notifications from Minetest";
Notification.Builder builder; Notification.Builder builder;
if (mNotifyManager == null) if (mNotifyManager == null)
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW; builder = new Notification.Builder(this, MainActivity.NOTIFICATION_CHANNEL_ID);
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);
} else { } else {
builder = new Notification.Builder(this); builder = new Notification.Builder(this);
} }
@ -135,9 +121,9 @@ public class UnzipService extends IntentService {
PendingIntent intent = PendingIntent.getActivity(this, 0, PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, pendingIntentFlag); notificationIntent, pendingIntentFlag);
builder.setContentTitle(getString(R.string.notification_title)) builder.setContentTitle(getString(R.string.unzip_notification_title))
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentText(getString(R.string.notification_description)) .setContentText(getString(R.string.unzip_notification_description))
.setContentIntent(intent) .setContentIntent(intent)
.setOngoing(true) .setOngoing(true)
.setProgress(0, 0, true); .setProgress(0, 0, true);

@ -2,7 +2,9 @@
<resources> <resources>
<string name="label">Minetest</string> <string name="label">Minetest</string>
<string name="loading">Loading&#8230;</string> <string name="loading">Loading&#8230;</string>
<string name="notification_title">Loading Minetest</string> <string name="notification_channel_name">General notification</string>
<string name="notification_description">Less than 1 minute&#8230;</string> <string name="notification_channel_description">Notifications from Minetest</string>
<string name="unzip_notification_title">Loading Minetest</string>
<string name="unzip_notification_description">Less than 1 minute&#8230;</string>
<string name="ime_dialog_done">Done</string> <string name="ime_dialog_done">Done</string>
</resources> </resources>