Use NotificationUtils to handle TermuxService and RunCommandService notifications

This commit is contained in:
agnostic-apollo
2021-03-25 11:00:00 +05:00
parent 0230698494
commit 4e5d14e4a2
4 changed files with 63 additions and 55 deletions

View File

@@ -1,10 +1,8 @@
package com.termux.app; package com.termux.app;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Binder; import android.os.Binder;
@@ -16,6 +14,7 @@ import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE; import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.app.utils.FileUtils; import com.termux.app.utils.FileUtils;
import com.termux.app.utils.Logger; import com.termux.app.utils.Logger;
import com.termux.app.utils.NotificationUtils;
import com.termux.app.utils.PluginUtils; import com.termux.app.utils.PluginUtils;
import com.termux.app.utils.TextDataUtils; import com.termux.app.utils.TextDataUtils;
import com.termux.models.ExecutionCommand; import com.termux.models.ExecutionCommand;
@@ -157,6 +156,7 @@ import com.termux.models.ExecutionCommand.ExecutionState;
public class RunCommandService extends Service { public class RunCommandService extends Service {
private static final String NOTIFICATION_CHANNEL_ID = "termux_run_command_notification_channel"; private static final String NOTIFICATION_CHANNEL_ID = "termux_run_command_notification_channel";
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " RunCommandService";
public static final int NOTIFICATION_ID = 1338; public static final int NOTIFICATION_ID = 1338;
private static final String LOG_TAG = "RunCommandService"; private static final String LOG_TAG = "RunCommandService";
@@ -313,22 +313,21 @@ public class RunCommandService extends Service {
} }
private Notification buildNotification() { private Notification buildNotification() {
Notification.Builder builder = new Notification.Builder(this); // Build the notification
builder.setContentTitle(TermuxConstants.TERMUX_APP_NAME + " Run Command"); Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
builder.setSmallIcon(R.drawable.ic_service_notification); NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_LOW,
NOTIFICATION_CHANNEL_NAME, null, null,
// Use a low priority: null, NotificationUtils.NOTIFICATION_MODE_SILENT);
builder.setPriority(Notification.PRIORITY_LOW); if(builder == null) return null;
// No need to show a timestamp: // No need to show a timestamp:
builder.setShowWhen(false); builder.setShowWhen(false);
// Background color for small notification icon: // Set notification icon
builder.setColor(0xFF607D8B); builder.setSmallIcon(R.drawable.ic_service_notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Set background color for small notification icon
builder.setChannelId(NOTIFICATION_CHANNEL_ID); builder.setColor(0xFF607D8B);
}
return builder.build(); return builder.build();
} }
@@ -336,12 +335,8 @@ public class RunCommandService extends Service {
private void setupNotificationChannel() { private void setupNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
String channelName = TermuxConstants.TERMUX_APP_NAME + " Run Command"; NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
int importance = NotificationManager.IMPORTANCE_LOW; NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
} }
} }

View File

@@ -729,4 +729,16 @@ public final class TermuxActivity extends Activity implements ServiceConnection
} }
}; };
public static void startTermuxActivity(@NonNull final Context context) {
context.startActivity(newInstance(context));
}
public static Intent newInstance(@NonNull final Context context) {
Intent intent = new Intent(context, TermuxActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
} }

View File

@@ -2,7 +2,6 @@ package com.termux.app;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
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.app.Service; import android.app.Service;
@@ -28,6 +27,7 @@ import com.termux.app.terminal.TermuxSession;
import com.termux.app.terminal.TermuxSessionClient; import com.termux.app.terminal.TermuxSessionClient;
import com.termux.app.terminal.TermuxSessionClientBase; import com.termux.app.terminal.TermuxSessionClientBase;
import com.termux.app.utils.Logger; import com.termux.app.utils.Logger;
import com.termux.app.utils.NotificationUtils;
import com.termux.app.utils.ShellUtils; import com.termux.app.utils.ShellUtils;
import com.termux.app.utils.TextDataUtils; import com.termux.app.utils.TextDataUtils;
import com.termux.models.ExecutionCommand; import com.termux.models.ExecutionCommand;
@@ -58,6 +58,7 @@ import javax.annotation.Nullable;
public final class TermuxService extends Service { public final class TermuxService extends Service {
private static final String NOTIFICATION_CHANNEL_ID = "termux_notification_channel"; private static final String NOTIFICATION_CHANNEL_ID = "termux_notification_channel";
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " App";
public static final int NOTIFICATION_ID = 1337; public static final int NOTIFICATION_ID = 1337;
private static int EXECUTION_ID = 1000; private static int EXECUTION_ID = 1000;
@@ -512,7 +513,7 @@ public final class TermuxService extends Service {
/** Launch the {@link }TermuxActivity} to bring it to foreground. */ /** Launch the {@link }TermuxActivity} to bring it to foreground. */
private void startTermuxActivity() { private void startTermuxActivity() {
startActivity(new Intent(this, TermuxActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); TermuxActivity.startTermuxActivity(this);
} }
@@ -568,69 +569,72 @@ public final class TermuxService extends Service {
private Notification buildNotification() { private Notification buildNotification() {
Intent notifyIntent = new Intent(this, TermuxActivity.class); Resources res = getResources();
// PendingIntent#getActivity(): "Note that the activity will be started outside of the context of an existing
// activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent":
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
// Set pending intent to be launched when notification is clicked
Intent notificationIntent = TermuxActivity.newInstance(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Set notification text
int sessionCount = getTermuxSessionsSize(); int sessionCount = getTermuxSessionsSize();
int taskCount = mTermuxTasks.size(); int taskCount = mTermuxTasks.size();
String contentText = sessionCount + " session" + (sessionCount == 1 ? "" : "s"); String notifiationText = sessionCount + " session" + (sessionCount == 1 ? "" : "s");
if (taskCount > 0) { if (taskCount > 0) {
contentText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s"); notifiationText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s");
} }
final boolean wakeLockHeld = mWakeLock != null; final boolean wakeLockHeld = mWakeLock != null;
if (wakeLockHeld) contentText += " (wake lock held)"; if (wakeLockHeld) notifiationText += " (wake lock held)";
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getText(R.string.application_name));
builder.setContentText(contentText);
builder.setSmallIcon(R.drawable.ic_service_notification);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
// Set notification priority
// If holding a wake or wifi lock consider the notification of high priority since it's using power, // If holding a wake or wifi lock consider the notification of high priority since it's using power,
// otherwise use a low priority // otherwise use a low priority
builder.setPriority((wakeLockHeld) ? Notification.PRIORITY_HIGH : Notification.PRIORITY_LOW); int priority = (wakeLockHeld) ? Notification.PRIORITY_HIGH : Notification.PRIORITY_LOW;
// Build the notification
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
NOTIFICATION_CHANNEL_ID, priority,
getText(R.string.application_name), notifiationText, null,
pendingIntent, NotificationUtils.NOTIFICATION_MODE_SILENT);
if(builder == null) return null;
// No need to show a timestamp: // No need to show a timestamp:
builder.setShowWhen(false); builder.setShowWhen(false);
// Background color for small notification icon: // Set notification icon
builder.setSmallIcon(R.drawable.ic_service_notification);
// Set background color for small notification icon
builder.setColor(0xFF607D8B); builder.setColor(0xFF607D8B);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Termux sessions are always ongoing
builder.setChannelId(NOTIFICATION_CHANNEL_ID); builder.setOngoing(true);
}
Resources res = getResources();
// Set Exit button action
Intent exitIntent = new Intent(this, TermuxService.class).setAction(TERMUX_SERVICE.ACTION_STOP_SERVICE); Intent exitIntent = new Intent(this, TermuxService.class).setAction(TERMUX_SERVICE.ACTION_STOP_SERVICE);
builder.addAction(android.R.drawable.ic_delete, res.getString(R.string.notification_action_exit), PendingIntent.getService(this, 0, exitIntent, 0)); builder.addAction(android.R.drawable.ic_delete, res.getString(R.string.notification_action_exit), PendingIntent.getService(this, 0, exitIntent, 0));
// Set Wakelock button actions
String newWakeAction = wakeLockHeld ? TERMUX_SERVICE.ACTION_WAKE_UNLOCK : TERMUX_SERVICE.ACTION_WAKE_LOCK; String newWakeAction = wakeLockHeld ? TERMUX_SERVICE.ACTION_WAKE_UNLOCK : TERMUX_SERVICE.ACTION_WAKE_LOCK;
Intent toggleWakeLockIntent = new Intent(this, TermuxService.class).setAction(newWakeAction); Intent toggleWakeLockIntent = new Intent(this, TermuxService.class).setAction(newWakeAction);
String actionTitle = res.getString(wakeLockHeld ? String actionTitle = res.getString(wakeLockHeld ? R.string.notification_action_wake_unlock : R.string.notification_action_wake_lock);
R.string.notification_action_wake_unlock :
R.string.notification_action_wake_lock);
int actionIcon = wakeLockHeld ? android.R.drawable.ic_lock_idle_lock : android.R.drawable.ic_lock_lock; int actionIcon = wakeLockHeld ? android.R.drawable.ic_lock_idle_lock : android.R.drawable.ic_lock_lock;
builder.addAction(actionIcon, actionTitle, PendingIntent.getService(this, 0, toggleWakeLockIntent, 0)); builder.addAction(actionIcon, actionTitle, PendingIntent.getService(this, 0, toggleWakeLockIntent, 0));
return builder.build(); return builder.build();
} }
private void setupNotificationChannel() { private void setupNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
String channelName = TermuxConstants.TERMUX_APP_NAME; NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
String channelDescription = "Notifications from " + TermuxConstants.TERMUX_APP_NAME; NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,importance);
channel.setDescription(channelDescription);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
} }
/** Update the shown foreground service notification after making any changes that affect it. */ /** Update the shown foreground service notification after making any changes that affect it. */

View File

@@ -167,13 +167,10 @@ public class ReportActivity extends AppCompatActivity {
public static void startReportActivity(@NonNull final Context context, @NonNull final ReportInfo reportInfo) { public static void startReportActivity(@NonNull final Context context, @NonNull final ReportInfo reportInfo) {
if(context == null) return;
context.startActivity(newInstance(context, reportInfo)); context.startActivity(newInstance(context, reportInfo));
} }
public static Intent newInstance(@NonNull final Context context, @NonNull final ReportInfo reportInfo) { public static Intent newInstance(@NonNull final Context context, @NonNull final ReportInfo reportInfo) {
if(context == null) return null;
Intent intent = new Intent(context, ReportActivity.class); Intent intent = new Intent(context, ReportActivity.class);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putSerializable(EXTRA_REPORT_INFO, reportInfo); bundle.putSerializable(EXTRA_REPORT_INFO, reportInfo);