1 /* 2 * Copyright (C) 2022 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 package com.android.telephony.qns.wfc; 17 18 import android.annotation.Nullable; 19 import android.annotation.SuppressLint; 20 import android.content.Intent; 21 import android.telephony.SubscriptionManager; 22 import android.telephony.ims.ImsMmTelManager; 23 import android.util.Log; 24 25 import androidx.activity.result.ActivityResultLauncher; 26 import androidx.annotation.VisibleForTesting; 27 28 public final class WfcUtils { 29 private static final String TAG = WfcActivationActivity.TAG; 30 31 // Constants shared by WifiCallingSettings 32 static final String EXTRA_LAUNCH_CARRIER_APP = "EXTRA_LAUNCH_CARRIER_APP"; 33 static final int LAUNCH_APP_ACTIVATE = 0; 34 static final int LAUNCH_APP_UPDATE = 1; 35 36 // OK to suppress warnings here because it's used only for unit tests 37 @SuppressLint("StaticFieldLeak") 38 private static WfcActivationHelper mWfcActivationHelper; 39 private static ActivityResultLauncher mWebViewResultsLauncher; 40 WfcUtils()41 private WfcUtils() {} 42 43 /** 44 * Returns {@code true} if the app is launched for WFC activation; {@code false} for emergency 45 * address update or displaying terms & conditions. 46 */ isActivationFlow(Intent intent)47 public static boolean isActivationFlow(Intent intent) { 48 int intention = getLaunchIntention(intent); 49 Log.d(TAG, "Start Activity intention : " + intention); 50 return intention == LAUNCH_APP_ACTIVATE; 51 } 52 53 /** Returns the launch intention extra in the {@code intent}. */ getLaunchIntention(Intent intent)54 public static int getLaunchIntention(Intent intent) { 55 if (intent == null) { 56 return LAUNCH_APP_ACTIVATE; 57 } 58 59 return intent.getIntExtra(EXTRA_LAUNCH_CARRIER_APP, LAUNCH_APP_ACTIVATE); 60 } 61 62 /** Returns the subscription id of starting the WFC activation activity. */ getSubId(Intent intent)63 public static int getSubId(Intent intent) { 64 if (intent == null) { 65 return SubscriptionManager.getDefaultDataSubscriptionId(); 66 } 67 int subId = 68 intent.getIntExtra( 69 SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 70 SubscriptionManager.getDefaultDataSubscriptionId()); 71 Log.d(TAG, "Start Activity with subId : " + subId); 72 return subId; 73 } 74 75 /** 76 * Returns {@link ImsMmTelManager} with specific subscription id. Returns {@code null} if 77 * provided subscription id invalid. 78 */ 79 @Nullable getImsMmTelManager(int subId)80 public static ImsMmTelManager getImsMmTelManager(int subId) { 81 if (SubscriptionManager.isValidSubscriptionId(subId)) { 82 try { 83 return ImsMmTelManager.createForSubscriptionId(subId); 84 } catch (IllegalArgumentException e) { 85 Log.e(TAG, "Can't get ImsMmTelManager, IllegalArgumentException: subId = " + subId); 86 } 87 } 88 return null; 89 } 90 91 /** 92 * Dependency providers. 93 * 94 * <p>In normal case, setters are not invoked, hence getters return null. The component is 95 * supposed to do null check and initialize dependencies by itself. In tests, setters can be 96 * invoked to provide mock dependencies. 97 */ 98 @VisibleForTesting setWfcActivationHelper(WfcActivationHelper obj)99 public static void setWfcActivationHelper(WfcActivationHelper obj) { 100 mWfcActivationHelper = obj; 101 } 102 103 @VisibleForTesting setWebviewResultLauncher(ActivityResultLauncher obj)104 public static void setWebviewResultLauncher(ActivityResultLauncher obj) { 105 mWebViewResultsLauncher = obj; 106 } 107 108 @Nullable getWfcActivationHelper()109 public static WfcActivationHelper getWfcActivationHelper() { 110 return mWfcActivationHelper; 111 } 112 113 @Nullable getWebviewResultLauncher()114 public static ActivityResultLauncher getWebviewResultLauncher() { 115 return mWebViewResultsLauncher; 116 } 117 } 118