Skip to content

Commit 11fb650

Browse files
committed
Use notification channels on Android 8.0+
Also use bin/login when launching a session.
1 parent 7cbeb65 commit 11fb650

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

app/src/main/java/com/termux/window/TermuxFloatService.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.Notification;
5+
import android.app.NotificationChannel;
56
import android.app.NotificationManager;
67
import android.app.PendingIntent;
78
import android.app.Service;
@@ -11,24 +12,25 @@
1112
import android.content.Intent;
1213
import android.content.SharedPreferences;
1314
import android.content.res.Resources;
15+
import android.os.Build;
1416
import android.os.IBinder;
1517
import android.os.Vibrator;
1618
import android.preference.PreferenceManager;
17-
import android.util.Log;
19+
import android.support.annotation.RequiresApi;
1820
import android.view.Gravity;
1921
import android.view.LayoutInflater;
2022
import android.view.View;
2123
import android.widget.TextView;
2224
import android.widget.Toast;
2325

24-
import com.termux.terminal.EmulatorDebug;
2526
import com.termux.terminal.TerminalSession;
2627

2728
import java.io.File;
28-
import java.io.IOException;
2929

3030
public class TermuxFloatService extends Service {
3131

32+
private static final String NOTIFICATION_CHANNEL_ID = "termux_notification_channel";
33+
3234
public static final String ACTION_HIDE = "com.termux.float.hide";
3335
public static final String ACTION_SHOW = "com.termux.float.show";
3436

@@ -99,6 +101,18 @@ public void onCreate() {
99101
startForeground(NOTIFICATION_ID, buildNotification());
100102
}
101103

104+
@RequiresApi(api = Build.VERSION_CODES.O)
105+
private void setupNotificationChannel() {
106+
String channelName = "Termux";
107+
String channelDescription = "Notifications from Termux";
108+
int importance = NotificationManager.IMPORTANCE_LOW;
109+
110+
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
111+
channel.setDescription(channelDescription);
112+
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
113+
manager.createNotificationChannel(channel);
114+
}
115+
102116
private Notification buildNotification() {
103117
final Resources res = getResources();
104118
final String contentTitle = res.getString(R.string.notification_title);
@@ -111,7 +125,13 @@ private Notification buildNotification() {
111125
.setPriority(Notification.PRIORITY_MIN).setSmallIcon(R.mipmap.ic_service_notification)
112126
.setColor(0xFF000000)
113127
.setContentIntent(PendingIntent.getService(this, 0, actionIntent, 0))
114-
.setOngoing(true).setShowWhen(false);
128+
.setOngoing(true)
129+
.setShowWhen(false);
130+
131+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
132+
setupNotificationChannel();
133+
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
134+
}
115135

116136
//final int messageId = mVisibleWindow ? R.string.toggle_hide : R.string.toggle_show;
117137
//builder.addAction(android.R.drawable.ic_menu_preferences, res.getString(messageId), PendingIntent.getService(this, 0, actionIntent, 0));
@@ -178,31 +198,13 @@ TerminalSession createTermSession() {
178198
String executablePath = null;
179199
String[] args;
180200
String shellName = null;
181-
File shell = new File(HOME_PATH, ".termux/shell");
182-
if (shell.exists()) {
183-
try {
184-
File canonicalFile = shell.getCanonicalFile();
185-
if (canonicalFile.isFile() && canonicalFile.canExecute()) {
186-
executablePath = canonicalFile.getAbsolutePath();
187-
String[] parts = executablePath.split("/");
188-
shellName = "-" + parts[parts.length - 1];
189-
} else {
190-
Log.w(EmulatorDebug.LOG_TAG, "$HOME/.termux/shell points to non-executable shell: " + canonicalFile.getAbsolutePath());
191-
}
192-
} catch (IOException e) {
193-
Log.e(EmulatorDebug.LOG_TAG, "Error checking $HOME/.termux/shell", e);
194-
}
195-
}
196201

197-
if (executablePath == null) {
198-
// Try bash, zsh and ash in that order:
199-
for (String shellBinary : new String[]{"bash", "zsh", "ash"}) {
200-
File shellFile = new File(PREFIX_PATH + "/bin/" + shellBinary);
201-
if (shellFile.canExecute()) {
202-
executablePath = shellFile.getAbsolutePath();
203-
shellName = "-" + shellBinary;
204-
break;
205-
}
202+
for (String shellBinary : new String[]{"login", "bash", "zsh"}) {
203+
File shellFile = new File(PREFIX_PATH + "/bin/" + shellBinary);
204+
if (shellFile.canExecute()) {
205+
executablePath = shellFile.getAbsolutePath();
206+
shellName = "-" + shellBinary;
207+
break;
206208
}
207209
}
208210

0 commit comments

Comments
 (0)