提交 8c44ce6a authored 作者: kongdywang's avatar kongdywang

fix ANR when brightness changed

上级 365473c9
...@@ -7,11 +7,9 @@ import android.content.ContentResolver; ...@@ -7,11 +7,9 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
...@@ -41,6 +39,7 @@ import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterNativeAPI; ...@@ -41,6 +39,7 @@ import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterNativeAPI;
import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterSuperPlayerPluginAPI; import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterSuperPlayerPluginAPI;
import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterVodPlayerApi; import com.tencent.vod.flutter.messages.FtxMessages.TXFlutterVodPlayerApi;
import com.tencent.vod.flutter.tools.CommonUtil; import com.tencent.vod.flutter.tools.CommonUtil;
import com.tencent.vod.flutter.tools.TXFlutterEngineHolder;
import com.tencent.vod.flutter.ui.Android12BridgeService; import com.tencent.vod.flutter.ui.Android12BridgeService;
import java.io.File; import java.io.File;
...@@ -84,6 +83,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -84,6 +83,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
private OrientationEventListener mOrientationManager; private OrientationEventListener mOrientationManager;
private int mCurrentOrientation = FTXEvent.ORIENTATION_PORTRAIT_UP; private int mCurrentOrientation = FTXEvent.ORIENTATION_PORTRAIT_UP;
private final TXFlutterEngineHolder mEngineHolder = new TXFlutterEngineHolder();
private final FTXAudioManager.AudioFocusChangeListener audioFocusChangeListener = private final FTXAudioManager.AudioFocusChangeListener audioFocusChangeListener =
new FTXAudioManager.AudioFocusChangeListener() { new FTXAudioManager.AudioFocusChangeListener() {
...@@ -102,9 +102,11 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -102,9 +102,11 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
@Override @Override
public void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags) { public void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags) {
super.onChange(selfChange, uris, flags); super.onChange(selfChange, uris, flags);
if (mEngineHolder.isInForeground()) {
double systemBrightness = getSystemScreenBrightness(); double systemBrightness = getSystemScreenBrightness();
setWindowBrightness(systemBrightness); setWindowBrightness(systemBrightness);
} }
}
}; };
@Override @Override
...@@ -262,7 +264,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -262,7 +264,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
public void onOrientationChanged(int orientation) { public void onOrientationChanged(int orientation) {
if (isDeviceAutoRotateOn()) { if (isDeviceAutoRotateOn()) {
int orientationEvent = mCurrentOrientation; int orientationEvent = mCurrentOrientation;
// 每个方向判断当前方向正负30度,共计60度的区间 // Each direction judges the current direction with an interval
// of 60 degrees, with a total of 6 intervals.
if (((orientation >= 0) && (orientation < 30)) || (orientation > 330)) { if (((orientation >= 0) && (orientation < 30)) || (orientation > 330)) {
orientationEvent = FTXEvent.ORIENTATION_PORTRAIT_UP; orientationEvent = FTXEvent.ORIENTATION_PORTRAIT_UP;
} else if (orientation > 240 && orientation < 300) { } else if (orientation > 240 && orientation < 300) {
...@@ -291,6 +294,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -291,6 +294,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
} }
/** /**
* Set the current window brightness.
*
* 设置当前window亮度 * 设置当前window亮度
*/ */
private void setWindowBrightness(Double brightness) { private void setWindowBrightness(Double brightness) {
...@@ -314,6 +319,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -314,6 +319,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
} }
/** /**
* Get the current window brightness. If the current window brightness is not assigned,
* return the current system brightness.
*
* 获得当前window亮度,如果当前window亮度未赋值,则返回当前系统亮度 * 获得当前window亮度,如果当前window亮度未赋值,则返回当前系统亮度
*/ */
private float getWindowBrightness() { private float getWindowBrightness() {
...@@ -334,7 +342,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -334,7 +342,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
try { try {
ContentResolver resolver = mActivityPluginBinding.getActivity().getContentResolver(); ContentResolver resolver = mActivityPluginBinding.getActivity().getContentResolver();
final int brightnessInt = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); final int brightnessInt = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
final float maxBrightness = getBrightnessMax(); final float maxBrightness = CommonUtil.getBrightnessMax();
screenBrightness = brightnessInt / maxBrightness; screenBrightness = brightnessInt / maxBrightness;
} catch (SettingNotFoundException e) { } catch (SettingNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -342,30 +350,6 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -342,30 +350,6 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
return screenBrightness; return screenBrightness;
} }
/**
* 获取最大亮度,兼容MIUI部分系统亮度最大值不是255的情况.
* MIUI在android 13以后,系统最大亮度与配置不符,变为128
*
* @return max
*/
private float getBrightnessMax() {
if (CommonUtil.isMIUI()) {
if (Build.VERSION.SDK_INT < 33) {
try {
Resources system = Resources.getSystem();
int resId = system.getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
if (resId != 0) {
return system.getInteger(resId);
}
} catch (Exception e) {
Log.getStackTraceString(e);
}
} else {
return 128;
}
}
return 255F;
}
private void initAudioManagerIfNeed() { private void initAudioManagerIfNeed() {
if (null == mTxAudioManager) { if (null == mTxAudioManager) {
...@@ -393,10 +377,14 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -393,10 +377,14 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
@Override @Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
if (null != mActivityPluginBinding && mActivityPluginBinding != binding) {
mEngineHolder.destroy(binding);
}
mActivityPluginBinding = binding; mActivityPluginBinding = binding;
initAudioManagerIfNeed(); initAudioManagerIfNeed();
initPipManagerIfNeed(); initPipManagerIfNeed();
registerReceiver(); registerReceiver();
mEngineHolder.attachBindLife(binding);
} }
@Override @Override
...@@ -412,10 +400,13 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -412,10 +400,13 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
if (null != mTxPipManager) { if (null != mTxPipManager) {
mTxPipManager.releaseActivityListener(); mTxPipManager.releaseActivityListener();
} }
// Close the solution to the problem of the picture-in-picture click restore
// failure on some versions of Android 12.
// 关闭用于解决Android12部分版本上画中画点击还原失灵的问题 // 关闭用于解决Android12部分版本上画中画点击还原失灵的问题
Intent serviceIntent = new Intent(mActivityPluginBinding.getActivity(), Android12BridgeService.class); Intent serviceIntent = new Intent(mActivityPluginBinding.getActivity(), Android12BridgeService.class);
mActivityPluginBinding.getActivity().stopService(serviceIntent); mActivityPluginBinding.getActivity().stopService(serviceIntent);
unregisterReceiver(); unregisterReceiver();
mEngineHolder.destroy(mActivityPluginBinding);
} }
void onHandleAudioFocusPause() { void onHandleAudioFocusPause() {
...@@ -427,9 +418,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -427,9 +418,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
} }
/** /**
* 系统是否允许自动旋转屏幕 * Whether the system allows automatic screen rotation.
* *
* @return * 系统是否允许自动旋转屏幕
*/ */
protected boolean isDeviceAutoRotateOn() { protected boolean isDeviceAutoRotateOn() {
//获取系统是否允许自动旋转屏幕 //获取系统是否允许自动旋转屏幕
...@@ -444,6 +435,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -444,6 +435,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
} }
/** /**
* Register volume broadcast receiver.
*
* 注册音量广播接收器 * 注册音量广播接收器
*/ */
public void registerReceiver() { public void registerReceiver() {
...@@ -459,6 +452,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -459,6 +452,8 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
} }
/** /**
* Unregister volume broadcast listener. It needs to be used in pairs with registerReceiver.
*
* 反注册音量广播监听器,需要与 registerReceiver 成对使用 * 反注册音量广播监听器,需要与 registerReceiver 成对使用
*/ */
public void unregisterReceiver() { public void unregisterReceiver() {
...@@ -552,7 +547,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware, ...@@ -552,7 +547,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, ActivityAware,
private class VolumeBroadcastReceiver extends BroadcastReceiver { private class VolumeBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
//媒体音量改变才通知 // Notify only when the media volume changes
if (VOLUME_CHANGED_ACTION.equals(intent.getAction()) if (VOLUME_CHANGED_ACTION.equals(intent.getAction())
&& (intent.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC)) { && (intent.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC)) {
mEventSink.success(getParams(FTXEvent.EVENT_VOLUME_CHANGED, null)); mEventSink.success(getParams(FTXEvent.EVENT_VOLUME_CHANGED, null));
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
package com.tencent.vod.flutter.tools; package com.tencent.vod.flutter.tools;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
...@@ -18,24 +19,26 @@ import com.tencent.vod.flutter.messages.FtxMessages.StringMsg; ...@@ -18,24 +19,26 @@ import com.tencent.vod.flutter.messages.FtxMessages.StringMsg;
import com.tencent.vod.flutter.messages.FtxMessages.UInt8ListMsg; import com.tencent.vod.flutter.messages.FtxMessages.UInt8ListMsg;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
/** /**
* Common utility class.
* <p>
* 通用工具类 * 通用工具类
*/ */
public class CommonUtil { public class CommonUtil {
private static final String TAG = "CommonUtil"; private static final String TAG = "CommonUtil";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; private static final String KEY_MAX_BRIGHTNESS = "max_brightness";
private static final String KEY_IS_MIUI = "is_miui";
private static final Map<String, Object> CACHE_MAP = new HashMap<>();
static final Map<Integer, Integer> DOWNLOAD_STATE_MAP = new HashMap<Integer, Integer>() {{ static final Map<Integer, Integer> DOWNLOAD_STATE_MAP = new HashMap<Integer, Integer>() {{
put(TXVodDownloadMediaInfo.STATE_INIT, FTXEvent.EVENT_DOWNLOAD_START); put(TXVodDownloadMediaInfo.STATE_INIT, FTXEvent.EVENT_DOWNLOAD_START);
...@@ -45,34 +48,52 @@ public class CommonUtil { ...@@ -45,34 +48,52 @@ public class CommonUtil {
put(TXVodDownloadMediaInfo.STATE_ERROR, FTXEvent.EVENT_DOWNLOAD_ERROR); put(TXVodDownloadMediaInfo.STATE_ERROR, FTXEvent.EVENT_DOWNLOAD_ERROR);
}}; }};
public static boolean isMIUI() { /**
String pro = getProp(KEY_MIUI_VERSION_NAME); * 获取最大亮度,兼容MIUI部分系统亮度最大值不是255的情况.
return TextUtils.equals(pro,"MIUI"); * MIUI在android 13以后,系统最大亮度与配置不符,变为128
} * <p>
* Get the maximum brightness, compatible with some MIUI systems where the maximum brightness is not 255.
public static String getProp(String name) { * After Android 13, MIUI's maximum brightness is inconsistent with the configuration and becomes 128.
String line = null; *
BufferedReader input = null; * @return max
try { */
Process p = Runtime.getRuntime().exec("getprop " + name); public static float getBrightnessMax() {
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); if (CACHE_MAP.containsKey(KEY_MAX_BRIGHTNESS)) {
line = input.readLine(); //noinspection ConstantConditions
input.close(); return (float) CACHE_MAP.get(KEY_MAX_BRIGHTNESS);
} catch (IOException ex) { } else if (CommonUtil.isMIUI()) {
Log.e(TAG, "Unable to read prop " + name, ex); float maxBrightness = 255f;
return null; if (Build.VERSION.SDK_INT < 33) {
} finally {
if (input != null) {
try { try {
input.close(); Resources system = Resources.getSystem();
} catch (IOException e) { int resId = system.getIdentifier("config_screenBrightnessSettingMaximum",
e.printStackTrace(); "integer", "android");
if (resId != 0) {
maxBrightness = system.getInteger(resId);
} }
} catch (Exception e) {
Log.getStackTraceString(e);
} }
} else {
maxBrightness = 128F;
}
CACHE_MAP.put(KEY_MAX_BRIGHTNESS, maxBrightness);
return maxBrightness;
}
CACHE_MAP.put(KEY_MAX_BRIGHTNESS, 255f);
return 255f;
} }
return line; public static boolean isMIUI() {
if (CACHE_MAP.containsKey(KEY_IS_MIUI)) {
//noinspection ConstantConditions
return (boolean) CACHE_MAP.get(KEY_IS_MIUI);
} else {
String pro = Build.MANUFACTURER;
boolean isMiui = TextUtils.equals(pro, "Xiaomi");
CACHE_MAP.put(KEY_IS_MIUI, isMiui);
return isMiui;
}
} }
public static Map<String, Object> getParams(int event, Bundle bundle) { public static Map<String, Object> getParams(int event, Bundle bundle) {
......
package com.tencent.vod.flutter.tools;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.Log;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
public class TXFlutterEngineHolder {
private static final String TAG = "TXFlutterEngineHolder";
private int mFrontContextCount = 0;
private Application.ActivityLifecycleCallbacks mLifeCallback;
public void attachBindLife(ActivityPluginBinding binding) {
if (mLifeCallback != null) {
Log.w(TAG, "TXFlutterEngineHolder is already attach");
return;
}
if (null == binding) {
return;
}
if (binding.getActivity().isDestroyed() || binding.getActivity().isFinishing()) {
return;
}
if (null == binding.getActivity().getApplication()) {
return;
}
mLifeCallback = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
mFrontContextCount++;
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
mFrontContextCount--;
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
}
};
binding.getActivity().getApplication().registerActivityLifecycleCallbacks(mLifeCallback);
}
public boolean isInForeground() {
return mFrontContextCount > 0;
}
public void destroy(ActivityPluginBinding binding) {
if (null == mLifeCallback) {
return;
}
if (null == binding) {
return;
}
if (binding.getActivity().isDestroyed()) {
return;
}
if (null == binding.getActivity().getApplication()) {
return;
}
binding.getActivity().getApplication().unregisterActivityLifecycleCallbacks(mLifeCallback);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论