1 /* 2 * Copyright (C) 2024 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.permissioncontroller.permission.ui.wear.model 18 19 import android.content.Context 20 import android.content.Intent 21 import android.content.pm.ApplicationInfo 22 import android.content.pm.PackageInfo 23 import android.content.pm.PackageManager 24 import android.provider.Settings 25 import androidx.lifecycle.LiveData 26 import androidx.lifecycle.MutableLiveData 27 import androidx.lifecycle.ViewModel 28 import androidx.lifecycle.ViewModelProvider 29 import com.android.permissioncontroller.R 30 import com.android.permissioncontroller.permission.utils.Utils 31 32 class WearLocationProviderInterceptDialogViewModel : ViewModel() { 33 private val showDialogLiveData = MutableLiveData<Boolean>() 34 val dialogVisibilityLiveData: LiveData<Boolean> = showDialogLiveData 35 private val _locationProviderInterceptDialogArgs = 36 MutableLiveData<LocationProviderInterceptDialogArgs?>() 37 var locationProviderInterceptDialogArgs: LiveData<LocationProviderInterceptDialogArgs?> = 38 _locationProviderInterceptDialogArgs 39 40 init { 41 showDialogLiveData.value = false 42 _locationProviderInterceptDialogArgs.value = null 43 } 44 applicationInfonull45 private fun applicationInfo(context: Context, packageName: String): ApplicationInfo? { 46 val packageInfo: PackageInfo? = 47 try { 48 context.packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS) 49 } catch (e: PackageManager.NameNotFoundException) { 50 null 51 } 52 return packageInfo?.applicationInfo 53 } 54 showDialognull55 fun showDialog(context: Context, packageName: String) { 56 val applicationInfo = applicationInfo(context, packageName) ?: return 57 val appLabel = Utils.getAppLabel(applicationInfo, context) 58 _locationProviderInterceptDialogArgs.value = 59 LocationProviderInterceptDialogArgs( 60 iconId = R.drawable.ic_dialog_alert_material, 61 titleId = android.R.string.dialog_alert_title, 62 message = context.getString(R.string.location_warning, appLabel), 63 okButtonTitleId = R.string.ok, 64 locationSettingsId = R.string.location_settings, 65 onOkButtonClick = { dismissDialog() }, 66 onLocationSettingsClick = { 67 context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) 68 }, 69 ) 70 showDialogLiveData.value = true 71 } 72 dismissDialognull73 fun dismissDialog() { 74 _locationProviderInterceptDialogArgs.value = null 75 showDialogLiveData.value = false 76 } 77 } 78 79 /** Factory for an AppPermissionGroupsRevokeDialogViewModel */ 80 class WearLocationProviderInterceptDialogViewModelFactory : ViewModelProvider.Factory { createnull81 override fun <T : ViewModel> create(modelClass: Class<T>): T { 82 @Suppress("UNCHECKED_CAST") 83 return WearLocationProviderInterceptDialogViewModel() as T 84 } 85 } 86 87 data class LocationProviderInterceptDialogArgs( 88 val iconId: Int, 89 val titleId: Int, 90 val message: String, 91 val okButtonTitleId: Int, 92 val locationSettingsId: Int, 93 val onOkButtonClick: () -> Unit, 94 val onLocationSettingsClick: () -> Unit, 95 ) 96