优化:保活措施-1像素透明Activity保活(使进程的优先级在屏幕锁屏时间由4提升为最高优先级1)

pull/127/head
pppscn 3 years ago
parent 1200264e4e
commit 17a538e3e2

@ -117,6 +117,16 @@
android:name=".AppListActivity"
android:exported="true"
android:label="@string/app_list" />
<activity
android:name=".OnePixelActivity"
android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:label="@string/one_pixel"
android:launchMode="singleInstance"
android:process=":live"
android:theme="@style/OnePixelActivity" />
<receiver
android:name=".receiver.RebootBroadcastReceiver"
@ -149,6 +159,7 @@
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".receiver.OnePixelReceiver" />
<service
android:name=".service.FrontService"

@ -45,6 +45,7 @@ import com.idormy.sms.forwarder.utils.HttpUtil;
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.OnePixelManager;
import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.RuleUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
@ -67,6 +68,7 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
private RefreshListView listView;
private Intent serviceIntent;
private String currentType = "sms";
OnePixelManager onePixelManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -78,9 +80,6 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
//是否同意隐私协议
if (!MyApplication.allowPrivacyPolicy) return;
//获取SIM信息
PhoneUtils.init(this);
//短信&网络组件初始化
SmsUtil.init(this);
NetUtil.init(this);
@ -118,6 +117,16 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
}
}
//1像素透明Activity保活
if (SettingUtil.getOnePixelActivity()) {
try {
onePixelManager = new OnePixelManager();
onePixelManager.registerOnePixelReceiver(this);//注册广播接收者
} catch (Exception e) {
Log.e(TAG, "OnePixelManager:", e);
}
}
HttpUtil.init(this);
//启用HttpServer
if (SettingUtil.getSwitchEnableHttpServer()) {
@ -359,6 +368,8 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
} catch (Exception e) {
Log.e(TAG, "onDestroy:", e);
}
if (onePixelManager != null) onePixelManager.unregisterOnePixelReceiver(this);
}
@Override

@ -115,6 +115,7 @@ public class MyApplication extends Application {
}
//SIM卡插拔状态广播监听
PhoneUtils.init(this);
IntentFilter simStateFilter = new IntentFilter(SimStateReceiver.ACTION_SIM_STATE_CHANGED);
registerReceiver(new SimStateReceiver(), simStateFilter);

@ -0,0 +1,65 @@
package com.idormy.sms.forwarder;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import com.idormy.sms.forwarder.utils.OnePixelManager;
public class OnePixelActivity extends Activity {
private static final String TAG = "OnePixelActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.setGravity(Gravity.START | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.height = 1;
params.width = 1;
window.setAttributes(params);
OnePixelManager manager = new OnePixelManager();
manager.setKeepAliveReference(this);//将引用传给OnePixelManager
Log.e(TAG, "onCreate");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
}

