整理:工具类代码

This commit is contained in:
pppscn 2021-11-20 07:55:07 +08:00
parent 64f6ec1369
commit e6cafa521a
11 changed files with 165 additions and 145 deletions

View File

@ -18,8 +18,8 @@ import androidx.appcompat.app.AppCompatActivity;
import com.idormy.sms.forwarder.receiver.RebootBroadcastReceiver;
import com.idormy.sms.forwarder.utils.CacheUtil;
import com.idormy.sms.forwarder.utils.CommonUtil;
import com.idormy.sms.forwarder.utils.Define;
import com.idormy.sms.forwarder.utils.aUtil;
import com.xuexiang.xupdate.easy.EasyUpdate;
import com.xuexiang.xupdate.proxy.impl.DefaultUpdateChecker;
@ -48,7 +48,7 @@ public class AboutActivity extends AppCompatActivity {
final TextView version_now = findViewById(R.id.version_now);
Button check_version_now = findViewById(R.id.check_version_now);
try {
version_now.setText(aUtil.getVersionName(AboutActivity.this));
version_now.setText(CommonUtil.getVersionName(AboutActivity.this));
} catch (Exception e) {
e.printStackTrace();
}
@ -56,7 +56,7 @@ public class AboutActivity extends AppCompatActivity {
check_version_now.setOnClickListener(v -> {
try {
String updateUrl = "https://xupdate.bms.ink/update/checkVersion?appKey=com.idormy.sms.forwarder&versionCode=";
updateUrl += aUtil.getVersionCode(AboutActivity.this);
updateUrl += CommonUtil.getVersionCode(AboutActivity.this);
EasyUpdate.create(AboutActivity.this, updateUrl)
.updateChecker(new DefaultUpdateChecker() {
@ -154,5 +154,4 @@ public class AboutActivity extends AppCompatActivity {
}
}
}

View File

@ -34,11 +34,12 @@ 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.LogUtil;
import com.idormy.sms.forwarder.utils.NetUtil;
import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.SmsUtil;
import com.idormy.sms.forwarder.utils.aUtil;
import com.idormy.sms.forwarder.utils.TimeUtil;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
@ -71,7 +72,7 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
//检查权限是否获取
PackageManager pm = getPackageManager();
PhoneUtils.CheckPermission(pm, this);
CommonUtil.CheckPermission(pm, this);
//获取SIM信息
PhoneUtils.init(this);
@ -298,9 +299,9 @@ public class MainActivity extends AppCompatActivity implements NotifyListener, R
builder.setTitle(R.string.details);
String simInfo = logVo.getSimInfo();
if (simInfo != null) {
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getSimInfo() + "\n\n" + logVo.getRule() + "\n\n" + aUtil.utc2Local(logVo.getTime()) + "\n\nResponse" + logVo.getForwardResponse());
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getSimInfo() + "\n\n" + logVo.getRule() + "\n\n" + TimeUtil.utc2Local(logVo.getTime()) + "\n\nResponse" + logVo.getForwardResponse());
} else {
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getRule() + "\n\n" + aUtil.utc2Local(logVo.getTime()) + "\n\nResponse" + logVo.getForwardResponse());
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getRule() + "\n\n" + TimeUtil.utc2Local(logVo.getTime()) + "\n\nResponse" + logVo.getForwardResponse());
}
//删除
builder.setNegativeButton(R.string.del, (dialog, which) -> {

View File

@ -10,7 +10,7 @@ import android.widget.TextView;
import com.idormy.sms.forwarder.R;
import com.idormy.sms.forwarder.model.vo.LogVo;
import com.idormy.sms.forwarder.utils.aUtil;
import com.idormy.sms.forwarder.utils.TimeUtil;
import java.util.List;
@ -76,7 +76,7 @@ public class LogAdapter extends ArrayAdapter<LogVo> {
viewHolder.tLogFrom.setText(logVo.getFrom());
viewHolder.tLogContent.setText(logVo.getContent());
viewHolder.tLogRule.setText(logVo.getRule());
viewHolder.tLogTime.setText(aUtil.friendlyTime(logVo.getTime()));
viewHolder.tLogTime.setText(TimeUtil.friendlyTime(logVo.getTime()));
viewHolder.senderImage.setImageResource(logVo.getSenderImageId());
viewHolder.simImage.setImageResource(logVo.getSimImageId());
viewHolder.statusImage.setImageResource(logVo.getStatusImageId());

View File

@ -0,0 +1,77 @@
package com.idormy.sms.forwarder.utils;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
/**
* 权限相关工具类
*/
public class CommonUtil {
//获取当前版本名称
public static String getVersionName(Context context) throws Exception {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packInfo.versionName;
}
//获取当前版本号
public static Integer getVersionCode(Context context) throws Exception {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packInfo.versionCode;
}
// 检查权限是否获取android6.0及以上系统可能默认关闭权限且没提示
@SuppressLint("InlinedApi")
public static void CheckPermission(PackageManager pm, Context that) {
//PackageManager pm = getPackageManager();
boolean permission_internet = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.INTERNET", that.getPackageName()));
boolean permission_receive_boot = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.RECEIVE_BOOT_COMPLETED", that.getPackageName()));
boolean permission_foreground_service = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.FOREGROUND_SERVICE", that.getPackageName()));
boolean permission_read_external_storage = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", that.getPackageName()));
boolean permission_write_external_storage = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", that.getPackageName()));
boolean permission_receive_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.RECEIVE_SMS", that.getPackageName()));
boolean permission_read_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_SMS", that.getPackageName()));
boolean permission_send_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SEND_SMS", that.getPackageName()));
boolean permission_read_phone_state = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_PHONE_STATE", that.getPackageName()));
boolean permission_read_phone_numbers = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_PHONE_NUMBERS", that.getPackageName()));
boolean permission_read_call_log = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_CALL_LOG", that.getPackageName()));
boolean permission_read_contacts = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_CONTACTS", that.getPackageName()));
boolean permission_battery_stats = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.BATTERY_STATS", that.getPackageName()));
boolean permission_bind_notification_listener_service = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.BIND_NOTIFICATION_LISTENER_SERVICE", that.getPackageName()));
if (!(permission_internet && permission_receive_boot && permission_foreground_service &&
permission_read_external_storage && permission_write_external_storage &&
permission_receive_sms && permission_read_sms && permission_send_sms &&
permission_read_call_log && permission_read_contacts &&
permission_read_phone_state && permission_read_phone_numbers && permission_battery_stats && permission_bind_notification_listener_service)) {
ActivityCompat.requestPermissions((Activity) that, new String[]{
Manifest.permission.INTERNET,
Manifest.permission.RECEIVE_BOOT_COMPLETED,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_PHONE_NUMBERS,
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.BATTERY_STATS,
Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE,
}, 0x01);
}
}
}

View File

@ -21,8 +21,6 @@ public class InitUtil {
Log.d(TAG, "init context");
SettingUtil.init(context);
}
}
}

View File

@ -1,39 +1,44 @@
package com.idormy.sms.forwarder.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.Toast;
import com.idormy.sms.forwarder.R;
public class KeepAliveUtils {
public static boolean isIgnoreBatteryOptimization(Activity activity) {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
return powerManager.isIgnoringBatteryOptimizations(activity.getPackageName());
} else {
return true;
}
}
public static void ignoreBatteryOptimization(Activity activity) {
if (isIgnoreBatteryOptimization(activity)) {
return;
}
@SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
ResolveInfo resolveInfo = activity.getPackageManager().resolveActivity(intent, 0);
if (resolveInfo != null) {
activity.startActivity(intent);
} else {
Toast.makeText(activity, R.string.unsupport, Toast.LENGTH_SHORT).show();
}
}
}
package com.idormy.sms.forwarder.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.idormy.sms.forwarder.R;
public class KeepAliveUtils {
@RequiresApi(api = Build.VERSION_CODES.M)
public static boolean isIgnoreBatteryOptimization(Activity activity) {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
return powerManager.isIgnoringBatteryOptimizations(activity.getPackageName());
} else {
return true;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
public static void ignoreBatteryOptimization(Activity activity) {
if (isIgnoreBatteryOptimization(activity)) {
return;
}
@SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
ResolveInfo resolveInfo = activity.getPackageManager().resolveActivity(intent, 0);
if (resolveInfo != null) {
activity.startActivity(intent);
} else {
Toast.makeText(activity, R.string.unsupport, Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -1,12 +1,16 @@
package com.idormy.sms.forwarder.utils;
import android.os.Environment;
import android.text.TextUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 使用方法:
* OSUtils.ROM_TYPE romType = OSUtils.getRomType();
* OSUtil.ROM_TYPE romType = OSUtil.getRomType();
* 可能您需要对其他的ROM进行区分那么只需三步
* 使用BuildProperties获取到所有的key,遍历获取到所有的value(getProperty),或者直接找到build.prop文件
* 找到定制ROM特征的标识key/value
@ -14,7 +18,30 @@ import java.io.IOException;
* 作者YouAreMyShine
* 链接https://www.jianshu.com/p/bb1f765a425f
*/
public class OSUtils {
public class OSUtil {
/**
* 判断是否为MIUI系统参考http://blog.csdn.net/xx326664162/article/details/52438706
*
* @return 返回结果
*/
@SuppressWarnings("unused")
public static boolean isMIUI() {
try {
String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}
/**
* MIUI ROM标识
* <p>

View File

@ -2,7 +2,6 @@ package com.idormy.sms.forwarder.utils;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
@ -528,49 +527,6 @@ public class PhoneUtils {
return list;
}
// 检查权限是否获取android6.0及以上系统可能默认关闭权限且没提示
@SuppressLint("InlinedApi")
public static void CheckPermission(PackageManager pm, Context that) {
//PackageManager pm = getPackageManager();
boolean permission_internet = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.INTERNET", that.getPackageName()));
boolean permission_receive_boot = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.RECEIVE_BOOT_COMPLETED", that.getPackageName()));
boolean permission_foreground_service = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.FOREGROUND_SERVICE", that.getPackageName()));
boolean permission_read_external_storage = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", that.getPackageName()));
boolean permission_write_external_storage = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", that.getPackageName()));
boolean permission_receive_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.RECEIVE_SMS", that.getPackageName()));
boolean permission_read_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_SMS", that.getPackageName()));
boolean permission_send_sms = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SEND_SMS", that.getPackageName()));
boolean permission_read_phone_state = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_PHONE_STATE", that.getPackageName()));
boolean permission_read_phone_numbers = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_PHONE_NUMBERS", that.getPackageName()));
boolean permission_read_call_log = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_CALL_LOG", that.getPackageName()));
boolean permission_read_contacts = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_CONTACTS", that.getPackageName()));
boolean permission_battery_stats = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.BATTERY_STATS", that.getPackageName()));
boolean permission_bind_notification_listener_service = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.BIND_NOTIFICATION_LISTENER_SERVICE", that.getPackageName()));
if (!(permission_internet && permission_receive_boot && permission_foreground_service &&
permission_read_external_storage && permission_write_external_storage &&
permission_receive_sms && permission_read_sms && permission_send_sms &&
permission_read_call_log && permission_read_contacts &&
permission_read_phone_state && permission_read_phone_numbers && permission_battery_stats && permission_bind_notification_listener_service)) {
ActivityCompat.requestPermissions((Activity) that, new String[]{
Manifest.permission.INTERNET,
Manifest.permission.RECEIVE_BOOT_COMPLETED,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_PHONE_NUMBERS,
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.BATTERY_STATS,
Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE,
}, 0x01);
}
}
/**
* SIM 卡信息
*/

View File

@ -53,7 +53,6 @@ public class SimUtil {
return 1;
}
//通过卡槽ID获取SubscriptionId
public static int getSubscriptionIdBySimId(int simId) {
try {

View File

@ -4,9 +4,12 @@ import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.SmsManager;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.util.ArrayList;
import java.util.Objects;
@ -25,6 +28,7 @@ public class SmsUtil {
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
public static String sendSms(int subId, String mobiles, String message) {
mobiles = mobiles.replace("", ";");
Log.d(TAG, "subId = " + subId + ", mobiles = " + mobiles + ", message = " + message);

View File

@ -1,59 +1,13 @@
package com.idormy.sms.forwarder.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
public class aUtil {
/**
* 判断是否为MIUI系统参考http://blog.csdn.net/xx326664162/article/details/52438706
*
* @return 返回结果
*/
@SuppressWarnings("unused")
public static boolean isMIUI() {
try {
String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}
public static String getVersionName(Context context) throws Exception {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packInfo.versionName;
}
public static Integer getVersionCode(Context context) throws Exception {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packInfo.versionCode;
}
public class TimeUtil {
//友好时间显示
public static String friendlyTime(String utcTime) {