Skip to content

Commit

Permalink
feat: Alert user when notification permission not granted
Browse files Browse the repository at this point in the history
  • Loading branch information
kaaass committed Sep 16, 2023
1 parent 30d7f79 commit aaf0e97
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kaaass.zerotierfix.ui;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
Expand Down Expand Up @@ -27,14 +28,14 @@
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.zerotier.sdk.NodeStatus;
import com.zerotier.sdk.Version;
import com.zerotier.sdk.VirtualNetworkConfig;

import net.kaaass.zerotierfix.BuildConfig;
import net.kaaass.zerotierfix.R;
Expand All @@ -44,12 +45,12 @@
import net.kaaass.zerotierfix.events.IsServiceRunningRequestEvent;
import net.kaaass.zerotierfix.events.NetworkListCheckedChangeEvent;
import net.kaaass.zerotierfix.events.NetworkListReplyEvent;
import net.kaaass.zerotierfix.events.NetworkListRequestEvent;
import net.kaaass.zerotierfix.events.NodeDestroyedEvent;
import net.kaaass.zerotierfix.events.NodeIDEvent;
import net.kaaass.zerotierfix.events.NodeStatusEvent;
import net.kaaass.zerotierfix.events.NodeStatusRequestEvent;
import net.kaaass.zerotierfix.events.OrbitMoonEvent;
import net.kaaass.zerotierfix.events.NetworkListRequestEvent;
import net.kaaass.zerotierfix.events.StopEvent;
import net.kaaass.zerotierfix.events.VPNErrorEvent;
import net.kaaass.zerotierfix.events.VirtualNetworkConfigChangedEvent;
Expand Down Expand Up @@ -196,9 +197,17 @@ public void onAttach(@NonNull Context context) {
public void onStart() {
super.onStart();
this.eventBus.post(new NetworkListRequestEvent());

// 初始化节点及服务状态
this.eventBus.post(new NodeStatusRequestEvent());
this.eventBus.post(new IsServiceRunningRequestEvent());

// 检查通知权限
var notificationManager = NotificationManagerCompat.from(requireContext());
if (!notificationManager.areNotificationsEnabled()) {
// 无通知权限
showNoNotificationAlertDialog();
}
}

@Override
Expand Down Expand Up @@ -587,6 +596,45 @@ public void onNetworkListCheckedChangeEvent(NetworkListCheckedChangeEvent event)
}
}

/**
* 显示无通知权限的提示框。若用户选择过 “不再提示”,则此方法将不进行任何操作
*/
private void showNoNotificationAlertDialog() {
// 检查是否选择过 “不再提示”,若是则不显示
var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext());

if (sharedPreferences.getBoolean(Constants.PREF_DISABLE_NO_NOTIFICATION_ALERT, false)) {
return;
}

// 显示提示框
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_no_notification_alert, null);

AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
.setView(view)
.setTitle(R.string.dialog_no_notification_alert_title)
.setPositiveButton(R.string.open_notification_settings, (dialog, which) -> {
// 打开 APP 的通知设置
var intent = new Intent();

intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("app_package", requireContext().getPackageName());
intent.putExtra("app_uid", requireContext().getApplicationInfo().uid);
intent.putExtra("android.provider.extra.APP_PACKAGE", requireContext().getPackageName());
startActivity(intent);
})
.setNegativeButton(R.string.dont_show_again, (dialog, which) -> {
// 设置不再提示此对话框
sharedPreferences.edit()
.putBoolean(Constants.PREF_DISABLE_NO_NOTIFICATION_ALERT, true)
.apply();
})
.setCancelable(true);

builder.create().show();
}

/**
* 网络信息列表适配器
*/
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/net/kaaass/zerotierfix/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Constants {

public static final String PREF_GENERAL_START_ZEROTIER_ON_BOOT = "general_start_zerotier_on_boot";

public static final String PREF_DISABLE_NO_NOTIFICATION_ALERT = "disable_no_notification_alert";

public static final String FILE_CUSTOM_PLANET = "planet.custom";

public static final String FILE_TEMP = "temp";
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/dialog_no_notification_alert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@id/text_moon_world_id"
android:text="@string/dialog_no_notification_alert_text" />

</LinearLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@
<string name="toast_vpn_error">VPN 服务发生错误!%1$s</string>
<string name="empty_network_name">未知</string>
<string name="app_version_format">(APP: %s)</string>
<string name="dialog_no_notification_alert_text">Zerotier Fix 未获得通知权限!您可能会遗漏用于提示连接状态的通知气泡与状态栏消息。Zerotier Fix 推荐您打开应用通知权限以获取连接状态信息。如果您不希望看到状态栏的常驻通知,您仍可以在打开通知权限后关闭对应通知类别的消息。</string>
<string name="dialog_no_notification_alert_title">未开启通知权限</string>
<string name="open_notification_settings">打开通知设置</string>
<string name="dont_show_again">不再提示</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@
<string name="toast_vpn_error">A VPN service error has occurred! %1$s</string>
<string name="empty_network_name">Unknown</string>
<string name="app_version_format">(APP: %s)</string>
<string name="dialog_no_notification_alert_text">Zerotier Fix does not have notification permissions! You might miss notification pop-ups and status bar messages that indicate connection status. Zerotier Fix recommends you enable notification permissions for the app to receive connection status information. If you don\'t want to see persistent notifications in the status bar, you can still turn off messages for that notification category after enabling the permissions.</string>
<string name="dialog_no_notification_alert_title">Notification Permission Not Granted</string>
<string name="open_notification_settings">Goto notification settings</string>
<string name="dont_show_again">Don\'t show again</string>
</resources>

0 comments on commit aaf0e97

Please sign in to comment.