Skip to content

Commit

Permalink
Merge remote-tracking branch 'sakura/11' into sakura/11-x86
Browse files Browse the repository at this point in the history
  • Loading branch information
hmtheboy154 committed Jan 26, 2022
2 parents 024c8d7 + 72d1d65 commit f302d6b
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 105 deletions.
7 changes: 7 additions & 0 deletions core/java/android/accounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.android.internal.annotations.GuardedBy;

import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -85,6 +86,12 @@ public Account(String name, String type, String accessId) {
if (TextUtils.isEmpty(type)) {
throw new IllegalArgumentException("the type must not be empty: " + type);
}
if (name.length() > 200) {
throw new IllegalArgumentException("account name is longer than 200 characters");
}
if (type.length() > 200) {
throw new IllegalArgumentException("account type is longer than 200 characters");
}
this.name = name;
this.type = type;
this.accessId = accessId;
Expand Down
8 changes: 8 additions & 0 deletions core/java/android/app/admin/DevicePolicyManagerInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package android.app.admin;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.content.ComponentName;
Expand Down Expand Up @@ -76,6 +77,13 @@ public interface OnCrossProfileWidgetProvidersChangeListener {
public abstract void addOnCrossProfileWidgetProvidersChangeListener(
OnCrossProfileWidgetProvidersChangeListener listener);

/**
* @param userHandle the handle of the user whose profile owner is being fetched.
* @return the configured supervision app if it exists and is the device owner or policy owner.
*/
public abstract @Nullable ComponentName getProfileOwnerOrDeviceOwnerSupervisionComponent(
@NonNull UserHandle userHandle);

/**
* Checks if an app with given uid is an active device admin of its user and has the policy
* specified.
Expand Down
116 changes: 76 additions & 40 deletions core/java/com/android/internal/infra/AndroidFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,21 @@

package com.android.internal.infra;

import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR;

import android.annotation.CallSuper;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Handler;
import android.os.Message;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.ExceptionUtils;
import android.util.EventLog;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
import com.android.internal.util.function.pooled.PooledLambda;

import java.lang.reflect.Constructor;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -75,14 +73,16 @@ public class AndroidFuture<T> extends CompletableFuture<T> implements Parcelable

private static final boolean DEBUG = false;
private static final String LOG_TAG = AndroidFuture.class.getSimpleName();
private static final Executor DIRECT_EXECUTOR = Runnable::run;
private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
private static @Nullable Handler sMainHandler;

private final @NonNull Object mLock = new Object();
@GuardedBy("mLock")
private @Nullable BiConsumer<? super T, ? super Throwable> mListener;
@GuardedBy("mLock")
private @Nullable Executor mListenerExecutor = DIRECT_EXECUTOR;
private @NonNull Handler mTimeoutHandler = Handler.getMain();
private @NonNull Handler mTimeoutHandler = getMainHandler();
private final @Nullable IAndroidFuture mRemoteOrigin;

public AndroidFuture() {
Expand All @@ -96,7 +96,7 @@ public AndroidFuture() {
// Done
if (in.readBoolean()) {
// Failed
completeExceptionally(unparcelException(in));
completeExceptionally(readThrowable(in));
} else {
// Success
complete((T) in.readValue(null));
Expand All @@ -108,6 +108,15 @@ public AndroidFuture() {
}
}

@NonNull
private static Handler getMainHandler() {
// This isn't thread-safe but we are okay with it.
if (sMainHandler == null) {
sMainHandler = new Handler(Looper.getMainLooper());
}
return sMainHandler;
}

/**
* Create a completed future with the given value.
*
Expand Down Expand Up @@ -236,9 +245,7 @@ private void callListenerAsync(BiConsumer<? super T, ? super Throwable> listener
if (mListenerExecutor == DIRECT_EXECUTOR) {
callListener(listener, res, err);
} else {
mListenerExecutor.execute(PooledLambda
.obtainRunnable(AndroidFuture::callListener, listener, res, err)
.recycleOnUse());
mListenerExecutor.execute(() -> callListener(listener, res, err));
}
}

Expand All @@ -260,7 +267,8 @@ static <TT> void callListener(
} else {
// listener exception-case threw
// give up on listener but preserve the original exception when throwing up
throw ExceptionUtils.appendCause(t, err);
t.addSuppressed(err);
throw t;
}
}
} catch (Throwable t2) {
Expand All @@ -272,9 +280,7 @@ static <TT> void callListener(
/** @inheritDoc */
//@Override //TODO uncomment once java 9 APIs are exposed to frameworks
public AndroidFuture<T> orTimeout(long timeout, @NonNull TimeUnit unit) {
Message msg = PooledLambda.obtainMessage(AndroidFuture::triggerTimeout, this);
msg.obj = this;
mTimeoutHandler.sendMessageDelayed(msg, unit.toMillis(timeout));
mTimeoutHandler.postDelayed(this::triggerTimeout, this, unit.toMillis(timeout));
return this;
}

Expand Down Expand Up @@ -507,7 +513,7 @@ public void writeToParcel(Parcel dest, int flags) {
result = get();
} catch (Throwable t) {
dest.writeBoolean(true);
parcelException(dest, unwrapExecutionException(t));
writeThrowable(dest, unwrapExecutionException(t));
return;
}
dest.writeBoolean(false);
Expand Down Expand Up @@ -545,45 +551,75 @@ Throwable unwrapExecutionException(Throwable t) {
* Alternative to {@link Parcel#writeException} that stores the stack trace, in a
* way consistent with the binder IPC exception propagation behavior.
*/
private static void parcelException(Parcel p, @Nullable Throwable t) {
p.writeBoolean(t == null);
if (t == null) {
private static void writeThrowable(@NonNull Parcel parcel, @Nullable Throwable throwable) {
boolean hasThrowable = throwable != null;
parcel.writeBoolean(hasThrowable);
if (!hasThrowable) {
return;
}

boolean isFrameworkParcelable = throwable instanceof Parcelable
&& throwable.getClass().getClassLoader() == Parcelable.class.getClassLoader();
parcel.writeBoolean(isFrameworkParcelable);
if (isFrameworkParcelable) {
parcel.writeParcelable((Parcelable) throwable,
Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
return;
}

p.writeInt(Parcel.getExceptionCode(t));
p.writeString(t.getClass().getName());
p.writeString(t.getMessage());
p.writeStackTrace(t);
parcelException(p, t.getCause());
parcel.writeString(throwable.getClass().getName());
parcel.writeString(throwable.getMessage());
StackTraceElement[] stackTrace = throwable.getStackTrace();
StringBuilder stackTraceBuilder = new StringBuilder();
int truncatedStackTraceLength = Math.min(stackTrace != null ? stackTrace.length : 0, 5);
for (int i = 0; i < truncatedStackTraceLength; i++) {
if (i > 0) {
stackTraceBuilder.append('\n');
}
stackTraceBuilder.append("\tat ").append(stackTrace[i]);
}
parcel.writeString(stackTraceBuilder.toString());
writeThrowable(parcel, throwable.getCause());
}

/**
* @see #parcelException
* @see #writeThrowable
*/
private static @Nullable Throwable unparcelException(Parcel p) {
if (p.readBoolean()) {
private static @Nullable Throwable readThrowable(@NonNull Parcel parcel) {
final boolean hasThrowable = parcel.readBoolean();
if (!hasThrowable) {
return null;
}

int exCode = p.readInt();
String cls = p.readString();
String msg = p.readString();
String stackTrace = p.readInt() > 0 ? p.readString() : "\t<stack trace unavailable>";
msg += "\n" + stackTrace;

Exception ex = p.createExceptionOrNull(exCode, msg);
if (ex == null) {
ex = new RuntimeException(cls + ": " + msg);
boolean isFrameworkParcelable = parcel.readBoolean();
if (isFrameworkParcelable) {
return parcel.readParcelable(Parcelable.class.getClassLoader());
}
ex.setStackTrace(EMPTY_STACK_TRACE);

Throwable cause = unparcelException(p);
String className = parcel.readString();
String message = parcel.readString();
String stackTrace = parcel.readString();
String messageWithStackTrace = message + '\n' + stackTrace;
Throwable throwable;
try {
Class<?> clazz = Class.forName(className, true, Parcelable.class.getClassLoader());
if (Throwable.class.isAssignableFrom(clazz)) {
Constructor<?> constructor = clazz.getConstructor(String.class);
throwable = (Throwable) constructor.newInstance(messageWithStackTrace);
} else {
android.util.EventLog.writeEvent(0x534e4554, "186530450", -1, "");
throwable = new RuntimeException(className + ": " + messageWithStackTrace);
}
} catch (Throwable t) {
throwable = new RuntimeException(className + ": " + messageWithStackTrace);
throwable.addSuppressed(t);
}
throwable.setStackTrace(EMPTY_STACK_TRACE);
Throwable cause = readThrowable(parcel);
if (cause != null) {
ex.initCause(ex);
throwable.initCause(cause);
}

return ex;
return throwable;
}

@Override
Expand Down
28 changes: 10 additions & 18 deletions core/java/com/android/internal/infra/ServiceConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.RemoteException;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.DebugUtils;
import android.util.Log;

import com.android.internal.util.function.pooled.PooledLambda;

import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand All @@ -47,7 +44,6 @@
import java.util.function.BiConsumer;
import java.util.function.Function;


/**
* Takes care of managing a {@link ServiceConnection} and auto-disconnecting from the service upon
* a certain timeout.
Expand Down Expand Up @@ -220,6 +216,7 @@ class Impl<I extends IInterface> extends ArrayDeque<Job<I, ?>>
private final @NonNull Queue<Job<I, ?>> mQueue = this;
private final @NonNull List<CompletionAwareJob<I, ?>> mUnfinishedJobs = new ArrayList<>();

private final @NonNull Handler mMainHandler = new Handler(Looper.getMainLooper());
private final @NonNull ServiceConnection mServiceConnection = this;
private final @NonNull Runnable mTimeoutDisconnect = this;

Expand Down Expand Up @@ -250,9 +247,8 @@ class Impl<I extends IInterface> extends ArrayDeque<Job<I, ?>>
* {@link IInterface}.
* Typically this is {@code IMyInterface.Stub::asInterface}
*/
public Impl(@NonNull Context context, @NonNull Intent intent,
@Context.BindServiceFlags int bindingFlags, @UserIdInt int userId,
@Nullable Function<IBinder, I> binderAsInterface) {
public Impl(@NonNull Context context, @NonNull Intent intent, int bindingFlags,
@UserIdInt int userId, @Nullable Function<IBinder, I> binderAsInterface) {
mContext = context;
mIntent = intent;
mBindingFlags = bindingFlags;
Expand All @@ -264,7 +260,7 @@ public Impl(@NonNull Context context, @NonNull Intent intent,
* {@link Handler} on which {@link Job}s will be called
*/
protected Handler getJobHandler() {
return Handler.getMain();
return mMainHandler;
}

/**
Expand Down Expand Up @@ -391,8 +387,7 @@ private void enqueue(@NonNull CompletionAwareJob<I, ?> task) {

private boolean enqueue(@NonNull Job<I, ?> job) {
cancelTimeout();
return getJobHandler().sendMessage(PooledLambda.obtainMessage(
ServiceConnector.Impl::enqueueJobThread, this, job));
return getJobHandler().post(() -> enqueueJobThread(job));
}

void enqueueJobThread(@NonNull Job<I, ?> job) {
Expand Down Expand Up @@ -422,7 +417,7 @@ private void cancelTimeout() {
if (DEBUG) {
logTrace();
}
Handler.getMain().removeCallbacks(mTimeoutDisconnect);
mMainHandler.removeCallbacks(mTimeoutDisconnect);
}

void completeExceptionally(@NonNull Job<?, ?> job, @NonNull Throwable ex) {
Expand Down Expand Up @@ -486,7 +481,7 @@ private void scheduleUnbindTimeout() {
}
long timeout = getAutoDisconnectTimeoutMs();
if (timeout > 0) {
Handler.getMain().postDelayed(mTimeoutDisconnect, timeout);
mMainHandler.postDelayed(mTimeoutDisconnect, timeout);
} else if (DEBUG) {
Log.i(LOG_TAG, "Not scheduling unbind for permanently bound " + this);
}
Expand All @@ -502,7 +497,7 @@ public void unbind() {
logTrace();
}
mUnbinding = true;
getJobHandler().sendMessage(PooledLambda.obtainMessage(Impl::unbindJobThread, this));
getJobHandler().post(this::unbindJobThread);
}

void unbindJobThread() {
Expand Down Expand Up @@ -659,10 +654,7 @@ private String stateToString() {
}

private void logTrace() {
Log.i(LOG_TAG,
TextUtils.join(" -> ",
DebugUtils.callersWithin(ServiceConnector.class, /* offset= */ 1))
+ "(" + this + ")");
Log.i(LOG_TAG, "See stacktrace", new Throwable());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ private void startListening() {
filter.addAction(DISABLE_PLUGIN);
filter.addDataScheme("package");
mContext.registerReceiver(this, filter, PluginInstanceManager.PLUGIN_PERMISSION, null);
mContext.registerReceiver(this, filter);
filter = new IntentFilter(Intent.ACTION_USER_UNLOCKED);
mContext.registerReceiver(this, filter);
}
Expand Down
Loading

0 comments on commit f302d6b

Please sign in to comment.