|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
package com.idormy.sms.forwarder;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
@ -9,7 +8,6 @@ import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.PowerManager;
|
|
|
|
|
import android.service.notification.StatusBarNotification;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
@ -24,41 +22,31 @@ import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
|
|
|
|
|
|
import com.idormy.sms.forwarder.adapter.LogAdapter;
|
|
|
|
|
import com.idormy.sms.forwarder.model.vo.LogVo;
|
|
|
|
|
import com.idormy.sms.forwarder.model.vo.SmsVo;
|
|
|
|
|
import com.idormy.sms.forwarder.notify.NotifyHelper;
|
|
|
|
|
import com.idormy.sms.forwarder.notify.NotifyListener;
|
|
|
|
|
import com.idormy.sms.forwarder.sender.SendUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.service.FrontService;
|
|
|
|
|
import com.idormy.sms.forwarder.service.NotifyService;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.CommonUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.KeepAliveUtils;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.LogUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.NetUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.PhoneUtils;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.SettingUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.SmsUtil;
|
|
|
|
|
import com.idormy.sms.forwarder.utils.TimeUtil;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements NotifyListener, RefreshListView.IRefreshListener {
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements RefreshListView.IRefreshListener {
|
|
|
|
|
|
|
|
|
|
private final String TAG = "MainActivity";
|
|
|
|
|
private static final String ACTION_NOTIFICATION_LISTENER_SETTINGS = "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS";
|
|
|
|
|
// logVoList用于存储数据
|
|
|
|
|
private List<LogVo> logVos = new ArrayList<>();
|
|
|
|
|
private LogAdapter adapter;
|
|
|
|
|
private RefreshListView listView;
|
|
|
|
|
private Intent serviceIntent;
|
|
|
|
|
private static final int REQUEST_CODE = 9999;
|
|
|
|
|
private String currentType = "sms";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -81,9 +69,6 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
SmsUtil.init(this);
|
|
|
|
|
NetUtil.init(this);
|
|
|
|
|
|
|
|
|
|
//应用通知
|
|
|
|
|
NotifyHelper.getInstance().setNotifyListener(this);
|
|
|
|
|
|
|
|
|
|
//前台服务
|
|
|
|
|
serviceIntent = new Intent(MainActivity.this, FrontService.class);
|
|
|
|
|
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
@ -171,20 +156,20 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
}
|
|
|
|
|
Log.d(TAG, "SimInfoList = " + MyApplication.SimInfoList.size());
|
|
|
|
|
|
|
|
|
|
//开启读取通知栏权限
|
|
|
|
|
if (!isNotificationListenerServiceEnabled(this)) {
|
|
|
|
|
openNotificationAccess();
|
|
|
|
|
toggleNotificationListenerService();
|
|
|
|
|
Toast.makeText(this, "请先勾选《短信转发器》的读取通知栏权限!", Toast.LENGTH_LONG).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//省电优化设置为无限制
|
|
|
|
|
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
|
|
|
if (!isIgnoringBatteryOptimizations()) {
|
|
|
|
|
Toast.makeText(this, "请将省电优化设置为无限制,有利于防止《短信转发器》被杀!!", Toast.LENGTH_LONG).show();
|
|
|
|
|
if (!KeepAliveUtils.isIgnoreBatteryOptimization(this)) {
|
|
|
|
|
Toast.makeText(this, "请将省电优化设置为无限制(不优化),有利于《短信转发器》保活!", Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//开启读取通知栏权限
|
|
|
|
|
if (SettingUtil.getSwitchEnableAppNotify() && !CommonUtil.isNotificationListenerServiceEnabled(this)) {
|
|
|
|
|
CommonUtil.toggleNotificationListenerService(this);
|
|
|
|
|
SettingUtil.switchEnableAppNotify(false);
|
|
|
|
|
Toast.makeText(this, "请先授予《短信转发器》通知使用权,否则无法转发APP通知,已经自动关闭转发!", Toast.LENGTH_LONG).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
startService(serviceIntent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -196,11 +181,6 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPause() {
|
|
|
|
|
//当界面返回到桌面之后.清除通知设置当中的数据.减少内存占有
|
|
|
|
|
//if (applicationList != null) {
|
|
|
|
|
// applicationList.setAdapter(null);
|
|
|
|
|
// adapter = null;
|
|
|
|
|
//}
|
|
|
|
|
super.onPause();
|
|
|
|
|
startService(serviceIntent);
|
|
|
|
|
}
|
|
|
|
@ -208,10 +188,10 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
if (requestCode == REQUEST_CODE) {
|
|
|
|
|
if (isNotificationListenerServiceEnabled(this)) {
|
|
|
|
|
if (requestCode == CommonUtil.NOTIFICATION_REQUEST_CODE) {
|
|
|
|
|
if (CommonUtil.isNotificationListenerServiceEnabled(this)) {
|
|
|
|
|
Toast.makeText(this, "通知服务已开启", Toast.LENGTH_SHORT).show();
|
|
|
|
|
toggleNotificationListenerService();
|
|
|
|
|
CommonUtil.toggleNotificationListenerService(this);
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "通知服务未开启", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
@ -224,19 +204,6 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toggleNotificationListenerService() {
|
|
|
|
|
PackageManager pm = getPackageManager();
|
|
|
|
|
pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), NotifyService.class),
|
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
|
|
|
|
|
|
|
|
|
|
pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), NotifyService.class),
|
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean isNotificationListenerServiceEnabled(Context context) {
|
|
|
|
|
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(context);
|
|
|
|
|
return packageNames.contains(context.getPackageName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断系统是否已经关闭省电优化
|
|
|
|
@ -257,11 +224,6 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
return isIgnoring;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openNotificationAccess() {
|
|
|
|
|
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化数据
|
|
|
|
|
private void initTLogs() {
|
|
|
|
|
logVos = LogUtil.getLog(null, null, currentType);
|
|
|
|
@ -416,71 +378,4 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
|
|
|
|
|
return super.onMenuOpened(featureId, menu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到通知
|
|
|
|
|
*
|
|
|
|
|
* @param type 通知类型
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceiveMessage(int type) {
|
|
|
|
|
Log.d(TAG, "收到通知=" + type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除通知
|
|
|
|
|
*
|
|
|
|
|
* @param type 通知类型
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onRemovedMessage(int type) {
|
|
|
|
|
Log.d(TAG, "移除通知=" + type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到通知
|
|
|
|
|
*
|
|
|
|
|
* @param sbn 状态栏通知
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceiveMessage(StatusBarNotification sbn) {
|
|
|
|
|
if (sbn.getNotification() == null) return;
|
|
|
|
|
if (sbn.getNotification().extras == null) return;
|
|
|
|
|
|
|
|
|
|
//推送通知的应用包名
|
|
|
|
|
String packageName = sbn.getPackageName();
|
|
|
|
|
//通知标题
|
|
|
|
|
String title = sbn.getNotification().extras.get("android.title").toString();
|
|
|
|
|
//通知内容
|
|
|
|
|
String text = sbn.getNotification().extras.get("android.text").toString();
|
|
|
|
|
if (text.isEmpty() && sbn.getNotification().tickerText != null) {
|
|
|
|
|
text = sbn.getNotification().tickerText.toString();
|
|
|
|
|
}
|
|
|
|
|
//通知时间
|
|
|
|
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date(sbn.getPostTime()));
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, String.format(
|
|
|
|
|
Locale.getDefault(),
|
|
|
|
|
"onNotificationPosted:\n应用包名:%s\n消息标题:%s\n消息内容:%s\n消息时间:%s\n",
|
|
|
|
|
packageName, title, text, time)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//不处理空消息
|
|
|
|
|
if (title.isEmpty() && text.isEmpty()) return;
|
|
|
|
|
|
|
|
|
|
SmsVo smsVo = new SmsVo(packageName, text, new Date(), title);
|
|
|
|
|
Log.d(TAG, "send_msg" + smsVo.toString());
|
|
|
|
|
SendUtil.send_msg(this, smsVo, 1, "app");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除掉通知栏消息
|
|
|
|
|
*
|
|
|
|
|
* @param sbn 状态栏通知
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onRemovedMessage(StatusBarNotification sbn) {
|
|
|
|
|
Log.d(TAG, "移除掉通知栏消息");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|