Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7d47f2c4 authored by Brian Stack's avatar Brian Stack
Browse files

Expose UwbManager through Context.getSystemService

Bug: 170323306
Test: atest UwbManagerTests
Change-Id: I04c1263a191fcfeee5824be5d6d1944354819d02
parent 28ed7b9c
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -194,6 +194,7 @@ import android.telephony.TelephonyRegistryManager;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
import android.uwb.UwbManager;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.WindowManager;
@@ -732,6 +733,14 @@ public final class SystemServiceRegistry {
                return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
            }});

        registerService(Context.UWB_SERVICE, UwbManager.class,
                new CachedServiceFetcher<UwbManager>() {
                    @Override
                    public UwbManager createService(ContextImpl ctx) {
                        return UwbManager.getInstance();
                    }
                });

        registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
                new CachedServiceFetcher<Vibrator>() {
            @Override
+10 −0
Original line number Diff line number Diff line
@@ -3544,6 +3544,7 @@ public abstract class Context {
            LIGHTS_SERVICE,
            //@hide: PEOPLE_SERVICE,
            //@hide: DEVICE_STATE_SERVICE,
            UWB_SERVICE,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ServiceName {}
@@ -5258,6 +5259,15 @@ public abstract class Context {
     */
    public static final String LIGHTS_SERVICE = "lights";

    /**
     * Use with {@link #getSystemService(String)} to retrieve a
     * {@link android.uwb.UwbManager}.
     *
     * @see #getSystemService(String)
     * @hide
     */
    public static final String UWB_SERVICE = "uwb";

    /**
     * Use with {@link #getSystemService(String)} to retrieve a
     * {@link android.app.DreamManager} for controlling Dream states.
+9 −0
Original line number Diff line number Diff line
@@ -2808,6 +2808,15 @@ public abstract class PackageManager {
    @SdkConstant(SdkConstantType.FEATURE)
    public static final String FEATURE_TELEPHONY_IMS = "android.hardware.telephony.ims";

    /**
     * Feature for {@link #getSystemAvailableFeatures} and
     * {@link #hasSystemFeature}: The device is capable of communicating with
     * other devices via ultra wideband.
     * @hide
     */
    @SdkConstant(SdkConstantType.FEATURE)
    public static final String FEATURE_UWB = "android.hardware.uwb";

    /**
     * Feature for {@link #getSystemAvailableFeatures} and
     * {@link #hasSystemFeature}: The device supports connecting to USB devices
+30 −2
Original line number Diff line number Diff line
@@ -19,7 +19,11 @@ package android.uwb;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.annotation.SystemService;
import android.content.Context;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.ServiceManager;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -36,7 +40,11 @@ import java.util.concurrent.Executor;
 *
 * @hide
 */
@SystemService(Context.UWB_SERVICE)
public final class UwbManager {
    private IUwbAdapter mUwbAdapter;
    private static final String SERVICE_NAME = "uwb";

    /**
     * Interface for receiving UWB adapter state changes
     */
@@ -96,10 +104,30 @@ public final class UwbManager {

    /**
     * Use <code>Context.getSystemService(UwbManager.class)</code> to get an instance.
     *
     * @param adapter an instance of an {@link android.uwb.IUwbAdapter}
     */
    private UwbManager() {
        throw new UnsupportedOperationException();
    private UwbManager(IUwbAdapter adapter) {
        mUwbAdapter = adapter;
    }

    /**
     * @hide
     */
    public static UwbManager getInstance() {
        IBinder b = ServiceManager.getService(SERVICE_NAME);
        if (b == null) {
            return null;
        }

        IUwbAdapter adapter = IUwbAdapter.Stub.asInterface(b);
        if (adapter == null) {
            return null;
        }

        return new UwbManager(adapter);
    }

    /**
     * Register an {@link AdapterStateCallback} to listen for UWB adapter state changes
     * <p>The provided callback will be invoked by the given {@link Executor}.
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 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.uwb;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import android.content.Context;

import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test of {@link UwbManager}.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class UwbManagerTest {

    public final Context mContext = InstrumentationRegistry.getContext();

    @Test
    public void testServiceAvailable() {
        UwbManager manager = mContext.getSystemService(UwbManager.class);
        if (UwbTestUtils.isUwbSupported(mContext)) {
            assertNotNull(manager);
        } else {
            assertNull(manager);
        }
    }
}
Loading