1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.imsserviceentitlement; 18 19 import static com.android.imsserviceentitlement.utils.Executors.getAsyncExecutor; 20 import static com.android.imsserviceentitlement.utils.Executors.getDirectExecutor; 21 22 import android.util.Log; 23 24 import androidx.annotation.MainThread; 25 import androidx.annotation.Nullable; 26 import androidx.annotation.WorkerThread; 27 28 import com.android.imsserviceentitlement.WfcActivationController.EntitlementResultCallback; 29 import com.android.imsserviceentitlement.entitlement.EntitlementResult; 30 31 import com.google.common.util.concurrent.FutureCallback; 32 import com.google.common.util.concurrent.Futures; 33 import com.google.common.util.concurrent.ListenableFuture; 34 35 /** Handles entitlement check from main thread. */ 36 public final class EntitlementUtils { 37 38 public static final String LOG_TAG = "IMSSE-EntitlementUtils"; 39 40 private static ListenableFuture<EntitlementResult> sCheckEntitlementFuture; 41 EntitlementUtils()42 private EntitlementUtils() {} 43 44 /** 45 * Performs the entitlement status check, and passes the result via {@link 46 * EntitlementResultCallback}. 47 */ 48 @MainThread entitlementCheck( ImsEntitlementApi activationApi, EntitlementResultCallback callback)49 public static void entitlementCheck( 50 ImsEntitlementApi activationApi, EntitlementResultCallback callback) { 51 sCheckEntitlementFuture = 52 Futures.submit(() -> getEntitlementStatus(activationApi), getAsyncExecutor()); 53 Futures.addCallback( 54 sCheckEntitlementFuture, 55 new FutureCallback<EntitlementResult>() { 56 @Override 57 public void onSuccess(EntitlementResult result) { 58 callback.onEntitlementResult(result); 59 sCheckEntitlementFuture = null; 60 } 61 62 @Override 63 public void onFailure(Throwable t) { 64 Log.w(LOG_TAG, "get entitlement status failed.", t); 65 sCheckEntitlementFuture = null; 66 } 67 }, 68 getDirectExecutor()); 69 } 70 71 /** Cancels the running task of entitlement status check if exist. */ cancelEntitlementCheck()72 public static void cancelEntitlementCheck() { 73 if (sCheckEntitlementFuture != null) { 74 Log.i(LOG_TAG, "cancel entitlement status check."); 75 sCheckEntitlementFuture.cancel(true); 76 } 77 } 78 79 /** 80 * Gets entitlement status via carrier-specific entitlement API over network; returns null on 81 * network falure or other unexpected failure from entitlement API. 82 */ 83 @WorkerThread 84 @Nullable getEntitlementStatus(ImsEntitlementApi activationApi)85 private static EntitlementResult getEntitlementStatus(ImsEntitlementApi activationApi) { 86 try { 87 return activationApi.checkEntitlementStatus(); 88 } catch (RuntimeException e) { 89 Log.e("WfcActivationActivity", "getEntitlementStatus failed.", e); 90 return null; 91 } 92 } 93 } 94