74 lines
2.6 KiB
Java
74 lines
2.6 KiB
Java
package androidx.core.os;
|
|
|
|
import android.os.Build;
|
|
import android.os.UserHandle;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RequiresApi;
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
@RequiresApi(17)
|
|
public class UserHandleCompat {
|
|
@Nullable
|
|
private static Method sGetUserIdMethod;
|
|
@Nullable
|
|
private static Constructor<UserHandle> sUserHandleConstructor;
|
|
|
|
@RequiresApi(24)
|
|
public static class Api24Impl {
|
|
private Api24Impl() {
|
|
}
|
|
|
|
@NonNull
|
|
public static UserHandle getUserHandleForUid(int i) {
|
|
return UserHandle.getUserHandleForUid(i);
|
|
}
|
|
}
|
|
|
|
private UserHandleCompat() {
|
|
}
|
|
|
|
private static Method getGetUserIdMethod() throws NoSuchMethodException {
|
|
if (sGetUserIdMethod == null) {
|
|
Method declaredMethod = UserHandle.class.getDeclaredMethod("getUserId", Integer.TYPE);
|
|
sGetUserIdMethod = declaredMethod;
|
|
declaredMethod.setAccessible(true);
|
|
}
|
|
return sGetUserIdMethod;
|
|
}
|
|
|
|
private static Constructor<UserHandle> getUserHandleConstructor() throws NoSuchMethodException {
|
|
if (sUserHandleConstructor == null) {
|
|
Constructor<UserHandle> declaredConstructor = UserHandle.class.getDeclaredConstructor(Integer.TYPE);
|
|
sUserHandleConstructor = declaredConstructor;
|
|
declaredConstructor.setAccessible(true);
|
|
}
|
|
return sUserHandleConstructor;
|
|
}
|
|
|
|
@NonNull
|
|
public static UserHandle getUserHandleForUid(int i) {
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
return Api24Impl.getUserHandleForUid(i);
|
|
}
|
|
try {
|
|
return getUserHandleConstructor().newInstance((Integer) getGetUserIdMethod().invoke(null, Integer.valueOf(i)));
|
|
} catch (NoSuchMethodException e) {
|
|
NoSuchMethodError noSuchMethodError = new NoSuchMethodError();
|
|
noSuchMethodError.initCause(e);
|
|
throw noSuchMethodError;
|
|
} catch (IllegalAccessException e2) {
|
|
IllegalAccessError illegalAccessError = new IllegalAccessError();
|
|
illegalAccessError.initCause(e2);
|
|
throw illegalAccessError;
|
|
} catch (InstantiationException e3) {
|
|
InstantiationError instantiationError = new InstantiationError();
|
|
instantiationError.initCause(e3);
|
|
throw instantiationError;
|
|
} catch (InvocationTargetException e4) {
|
|
throw new RuntimeException(e4);
|
|
}
|
|
}
|
|
}
|