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或者false
|
*/
|
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));
|
}
|
}
|
|
|
}
|