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

Commit 28590959 authored by Kangping Dong's avatar Kangping Dong
Browse files

Fix getting CONTEXTHUB_SERVICE throws ServiceNotFoundException

The CONTEXTHUB_SERVICE may not be published if the
FEATURE_CONTEXT_HUB feature is not present. So getting the ContextHub
serivce with "context.getSystemService(ContextHubManager.class)"
will throw ServiceNotFoundException.

This commit fixes this issue by checking the CONTEXTHUB_SERVICE
before creating the ContextHubManager instance.

Test: verified with cuttlefish that it won't throw from NearbyService
Bug: 299388818
Change-Id: Icd51a0f88c10259f4a912f5dd82bad49b70562e3
parent 2d2d98a8
Loading
Loading
Loading
Loading
+7 −2
Original line number Original line Diff line number Diff line
@@ -113,6 +113,7 @@ import android.hardware.iris.IrisManager;
import android.hardware.lights.LightsManager;
import android.hardware.lights.LightsManager;
import android.hardware.lights.SystemLightsManager;
import android.hardware.lights.SystemLightsManager;
import android.hardware.location.ContextHubManager;
import android.hardware.location.ContextHubManager;
import android.hardware.location.IContextHubService;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioManager;
import android.hardware.usb.IUsbManager;
import android.hardware.usb.IUsbManager;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbManager;
@@ -1125,7 +1126,11 @@ public final class SystemServiceRegistry {
                new CachedServiceFetcher<ContextHubManager>() {
                new CachedServiceFetcher<ContextHubManager>() {
            @Override
            @Override
            public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
            public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                return new ContextHubManager(ctx.getOuterContext(),
                IBinder b = ServiceManager.getService(Context.CONTEXTHUB_SERVICE);
                if (b == null) {
                    return null;
                }
                return new ContextHubManager(IContextHubService.Stub.asInterface(b),
                        ctx.mMainThread.getHandler().getLooper());
                        ctx.mMainThread.getHandler().getLooper());
            }});
            }});


+7 −5
Original line number Original line Diff line number Diff line
@@ -15,6 +15,8 @@
 */
 */
package android.hardware.location;
package android.hardware.location;


import static java.util.Objects.requireNonNull;

import android.annotation.CallbackExecutor;
import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
@@ -1111,12 +1113,12 @@ public final class ContextHubManager {
        }
        }
    };
    };


    /** @throws ServiceNotFoundException
    /** @hide */
     * @hide */
    public ContextHubManager(@NonNull IContextHubService service, @NonNull Looper mainLooper) {
    public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException {
        requireNonNull(service, "service cannot be null");
        requireNonNull(mainLooper, "mainLooper cannot be null");
        mService = service;
        mMainLooper = mainLooper;
        mMainLooper = mainLooper;
        mService = IContextHubService.Stub.asInterface(
                ServiceManager.getServiceOrThrow(Context.CONTEXTHUB_SERVICE));
        try {
        try {
            mService.registerCallback(mClientCallback);
            mService.registerCallback(mClientCallback);
        } catch (RemoteException e) {
        } catch (RemoteException e) {