/** * java.io always writes every byte it's asked to, or fails with an error. (That is, unlike * Unix it never just writes as many bytes as happens to be convenient.) */ public static void write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { ArrayUtils.checkOffsetAndCount(bytes.length, byteOffset, byteCount); if (byteCount == 0) { return; } try { while (byteCount > 0) { int bytesWritten = Os.write(fd, bytes, byteOffset, byteCount); byteCount -= bytesWritten; byteOffset += bytesWritten; } } catch (ErrnoException errnoException) { throw new IOException(errnoException); } } }
public boolean exists(int userId) { synchronized (mPackagesLock) { return ArrayUtils.contains(mUserIds, userId); } }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { Intent[] intents = ArrayUtils.getFirst(args, Intent[].class); String[] resolvedTypes = ArrayUtils.getFirst(args, String[].class); IBinder token = null; int tokenIndex = ArrayUtils.indexOfObject(args, IBinder.class, 2); if (tokenIndex != -1) { token = (IBinder) args[tokenIndex]; } Bundle options = ArrayUtils.getFirst(args, Bundle.class); return VActivityManager.get().startActivities(intents, resolvedTypes, token, options, VUserHandle.myUserId()); }
public static <T> T getFirstParam(Object[] args, Class<T> tClass) { if (args == null) { return null; } int index = ArrayUtils.indexOfFirst(args, tClass); if (index != -1) { return (T) args[index]; } return null; }
public static int indexOfLast(Object[] array, Class<?> type) { if (!isEmpty(array)) { for (int N = array.length; N > 0; N--) { Object one = array[N - 1]; if (one != null && one.getClass() == type) { return N - 1; } } } return -1; }
@Override public Object call(final Object who, Method method, Object... args) throws Throwable { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { LocationRequest request = (LocationRequest) args[0]; fixLocationRequest(request); } if (isFakeLocationEnable()) { Object transport = ArrayUtils.getFirst(args, mirror.android.location.LocationManager.ListenerTransport.TYPE); if (transport != null) { Object locationManager = mirror.android.location.LocationManager.ListenerTransport.this$0.get(transport); MockLocationHelper.setGpsStatus(locationManager); GPSListenerThread.get().addListenerTransport(locationManager); } return 0; } return super.call(who, method, args); } }
static ComponentName replaceLastAppComponent(Object[] args, String hostPkg) { int index = ArrayUtils.indexOfLast(args, ComponentName.class); if (index != -1) { ComponentName orig = (ComponentName) args[index]; ComponentName newComponent = new ComponentName(hostPkg, orig.getClassName()); args[index] = newComponent; return newComponent; } return null; } }
public static <T> boolean contains(T[] array, T value) { return indexOf(array, value) != -1; } public static boolean contains(int[] array, int value) {
private void realStartActivitiesLocked(IBinder resultTo, Intent[] intents, String[] resolvedTypes, Bundle options) { Class<?>[] types = IActivityManager.startActivities.paramList(); Object[] args = new Object[types.length]; if (types[0] == IApplicationThread.TYPE) { args[0] = ActivityThread.getApplicationThread.call(VirtualCore.mainThread()); } int pkgIndex = ArrayUtils.protoIndexOf(types, String.class); int intentsIndex = ArrayUtils.protoIndexOf(types, Intent[].class); int resultToIndex = ArrayUtils.protoIndexOf(types, IBinder.class, 2); int optionsIndex = ArrayUtils.protoIndexOf(types, Bundle.class); int resolvedTypesIndex = intentsIndex + 1; if (pkgIndex != -1) { args[pkgIndex] = VirtualCore.get().getHostPkg(); } args[intentsIndex] = intents; args[resultToIndex] = resultTo; args[resolvedTypesIndex] = resolvedTypes; args[optionsIndex] = options; ClassUtils.fixArgs(types, args); IActivityManager.startActivities.call(ActivityManagerNative.getDefault.call(), (Object[]) args); }
@SuppressWarnings("unchecked") public static <T> T getFirst(Object[] args, Class<?> clazz) { int index = indexOfFirst(args, clazz); if (index != -1) { return (T) args[index]; } return null; }
public static int indexOfFirst(Object[] array, Class<?> type) { if (!isEmpty(array)) { int N = -1; for (Object one : array) { N++; if (one != null && type == one.getClass()) { return N; } } } return -1; }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { if (!isFakeLocationEnable()) { return super.call(who, method, args); } Object transport = ArrayUtils.getFirst(args, mirror.android.location.LocationManager.GnssStatusListenerTransport.TYPE); if (transport != null) { mirror.android.location.LocationManager.GnssStatusListenerTransport.onGnssStarted.call(transport, new Object[0]); if (mirror.android.location.LocationManager.GnssStatusListenerTransport.mGpsListener.get(transport) != null) { MockLocationHelper.invokeSvStatusChanged(transport); } else { MockLocationHelper.invokeNmeaReceived(transport); } mirror.android.location.LocationManager.GnssStatusListenerTransport.onFirstFix.call(transport, Integer.valueOf(0)); Object locationManager = mirror.android.location.LocationManager.GnssStatusListenerTransport.this$0.get(transport); GPSListenerThread.get().addListenerTransport(locationManager); } return true; } }
public static String replaceLastAppPkg(Object[] args) { int index = ArrayUtils.indexOfLast(args, String.class); if (index != -1) { String pkg = (String) args[index]; args[index] = VirtualCore.get().getHostPkg(); return pkg; } return null; }
public static String replaceSequenceAppPkg(Object[] args, int sequence) { int index = ArrayUtils.indexOf(args, String.class, sequence); if (index != -1) { String pkg = (String) args[index]; args[index] = VirtualCore.get().getHostPkg(); return pkg; } return null; }
private void realStartActivityLocked(IBinder resultTo, Intent intent, String resultWho, int requestCode, Bundle options) { Class<?>[] types = mirror.android.app.IActivityManager.startActivity.paramList(); Object[] args = new Object[types.length]; if (types[0] == IApplicationThread.TYPE) { args[0] = ActivityThread.getApplicationThread.call(VirtualCore.mainThread()); } int intentIndex = ArrayUtils.protoIndexOf(types, Intent.class); int resultToIndex = ArrayUtils.protoIndexOf(types, IBinder.class, 2); int optionsIndex = ArrayUtils.protoIndexOf(types, Bundle.class); int resolvedTypeIndex = intentIndex + 1; int resultWhoIndex = resultToIndex + 1; int requestCodeIndex = resultToIndex + 2; args[intentIndex] = intent; args[resultToIndex] = resultTo; args[resultWhoIndex] = resultWho; args[requestCodeIndex] = requestCode; if (optionsIndex != -1) { args[optionsIndex] = options; } args[resolvedTypeIndex] = intent.getType(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { args[intentIndex - 1] = VirtualCore.get().getHostPkg(); } ClassUtils.fixArgs(types, args); mirror.android.app.IActivityManager.startActivity.call(ActivityManagerNative.getDefault.call(), (Object[]) args); }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { int index = ArrayUtils.indexOfFirst(args, WindowManager.LayoutParams.class); if (index != -1) { WindowManager.LayoutParams attrs = (WindowManager.LayoutParams) args[index]; if (attrs != null) { attrs.packageName = getHostPkg(); } } return method.invoke(who, args); } }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { int intentIndex = ArrayUtils.indexOfObject(args, Intent.class, 1); if (intentIndex < 0) { return ActivityManagerCompat.START_INTENT_NOT_RESOLVED; int resultToIndex = ArrayUtils.indexOfObject(args, IBinder.class, 2); String resolvedType = (String) args[intentIndex + 1]; Intent intent = (Intent) args[intentIndex]; Bundle options = ArrayUtils.getFirst(args, Bundle.class); if (resultTo != null) { resultWho = (String) args[resultToIndex + 1];
public static int indexOf(Object[] array, Class<?> type, int sequence) { if (!isEmpty(array)) { int N = -1; for (Object one : array) { N++; if (one != null && one.getClass() == type) { if (--sequence <= 0) { return N; } } } } return -1; }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { if (isFakeLocationEnable()) { Object transport = ArrayUtils.getFirst(args, mirror.android.location.LocationManager.GpsStatusListenerTransport.TYPE); Object locationManager = mirror.android.location.LocationManager.GpsStatusListenerTransport.this$0.get(transport); mirror.android.location.LocationManager.GpsStatusListenerTransport.onGpsStarted.call(transport); mirror.android.location.LocationManager.GpsStatusListenerTransport.onFirstFix.call(transport, 0); if (mirror.android.location.LocationManager.GpsStatusListenerTransport.mListener.get(transport) != null) { MockLocationHelper.invokeSvStatusChanged(transport); } else { MockLocationHelper.invokeNmeaReceived(transport); } GPSListenerThread.get().addListenerTransport(locationManager); return true; } return super.call(who, method, args); } }
@Override public boolean beforeCall(Object who, Method method, Object... args) { int index = ArrayUtils.indexOfLast(args, Integer.class); if (index != -1) { int uid = (int) args[index]; if (uid == Process.myUid()) { args[index] = getRealUid(); } } return super.beforeCall(who, method, args); } }