@ -45,6 +45,7 @@ import com.idormy.sms.forwarder.utils.Define;
import com.idormy.sms.forwarder.utils.HttpUtil;
import com.idormy.sms.forwarder.utils.KeepAliveUtils;
import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.OnePixelManager;
import com.idormy.sms.forwarder.utils.RuleUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.view.ClearEditText;
@ -109,6 +110,8 @@ public class SettingActivity extends AppCompatActivity {
switchExcludeFromRecents(findViewById(R.id.switch_exclude_from_recents));
//后台播放无声音乐
switchPlaySilenceMusic(findViewById(R.id.switch_play_silence_music));
//1像素透明Activity保活
switchOnePixelActivity(findViewById(R.id.switch_one_pixel_activity));
//接口请求失败重试时间间隔
editRetryDelayTime(findViewById(R.id.et_retry_times), findViewById(R.id.et_delay_time));
@ -688,6 +691,24 @@ public class SettingActivity extends AppCompatActivity {
});
}
//1像素透明Activity保活
@SuppressLint("ObsoleteSdkInt,UseSwitchCompatOrMaterialCode")
private void switchOnePixelActivity(Switch switch_one_pixel_activity) {
switch_one_pixel_activity.setChecked(SettingUtil.getOnePixelActivity());
switch_one_pixel_activity.setOnCheckedChangeListener((buttonView, isChecked) -> {
SettingUtil.switchOnePixelActivity(isChecked);
OnePixelManager onePixelManager = new OnePixelManager();
if (isChecked) {
onePixelManager.registerOnePixelReceiver(this);//注册广播接收者
} else {
onePixelManager.unregisterOnePixelReceiver(this);
}
Log.d(TAG, "onCheckedChanged:" + isChecked);
});
}
//接口请求失败重试时间间隔
private void editRetryDelayTime(final EditText et_retry_times, final EditText et_delay_time) {
et_retry_times.setText(String.valueOf(SettingUtil.getRetryTimes()));

@ -0,0 +1,24 @@
package com.idormy.sms.forwarder.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.idormy.sms.forwarder.utils.OnePixelManager;
public class OnePixelReceiver extends BroadcastReceiver {
private static final String TAG = "OnePixelReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
OnePixelManager manager = new OnePixelManager();
if (Intent.ACTION_SCREEN_ON.equals(action)) {//如果亮屏则关闭1像素Activity
manager.finishOnePixelActivity();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {//如果息屏则开启1像素Activity
manager.startOnePixelActivity(context);
}
}
}

@ -12,6 +12,7 @@ public class Define {
public static final String SP_MSG_KEY_STRING_CANCEL_APP_NOTIFY = "tsms_msg_key_switch_cancel_app_notify";
public static final String SP_MSG_KEY_STRING_ENABLE_EXCLUDE_FROM_RECENTS = "tsms_msg_key_switch_enable_exclude_from_recents";
public static final String SP_MSG_KEY_STRING_ENABLE_PLAY_SILENCE_MUSIC = "tsms_msg_key_switch_enable_play_silence_music";
public static final String SP_MSG_KEY_STRING_ENABLE_ONE_PIXEL_ACTIVITY = "tsms_msg_key_switch_enable_one_pixel_activity";
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_DEVICE_MARK = "tsms_msg_key_string_add_extra_device_mark";
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_SIM1 = "tsms_msg_key_string_add_extra_sim1";
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_SIM2 = "tsms_msg_key_string_add_extra_sim2";

@ -0,0 +1,72 @@
package com.idormy.sms.forwarder.utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.idormy.sms.forwarder.OnePixelActivity;
import com.idormy.sms.forwarder.receiver.OnePixelReceiver;
import java.lang.ref.WeakReference;
public class OnePixelManager {
private static final String TAG = "OnePixelManager";
private WeakReference<Activity> mActivity;
private OnePixelReceiver onePixelReceiver;
/**
* 广OnePixelReceiver
* 广
*/
public void registerOnePixelReceiver(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
onePixelReceiver = new OnePixelReceiver();
context.registerReceiver(onePixelReceiver, filter);
}
/**
* 广
*/
public void unregisterOnePixelReceiver(Context context) {
if (null != onePixelReceiver) {
context.unregisterReceiver(onePixelReceiver);
}
}
/**
* Activity
*/
public void startOnePixelActivity(Context context) {
Intent intent = new Intent();
intent.setClass(context, OnePixelActivity.class);
context.startActivity(intent);
}
/**
* Activity
*/
public void finishOnePixelActivity() {
if (null != mActivity) {
Activity activity = mActivity.get();
if (null != activity) {
activity.finish();
}
mActivity = null;
}
}
/**
* 使
*/
public void setKeepAliveReference(OnePixelActivity activity) {
mActivity = new WeakReference<>(activity);
}
}

@ -332,65 +332,63 @@ public class PhoneUtils {
@SuppressLint({"ObsoleteSdkInt", "Range"})
public static List<SimInfo> getSimMultiInfo() {
List<SimInfo> infos = new ArrayList<>();
//Log.d(TAG, "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
//Log.d(TAG, "Build.VERSION_CODES.LOLLIPOP_MR1 = " + Build.VERSION_CODES.LOLLIPOP_MR1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
Log.d(TAG, "1.版本超过5.1,调用系统方法");
//1.版本超过5.1,调用系统方法
SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> activeSubscriptionInfoList = null;
if (mSubscriptionManager != null) {
try {
try {
//Log.d(TAG, "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
//Log.d(TAG, "Build.VERSION_CODES.LOLLIPOP_MR1 = " + Build.VERSION_CODES.LOLLIPOP_MR1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
Log.d(TAG, "1.版本超过5.1,调用系统方法");
//1.版本超过5.1,调用系统方法
SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> activeSubscriptionInfoList = null;
if (mSubscriptionManager != null) {
ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
activeSubscriptionInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
} catch (Exception ignored) {
}
}
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.size() > 0) {
//1.1.1 有使用的卡,就遍历所有卡
for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
SimInfo simInfo = new SimInfo();
simInfo.mCarrierName = subscriptionInfo.getCarrierName();
simInfo.mIccId = subscriptionInfo.getIccId();
simInfo.mSimSlotIndex = subscriptionInfo.getSimSlotIndex();
simInfo.mNumber = subscriptionInfo.getNumber();
simInfo.mCountryIso = subscriptionInfo.getCountryIso();
simInfo.mSubscriptionId = subscriptionInfo.getSubscriptionId();
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.size() > 0) {
//1.1.1 有使用的卡,就遍历所有卡
for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
SimInfo simInfo = new SimInfo();
simInfo.mCarrierName = subscriptionInfo.getCarrierName();
simInfo.mIccId = subscriptionInfo.getIccId();
simInfo.mSimSlotIndex = subscriptionInfo.getSimSlotIndex();
simInfo.mNumber = subscriptionInfo.getNumber();
simInfo.mCountryIso = subscriptionInfo.getCountryIso();
simInfo.mSubscriptionId = subscriptionInfo.getSubscriptionId();
/*try {
simInfo.mImei = getReflexMethodWithId(context, "getDeviceId", String.valueOf(simInfo.mSimSlotIndex));
simInfo.mImsi = getReflexMethodWithId(context, "getSubscriberId", String.valueOf(subscriptionInfo.getSubscriptionId()));
} catch (MethodNotFoundException ignored) {
}*/
Log.d(TAG, String.valueOf(simInfo));
infos.add(simInfo);
Log.d(TAG, String.valueOf(simInfo));
infos.add(simInfo);
}
}
}
} else {
Log.d(TAG, "2.版本低于5.1的系统,首先调用数据库,看能不能访问到");
//2.版本低于5.1的系统,首先调用数据库,看能不能访问到
Uri uri = Uri.parse("content://telephony/siminfo"); //访问raw_contacts表
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "icc_id", "sim_id", "display_name", "carrier_name", "name_source", "color", "number", "display_number_format", "data_roaming", "mcc", "mnc"}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
SimInfo simInfo = new SimInfo();
simInfo.mCarrierName = cursor.getString(cursor.getColumnIndex("carrier_name"));
simInfo.mIccId = cursor.getString(cursor.getColumnIndex("icc_id"));
simInfo.mSimSlotIndex = cursor.getInt(cursor.getColumnIndex("sim_id"));
simInfo.mNumber = cursor.getString(cursor.getColumnIndex("number"));
simInfo.mCountryIso = cursor.getString(cursor.getColumnIndex("mcc"));
String id = cursor.getString(cursor.getColumnIndex("_id"));
} else {
Log.d(TAG, "2.版本低于5.1的系统,首先调用数据库,看能不能访问到");
//2.版本低于5.1的系统,首先调用数据库,看能不能访问到
Uri uri = Uri.parse("content://telephony/siminfo"); //访问raw_contacts表
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "icc_id", "sim_id", "display_name", "carrier_name", "name_source", "color", "number", "display_number_format", "data_roaming", "mcc", "mnc"}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
SimInfo simInfo = new SimInfo();
simInfo.mCarrierName = cursor.getString(cursor.getColumnIndex("carrier_name"));
simInfo.mIccId = cursor.getString(cursor.getColumnIndex("icc_id"));
simInfo.mSimSlotIndex = cursor.getInt(cursor.getColumnIndex("sim_id"));
simInfo.mNumber = cursor.getString(cursor.getColumnIndex("number"));
simInfo.mCountryIso = cursor.getString(cursor.getColumnIndex("mcc"));
String id = cursor.getString(cursor.getColumnIndex("_id"));
/*try {
simInfo.mImei = getReflexMethodWithId(context, "getDeviceId", String.valueOf(simInfo.mSimSlotIndex));
simInfo.mImsi = getReflexMethodWithId(context, "getSubscriberId", String.valueOf(id));
} catch (MethodNotFoundException ignored) {
}*/
Log.d(TAG, String.valueOf(simInfo));
infos.add(simInfo);
} while (cursor.moveToNext());
cursor.close();
Log.d(TAG, String.valueOf(simInfo));
infos.add(simInfo);
} while (cursor.moveToNext());
cursor.close();
}
}
}
/*Log.d(TAG, "3.通过反射读取卡槽信息最后通过IMEI去重");
//3.通过反射读取卡槽信息最后通过IMEI去重
@ -404,6 +402,9 @@ public class PhoneUtils {
}
}
return simInfos;*/
} catch (Exception e) {
e.printStackTrace();
}
return infos;
}

