1 /* 2 * Copyright (C) 2023 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.systemui.telephony.ui.activity 18 19 import android.app.ActivityOptions 20 import android.content.DialogInterface 21 import android.content.Intent 22 import android.net.Uri 23 import android.os.Bundle 24 import android.os.UserHandle 25 import android.telecom.TelecomManager 26 import android.util.Log 27 import android.view.WindowManager 28 import com.android.internal.app.AlertActivity 29 import com.android.systemui.res.R 30 import javax.inject.Inject 31 32 /** Dialog shown to the user to switch to managed profile for making a call using work SIM. */ 33 class SwitchToManagedProfileForCallActivity 34 @Inject 35 constructor( 36 private val telecomManager: TelecomManager?, 37 ) : AlertActivity(), DialogInterface.OnClickListener { 38 private lateinit var phoneNumber: Uri 39 private lateinit var positiveActionIntent: Intent 40 private var managedProfileUserId = UserHandle.USER_NULL 41 onCreatenull42 override fun onCreate(savedInstanceState: Bundle?) { 43 window.addSystemFlags( 44 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS 45 ) 46 super.onCreate(savedInstanceState) 47 phoneNumber = intent.data ?: Uri.EMPTY 48 managedProfileUserId = 49 intent.getIntExtra( 50 "android.telecom.extra.MANAGED_PROFILE_USER_ID", 51 UserHandle.USER_NULL 52 ) 53 54 mAlertParams.apply { 55 mTitle = getString(R.string.call_from_work_profile_title) 56 mMessage = getString(R.string.call_from_work_profile_text) 57 mNegativeButtonText = getString(R.string.call_from_work_profile_close) 58 mPositiveButtonListener = this@SwitchToManagedProfileForCallActivity 59 mNegativeButtonListener = this@SwitchToManagedProfileForCallActivity 60 } 61 62 // A dialer app may not be available in the managed profile. We try to handle that 63 // gracefully by redirecting the user to the app market to install a suitable app. 64 val defaultDialerPackageName: String? = 65 telecomManager?.getDefaultDialerPackage(UserHandle.of(managedProfileUserId)) 66 67 val (intent, positiveButtonText) = 68 defaultDialerPackageName?.let { 69 Intent( 70 Intent.ACTION_CALL, 71 phoneNumber, 72 ) to R.string.call_from_work_profile_action 73 } 74 ?: Intent( 75 Intent.ACTION_VIEW, 76 Uri.parse(APP_STORE_DIALER_QUERY), 77 ) to R.string.install_dialer_on_work_profile_action 78 79 positiveActionIntent = intent 80 mAlertParams.apply { mPositiveButtonText = getString(positiveButtonText) } 81 82 setupAlert() 83 } 84 onClicknull85 override fun onClick(dialog: DialogInterface?, which: Int) { 86 if (which == BUTTON_POSITIVE) { 87 switchToManagedProfile() 88 } 89 finish() 90 } 91 switchToManagedProfilenull92 private fun switchToManagedProfile() { 93 try { 94 applicationContext.startActivityAsUser( 95 positiveActionIntent, 96 ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle(), 97 UserHandle.of(managedProfileUserId) 98 ) 99 } catch (e: Exception) { 100 Log.e(TAG, "Failed to launch activity", e) 101 } 102 } 103 104 companion object { 105 private const val TAG = "SwitchToManagedProfileForCallActivity" 106 private const val APP_STORE_DIALER_QUERY = "market://search?q=dialer" 107 } 108 } 109