Add try/catch, shim in Nearby initialization

The try/catch allows using a stub NearbyService on branches that do not
support it yet.

Also update Context.NEARBY_SERVICE to ConstantsShim to be compatible
with all branches.

Bug: 189355156
Test: boots
Change-Id: I7db0035b0d9ada79f00d6ef1ac5b54b2e98489d0
This commit is contained in:
Remi NGUYEN VAN
2022-03-04 15:59:51 +09:00
parent e96e7c4de4
commit 46d7ddb725

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.util.Log;
import com.android.modules.utils.build.SdkLevel;
import com.android.networkstack.apishim.ConstantsShim;
import com.android.server.nearby.NearbyService;
/**
@@ -60,8 +61,8 @@ public final class ConnectivityServiceInitializer extends SystemService {
}
if (mNearbyService != null) {
Log.i(TAG, "Registering " + Context.NEARBY_SERVICE);
publishBinderService(Context.NEARBY_SERVICE, mNearbyService,
Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
/* allowIsolated= */ false);
}
}
@@ -96,6 +97,13 @@ public final class ConnectivityServiceInitializer extends SystemService {
/** Return Nearby service instance or null if current SDK is lower than T */
private NearbyService createNearbyService(final Context context) {
if (!SdkLevel.isAtLeastT()) return null;
return new NearbyService(context);
try {
return new NearbyService(context);
} catch (UnsupportedOperationException e) {
// Nearby is not yet supported in all branches
// TODO: remove catch clause when it is available.
Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
return null;
}
}
}