Skip to content

Commit

Permalink
Android: Use the correct value for notification (#14209)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
srifqi and sfan5 committed Jan 13, 2024
1 parent b12be04 commit 5089e83
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
34 changes: 34 additions & 0 deletions android/app/src/main/java/net/minetest/minetest/MainActivity.java
Expand Up @@ -20,23 +20,29 @@

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";
Expand Down Expand Up @@ -81,12 +87,18 @@ public void onReceive(Context context, Intent intent) {
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() {
Expand Down Expand Up @@ -114,6 +126,28 @@ private void startNative() {
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
Expand Down
28 changes: 7 additions & 21 deletions android/app/src/main/java/net/minetest/minetest/UnzipService.java
Expand Up @@ -22,7 +22,6 @@

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -99,28 +100,13 @@ protected void onHandleIntent(Intent intent) {
}
}

@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);
}
Expand All @@ -135,9 +121,9 @@ private Notification.Builder createNotification() {
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);
Expand Down Expand Up @@ -198,7 +184,7 @@ boolean recursivelyDeleteDirectory(@NonNull File loc) {
}
}

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);
Expand Down
6 changes: 4 additions & 2 deletions android/app/src/main/res/values/strings.xml
Expand Up @@ -2,7 +2,9 @@
<resources>
<string name="label">Minetest</string>
<string name="loading">Loading&#8230;</string>
<string name="notification_title">Loading Minetest</string>
<string name="notification_description">Less than 1 minute&#8230;</string>
<string name="notification_channel_name">General notification</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>
</resources>

0 comments on commit 5089e83

Please sign in to comment.