@ -101,6 +101,14 @@ public class SettingUtil {
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_PLAY_SILENCE_MUSIC, false);
}
public static void switchOnePixelActivity(Boolean enable) {
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_STRING_ENABLE_ONE_PIXEL_ACTIVITY, enable).apply();
}
public static boolean getOnePixelActivity() {
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_ONE_PIXEL_ACTIVITY, false);
}
public static void switchSmsTemplate(Boolean switchSmsTemplate) {
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_SWITCH_SMS_TEMPLATE, switchSmsTemplate).apply();
}

@ -700,6 +700,47 @@
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="@color/setting_bar_color"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/one_pixel_activity"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/one_pixel_activity_tips"
android:textSize="9sp"
tools:ignore="SmallSp" />
</LinearLayout>
<Switch
android:id="@+id/switch_one_pixel_activity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textSize="16sp"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

@ -436,4 +436,7 @@
<string name="tips_first_time">\n&#160;&#160;&#160;&#160;首次使用请按照1234步骤顺序设置数字点亮表示该步骤已设置(4点亮表示有成功日志)\n\n&#160;&#160;&#160;&#160;根据以往用户反馈90%的新用户最终排查结果都是手机权限设置问题5%的用户是APP保活问题其他问题排查参考Wiki中的常见问题APP右上角使用帮助入口\n\n&#160;&#160;&#160;&#160;强烈建议手动排查一下系统设置自启动、电源策略、APP的权限把所有权限打开始终允许不要“使用中允许”MIUI系统就全部变绿特别是一些国产定制系统加入隐私保护、验证码保护等例如MIUI 系统的 空白通行证)\n\n&#160;&#160;&#160;&#160;请选择是否立即“前往系统设置”检查,或“稍后自行处理”?</string>
<string name="play_silence_music">Play silent music in the background</string>
<string name="play_silence_music_tips">Play silent music to keep running in the background, which may consume more power, enable it on demand</string>
<string name="one_pixel">One Pixel</string>
<string name="one_pixel_activity">One Pixel Activity</string>
<string name="one_pixel_activity_tips">This can change the process priority from 4 to 1</string>
</resources>

