Skip to content

Commit

Permalink
show running status notification when started
Browse files Browse the repository at this point in the history
Signed-off-by: Syrone Wong <[email protected]>
  • Loading branch information
wongsyrone committed Feb 16, 2020
1 parent 01d798b commit 42d6ceb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ repositories {
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-compat:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
32 changes: 31 additions & 1 deletion app/src/main/java/io/github/trojan_gfw/igniter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@


import android.app.Activity;
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.net.VpnService;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
Expand Down Expand Up @@ -48,6 +50,33 @@ public class MainActivity extends AppCompatActivity {

private BroadcastReceiver serviceStateReceiver;

private void createNotificationChannel(String channelId) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.notification_channel_name);
String description = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

private void destoryNotificationChannel(String channelId) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.deleteNotificationChannel(channelId);
}
}

private void copyRawResourceToDir(int resId, String destPathName, boolean override) {
File file = new File(destPathName);
if (override || !file.exists()) {
Expand Down Expand Up @@ -120,6 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {
startStopButton = findViewById(R.id.startStopButton);

Globals.Init(this);
createNotificationChannel(getString(R.string.notification_channel_id));

copyRawResourceToDir(R.raw.cacert, Globals.getCaCertPath(), true);
copyRawResourceToDir(R.raw.country, Globals.getCountryMmdbPath(), true);
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/io/github/trojan_gfw/igniter/ProxyService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.github.trojan_gfw.igniter;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.VpnService;
import android.os.ParcelFileDescriptor;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

Expand All @@ -24,8 +27,10 @@ public class ProxyService extends VpnService {
public static final int STOPPED = 3;
public static final String STATUS_EXTRA_NAME = "service_state";
public static final String CLASH_EXTRA_NAME = "enable_clash";
public static final int IGNITER_STATUS_NOTIFY_MSG_ID = 0;
public long tun2socksPort;
public boolean enable_clash = false;

public static ProxyService getInstance() {
return instance;
}
Expand Down Expand Up @@ -178,8 +183,39 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("igniter", "tun2socks port is " + tun2socksPort);
Tun2socks.start(fd, "127.0.0.1:" + tun2socksPort, "255.0.128.1", "255.0.143.254", VPN_MTU);

StringBuilder runningStatusStringBuilder = new StringBuilder();
runningStatusStringBuilder.append("Trojan SOCKS5 port: ")
.append(trojanPort)
.append("\n")
.append("Tun2socks port: ")
.append(tun2socksPort)
.append("\n");
if (enable_clash) {
runningStatusStringBuilder.append("Clash SOCKS listen port: ")
.append(clashSocksPort)
.append("\n");
}

setState(STARTED);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
Intent openMainActivityIntent = new Intent(this, MainActivity.class);
openMainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingOpenMainActivityIntent = PendingIntent.getActivity(this, 0, openMainActivityIntent, 0);
String igniterRunningStatusStr = runningStatusStringBuilder.toString();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Igniter is running")
.setContentText(igniterRunningStatusStr)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(igniterRunningStatusStr))
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingOpenMainActivityIntent)
.setAutoCancel(false)
.setOngoing(true);
notificationManager.notify(IGNITER_STATUS_NOTIFY_MSG_ID, builder.build());

return START_STICKY;
}

Expand All @@ -192,6 +228,10 @@ private void shutdown() {
Log.e("Clash", "clash stopped");
}
Tun2socks.stop();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(IGNITER_STATUS_NOTIFY_MSG_ID);

stopSelf();

setState(STOPPED);
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<string name="connected_to__in__ms">Connected to %1$s in %2$sms.</string>
<string name="failed_to_connect_to__">Failed to connect to %1$s: %2$s</string>
<string name="invalid_configuration">Invalid Configuration</string>
<string name="notification_channel_id">igniter_notify_chan</string>
<string name="notification_channel_name">igniter_notify_channel</string>
<string name="notification_channel_description">Igniter notifications channel</string>

<!-- App Bar Menu -->
<string name="action_test_connection">Test Connection</string>
Expand Down

0 comments on commit 42d6ceb

Please sign in to comment.