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 @file:Suppress("DEPRECATION") 17 18 package com.android.permissioncontroller.permission.service.v33 19 20 import android.app.PendingIntent 21 import android.app.PendingIntent.FLAG_IMMUTABLE 22 import android.content.ComponentName 23 import android.content.Intent 24 import android.content.pm.PackageManager 25 import android.os.IBinder 26 import android.provider.DeviceConfig 27 import android.safetycenter.SafetyCenterManager 28 import android.service.quicksettings.Tile 29 import android.service.quicksettings.TileService 30 import android.util.Log 31 import com.android.modules.utils.build.SdkLevel 32 import com.android.permissioncontroller.R 33 34 /** 35 * The service backing a Quick Settings Tile which will take users to the Safety Center QS Fragment. 36 */ 37 class SafetyCenterQsTileService : TileService() { 38 private var disabled = false 39 onBindnull40 override fun onBind(intent: Intent?): IBinder? { 41 val scManager = getSystemService(SafetyCenterManager::class.java)!! 42 val qsTileComponentSettingFlags = 43 DeviceConfig.getInt( 44 DeviceConfig.NAMESPACE_PRIVACY, 45 QS_TILE_COMPONENT_SETTING_FLAGS, 46 PackageManager.DONT_KILL_APP 47 ) 48 if (!scManager.isSafetyCenterEnabled) { 49 packageManager.setComponentEnabledSetting( 50 ComponentName(this, this::class.java), 51 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 52 qsTileComponentSettingFlags 53 ) 54 disabled = true 55 } 56 57 return super.onBind(intent) 58 } onStartListeningnull59 override fun onStartListening() { 60 super.onStartListening() 61 if (disabled) { 62 return 63 } 64 if (qsTile == null) { 65 Log.w(TAG, "qsTile was null, skipping tile update") 66 return 67 } 68 69 qsTile.label = getString(R.string.safety_privacy_qs_tile_title) 70 qsTile.subtitle = getString(R.string.safety_privacy_qs_tile_subtitle) 71 qsTile.state = Tile.STATE_ACTIVE 72 qsTile.updateTile() 73 } 74 onClicknull75 override fun onClick() { 76 val intent = Intent(Intent.ACTION_VIEW_SAFETY_CENTER_QS) 77 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK 78 if (SdkLevel.isAtLeastU()) { 79 startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, FLAG_IMMUTABLE)) 80 } else { 81 startActivityAndCollapse(intent) 82 } 83 } 84 85 companion object { 86 /** 87 * Device config property to make sure toggling the tile does not kill the app during CTS 88 * tests and cause flakiness. 89 */ 90 const val QS_TILE_COMPONENT_SETTING_FLAGS = "safety_center_qs_tile_component_setting_flags" 91 92 private const val TAG = "SafetyCenterQsTile" 93 } 94 } 95