@ -102,4 +102,8 @@
<attr name="current_step" format="string" />
<attr name="help_tip" format="string" />
</declare-styleable>
<style name="OnePixelActivity" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>

@ -434,5 +434,8 @@
<string name="toast_denied">获取必需的权限失败APP功能可能受限</string>
<string name="tips_first_time">\n&#160;&#160;&#160;&#160;首次使用请按照1234步骤顺序设置数字点亮表示该步骤已设置(4点亮表示有成功日志)\n\n&#160;&#160;&#160;&#160;根据以往用户反馈90%的新用户最终排查结果都是手机权限设置问题5%的用户是APP保活问题其他问题排查参考Wiki中的常见问题APP右上角使用帮助入口\n\n&#160;&#160;&#160;&#160;强烈建议手动排查一下系统设置自启动、电源策略、APP的权限把所有权限打开始终允许不要“使用中允许”MIUI系统就全部变绿特别是一些国产定制系统加入隐私保护、验证码保护等例如MIUI 系统的 空白通行证)\n\n&#160;&#160;&#160;&#160;请选择是否立即“前往系统设置”检查,或“稍后自行处理”?</string>
<string name="play_silence_music">后台播放无声音乐</string>
<string name="play_silence_music_tips">播放无声音乐让后台一直运行,可能比较耗电,按需启用</string>
<string name="play_silence_music_tips">【按需启用】播放无声音乐让后台一直运行,可能比较耗电</string>
<string name="one_pixel">1像素</string>
<string name="one_pixel_activity">1像素透明Activity保活</string>
<string name="one_pixel_activity_tips">【按需启用】使进程的优先级在屏幕锁屏时间由4提升为最高优先级1</string>
</resources>

@ -102,4 +102,8 @@
<attr name="current_step" format="string" />
<attr name="help_tip" format="string" />
</declare-styleable>
<style name="OnePixelActivity" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>

Loading…
Cancel
Save