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.systemui.screenrecord 17 18 import android.content.Context 19 import android.os.Bundle 20 import android.view.Gravity 21 import android.view.View 22 import android.view.ViewStub 23 import android.view.WindowManager 24 import android.widget.AdapterView 25 import android.widget.ArrayAdapter 26 import android.widget.ImageView 27 import android.widget.Spinner 28 import android.widget.TextView 29 import androidx.annotation.ColorRes 30 import androidx.annotation.DrawableRes 31 import androidx.annotation.LayoutRes 32 import androidx.annotation.StringRes 33 import com.android.systemui.R 34 import com.android.systemui.statusbar.phone.SystemUIDialog 35 36 /** Base permission dialog for screen share and recording */ 37 open class BaseScreenSharePermissionDialog( 38 context: Context?, 39 private val screenShareOptions: List<ScreenShareOption>, 40 private val appName: String?, 41 @DrawableRes private val dialogIconDrawable: Int? = null, 42 @ColorRes private val dialogIconTint: Int? = null 43 ) : SystemUIDialog(context), AdapterView.OnItemSelectedListener { 44 private lateinit var dialogTitle: TextView 45 private lateinit var startButton: TextView 46 private lateinit var warning: TextView 47 private lateinit var screenShareModeSpinner: Spinner 48 var selectedScreenShareOption: ScreenShareOption = screenShareOptions.first() 49 onCreatenull50 public override fun onCreate(savedInstanceState: Bundle?) { 51 super.onCreate(savedInstanceState) 52 window.apply { 53 addPrivateFlags(WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS) 54 setGravity(Gravity.CENTER) 55 } 56 setContentView(R.layout.screen_share_dialog) 57 dialogTitle = findViewById(R.id.screen_share_dialog_title) 58 warning = findViewById(R.id.text_warning) 59 startButton = findViewById(R.id.button_start) 60 findViewById<TextView>(R.id.button_cancel).setOnClickListener { dismiss() } 61 updateIcon() 62 initScreenShareOptions() 63 createOptionsView(getOptionsViewLayoutId()) 64 } 65 updateIconnull66 private fun updateIcon() { 67 val icon = findViewById<ImageView>(R.id.screen_share_dialog_icon) 68 if (dialogIconTint != null) { 69 icon.setColorFilter(context.getColor(dialogIconTint)) 70 } 71 if (dialogIconDrawable != null) { 72 icon.setImageDrawable(context.getDrawable(dialogIconDrawable)) 73 } 74 } 75 initScreenShareOptionsnull76 protected fun initScreenShareOptions() { 77 selectedScreenShareOption = screenShareOptions.first() 78 warning.text = warningText 79 initScreenShareSpinner() 80 } 81 82 private val warningText: String 83 get() = context.getString(selectedScreenShareOption.warningText, appName) 84 initScreenShareSpinnernull85 private fun initScreenShareSpinner() { 86 val options = screenShareOptions.map { context.getString(it.spinnerText) }.toTypedArray() 87 val adapter = 88 ArrayAdapter( 89 context.applicationContext, 90 R.layout.screen_share_dialog_spinner_text, 91 options 92 ) 93 adapter.setDropDownViewResource(R.layout.screen_share_dialog_spinner_item_text) 94 screenShareModeSpinner = findViewById(R.id.screen_share_mode_spinner) 95 screenShareModeSpinner.adapter = adapter 96 screenShareModeSpinner.onItemSelectedListener = this 97 } 98 onItemSelectednull99 override fun onItemSelected(adapterView: AdapterView<*>?, view: View, pos: Int, id: Long) { 100 selectedScreenShareOption = screenShareOptions[pos] 101 warning.text = warningText 102 } 103 onNothingSelectednull104 override fun onNothingSelected(parent: AdapterView<*>?) {} 105 106 /** Protected methods for the text updates & functionality */ setDialogTitlenull107 protected fun setDialogTitle(@StringRes stringId: Int) { 108 val title = context.getString(stringId, appName) 109 dialogTitle.text = title 110 } 111 setStartButtonTextnull112 protected fun setStartButtonText(@StringRes stringId: Int) { 113 startButton.setText(stringId) 114 } 115 setStartButtonOnClickListenernull116 protected fun setStartButtonOnClickListener(listener: View.OnClickListener?) { 117 startButton.setOnClickListener(listener) 118 } 119 120 // Create additional options that is shown under the share mode spinner 121 // Eg. the audio and tap toggles in SysUI Recorder getOptionsViewLayoutIdnull122 @LayoutRes protected open fun getOptionsViewLayoutId(): Int? = null 123 124 private fun createOptionsView(@LayoutRes layoutId: Int?) { 125 if (layoutId == null) return 126 val stub = findViewById<View>(R.id.options_stub) as ViewStub 127 stub.layoutResource = layoutId 128 stub.inflate() 129 } 130 } 131