Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce8d3ac6d1 | ||
|
|
6414613113 |
@@ -2,23 +2,42 @@ package com.genymobile.scrcpy;
|
|||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.AttributionSource;
|
import android.content.AttributionSource;
|
||||||
import android.content.MutableContextWrapper;
|
import android.content.Context;
|
||||||
|
import android.content.ContextWrapper;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
|
|
||||||
public final class FakeContext extends MutableContextWrapper {
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public final class FakeContext extends ContextWrapper {
|
||||||
|
|
||||||
public static final String PACKAGE_NAME = "com.android.shell";
|
public static final String PACKAGE_NAME = "com.android.shell";
|
||||||
public static final int ROOT_UID = 0; // Like android.os.Process.ROOT_UID, but before API 29
|
public static final int ROOT_UID = 0; // Like android.os.Process.ROOT_UID, but before API 29
|
||||||
|
|
||||||
private static final FakeContext INSTANCE = new FakeContext();
|
private static final FakeContext INSTANCE = new FakeContext();
|
||||||
|
|
||||||
|
private static Context retrieveSystemContext() {
|
||||||
|
try {
|
||||||
|
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
|
||||||
|
Constructor<?> activityThreadConstructor = activityThreadClass.getDeclaredConstructor();
|
||||||
|
activityThreadConstructor.setAccessible(true);
|
||||||
|
Object activityThread = activityThreadConstructor.newInstance();
|
||||||
|
|
||||||
|
Method getSystemContextMethod = activityThread.getClass().getDeclaredMethod("getSystemContext");
|
||||||
|
return (Context) getSystemContextMethod.invoke(activityThread);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Ln.e("Cannot retrieve system context", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static FakeContext get() {
|
public static FakeContext get() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private FakeContext() {
|
private FakeContext() {
|
||||||
super(null);
|
super(retrieveSystemContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void scrcpy(Options options) throws IOException, ConfigurationException {
|
private static void scrcpy(Options options) throws IOException, ConfigurationException {
|
||||||
Ln.i("Device: [" + Build.MANUFACTURER + "] " + Build.BRAND + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||||
final Device device = new Device(options);
|
final Device device = new Device(options);
|
||||||
|
|
||||||
Thread initThread = startInitThread(options);
|
Thread initThread = startInitThread(options);
|
||||||
@@ -101,23 +101,16 @@ public final class Server {
|
|||||||
|
|
||||||
Workarounds.prepareMainLooper();
|
Workarounds.prepareMainLooper();
|
||||||
|
|
||||||
|
// Workarounds must be applied for Meizu phones:
|
||||||
|
// - <https://github.com/Genymobile/scrcpy/issues/240>
|
||||||
|
// - <https://github.com/Genymobile/scrcpy/issues/365>
|
||||||
|
// - <https://github.com/Genymobile/scrcpy/issues/2656>
|
||||||
|
//
|
||||||
|
// But only apply when strictly necessary, since workarounds can cause other issues:
|
||||||
|
// - <https://github.com/Genymobile/scrcpy/issues/940>
|
||||||
|
// - <https://github.com/Genymobile/scrcpy/issues/994>
|
||||||
if (Build.BRAND.equalsIgnoreCase("meizu")) {
|
if (Build.BRAND.equalsIgnoreCase("meizu")) {
|
||||||
// Workarounds must be applied for Meizu phones:
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/240>
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/365>
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/2656>
|
|
||||||
//
|
|
||||||
// But only apply when strictly necessary, since workarounds can cause other issues:
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/940>
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/994>
|
|
||||||
Workarounds.fillAppInfo();
|
Workarounds.fillAppInfo();
|
||||||
} else if (Build.BRAND.equalsIgnoreCase("honor")) {
|
|
||||||
// Honor devices require a system context as a base context of FakeContext (and a current ActivityThread):
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/4015>
|
|
||||||
// The system context must not be set for all devices, because it would cause other problems:
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/4015#issuecomment-1595382142>
|
|
||||||
// - <https://github.com/Genymobile/scrcpy/issues/3805#issuecomment-1596148031>
|
|
||||||
Workarounds.fillBaseContext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before Android 11, audio is not supported.
|
// Before Android 11, audio is not supported.
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
|
|||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.content.AttributionSource;
|
import android.content.AttributionSource;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.ContextWrapper;
|
import android.content.ContextWrapper;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.media.AudioAttributes;
|
import android.media.AudioAttributes;
|
||||||
@@ -106,19 +105,6 @@ public final class Workarounds {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fillBaseContext() {
|
|
||||||
try {
|
|
||||||
fillActivityThread();
|
|
||||||
|
|
||||||
Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext");
|
|
||||||
Context context = (Context) getSystemContextMethod.invoke(activityThread);
|
|
||||||
FakeContext.get().setBaseContext(context);
|
|
||||||
} catch (Throwable throwable) {
|
|
||||||
// this is a workaround, so failing is not an error
|
|
||||||
Ln.d("Could not fill base context: " + throwable.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.R)
|
@TargetApi(Build.VERSION_CODES.R)
|
||||||
@SuppressLint("WrongConstant,MissingPermission,BlockedPrivateApi,SoonBlockedPrivateApi,DiscouragedPrivateApi")
|
@SuppressLint("WrongConstant,MissingPermission,BlockedPrivateApi,SoonBlockedPrivateApi,DiscouragedPrivateApi")
|
||||||
public static AudioRecord createAudioRecord(int source, int sampleRate, int channelConfig, int channels, int channelMask, int encoding) {
|
public static AudioRecord createAudioRecord(int source, int sampleRate, int channelConfig, int channels, int channelMask, int encoding) {
|
||||||
|
|||||||
Reference in New Issue
Block a user