Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
| 19 | import android.content.Intent; |
| 20 | import android.service.quicksettings.Tile; |
Fabian Kozynski | 0daa970 | 2020-04-20 15:03:50 -0400 | [diff] [blame] | 21 | import android.text.TextUtils; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 22 | import android.util.Log; |
Fabian Kozynski | 0daa970 | 2020-04-20 15:03:50 -0400 | [diff] [blame] | 23 | import android.widget.Switch; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 24 | |
Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 25 | import com.android.internal.logging.UiEventLogger; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 26 | import com.android.systemui.R; |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 27 | import com.android.systemui.plugins.ActivityStarter; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 28 | import com.android.systemui.plugins.qs.QSTile; |
| 29 | import com.android.systemui.qs.QSHost; |
| 30 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
| 31 | import com.android.systemui.screenrecord.RecordingController; |
| 32 | |
| 33 | import javax.inject.Inject; |
| 34 | |
| 35 | /** |
| 36 | * Quick settings tile for screen recording |
| 37 | */ |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 38 | public class ScreenRecordTile extends QSTileImpl<QSTile.BooleanState> |
| 39 | implements RecordingController.RecordingStateChangeCallback { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 40 | private static final String TAG = "ScreenRecordTile"; |
| 41 | private RecordingController mController; |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 42 | private ActivityStarter mActivityStarter; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 43 | private long mMillisUntilFinished = 0; |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 44 | private Callback mCallback = new Callback(); |
Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 45 | private UiEventLogger mUiEventLogger; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 46 | |
| 47 | @Inject |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 48 | public ScreenRecordTile(QSHost host, RecordingController controller, |
Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 49 | ActivityStarter activityStarter, UiEventLogger uiEventLogger) { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 50 | super(host); |
| 51 | mController = controller; |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 52 | mController.observe(this, mCallback); |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 53 | mActivityStarter = activityStarter; |
Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 54 | mUiEventLogger = uiEventLogger; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public BooleanState newTileState() { |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 59 | BooleanState state = new BooleanState(); |
| 60 | state.label = mContext.getString(R.string.quick_settings_screen_record_label); |
| 61 | state.handlesLongClick = false; |
| 62 | return state; |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 63 | } |
| 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 Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 77 | @Override |
| 78 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 79 | boolean isStarting = mController.isStarting(); |
| 80 | boolean isRecording = mController.isRecording(); |
| 81 | |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 82 | state.value = isRecording || isStarting; |
| 83 | state.state = (isRecording || isStarting) ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Beth Thibodeau | c5dbffa | 2020-04-14 17:56:45 -0400 | [diff] [blame] | 84 | state.label = mContext.getString(R.string.quick_settings_screen_record_label); |
Beth Thibodeau | 47811b4 | 2020-04-22 01:22:39 -0400 | [diff] [blame] | 85 | state.icon = ResourceIcon.get(R.drawable.ic_screenrecord); |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 86 | |
| 87 | if (isRecording) { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 88 | 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 Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 92 | state.secondaryLabel = String.format("%d...", countdown); |
| 93 | } else { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 94 | state.secondaryLabel = mContext.getString(R.string.quick_settings_screen_record_start); |
| 95 | } |
Fabian Kozynski | 0daa970 | 2020-04-20 15:03:50 -0400 | [diff] [blame] | 96 | state.contentDescription = TextUtils.isEmpty(state.secondaryLabel) |
| 97 | ? state.label |
| 98 | : TextUtils.concat(state.label, ", ", state.secondaryLabel); |
| 99 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 100 | } |
| 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 Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 113 | public CharSequence getTileLabel() { |
| 114 | return mContext.getString(R.string.quick_settings_screen_record_label); |
| 115 | } |
| 116 | |
| 117 | private void startCountdown() { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 118 | // Close QS, otherwise the permission dialog appears beneath it |
| 119 | getHost().collapsePanels(); |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 120 | Intent intent = mController.getPromptIntent(); |
| 121 | mActivityStarter.postStartActivityDismissingKeyguard(intent, 0); |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private void cancelCountdown() { |
| 125 | Log.d(TAG, "Cancelling countdown"); |
| 126 | mController.cancelCountdown(); |
| 127 | } |
| 128 | |
| 129 | private void stopRecording() { |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 130 | mController.stopRecording(); |
| 131 | } |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 132 | |
| 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 Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 141 | public void onCountdownEnd() { |
| 142 | refreshState(); |
| 143 | } |
| 144 | |
| 145 | @Override |
Beth Thibodeau | 8bfbf1f | 2020-02-13 13:55:37 -0500 | [diff] [blame] | 146 | public void onRecordingStart() { |
| 147 | refreshState(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onRecordingEnd() { |
| 152 | refreshState(); |
| 153 | } |
| 154 | } |
Beth Thibodeau | 77c2545 | 2020-01-09 14:33:47 -0500 | [diff] [blame] | 155 | } |