Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotificationAPI: Dark mode support for icons #535

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions app/src/main/java/com/termux/api/apis/NotificationAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.RemoteInput;
import androidx.core.graphics.drawable.IconCompat;
import androidx.core.util.Pair;

import com.termux.api.R;
Expand All @@ -27,6 +28,9 @@
import com.termux.shared.shell.command.ExecutionCommand;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.shared.termux.theme.TermuxThemeUtils;
import com.termux.shared.theme.NightMode;
import com.termux.shared.theme.ThemeUtils;

import java.io.File;
import java.io.PrintWriter;
Expand Down Expand Up @@ -174,7 +178,7 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context
int ledOffMs = intent.getIntExtra("led-off", 800);

long[] vibratePattern = intent.getLongArrayExtra("vibrate");
boolean useSound = intent.getBooleanExtra("sound", false);
boolean silent = intent.getBooleanExtra("silent", false);
boolean ongoing = intent.getBooleanExtra("ongoing", false);
boolean alertOnce = intent.getBooleanExtra("alert-once", false);

Expand Down Expand Up @@ -202,17 +206,28 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context

String SmallIcon = intent.getStringExtra("icon");

// When dark mode is enabled, assume the notification icon is shown on a dark background
// and color it white for better visibility
TermuxThemeUtils.setAppNightMode(context);
boolean shouldUseWhiteIcon = ThemeUtils.shouldEnableDarkTheme(context, NightMode.getAppNightMode().getName());

if (SmallIcon != null) {
final Class<?> clz = R.drawable.class;
final Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
if (name.equals("ic_" + SmallIcon + "_black_24dp")) {
try {
notification.setSmallIcon(field.getInt(clz));
} catch (Exception e) {
break;
}
int id = field.getInt(clz);
if (shouldUseWhiteIcon) {
IconCompat icon = IconCompat.createWithResource(context, id);
icon.setTint(0xFFFFFFFF);
notification.setSmallIcon(icon);
} else {
notification.setSmallIcon(id);
}
} catch (Exception ignored) {}
break;
}
}
}
Expand Down Expand Up @@ -275,7 +290,7 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context
notification.setVibrate(vibrateArg);
}

if (useSound) notification.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
if (silent) notification.setSilent(true);

notification.setAutoCancel(true);

Expand Down Expand Up @@ -421,6 +436,6 @@ static Intent createExecuteIntent(String action){

static PendingIntent createAction(final Context context, String action){
Intent executeIntent = createExecuteIntent(action);
return PendingIntent.getService(context, 0, executeIntent, 0);
return PendingIntent.getService(context, UUID.randomUUID().hashCode(), executeIntent, 0);
}
}