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