Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 1 | /* |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 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 | |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 17 | package com.android.systemui.statusbar.tv.micdisclosure; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 18 | |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 19 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 20 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.AnimatorSet; |
| 24 | import android.animation.ObjectAnimator; |
| 25 | import android.animation.PropertyValuesHolder; |
| 26 | import android.annotation.IntDef; |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 27 | import android.annotation.UiThread; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.pm.ApplicationInfo; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.graphics.PixelFormat; |
Robin Lee | b7f16fd | 2020-06-09 14:15:58 +0200 | [diff] [blame] | 32 | import android.provider.Settings; |
| 33 | import android.text.TextUtils; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 34 | import android.util.ArraySet; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.Gravity; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 39 | import android.view.ViewTreeObserver; |
| 40 | import android.view.WindowManager; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 41 | import android.widget.TextView; |
| 42 | |
| 43 | import com.android.systemui.R; |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 44 | import com.android.systemui.statusbar.tv.TvStatusBar; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 45 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 46 | import java.lang.annotation.Retention; |
| 47 | import java.lang.annotation.RetentionPolicy; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 48 | import java.util.Arrays; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 49 | import java.util.LinkedList; |
| 50 | import java.util.Queue; |
| 51 | import java.util.Set; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 52 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 53 | /** |
| 54 | * A component of {@link TvStatusBar} responsible for notifying the user whenever an application is |
| 55 | * recording audio. |
| 56 | * |
| 57 | * @see TvStatusBar |
| 58 | */ |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 59 | public class AudioRecordingDisclosureBar implements |
| 60 | AudioActivityObserver.OnAudioActivityStateChangeListener { |
| 61 | private static final String TAG = "AudioRecordingDisclosure"; |
| 62 | static final boolean DEBUG = false; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 63 | |
Sergey Nikolaienkov | b45c80f | 2019-10-23 10:58:49 +0200 | [diff] [blame] | 64 | // This title is used to test the microphone disclosure indicator in |
| 65 | // CtsSystemUiHostTestCases:TvMicrophoneCaptureIndicatorTest |
| 66 | private static final String LAYOUT_PARAMS_TITLE = "MicrophoneCaptureIndicator"; |
| 67 | |
Robin Lee | b7f16fd | 2020-06-09 14:15:58 +0200 | [diff] [blame] | 68 | private static final String EXEMPT_PACKAGES_LIST = "sysui_mic_disclosure_exempt"; |
| 69 | private static final String FORCED_PACKAGES_LIST = "sysui_mic_disclosure_forced"; |
| 70 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 71 | @Retention(RetentionPolicy.SOURCE) |
| 72 | @IntDef(prefix = {"STATE_"}, value = { |
| 73 | STATE_NOT_SHOWN, |
| 74 | STATE_APPEARING, |
| 75 | STATE_SHOWN, |
| 76 | STATE_MINIMIZING, |
| 77 | STATE_MINIMIZED, |
| 78 | STATE_MAXIMIZING, |
| 79 | STATE_DISAPPEARING |
| 80 | }) |
| 81 | public @interface State {} |
| 82 | |
| 83 | private static final int STATE_NOT_SHOWN = 0; |
| 84 | private static final int STATE_APPEARING = 1; |
| 85 | private static final int STATE_SHOWN = 2; |
| 86 | private static final int STATE_MINIMIZING = 3; |
| 87 | private static final int STATE_MINIMIZED = 4; |
| 88 | private static final int STATE_MAXIMIZING = 5; |
| 89 | private static final int STATE_DISAPPEARING = 6; |
| 90 | |
| 91 | private static final int ANIMATION_DURATION = 600; |
| 92 | private static final int MAXIMIZED_DURATION = 3000; |
| 93 | private static final int PULSE_BIT_DURATION = 1000; |
| 94 | private static final float PULSE_SCALE = 1.25f; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 95 | |
| 96 | private final Context mContext; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 97 | |
| 98 | private View mIndicatorView; |
| 99 | private View mIconTextsContainer; |
| 100 | private View mIconContainerBg; |
| 101 | private View mIcon; |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 102 | private View mBgEnd; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 103 | private View mTextsContainers; |
| 104 | private TextView mTextView; |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 105 | private boolean mIsLtr; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 106 | |
| 107 | @State private int mState = STATE_NOT_SHOWN; |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 108 | |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 109 | /** |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 110 | * Array of the observers that monitor different aspects of the system, such as AppOps and |
| 111 | * microphone foreground services |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 112 | */ |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 113 | private final AudioActivityObserver[] mAudioActivityObservers; |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 114 | /** |
| 115 | * Set of applications that we've notified the user about since the indicator came up. Meaning |
| 116 | * that if an application is in this list then at some point since the indicator came up, it |
| 117 | * was expanded showing this application's title. |
| 118 | * Used not to notify the user about the same application again while the indicator is shown. |
| 119 | * We empty this set every time the indicator goes off the screen (we always call {@code |
| 120 | * mSessionNotifiedPackages.clear()} before calling {@link #hide()}). |
| 121 | */ |
| 122 | private final Set<String> mSessionNotifiedPackages = new ArraySet<>(); |
| 123 | /** |
| 124 | * If an application starts recording while the TV indicator is neither in {@link |
| 125 | * #STATE_NOT_SHOWN} nor in {@link #STATE_MINIMIZED}, then we add the application's package |
| 126 | * name to the queue, from which we take packages names one by one to disclose the |
| 127 | * corresponding applications' titles to the user, whenever the indicator eventually comes to |
| 128 | * one of the two aforementioned states. |
| 129 | */ |
| 130 | private final Queue<String> mPendingNotificationPackages = new LinkedList<>(); |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 131 | /** |
| 132 | * Set of applications for which we make an exception and do not show the indicator. This gets |
| 133 | * populated once - in {@link #AudioRecordingDisclosureBar(Context)}. |
| 134 | */ |
| 135 | private final Set<String> mExemptPackages; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 136 | |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 137 | public AudioRecordingDisclosureBar(Context context) { |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 138 | mContext = context; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 139 | |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 140 | mExemptPackages = new ArraySet<>( |
| 141 | Arrays.asList(mContext.getResources().getStringArray( |
| 142 | R.array.audio_recording_disclosure_exempt_apps))); |
Robin Lee | b7f16fd | 2020-06-09 14:15:58 +0200 | [diff] [blame] | 143 | mExemptPackages.addAll(Arrays.asList(getGlobalStringArray(EXEMPT_PACKAGES_LIST))); |
| 144 | mExemptPackages.removeAll(Arrays.asList(getGlobalStringArray(FORCED_PACKAGES_LIST))); |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 145 | |
| 146 | mAudioActivityObservers = new AudioActivityObserver[]{ |
| 147 | new RecordAudioAppOpObserver(mContext, this), |
| 148 | new MicrophoneForegroundServicesObserver(mContext, this), |
| 149 | }; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Robin Lee | b7f16fd | 2020-06-09 14:15:58 +0200 | [diff] [blame] | 152 | private String[] getGlobalStringArray(String setting) { |
| 153 | String result = Settings.Global.getString(mContext.getContentResolver(), setting); |
| 154 | return TextUtils.isEmpty(result) ? new String[0] : result.split(","); |
| 155 | } |
| 156 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 157 | @UiThread |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 158 | @Override |
| 159 | public void onAudioActivityStateChange(boolean active, String packageName) { |
| 160 | if (DEBUG) { |
| 161 | Log.d(TAG, |
| 162 | "onAudioActivityStateChange, packageName=" + packageName + ", active=" |
| 163 | + active); |
| 164 | } |
| 165 | |
| 166 | if (mExemptPackages.contains(packageName)) { |
| 167 | if (DEBUG) Log.d(TAG, " - exempt package: ignoring"); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 168 | return; |
| 169 | } |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 170 | |
| 171 | if (active) { |
| 172 | showIndicatorForPackageIfNeeded(packageName); |
| 173 | } else { |
| 174 | hideIndicatorIfNeeded(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @UiThread |
| 179 | private void showIndicatorForPackageIfNeeded(String packageName) { |
| 180 | if (DEBUG) Log.d(TAG, "showIndicatorForPackageIfNeeded, packageName=" + packageName); |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 181 | if (!mSessionNotifiedPackages.add(packageName)) { |
| 182 | // We've already notified user about this app, no need to do it again. |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 183 | if (DEBUG) Log.d(TAG, " - already notified"); |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 184 | return; |
| 185 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 186 | |
| 187 | switch (mState) { |
| 188 | case STATE_NOT_SHOWN: |
| 189 | show(packageName); |
| 190 | break; |
| 191 | |
| 192 | case STATE_MINIMIZED: |
| 193 | expand(packageName); |
| 194 | break; |
| 195 | |
| 196 | case STATE_DISAPPEARING: |
| 197 | case STATE_APPEARING: |
| 198 | case STATE_MAXIMIZING: |
| 199 | case STATE_SHOWN: |
| 200 | case STATE_MINIMIZING: |
| 201 | // Currently animating or expanded. Thus add to the pending notifications, and it |
| 202 | // will be picked up once the indicator comes to the STATE_MINIMIZED. |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 203 | mPendingNotificationPackages.add(packageName); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 208 | @UiThread |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 209 | private void hideIndicatorIfNeeded() { |
| 210 | if (DEBUG) Log.d(TAG, "hideIndicatorIfNeeded"); |
| 211 | // If not MINIMIZED, will check whether the indicator should be hidden when the indicator |
| 212 | // comes to the STATE_MINIMIZED eventually. |
| 213 | if (mState != STATE_MINIMIZED) return; |
| 214 | |
| 215 | // If is in the STATE_MINIMIZED, but there are other active recorders - simply ignore. |
| 216 | for (int index = mAudioActivityObservers.length - 1; index >= 0; index--) { |
| 217 | for (String activePackage : mAudioActivityObservers[index].getActivePackages()) { |
| 218 | if (mExemptPackages.contains(activePackage)) continue; |
| 219 | if (DEBUG) Log.d(TAG, " - there are still ongoing activities"); |
| 220 | return; |
| 221 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 224 | // Clear the state and hide the indicator. |
| 225 | mSessionNotifiedPackages.clear(); |
| 226 | hide(); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 229 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 230 | private void show(String packageName) { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 231 | final String label = getApplicationLabel(packageName); |
| 232 | if (DEBUG) { |
| 233 | Log.d(TAG, "Showing indicator for " + packageName + " (" + label + ")..."); |
| 234 | } |
| 235 | |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 236 | mIsLtr = mContext.getResources().getConfiguration().getLayoutDirection() |
| 237 | == View.LAYOUT_DIRECTION_LTR; |
| 238 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 239 | // Inflate the indicator view |
| 240 | mIndicatorView = LayoutInflater.from(mContext).inflate( |
| 241 | R.layout.tv_audio_recording_indicator, |
| 242 | null); |
| 243 | mIconTextsContainer = mIndicatorView.findViewById(R.id.icon_texts_container); |
| 244 | mIconContainerBg = mIconTextsContainer.findViewById(R.id.icon_container_bg); |
| 245 | mIcon = mIconTextsContainer.findViewById(R.id.icon_mic); |
| 246 | mTextsContainers = mIconTextsContainer.findViewById(R.id.texts_container); |
| 247 | mTextView = mTextsContainers.findViewById(R.id.text); |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 248 | mBgEnd = mIndicatorView.findViewById(R.id.bg_end); |
| 249 | |
| 250 | // Swap background drawables depending on layout directions (both drawables have rounded |
| 251 | // corners only on one side) |
| 252 | if (mIsLtr) { |
| 253 | mBgEnd.setBackgroundResource(R.drawable.tv_rect_dark_right_rounded); |
| 254 | mIconContainerBg.setBackgroundResource(R.drawable.tv_rect_dark_left_rounded); |
| 255 | } else { |
| 256 | mBgEnd.setBackgroundResource(R.drawable.tv_rect_dark_left_rounded); |
| 257 | mIconContainerBg.setBackgroundResource(R.drawable.tv_rect_dark_right_rounded); |
| 258 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 259 | |
| 260 | // Set up the notification text |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 261 | mTextView.setText(mContext.getString(R.string.app_accessed_mic, label)); |
| 262 | |
| 263 | // Initially change the visibility to INVISIBLE, wait until and receives the size and |
| 264 | // then animate it moving from "off" the screen correctly |
| 265 | mIndicatorView.setVisibility(View.INVISIBLE); |
| 266 | mIndicatorView |
| 267 | .getViewTreeObserver() |
| 268 | .addOnGlobalLayoutListener( |
| 269 | new ViewTreeObserver.OnGlobalLayoutListener() { |
| 270 | @Override |
| 271 | public void onGlobalLayout() { |
| 272 | // Remove the observer |
| 273 | mIndicatorView.getViewTreeObserver().removeOnGlobalLayoutListener( |
| 274 | this); |
| 275 | |
| 276 | // Now that the width of the indicator has been assigned, we can |
| 277 | // move it in from off the screen. |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 278 | final int initialOffset = |
| 279 | (mIsLtr ? 1 : -1) * mIndicatorView.getWidth(); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 280 | final AnimatorSet set = new AnimatorSet(); |
| 281 | set.setDuration(ANIMATION_DURATION); |
| 282 | set.playTogether( |
| 283 | ObjectAnimator.ofFloat(mIndicatorView, |
| 284 | View.TRANSLATION_X, initialOffset, 0), |
| 285 | ObjectAnimator.ofFloat(mIndicatorView, View.ALPHA, 0f, |
| 286 | 1f)); |
| 287 | set.addListener( |
| 288 | new AnimatorListenerAdapter() { |
| 289 | @Override |
| 290 | public void onAnimationStart(Animator animation, |
| 291 | boolean isReverse) { |
| 292 | // Indicator is INVISIBLE at the moment, change it. |
| 293 | mIndicatorView.setVisibility(View.VISIBLE); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void onAnimationEnd(Animator animation) { |
| 298 | startPulsatingAnimation(); |
| 299 | onExpanded(); |
| 300 | } |
| 301 | }); |
| 302 | set.start(); |
| 303 | } |
| 304 | }); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 305 | |
| 306 | final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 307 | WRAP_CONTENT, |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 308 | WRAP_CONTENT, |
| 309 | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, |
| 310 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 311 | PixelFormat.TRANSLUCENT); |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 312 | layoutParams.gravity = Gravity.TOP | (mIsLtr ? Gravity.RIGHT : Gravity.LEFT); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 313 | layoutParams.setTitle(LAYOUT_PARAMS_TITLE); |
| 314 | layoutParams.packageName = mContext.getPackageName(); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 315 | final WindowManager windowManager = (WindowManager) mContext.getSystemService( |
| 316 | Context.WINDOW_SERVICE); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 317 | windowManager.addView(mIndicatorView, layoutParams); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 318 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 319 | mState = STATE_APPEARING; |
| 320 | } |
| 321 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 322 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 323 | private void expand(String packageName) { |
| 324 | final String label = getApplicationLabel(packageName); |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 325 | if (DEBUG) { |
| 326 | Log.d(TAG, "Expanding for " + packageName + " (" + label + ")..."); |
| 327 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 328 | mTextView.setText(mContext.getString(R.string.app_accessed_mic, label)); |
| 329 | |
| 330 | final AnimatorSet set = new AnimatorSet(); |
| 331 | set.playTogether( |
| 332 | ObjectAnimator.ofFloat(mIconTextsContainer, View.TRANSLATION_X, 0), |
| 333 | ObjectAnimator.ofFloat(mIconContainerBg, View.ALPHA, 1f), |
| 334 | ObjectAnimator.ofFloat(mTextsContainers, View.ALPHA, 1f), |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 335 | ObjectAnimator.ofFloat(mBgEnd, View.ALPHA, 1f)); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 336 | set.setDuration(ANIMATION_DURATION); |
| 337 | set.addListener( |
| 338 | new AnimatorListenerAdapter() { |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 339 | @Override |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 340 | public void onAnimationEnd(Animator animation) { |
| 341 | onExpanded(); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 342 | } |
| 343 | }); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 344 | set.start(); |
| 345 | |
| 346 | mState = STATE_MAXIMIZING; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 349 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 350 | private void minimize() { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 351 | if (DEBUG) Log.d(TAG, "Minimizing..."); |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 352 | final int targetOffset = (mIsLtr ? 1 : -1) * mTextsContainers.getWidth(); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 353 | final AnimatorSet set = new AnimatorSet(); |
| 354 | set.playTogether( |
| 355 | ObjectAnimator.ofFloat(mIconTextsContainer, View.TRANSLATION_X, targetOffset), |
| 356 | ObjectAnimator.ofFloat(mIconContainerBg, View.ALPHA, 0f), |
| 357 | ObjectAnimator.ofFloat(mTextsContainers, View.ALPHA, 0f), |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 358 | ObjectAnimator.ofFloat(mBgEnd, View.ALPHA, 0f)); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 359 | set.setDuration(ANIMATION_DURATION); |
| 360 | set.addListener( |
| 361 | new AnimatorListenerAdapter() { |
| 362 | @Override |
| 363 | public void onAnimationEnd(Animator animation) { |
| 364 | onMinimized(); |
| 365 | } |
| 366 | }); |
| 367 | set.start(); |
| 368 | |
| 369 | mState = STATE_MINIMIZING; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 372 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 373 | private void hide() { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 374 | if (DEBUG) Log.d(TAG, "Hiding..."); |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 375 | final int targetOffset = (mIsLtr ? 1 : -1) * (mIndicatorView.getWidth() |
| 376 | - (int) mIconTextsContainer.getTranslationX()); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 377 | final AnimatorSet set = new AnimatorSet(); |
| 378 | set.playTogether( |
| 379 | ObjectAnimator.ofFloat(mIndicatorView, View.TRANSLATION_X, targetOffset), |
| 380 | ObjectAnimator.ofFloat(mIcon, View.ALPHA, 0f)); |
| 381 | set.setDuration(ANIMATION_DURATION); |
| 382 | set.addListener( |
| 383 | new AnimatorListenerAdapter() { |
| 384 | @Override |
| 385 | public void onAnimationEnd(Animator animation) { |
| 386 | onHidden(); |
| 387 | } |
| 388 | }); |
| 389 | set.start(); |
| 390 | |
| 391 | mState = STATE_DISAPPEARING; |
| 392 | } |
| 393 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 394 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 395 | private void onExpanded() { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 396 | if (DEBUG) Log.d(TAG, "Expanded"); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 397 | mState = STATE_SHOWN; |
| 398 | |
| 399 | mIndicatorView.postDelayed(this::minimize, MAXIMIZED_DURATION); |
| 400 | } |
| 401 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 402 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 403 | private void onMinimized() { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 404 | if (DEBUG) Log.d(TAG, "Minimized"); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 405 | mState = STATE_MINIMIZED; |
| 406 | |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 407 | if (!mPendingNotificationPackages.isEmpty()) { |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 408 | // There is a new application that started recording, tell the user about it. |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 409 | expand(mPendingNotificationPackages.poll()); |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 410 | } else { |
| 411 | hideIndicatorIfNeeded(); |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 415 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 416 | private void onHidden() { |
Sergey Nikolaienkov | d07dc4d | 2020-03-26 17:57:40 +0100 | [diff] [blame] | 417 | if (DEBUG) Log.d(TAG, "Hidden"); |
| 418 | |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 419 | final WindowManager windowManager = (WindowManager) mContext.getSystemService( |
| 420 | Context.WINDOW_SERVICE); |
| 421 | windowManager.removeView(mIndicatorView); |
| 422 | |
| 423 | mIndicatorView = null; |
| 424 | mIconTextsContainer = null; |
| 425 | mIconContainerBg = null; |
| 426 | mIcon = null; |
| 427 | mTextsContainers = null; |
| 428 | mTextView = null; |
Sergey Nikolaienkov | 8dfc49b | 2020-06-22 11:38:35 +0200 | [diff] [blame] | 429 | mBgEnd = null; |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 430 | |
| 431 | mState = STATE_NOT_SHOWN; |
Sergey Nikolaienkov | fa016df | 2020-03-05 07:27:58 +0100 | [diff] [blame] | 432 | |
| 433 | // Check if anybody started recording while we were in STATE_DISAPPEARING |
| 434 | if (!mPendingNotificationPackages.isEmpty()) { |
| 435 | // There is a new application that started recording, tell the user about it. |
| 436 | show(mPendingNotificationPackages.poll()); |
| 437 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Sergey Nikolaienkov | 224e962 | 2020-03-10 13:58:08 +0100 | [diff] [blame] | 440 | @UiThread |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 441 | private void startPulsatingAnimation() { |
| 442 | final View pulsatingView = mIconTextsContainer.findViewById(R.id.pulsating_circle); |
| 443 | final ObjectAnimator animator = |
| 444 | ObjectAnimator.ofPropertyValuesHolder( |
| 445 | pulsatingView, |
| 446 | PropertyValuesHolder.ofFloat(View.SCALE_X, PULSE_SCALE), |
| 447 | PropertyValuesHolder.ofFloat(View.SCALE_Y, PULSE_SCALE)); |
| 448 | animator.setDuration(PULSE_BIT_DURATION); |
| 449 | animator.setRepeatCount(ObjectAnimator.INFINITE); |
| 450 | animator.setRepeatMode(ObjectAnimator.REVERSE); |
| 451 | animator.start(); |
| 452 | } |
| 453 | |
| 454 | private String getApplicationLabel(String packageName) { |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 455 | final PackageManager pm = mContext.getPackageManager(); |
| 456 | final ApplicationInfo appInfo; |
| 457 | try { |
| 458 | appInfo = pm.getApplicationInfo(packageName, 0); |
| 459 | } catch (PackageManager.NameNotFoundException e) { |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 460 | return packageName; |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 461 | } |
Sergey Nikolaienkov | 5b514ee | 2019-12-06 10:42:59 +0100 | [diff] [blame] | 462 | return pm.getApplicationLabel(appInfo).toString(); |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 463 | } |
Sergey Nikolaienkov | c7a95ac | 2019-08-29 07:23:11 +0200 | [diff] [blame] | 464 | } |