Skip to content

Commit

Permalink
feat: notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythologyli committed Nov 4, 2023
1 parent d9ba9a3 commit 34b71db
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/cx/myth/zjuconnect/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package cx.myth.zjuconnect;

import static android.Manifest.permission.POST_NOTIFICATIONS;

import android.app.Notification;
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.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -13,6 +20,9 @@
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
Expand All @@ -31,6 +41,8 @@ public class MainActivity extends AppCompatActivity {

private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
private NotificationManager notificationManager;
private Notification notification;
private boolean isRunning = false;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
Expand All @@ -40,13 +52,22 @@ public void onReceive(Context context, Intent intent) {
binding.fab.setImageResource(android.R.drawable.ic_media_play);
Snackbar.make(binding.getRoot(), R.string.login_failed, Snackbar.LENGTH_SHORT).setAnchorView(binding.fab).show();
binding.fab.setEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.cancel(1);
}
} else if (Objects.equals(intent.getAction(), "cx.myth.zjuconnect.STACK_STOPPED")) {
stopVpnService();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.cancel(1);
}
} else if (Objects.equals(intent.getAction(), "cx.myth.zjuconnect.LOGIN_SUCCEEDED")) {
isRunning = true;
binding.fab.setImageResource(android.R.drawable.ic_media_pause);
Snackbar.make(binding.getRoot(), R.string.started, Snackbar.LENGTH_SHORT).setAnchorView(binding.fab).show();
binding.fab.setEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.notify(1, notification);
}
}
}
};
Expand Down Expand Up @@ -89,6 +110,27 @@ protected void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("cx.myth.zjuconnect.STACK_STOPPED"));
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("cx.myth.zjuconnect.LOGIN_SUCCEEDED"));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("zjuconnect", "Notification", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "zjuconnect")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("ZJU Connect")
.setContentText(getResources().getString(R.string.connected));

builder.setOngoing(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, 1);
}
}

notification = builder.build();
}

ActivityResultLauncher<Intent> getPermission = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() != RESULT_OK) {
Snackbar.make(binding.getRoot(), R.string.ask_permission, Snackbar.LENGTH_SHORT).setAnchorView(binding.fab).show();
Expand All @@ -108,6 +150,9 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("cx.myth.zjuconnect.STOP_VPN"));
stopVpnService();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.cancel(1);
}
}
}
});
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<string name="action_about">关于</string>
<string name="about_fragment_label">关于</string>
<string name="icon">图标</string>
<string name="connected">已连接</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<string name="action_about">About</string>
<string name="about_fragment_label">About</string>
<string name="icon">Icon</string>
<string name="connected">Connected</string>
</resources>

0 comments on commit 34b71db

Please sign in to comment.