Skip to content
Snippets Groups Projects
Commit d4878b8f authored by Wei Wang's avatar Wei Wang
Browse files

Add a NearbyScanRequest for requesting scan.

Currently only scan type and worksource are supported in the scan
request. More parameters will be added in the future.

Bug: 189355156
Test: atest NearbyUnitTests

Change-Id: I73d9e48865d33f5b13b29acac30eadc1a9eb9168
parent f9a3c6a3
No related branches found
No related tags found
No related merge requests found
{
"presubmit": [
{
"name": "NearbyUnitTests"
}
],
"postsubmit": [
{
"name": "NearbyUnitTests"
}
],
"mainline-presubmit": [
{
"name": "NearbyUnitTests[com.google.android.nearby.apex]"
}
]
}
\ No newline at end of file
......@@ -42,6 +42,11 @@ apex {
],
}
filegroup {
name: "nearby-jarjar-rules",
srcs: ["jarjar-rules.txt"],
}
sdk {
name: "nearby-module-sdk",
java_sdk_libs: [
......
rule com.android.internal.** com.android.nearby.jarjar.@0
......@@ -33,6 +33,9 @@ java_defaults {
srcs: [
":nearby-java-sources",
],
static_libs: [
"modules-utils-preconditions",
],
}
// Defaults for SDK versions.
......@@ -42,7 +45,6 @@ java_defaults {
target_sdk_version: "current",
}
// Nearby-service needs pre-jarjared version of framework-nearby so it can reference copied utility
// classes before they are renamed.
java_library {
......@@ -55,6 +57,7 @@ java_library {
installable: false,
visibility: [
"//packages/modules/Nearby/service",
"//packages/modules/Nearby/tests",
],
}
......@@ -66,6 +69,7 @@ java_sdk_library {
"framework-nearby-defaults",
],
jarjar_rules: ":nearby-jarjar-rules",
apex_available: [
"com.android.nearby",
],
......@@ -80,5 +84,6 @@ java_sdk_library {
],
permitted_packages: [
"android.nearby",
"com.android.nearby",
],
}
/*
* Copyright (C) 2021 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.nearby;
import android.Manifest;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.WorkSource;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/**
* An encapsulation of various parameters for requesting nearby scans.
*
* @hide
*/
public final class ScanRequest implements Parcelable {
/** @hide **/
@Retention(RetentionPolicy.SOURCE)
@IntDef({SCAN_TYPE_FAST_PAIR, SCAN_TYPE_NEARBY_SHARE, SCAN_TYPE_NEARBY_PRESENCE,
SCAN_TYPE_EXPOSURE_NOTIFICATION})
public @interface ScanType{}
/** Scan type for scanning devices using fast pair protocol. */
public static final int SCAN_TYPE_FAST_PAIR = 1;
/** Scan type for scanning devices using nearby share protocol. */
public static final int SCAN_TYPE_NEARBY_SHARE = 2;
/** Scan type for scanning devices using nearby presence protocol. */
public static final int SCAN_TYPE_NEARBY_PRESENCE = 3;
/** Scan type for scanning devices using exposure notification protocol. */
public static final int SCAN_TYPE_EXPOSURE_NOTIFICATION = 4;
private final @ScanType int mScanType;
private final @Nullable WorkSource mWorkSource;
private ScanRequest(@ScanType int scanType, @Nullable WorkSource workSource) {
mScanType = scanType;
mWorkSource = workSource;
}
/**
* Returns the scan type for this request.
*/
public @ScanType int getScanType() {
return mScanType;
}
/**
* Returns the work source used for power attribution of this request.
*
* @hide
*/
public @Nullable WorkSource getWorkSource() {
return mWorkSource;
}
public static final Creator<ScanRequest> CREATOR = new Creator<ScanRequest>() {
@Override
public ScanRequest createFromParcel(Parcel in) {
return new ScanRequest(
/* scanType= */ in.readInt(),
/* workSource= */ in.readTypedObject(WorkSource.CREATOR));
}
@Override
public ScanRequest[] newArray(int size) {
return new ScanRequest[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Request[")
.append("scanType=").append(mScanType);
if (mWorkSource != null && !mWorkSource.isEmpty()) {
stringBuilder.append(", workSource=").append(mWorkSource);
}
stringBuilder.append("]");
return stringBuilder.toString();
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mScanType);
dest.writeTypedObject(mWorkSource, /* parcelableFlags= */0);
}
@Override
public boolean equals(Object other) {
if (other instanceof ScanRequest) {
ScanRequest otherRequest = (ScanRequest) other;
return mScanType == otherRequest.mScanType
&& Objects.equals(mWorkSource, otherRequest.mWorkSource);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(mScanType, mWorkSource);
}
/** A builder class for {@link ScanRequest}. */
public static final class Builder {
private static final int INVALID_SCAN_TYPE = -1;
private @ScanType int mScanType;
private @Nullable WorkSource mWorkSource;
/** Creates a new Builder with the given scan type. */
public Builder() {
mScanType = INVALID_SCAN_TYPE;
mWorkSource = null;
}
/**
* Sets the scan type for the request. The scan type must be one of the SCAN_TYPE_ constants
* in {@link ScanRequest}.
*/
public Builder setScanType(@ScanType int scanType) {
mScanType = scanType;
return this;
}
/**
* Sets the work source to use for power attribution for this scan request. Defaults to
* empty work source, which implies the caller that sends the scan request will be used
* for power attribution.
*
* <p>Permission enforcement occurs when the resulting scan request is used, not when
* this method is invoked.
*
* @hide
*/
@RequiresPermission(Manifest.permission.UPDATE_DEVICE_STATS)
public @NonNull Builder setWorkSource(@Nullable WorkSource workSource) {
this.mWorkSource = workSource;
return this;
}
/**
* Builds a scan request from this builder.
*
* @throws IllegalStateException if the scanType is not one of the SCAN_TYPE_ constants in
* {@link ScanRequest}.
* @return a new nearby scan request.
*/
public @NonNull ScanRequest build() {
Preconditions.checkState(isValidScanType(mScanType),
"invalid scan type : " + mScanType
+ ", scan type must be one of ScanRequest#SCAN_TYPE_");
return new ScanRequest(mScanType, mWorkSource);
}
private static boolean isValidScanType(int scanType) {
return scanType == SCAN_TYPE_FAST_PAIR
|| scanType == SCAN_TYPE_NEARBY_SHARE
|| scanType == SCAN_TYPE_NEARBY_PRESENCE
|| scanType == SCAN_TYPE_EXPOSURE_NOTIFICATION;
}
}
}
// Copyright (C) 2021 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 {
default_applicable_licenses: ["Android-Apache-2.0"],
}
android_test {
name: "NearbyUnitTests",
defaults: ["mts-target-sdk-version-current"],
sdk_version: "test_current",
min_sdk_version: "31",
// Include all test java files.
srcs: ["src/**/*.java"],
libs: [
"android.test.runner",
"android.test.base",
],
compile_multilib: "both",
static_libs: [
"androidx.test.rules",
"truth-prebuilt",
"framework-nearby-pre-jarjar",
],
test_suites: [
"general-tests",
"mts",
],
}
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.nearby.test">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="android.nearby.test"
android:label="Nearby Mainline Module Tests" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 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.
-->
<configuration description="Runs Nearby Mainline API Tests.">
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="test-file-name" value="NearbyUnitTests.apk" />
</target_preparer>
<option name="test-suite-tag" value="apct" />
<option name="test-tag" value="NearbyUnitTests" />
<option name="config-descriptor:metadata" key="mainline-param"
value="com.google.android.nearby.apex" />
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.nearby.test" />
<option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
<option name="hidden-api-checks" value="false"/>
</test>
<!-- Only run NearbyUnitTests in MTS if the Nearby Mainline module is installed. -->
<object type="module_controller"
class="com.android.tradefed.testtype.suite.module.MainlineTestModuleController">
<option name="mainline-module-package-name" value="com.google.android.nearby" />
</object>
</configuration>
/*
* Copyright (C) 2021 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.nearby;
import static android.nearby.ScanRequest.SCAN_TYPE_EXPOSURE_NOTIFICATION;
import static android.nearby.ScanRequest.SCAN_TYPE_FAST_PAIR;
import static android.nearby.ScanRequest.SCAN_TYPE_NEARBY_PRESENCE;
import static android.nearby.ScanRequest.SCAN_TYPE_NEARBY_SHARE;
import static com.google.common.truth.Truth.assertThat;
import android.os.Parcel;
import android.os.WorkSource;
import androidx.test.filters.SmallTest;
import org.junit.Test;
/** Units tests for {@link ScanRequest}. */
@SmallTest
public class ScanRequestTest {
/** Test creating a scan request. */
@Test
public void testScanRequestBuilder() {
final int scanType = SCAN_TYPE_FAST_PAIR;
ScanRequest request = new ScanRequest.Builder().setScanType(scanType).build();
assertThat(request.getScanType()).isEqualTo(scanType);
// Work source is null if not set.
assertThat(request.getWorkSource()).isNull();
}
/** Verify RuntimeException is thrown when creating scan request with invalid scan type. */
@Test(expected = RuntimeException.class)
public void testScanRequestBuilder_invalidScanType() {
final int invalidScanType = -1;
ScanRequest.Builder builder = new ScanRequest.Builder().setScanType(
invalidScanType);
builder.build();
}
/** Verify setting work source in the scan request. */
@Test
public void testSetWorkSource() {
WorkSource workSource = getWorkSource();
ScanRequest request = new ScanRequest.Builder()
.setScanType(SCAN_TYPE_NEARBY_SHARE)
.setWorkSource(workSource)
.build();
assertThat(request.getWorkSource()).isEqualTo(workSource);
}
/** Verify setting work source with null value in the scan request. */
@Test
public void testSetWorkSource_nullValue() {
ScanRequest request = new ScanRequest.Builder()
.setScanType(SCAN_TYPE_EXPOSURE_NOTIFICATION)
.setWorkSource(null)
.build();
// Null work source is allowed.
assertThat(request.getWorkSource()).isNull();
}
/** Verify toString returns expected string. */
@Test
public void testToString() {
WorkSource workSource = getWorkSource();
ScanRequest request = new ScanRequest.Builder()
.setScanType(SCAN_TYPE_NEARBY_SHARE)
.setWorkSource(workSource)
.build();
assertThat(request.toString()).isEqualTo(
"Request[scanType=2, workSource=WorkSource{1001 android.nearby.tests}]");
}
/** Verify toString works correctly with null WorkSource. */
@Test
public void testToString_nullWorkSource() {
ScanRequest request = new ScanRequest.Builder().setScanType(
SCAN_TYPE_FAST_PAIR).build();
assertThat(request.toString()).isEqualTo("Request[scanType=1]");
}
/** Verify writing and reading from parcel for scan request. */
@Test
public void testParceling() {
final int scanType = SCAN_TYPE_NEARBY_PRESENCE;
WorkSource workSource = getWorkSource();
ScanRequest originalRequest = new ScanRequest.Builder()
.setScanType(scanType)
.setWorkSource(workSource)
.build();
// Write the scan request to parcel, then read from it.
ScanRequest request = writeReadFromParcel(originalRequest);
// Verify the request read from parcel equals to the original request.
assertThat(request).isEqualTo(originalRequest);
}
/** Verify parceling with null WorkSource. */
@Test
public void testParceling_nullWorkSource() {
final int scanType = SCAN_TYPE_NEARBY_PRESENCE;
ScanRequest originalRequest = new ScanRequest.Builder()
.setScanType(scanType).build();
ScanRequest request = writeReadFromParcel(originalRequest);
assertThat(request).isEqualTo(originalRequest);
}
private ScanRequest writeReadFromParcel(ScanRequest originalRequest) {
Parcel parcel = Parcel.obtain();
originalRequest.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
return ScanRequest.CREATOR.createFromParcel(parcel);
}
private static WorkSource getWorkSource() {
final int uid = 1001;
final String appName = "android.nearby.tests";
return new WorkSource(uid, appName);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment