From c3542b10ea66a1c3fad44fe8e0efd357d7d3857e Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 19 十一月 2025 19:32:41 +0800
Subject: [PATCH] 2025年11月19日19:32:39

---
 app/src/main/java/com/hdl/photovoltaic/ui/me/NotificationSettingsActivity.java |  319 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 319 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/me/NotificationSettingsActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/me/NotificationSettingsActivity.java
new file mode 100644
index 0000000..e30b911
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/me/NotificationSettingsActivity.java
@@ -0,0 +1,319 @@
+package com.hdl.photovoltaic.ui.me;
+
+import android.app.AlertDialog;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Handler;
+import android.provider.Settings;
+import android.view.View;
+import android.widget.ImageView;
+
+
+import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+import androidx.appcompat.content.res.AppCompatResources;
+
+import com.hdl.linkpm.sdk.core.exception.HDLException;
+import com.hdl.photovoltaic.R;
+import com.hdl.photovoltaic.base.CustomBaseActivity;
+import com.hdl.photovoltaic.config.UserConfigManage;
+import com.hdl.photovoltaic.databinding.ActivityNotificationSettingsBinding;
+import com.hdl.photovoltaic.listener.CloudCallBeak;
+import com.hdl.photovoltaic.other.HdlMessageLogic;
+import com.hdl.photovoltaic.other.HdlThreadLogic;
+import com.hdl.photovoltaic.ui.bean.MessageNoticeBean;
+
+
+public class NotificationSettingsActivity extends CustomBaseActivity {
+    private static final int REQUEST_CODE_NOTIFICATION_SETTINGS = 1001;
+    private ActivityNotificationSettingsBinding viewBinding;
+
+    @Override
+    public Object getContentView() {
+        viewBinding = ActivityNotificationSettingsBinding.inflate(getLayoutInflater());
+        return viewBinding.getRoot();
+    }
+
+    @Override
+    public void onBindView(Bundle savedInstanceState) {
+        setStatusBarTranslucent();
+        getWindow().setNavigationBarColor(getColor(R.color.text_FF000000));
+        //鍒濆鍖�
+        initView();
+        //鍒濆鍖栫晫闈㈢洃鍚櫒
+        initEvent();
+        initData();
+    }
+
+    private void initData() {
+        HdlMessageLogic.getInstance().getMessageNoticeInfo(new CloudCallBeak<MessageNoticeBean>() {
+            @Override
+            public void onSuccess(MessageNoticeBean messageNoticeBean) {
+                HdlThreadLogic.runMainThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        intiBtnState(messageNoticeBean);
+                    }
+                });
+
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HdlThreadLogic.toast(_mActivity, e);
+            }
+        });
+    }
+
+    /**
+     * @param type          (aiPush,eventPush,warnPush,faultPush)
+     * @param isTypeBoolean true鎴栬�協alse
+     */
+    private void setMessageNoticeInfo(String type, boolean isTypeBoolean) {
+        HdlMessageLogic.getInstance().setMessageNoticeInfo(type, isTypeBoolean, new CloudCallBeak<Boolean>() {
+            @Override
+            public void onSuccess(Boolean obj) {
+
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HdlThreadLogic.toast(_mActivity, e);
+            }
+        });
+    }
+
+    private void initEvent() {
+        viewBinding.toolbarTopRl.topBackLl.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                finish();
+            }
+        });
+        viewBinding.notificationLl.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                openNotificationSettings();
+            }
+        });
+        viewBinding.aiIv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                v.setSelected(!v.isSelected());
+                setBtnClick(viewBinding.aiIv, v.isSelected());
+                setMessageNoticeInfo("aiPush", v.isSelected());
+            }
+        });
+        viewBinding.tipIv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                v.setSelected(!v.isSelected());
+                setBtnClick(viewBinding.tipIv, v.isSelected());
+                setMessageNoticeInfo("eventPush", v.isSelected());
+            }
+        });
+        viewBinding.warningIv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                v.setSelected(!v.isSelected());
+                setBtnClick(viewBinding.warningIv, v.isSelected());
+                setMessageNoticeInfo("warnPush", v.isSelected());
+            }
+        });
+        viewBinding.malfunctionIv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                v.setSelected(!v.isSelected());
+                setBtnClick(viewBinding.malfunctionIv, v.isSelected());
+                setMessageNoticeInfo("faultPush", v.isSelected());
+            }
+        });
+
+    }
+
+    private void initView() {
+        viewBinding.toolbarTopRl.topTitleTv.setText(R.string.notification_settings);
+        viewBinding.toolbarTopRl.topBackLl.setVisibility(View.VISIBLE);
+        if (UserConfigManage.getInstance().isBAccount()) {
+            viewBinding.aiLl.setVisibility(View.GONE);
+            viewBinding.lineV1.setVisibility(View.GONE);
+        } else {
+            viewBinding.aiLl.setVisibility(View.VISIBLE);
+            viewBinding.lineV1.setVisibility(View.VISIBLE);
+        }
+        checkAndRequestNotificationPermission();
+
+    }
+
+    /**
+     * 妫�鏌ュ苟璇锋眰閫氱煡鏉冮檺
+     */
+    private void checkAndRequestNotificationPermission() {
+        if (!areNotificationsEnabled(_mActivity)) {
+            setBtnEnabled(false);
+        } else {
+            setBtnEnabled(true);
+        }
+    }
+
+    /**
+     * 鏄剧ず閫氱煡鏉冮檺寮曞瀵硅瘽妗�
+     */
+    private void showNotificationPermissionDialog() {
+        AlertDialog.Builder builder = new AlertDialog.Builder(this);
+        builder.setTitle("寮�鍚�氱煡鏉冮檺")
+                .setMessage("涓轰簡鍙婃椂鎺ユ敹閲嶈娑堟伅锛岃寮�鍚�氱煡鏉冮檺")
+                .setPositiveButton("绔嬪嵆寮�鍚�", (dialog, which) -> {
+                    openNotificationSettings();
+                })
+                .setNegativeButton("鏆備笉寮�鍚�", (dialog, which) -> {
+                    dialog.dismiss();
+                })
+                .setCancelable(false)
+                .show();
+    }
+
+    /**
+     * 璺宠浆鍒伴�氱煡璁剧疆椤甸潰
+     */
+    private void openNotificationSettings() {
+        Intent intent = new Intent();
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            // Android 8.0+ 鐩存帴璺宠浆鍒板簲鐢ㄧ殑閫氱煡璁剧疆
+            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
+            intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            // Android 5.0-7.1 璺宠浆鍒板簲鐢ㄨ鎯呴〉闈�
+            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
+            intent.setData(Uri.fromParts("package", getPackageName(), null));
+        } else {
+            // 鏃х増鏈� Android
+            intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
+        }
+
+        try {
+            startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_SETTINGS);
+        } catch (Exception e) {
+            e.printStackTrace();
+            // 澶囩敤鏂规
+            openAppDetailsSettings();
+        }
+    }
+
+    /**
+     * 澶囩敤鏂规锛氳烦杞埌搴旂敤璇︽儏椤甸潰
+     */
+    private void openAppDetailsSettings() {
+        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
+        intent.setData(Uri.fromParts("package", getPackageName(), null));
+        try {
+            startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_SETTINGS);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (requestCode == REQUEST_CODE_NOTIFICATION_SETTINGS) {
+            // 鐢ㄦ埛浠庤缃〉闈㈣繑鍥烇紝閲嶆柊妫�鏌ユ潈闄�
+            new Handler().postDelayed(() -> {
+                checkAndRequestNotificationPermission();
+            }, 500); // 寤惰繜妫�鏌ワ紝纭繚璁剧疆宸茬敓鏁�
+        }
+    }
+
+    /**
+     * 鍙戦�佹祴璇曢�氱煡
+     */
+    private void sendTestNotification() {
+        if (!areNotificationsEnabled(this)) {
+            showNotificationPermissionDialog();
+            return;
+        }
+
+    }
+
+
+    /**
+     * 妫�鏌ラ�氱煡鏉冮檺鏄惁寮�鍚�
+     */
+    public boolean areNotificationsEnabled(Context context) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            NotificationManager notificationManager =
+                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+            return notificationManager.areNotificationsEnabled();
+        } else {
+            // Android 8.0 浠ヤ笅鐗堟湰锛岄�氱煡鏉冮檺榛樿寮�鍚垨閫氳繃鍏朵粬鏂瑰紡妫�鏌�
+            return true;
+        }
+    }
+
+    /**
+     * 妫�鏌ョ壒瀹氭笭閬撶殑閫氱煡鏉冮檺 (Android 8.0+)
+     */
+    @RequiresApi(api = Build.VERSION_CODES.O)
+    public boolean isNotificationChannelEnabled(Context context, String channelId) {
+        NotificationManager notificationManager =
+                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
+        return channel != null && channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
+    }
+
+    /**
+     * 璁剧疆鎸夐挳鏄惁寮�鍚偣鍑绘牱寮�
+     *
+     * @param enabled 鏄惁寮�鍚�
+     */
+    private void setBtnEnabled(boolean enabled) {
+        final float alpha = enabled ? 1.0f : 0.5f;
+        viewBinding.aiIv.setAlpha(alpha);
+        viewBinding.aiIv.setEnabled(enabled);
+        viewBinding.tipIv.setAlpha(alpha);
+        viewBinding.tipIv.setEnabled(enabled);
+        viewBinding.warningIv.setAlpha(alpha);
+        viewBinding.warningIv.setEnabled(enabled);
+        viewBinding.malfunctionIv.setAlpha(alpha);
+        viewBinding.malfunctionIv.setEnabled(enabled);
+        if (enabled) {
+            viewBinding.notificationLl.setVisibility(View.GONE);
+        } else {
+            viewBinding.notificationLl.setVisibility(View.VISIBLE);
+        }
+    }
+
+    /**
+     * 鍒濆鍖栨寜閽姸鎬�
+     *
+     * @param messageNoticeBean 浜戠杩斿洖鐨勬ā鍨�
+     */
+    void intiBtnState(MessageNoticeBean messageNoticeBean) {
+        setBtnClick(viewBinding.aiIv, messageNoticeBean.isAiPush());
+        setBtnClick(viewBinding.tipIv, messageNoticeBean.isEventPush());
+        setBtnClick(viewBinding.warningIv, messageNoticeBean.isWarnPush());
+        setBtnClick(viewBinding.malfunctionIv, messageNoticeBean.isFaultPush());
+    }
+
+    /**
+     * 鎸夐挳鐐瑰嚮鏍峰紡
+     *
+     * @param v          鎸夐挳缁勪欢
+     * @param isSelected 鐐瑰嚮鐘舵��
+     */
+    private void setBtnClick(ImageView v, boolean isSelected) {
+        if (isSelected) {
+            v.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.open));
+        } else {
+            v.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.close));
+        }
+    }
+
+
+}
\ No newline at end of file

--
Gitblit v1.8.0