Merge "Use ServiceConnector and @DataClass in companion service"

This commit is contained in:
TreeHugger Robot
2019-12-21 01:37:44 +00:00
committed by Android (Google) Code Review
8 changed files with 341 additions and 163 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -10,16 +10,9 @@
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing per missions and
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.companion;
/** @hide */
interface ICompanionDeviceDiscoveryServiceCallback {
@UnsupportedAppUsage
oneway void onDeviceSelected(String packageName, int userId, String deviceAddress);
@UnsupportedAppUsage
oneway void onDeviceSelectionCancel();
}
parcelable Association;

View File

@@ -0,0 +1,179 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.companion;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.DataClass;
import java.util.Objects;
/**
* A record indicating that a device with a given address was confirmed by the user to be
* associated to a given companion app
*
* @hide
*/
@DataClass(genEqualsHashCode = true, genToString = true)
public class Association implements Parcelable {
public final int userId;
public final @NonNull String deviceAddress;
public final @NonNull String companionAppPackage;
// Code below generated by codegen v1.0.13.
//
// DO NOT MODIFY!
// CHECKSTYLE:OFF Generated code
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/companion/Association.java
//
// To exclude the generated code from IntelliJ auto-formatting enable (one-time):
// Settings > Editor > Code Style > Formatter Control
//@formatter:off
@DataClass.Generated.Member
public Association(
int userId,
@NonNull String deviceAddress,
@NonNull String companionAppPackage) {
this.userId = userId;
this.deviceAddress = deviceAddress;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, deviceAddress);
this.companionAppPackage = companionAppPackage;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, companionAppPackage);
// onConstructed(); // You can define this method to get a callback
}
@Override
@DataClass.Generated.Member
public String toString() {
// You can override field toString logic by defining methods like:
// String fieldNameToString() { ... }
return "Association { " +
"userId = " + userId + ", " +
"deviceAddress = " + deviceAddress + ", " +
"companionAppPackage = " + companionAppPackage +
" }";
}
@Override
@DataClass.Generated.Member
public boolean equals(@Nullable Object o) {
// You can override field equality logic by defining either of the methods like:
// boolean fieldNameEquals(Association other) { ... }
// boolean fieldNameEquals(FieldType otherValue) { ... }
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@SuppressWarnings("unchecked")
Association that = (Association) o;
//noinspection PointlessBooleanExpression
return true
&& userId == that.userId
&& Objects.equals(deviceAddress, that.deviceAddress)
&& Objects.equals(companionAppPackage, that.companionAppPackage);
}
@Override
@DataClass.Generated.Member
public int hashCode() {
// You can override field hashCode logic by defining methods like:
// int fieldNameHashCode() { ... }
int _hash = 1;
_hash = 31 * _hash + userId;
_hash = 31 * _hash + Objects.hashCode(deviceAddress);
_hash = 31 * _hash + Objects.hashCode(companionAppPackage);
return _hash;
}
@Override
@DataClass.Generated.Member
public void writeToParcel(@NonNull Parcel dest, int flags) {
// You can override field parcelling by defining methods like:
// void parcelFieldName(Parcel dest, int flags) { ... }
dest.writeInt(userId);
dest.writeString(deviceAddress);
dest.writeString(companionAppPackage);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
/** @hide */
@SuppressWarnings({"unchecked", "RedundantCast"})
@DataClass.Generated.Member
protected Association(@NonNull Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
int _userId = in.readInt();
String _deviceAddress = in.readString();
String _companionAppPackage = in.readString();
this.userId = _userId;
this.deviceAddress = _deviceAddress;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, deviceAddress);
this.companionAppPackage = _companionAppPackage;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, companionAppPackage);
// onConstructed(); // You can define this method to get a callback
}
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<Association> CREATOR
= new Parcelable.Creator<Association>() {
@Override
public Association[] newArray(int size) {
return new Association[size];
}
@Override
public Association createFromParcel(@NonNull Parcel in) {
return new Association(in);
}
};
@DataClass.Generated(
time = 1573767103332L,
codegenVersion = "1.0.13",
sourceFile = "frameworks/base/core/java/android/companion/Association.java",
inputSignatures = "public final int userId\npublic final @android.annotation.NonNull java.lang.String deviceAddress\npublic final @android.annotation.NonNull java.lang.String companionAppPackage\nclass Association extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true)")
@Deprecated
private void __metadata() {}
//@formatter:on
// End of generated code
}

View File

@@ -16,9 +16,10 @@
package android.companion;
import android.companion.Association;
import android.companion.AssociationRequest;
import android.companion.ICompanionDeviceDiscoveryServiceCallback;
import android.companion.IFindDeviceCallback;
import com.android.internal.infra.AndroidFuture;
/** @hide */
@@ -27,5 +28,5 @@ interface ICompanionDeviceDiscoveryService {
in AssociationRequest request,
in String callingPackage,
in IFindDeviceCallback findCallback,
in ICompanionDeviceDiscoveryServiceCallback serviceCallback);
in AndroidFuture<Association> serviceCallback);
}

View File

@@ -17,4 +17,4 @@
package com.android.internal.infra;
/** @hide */
parcelable AndroidFuture;
parcelable AndroidFuture<T>;

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.infra;
import android.annotation.NonNull;
import android.util.SparseArray;
import com.android.internal.util.Preconditions;
/**
* A {@link SparseArray} customized for a common use-case of storing state per-user.
*
* Unlike a normal {@link SparseArray} this will always create a value on {@link #get} if one is
* not present instead of returning null.
*
* @param <T> user state type
*/
public abstract class PerUser<T> extends SparseArray<T> {
/**
* Initialize state for the given user
*/
protected abstract @NonNull T create(int userId);
/**
* Same as {@link #get(int)}, renamed for readability.
*
* This will never return null, deferring to {@link #create} instead
* when called for the first time.
*/
public @NonNull T forUser(int userId) {
return get(userId);
}
@Override
public @NonNull T get(int userId) {
T userState = super.get(userId);
if (userState != null) {
return userState;
} else {
userState = Preconditions.checkNotNull(create(userId));
put(userId, userState);
return userState;
}
}
}

View File

@@ -19,6 +19,7 @@ package com.android.internal.util;
import android.os.RemoteException;
import android.util.ExceptionUtils;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -51,6 +52,13 @@ public class FunctionalUtils {
return action;
}
/**
* @see #uncheckExceptions(ThrowingConsumer)
*/
public static <A, B> BiConsumer<A, B> uncheckExceptions(ThrowingBiConsumer<A, B> action) {
return action;
}
/**
* @see #uncheckExceptions(ThrowingConsumer)
*/
@@ -185,4 +193,29 @@ public class FunctionalUtils {
}
}
}
/**
* A {@link BiConsumer} that allows throwing checked exceptions from its single abstract method.
*
* Can be used together with {@link #uncheckExceptions} to effectively turn a lambda expression
* that throws a checked exception into a regular {@link Function}
*
* @param <A> see {@link BiConsumer}
* @param <B> see {@link BiConsumer}
*/
@FunctionalInterface
@SuppressWarnings("FunctionalInterfaceMethodChanged")
public interface ThrowingBiConsumer<A, B> extends BiConsumer<A, B> {
/** @see ThrowingFunction */
void acceptOrThrow(A a, B b) throws Exception;
@Override
default void accept(A a, B b) {
try {
acceptOrThrow(a, b);
} catch (Exception ex) {
throw ExceptionUtils.propagate(ex);
}
}
}
}