From 34b71db40df90116d9dbdefe35893254030624e3 Mon Sep 17 00:00:00 2001 From: Myth Date: Sun, 5 Nov 2023 01:26:52 +0800 Subject: [PATCH] feat: notification --- app/src/main/AndroidManifest.xml | 2 + .../java/cx/myth/zjuconnect/MainActivity.java | 45 +++++++++++++++++++ app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 49 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 56df235..eadc26c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,8 @@ + + = 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); + } } } }; @@ -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 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(); @@ -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); + } } } }); diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 91b5557..ce6f219 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -15,4 +15,5 @@ 关于 关于 图标 + 已连接 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce1a573..dc57c84 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,4 +16,5 @@ About About Icon + Connected \ No newline at end of file