blob: 32ef063a55be1b91a986c67a50a0ba7b5e79dbc4 [file] [log] [blame]
Beth Thibodeau77c25452020-01-09 14:33:47 -05001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.qs.tiles;
18
19import android.content.Intent;
20import android.service.quicksettings.Tile;
Fabian Kozynski0daa9702020-04-20 15:03:50 -040021import android.text.TextUtils;
Beth Thibodeau77c25452020-01-09 14:33:47 -050022import android.util.Log;
Fabian Kozynski0daa9702020-04-20 15:03:50 -040023import android.widget.Switch;
Beth Thibodeau77c25452020-01-09 14:33:47 -050024
Beth Thibodeauabb338f2020-03-03 16:17:24 -050025import com.android.internal.logging.UiEventLogger;
Beth Thibodeau77c25452020-01-09 14:33:47 -050026import com.android.systemui.R;
Beth Thibodeauf54cace2020-04-23 12:46:55 -040027import com.android.systemui.plugins.ActivityStarter;
Beth Thibodeau77c25452020-01-09 14:33:47 -050028import com.android.systemui.plugins.qs.QSTile;
29import com.android.systemui.qs.QSHost;
30import com.android.systemui.qs.tileimpl.QSTileImpl;
31import com.android.systemui.screenrecord.RecordingController;
32
33import javax.inject.Inject;
34
35/**
36 * Quick settings tile for screen recording
37 */
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -050038public class ScreenRecordTile extends QSTileImpl<QSTile.BooleanState>
39 implements RecordingController.RecordingStateChangeCallback {
Beth Thibodeau77c25452020-01-09 14:33:47 -050040 private static final String TAG = "ScreenRecordTile";
41 private RecordingController mController;
Beth Thibodeauf54cace2020-04-23 12:46:55 -040042 private ActivityStarter mActivityStarter;
Beth Thibodeau77c25452020-01-09 14:33:47 -050043 private long mMillisUntilFinished = 0;
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -050044 private Callback mCallback = new Callback();
Beth Thibodeauabb338f2020-03-03 16:17:24 -050045 private UiEventLogger mUiEventLogger;
Beth Thibodeau77c25452020-01-09 14:33:47 -050046
47 @Inject
Beth Thibodeauf54cace2020-04-23 12:46:55 -040048 public ScreenRecordTile(QSHost host, RecordingController controller,
Beth Thibodeauabb338f2020-03-03 16:17:24 -050049 ActivityStarter activityStarter, UiEventLogger uiEventLogger) {
Beth Thibodeau77c25452020-01-09 14:33:47 -050050 super(host);
51 mController = controller;
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -050052 mController.observe(this, mCallback);
Beth Thibodeauf54cace2020-04-23 12:46:55 -040053 mActivityStarter = activityStarter;
Beth Thibodeauabb338f2020-03-03 16:17:24 -050054 mUiEventLogger = uiEventLogger;
Beth Thibodeau77c25452020-01-09 14:33:47 -050055 }
56
57 @Override
58 public BooleanState newTileState() {
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -050059 BooleanState state = new BooleanState();
60 state.label = mContext.getString(R.string.quick_settings_screen_record_label);
61 state.handlesLongClick = false;
62 return state;
Beth Thibodeau77c25452020-01-09 14:33:47 -050063 }
64
65 @Override
66 protected void handleClick() {
67 if (mController.isStarting()) {
68 cancelCountdown();
69 } else if (mController.isRecording()) {
70 stopRecording();
71 } else {
72 startCountdown();
73 }
74 refreshState();
75 }
76
Beth Thibodeau77c25452020-01-09 14:33:47 -050077 @Override
78 protected void handleUpdateState(BooleanState state, Object arg) {
79 boolean isStarting = mController.isStarting();
80 boolean isRecording = mController.isRecording();
81
Beth Thibodeau77c25452020-01-09 14:33:47 -050082 state.value = isRecording || isStarting;
83 state.state = (isRecording || isStarting) ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Beth Thibodeauc5dbffa2020-04-14 17:56:45 -040084 state.label = mContext.getString(R.string.quick_settings_screen_record_label);
Beth Thibodeau47811b42020-04-22 01:22:39 -040085 state.icon = ResourceIcon.get(R.drawable.ic_screenrecord);
Beth Thibodeau77c25452020-01-09 14:33:47 -050086
87 if (isRecording) {
Beth Thibodeau77c25452020-01-09 14:33:47 -050088 state.secondaryLabel = mContext.getString(R.string.quick_settings_screen_record_stop);
89 } else if (isStarting) {
90 // round, since the timer isn't exact
91 int countdown = (int) Math.floorDiv(mMillisUntilFinished + 500, 1000);
Beth Thibodeau77c25452020-01-09 14:33:47 -050092 state.secondaryLabel = String.format("%d...", countdown);
93 } else {
Beth Thibodeau77c25452020-01-09 14:33:47 -050094 state.secondaryLabel = mContext.getString(R.string.quick_settings_screen_record_start);
95 }
Fabian Kozynski0daa9702020-04-20 15:03:50 -040096 state.contentDescription = TextUtils.isEmpty(state.secondaryLabel)
97 ? state.label
98 : TextUtils.concat(state.label, ", ", state.secondaryLabel);
99 state.expandedAccessibilityClassName = Switch.class.getName();
Beth Thibodeau77c25452020-01-09 14:33:47 -0500100 }
101
102 @Override
103 public int getMetricsCategory() {
104 return 0;
105 }
106
107 @Override
108 public Intent getLongClickIntent() {
109 return null;
110 }
111
112 @Override
Beth Thibodeau77c25452020-01-09 14:33:47 -0500113 public CharSequence getTileLabel() {
114 return mContext.getString(R.string.quick_settings_screen_record_label);
115 }
116
117 private void startCountdown() {
Beth Thibodeau77c25452020-01-09 14:33:47 -0500118 // Close QS, otherwise the permission dialog appears beneath it
119 getHost().collapsePanels();
Beth Thibodeauf54cace2020-04-23 12:46:55 -0400120 Intent intent = mController.getPromptIntent();
121 mActivityStarter.postStartActivityDismissingKeyguard(intent, 0);
Beth Thibodeau77c25452020-01-09 14:33:47 -0500122 }
123
124 private void cancelCountdown() {
125 Log.d(TAG, "Cancelling countdown");
126 mController.cancelCountdown();
127 }
128
129 private void stopRecording() {
Beth Thibodeau77c25452020-01-09 14:33:47 -0500130 mController.stopRecording();
131 }
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -0500132
133 private final class Callback implements RecordingController.RecordingStateChangeCallback {
134 @Override
135 public void onCountdown(long millisUntilFinished) {
136 mMillisUntilFinished = millisUntilFinished;
137 refreshState();
138 }
139
140 @Override
Beth Thibodeau58772782020-02-18 20:55:38 -0500141 public void onCountdownEnd() {
142 refreshState();
143 }
144
145 @Override
Beth Thibodeau8bfbf1f2020-02-13 13:55:37 -0500146 public void onRecordingStart() {
147 refreshState();
148 }
149
150 @Override
151 public void onRecordingEnd() {
152 refreshState();
153 }
154 }
Beth Thibodeau77c25452020-01-09 14:33:47 -0500155}