136 lines
5.6 KiB
Java
136 lines
5.6 KiB
Java
|
package com.itrycn.tellmenotice;
|
||
|
|
||
|
import androidx.appcompat.app.AppCompatActivity;
|
||
|
|
||
|
import android.content.ActivityNotFoundException;
|
||
|
import android.content.ComponentName;
|
||
|
import android.content.Context;
|
||
|
import android.content.Intent;
|
||
|
import android.media.AudioManager;
|
||
|
import android.net.Uri;
|
||
|
import android.os.Bundle;
|
||
|
import android.provider.Settings;
|
||
|
import android.text.TextUtils;
|
||
|
import android.view.View;
|
||
|
import android.widget.TextView;
|
||
|
import android.widget.Toast;
|
||
|
|
||
|
import java.util.Calendar;
|
||
|
|
||
|
public class MainActivity extends AppCompatActivity {
|
||
|
ScreenListener screenlistener;
|
||
|
@Override
|
||
|
protected void onCreate(Bundle savedInstanceState) {
|
||
|
super.onCreate(savedInstanceState);
|
||
|
setContentView(R.layout.activity_main);
|
||
|
if(!isNotificationListenersEnabled())
|
||
|
{
|
||
|
gotoNotificationAccessSetting(MainActivity.this);
|
||
|
}
|
||
|
Intent intent2 = new Intent(MainActivity.this,NoticeService.class);
|
||
|
startService(intent2);
|
||
|
startFloatingButtonService();
|
||
|
TextView tv=findViewById(R.id.IdState);
|
||
|
tv.setText("睿元智能管家,你的私人管家。\n" +
|
||
|
"1.帮你监控系统通知,自动筛选重要通知。\n" +
|
||
|
"2.不同通知展现不同的提醒界面和声音。\n" +
|
||
|
"3.支持免打扰功能,深夜自动免打扰。");
|
||
|
ScreenListener screenlistener = new ScreenListener(this);
|
||
|
screenlistener.begin(new ScreenListener.ScreenStateListener() {
|
||
|
|
||
|
@Override
|
||
|
public void onUserPresent() {
|
||
|
//Log.e("onUserPresent", "onUserPresent");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onScreenOn() {
|
||
|
//Log.e("onScreenOn", "onScreenOn");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onScreenOff() {
|
||
|
//region 关闭屏幕自动将通知音量调整到最大
|
||
|
Calendar c = Calendar.getInstance();
|
||
|
int hour = c.get(Calendar.HOUR_OF_DAY); // 时
|
||
|
if(hour>=23 || hour<7) {return; } //23点到早上7点,免打扰时间段
|
||
|
AudioManager audioManager = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
|
||
|
int NoticeVolume= audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
|
||
|
int NoticeMaxVolume= audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
|
||
|
if(NoticeVolume<NoticeMaxVolume)
|
||
|
{
|
||
|
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, NoticeMaxVolume, AudioManager.FLAG_VIBRATE);
|
||
|
}
|
||
|
//endregion
|
||
|
//Log.e("onScreenOff", "onScreenOff");
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
@Override
|
||
|
protected void onDestroy() {
|
||
|
screenlistener.unregisterListener();
|
||
|
super.onDestroy();
|
||
|
}
|
||
|
@Override
|
||
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||
|
super.onActivityResult(requestCode, resultCode, data);
|
||
|
if (requestCode == 0) {
|
||
|
if (!Settings.canDrawOverlays(this)) {
|
||
|
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
|
||
|
} else {
|
||
|
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
|
||
|
private boolean isNotificationListenersEnabled () {
|
||
|
String pkgName = getPackageName();
|
||
|
final String flat = Settings.Secure.getString(getContentResolver(), ENABLED_NOTIFICATION_LISTENERS);
|
||
|
if (!TextUtils.isEmpty(flat)) {
|
||
|
final String[] names = flat.split(":");
|
||
|
for (int i = 0; i < names.length; i++) {
|
||
|
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
|
||
|
if (cn != null) {
|
||
|
if (TextUtils.equals(pkgName, cn.getPackageName())) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
public static boolean gotoNotificationAccessSetting(Context context) {
|
||
|
try {
|
||
|
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
|
context.startActivity(intent);
|
||
|
return true;
|
||
|
|
||
|
} catch (ActivityNotFoundException e) {//普通情况下找不到的时候需要再特殊处理找一次
|
||
|
try {
|
||
|
Intent intent = new Intent();
|
||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
|
ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity");
|
||
|
intent.setComponent(cn);
|
||
|
intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
|
||
|
context.startActivity(intent);
|
||
|
return true;
|
||
|
} catch (Exception e1) {
|
||
|
e1.printStackTrace();
|
||
|
}
|
||
|
Toast.makeText(context, "对不起,您的手机暂不支持", Toast.LENGTH_SHORT).show();
|
||
|
e.printStackTrace();
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
public void startFloatingButtonService() {
|
||
|
if (NoticeService.isStarted) {
|
||
|
return;
|
||
|
}
|
||
|
if (!Settings.canDrawOverlays(this)) {
|
||
|
Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
|
||
|
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|