1 /* 2 * Copyright (C) 2018 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.screenrecord; 18 19 import static com.android.systemui.screenrecord.ScreenRecordingAudioSource.INTERNAL; 20 import static com.android.systemui.screenrecord.ScreenRecordingAudioSource.MIC; 21 import static com.android.systemui.screenrecord.ScreenRecordingAudioSource.MIC_AND_INTERNAL; 22 import static com.android.systemui.screenrecord.ScreenRecordingAudioSource.NONE; 23 24 import android.app.Activity; 25 import android.app.PendingIntent; 26 import android.content.Context; 27 import android.os.Bundle; 28 import android.view.Gravity; 29 import android.view.ViewGroup; 30 import android.view.Window; 31 import android.view.WindowManager; 32 import android.widget.ArrayAdapter; 33 import android.widget.Spinner; 34 import android.widget.Switch; 35 import android.widget.TextView; 36 37 import com.android.systemui.R; 38 import com.android.systemui.settings.UserContextProvider; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 import javax.inject.Inject; 44 45 /** 46 * Activity to select screen recording options 47 */ 48 public class ScreenRecordDialog extends Activity { 49 private static final long DELAY_MS = 3000; 50 private static final long INTERVAL_MS = 1000; 51 private static final String TAG = "ScreenRecordDialog"; 52 53 private final RecordingController mController; 54 private final UserContextProvider mUserContextProvider; 55 private Switch mTapsSwitch; 56 private Switch mAudioSwitch; 57 private Spinner mOptions; 58 private List<ScreenRecordingAudioSource> mModes; 59 60 @Inject ScreenRecordDialog(RecordingController controller, UserContextProvider userContextProvider)61 public ScreenRecordDialog(RecordingController controller, 62 UserContextProvider userContextProvider) { 63 mController = controller; 64 mUserContextProvider = userContextProvider; 65 } 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 71 Window window = getWindow(); 72 // Inflate the decor view, so the attributes below are not overwritten by the theme. 73 window.getDecorView(); 74 window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 75 window.addPrivateFlags(WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS); 76 window.setGravity(Gravity.TOP); 77 setTitle(R.string.screenrecord_name); 78 79 setContentView(R.layout.screen_record_dialog); 80 81 TextView cancelBtn = findViewById(R.id.button_cancel); 82 cancelBtn.setOnClickListener(v -> { 83 finish(); 84 }); 85 86 TextView startBtn = findViewById(R.id.button_start); 87 startBtn.setOnClickListener(v -> { 88 requestScreenCapture(); 89 finish(); 90 }); 91 92 mModes = new ArrayList<>(); 93 mModes.add(INTERNAL); 94 mModes.add(MIC); 95 mModes.add(MIC_AND_INTERNAL); 96 97 mAudioSwitch = findViewById(R.id.screenrecord_audio_switch); 98 mTapsSwitch = findViewById(R.id.screenrecord_taps_switch); 99 mOptions = findViewById(R.id.screen_recording_options); 100 ArrayAdapter a = new ScreenRecordingAdapter(getApplicationContext(), 101 android.R.layout.simple_spinner_dropdown_item, 102 mModes); 103 a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 104 mOptions.setAdapter(a); 105 mOptions.setOnItemClickListenerInt((parent, view, position, id) -> { 106 mAudioSwitch.setChecked(true); 107 }); 108 } 109 requestScreenCapture()110 private void requestScreenCapture() { 111 Context userContext = mUserContextProvider.getUserContext(); 112 boolean showTaps = mTapsSwitch.isChecked(); 113 ScreenRecordingAudioSource audioMode = mAudioSwitch.isChecked() 114 ? (ScreenRecordingAudioSource) mOptions.getSelectedItem() 115 : NONE; 116 PendingIntent startIntent = PendingIntent.getForegroundService(userContext, 117 RecordingService.REQUEST_CODE, 118 RecordingService.getStartIntent( 119 userContext, RESULT_OK, 120 audioMode.ordinal(), showTaps), 121 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); 122 PendingIntent stopIntent = PendingIntent.getService(userContext, 123 RecordingService.REQUEST_CODE, 124 RecordingService.getStopIntent(userContext), 125 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); 126 mController.startCountdown(DELAY_MS, INTERVAL_MS, startIntent, stopIntent); 127 } 128 } 129