Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.keyguard; |
| 17 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 18 | import static android.view.Display.DEFAULT_DISPLAY; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 19 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 21 | import android.app.Presentation; |
| 22 | import android.content.Context; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 23 | import android.graphics.Point; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 24 | import android.hardware.display.DisplayManager; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 25 | import android.media.MediaRouter; |
| 26 | import android.media.MediaRouter.RouteInfo; |
| 27 | import android.os.Bundle; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.util.SparseArray; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 30 | import android.view.Display; |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame^] | 31 | import android.view.DisplayInfo; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.WindowManager; |
| 34 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 35 | import java.util.function.BooleanSupplier; |
| 36 | |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 37 | // TODO(multi-display): Support multiple external displays |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 38 | public class KeyguardDisplayManager { |
| 39 | protected static final String TAG = "KeyguardDisplayManager"; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 40 | private static boolean DEBUG = KeyguardConstants.DEBUG; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 41 | |
| 42 | private final ViewMediatorCallback mCallback; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 43 | |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 44 | private final MediaRouter mMediaRouter; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 45 | private final DisplayManager mDisplayService; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 46 | private final Context mContext; |
| 47 | |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 48 | private boolean mShowing; |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame^] | 49 | private final DisplayInfo mTmpDisplayInfo = new DisplayInfo(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 50 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 51 | private final SparseArray<Presentation> mPresentations = new SparseArray<>(); |
| 52 | |
| 53 | private final DisplayManager.DisplayListener mDisplayListener = |
| 54 | new DisplayManager.DisplayListener() { |
| 55 | |
| 56 | @Override |
| 57 | public void onDisplayAdded(int displayId) { |
| 58 | final Display display = mDisplayService.getDisplay(displayId); |
| 59 | if (mShowing) { |
| 60 | notifyIfChanged(() -> showPresentation(display)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onDisplayChanged(int displayId) { |
| 66 | if (displayId == DEFAULT_DISPLAY) return; |
| 67 | final Display display = mDisplayService.getDisplay(displayId); |
| 68 | if (display != null && mShowing) { |
| 69 | final Presentation presentation = mPresentations.get(displayId); |
| 70 | if (presentation != null && !presentation.getDisplay().equals(display)) { |
| 71 | hidePresentation(displayId); |
| 72 | showPresentation(display); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onDisplayRemoved(int displayId) { |
| 79 | notifyIfChanged(() -> hidePresentation(displayId)); |
| 80 | } |
| 81 | }; |
| 82 | |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 83 | public KeyguardDisplayManager(Context context, ViewMediatorCallback callback) { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 84 | mContext = context; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 85 | mCallback = callback; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 86 | mMediaRouter = mContext.getSystemService(MediaRouter.class); |
| 87 | mDisplayService = mContext.getSystemService(DisplayManager.class); |
| 88 | mDisplayService.registerDisplayListener(mDisplayListener, null /* handler */); |
| 89 | } |
| 90 | |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame^] | 91 | private boolean isKeyguardShowable(Display display) { |
| 92 | if (display == null) { |
| 93 | if (DEBUG) Log.i(TAG, "Cannot show Keyguard on null display"); |
| 94 | return false; |
| 95 | } |
| 96 | if (display.getDisplayId() == DEFAULT_DISPLAY) { |
| 97 | if (DEBUG) Log.i(TAG, "Do not show KeyguardPresentation on the default display"); |
| 98 | return false; |
| 99 | } |
| 100 | display.getDisplayInfo(mTmpDisplayInfo); |
| 101 | if ((mTmpDisplayInfo.flags & Display.FLAG_PRIVATE) != 0) { |
| 102 | if (DEBUG) Log.i(TAG, "Do not show KeyguardPresentation on a private display"); |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 107 | /** |
| 108 | * @param display The display to show the presentation on. |
| 109 | * @return {@code true} if a presentation was added. |
| 110 | * {@code false} if the presentation cannot be added on that display or the presentation |
| 111 | * was already there. |
| 112 | */ |
| 113 | private boolean showPresentation(Display display) { |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame^] | 114 | if (!isKeyguardShowable(display)) return false; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 115 | if (DEBUG) Log.i(TAG, "Keyguard enabled on display: " + display); |
| 116 | final int displayId = display.getDisplayId(); |
| 117 | Presentation presentation = mPresentations.get(displayId); |
| 118 | if (presentation == null) { |
| 119 | presentation = new KeyguardPresentation(mContext, display); |
| 120 | presentation.setOnDismissListener(dialog -> { |
| 121 | if (null != mPresentations.get(displayId)) { |
| 122 | mPresentations.remove(displayId); |
| 123 | } |
| 124 | }); |
| 125 | try { |
| 126 | presentation.show(); |
| 127 | } catch (WindowManager.InvalidDisplayException ex) { |
| 128 | Log.w(TAG, "Invalid display:", ex); |
| 129 | presentation = null; |
| 130 | } |
| 131 | if (presentation != null) { |
| 132 | mPresentations.append(displayId, presentation); |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param displayId The id of the display to hide the presentation off. |
| 141 | * @return {@code true} if the a presentation was removed. |
| 142 | * {@code false} if the presentation was not added before. |
| 143 | */ |
| 144 | private boolean hidePresentation(int displayId) { |
| 145 | final Presentation presentation = mPresentations.get(displayId); |
| 146 | if (presentation != null) { |
| 147 | presentation.dismiss(); |
| 148 | mPresentations.remove(displayId); |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | private void notifyIfChanged(BooleanSupplier updateMethod) { |
| 155 | if (updateMethod.getAsBoolean()) { |
| 156 | final int[] displayList = getPresentationDisplayIds(); |
| 157 | mCallback.onSecondaryDisplayShowingChanged(displayList); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return An array of displayId's on which a {@link KeyguardPresentation} is showing on. |
| 163 | */ |
| 164 | @Nullable |
| 165 | private int[] getPresentationDisplayIds() { |
| 166 | final int size = mPresentations.size(); |
| 167 | if (size == 0) return null; |
| 168 | |
| 169 | final int[] displayIds = new int[size]; |
| 170 | for (int i = mPresentations.size() - 1; i >= 0; i--) { |
| 171 | final Presentation presentation = mPresentations.valueAt(i); |
| 172 | if (presentation != null) { |
| 173 | displayIds[i] = presentation.getDisplay().getDisplayId(); |
| 174 | } |
| 175 | } |
| 176 | return displayIds; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 179 | public void show() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 180 | if (!mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 181 | if (DEBUG) Log.v(TAG, "show"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 182 | mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY, |
| 183 | mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PASSIVE_DISCOVERY); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 184 | notifyIfChanged(() -> updateDisplays(true /* showing */)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 185 | } |
| 186 | mShowing = true; |
| 187 | } |
| 188 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 189 | public void hide() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 190 | if (mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 191 | if (DEBUG) Log.v(TAG, "hide"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 192 | mMediaRouter.removeCallback(mMediaRouterCallback); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 193 | notifyIfChanged(() -> updateDisplays(false /* showing */)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 194 | } |
| 195 | mShowing = false; |
| 196 | } |
| 197 | |
| 198 | private final MediaRouter.SimpleCallback mMediaRouterCallback = |
| 199 | new MediaRouter.SimpleCallback() { |
| 200 | @Override |
| 201 | public void onRouteSelected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 202 | if (DEBUG) Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info); |
| 203 | notifyIfChanged(() -> updateDisplays(mShowing)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 208 | if (DEBUG) Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info); |
| 209 | notifyIfChanged(() -> updateDisplays(mShowing)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 214 | if (DEBUG) Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info); |
| 215 | notifyIfChanged(() -> updateDisplays(mShowing)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 219 | protected boolean updateDisplays(boolean showing) { |
| 220 | boolean changed = false; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 221 | if (showing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 222 | final Display[] displays = mDisplayService.getDisplays(); |
| 223 | for (Display display : displays) { |
| 224 | changed |= showPresentation(display); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 225 | } |
| 226 | } else { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 227 | changed = mPresentations.size() > 0; |
| 228 | for (int i = mPresentations.size() - 1; i >= 0; i--) { |
| 229 | mPresentations.valueAt(i).dismiss(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 230 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 231 | mPresentations.clear(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 232 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 233 | return changed; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | private final static class KeyguardPresentation extends Presentation { |
| 237 | private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height |
| 238 | private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s |
| 239 | private View mClock; |
| 240 | private int mUsableWidth; |
| 241 | private int mUsableHeight; |
| 242 | private int mMarginTop; |
| 243 | private int mMarginLeft; |
| 244 | Runnable mMoveTextRunnable = new Runnable() { |
| 245 | @Override |
| 246 | public void run() { |
| 247 | int x = mMarginLeft + (int) (Math.random() * (mUsableWidth - mClock.getWidth())); |
| 248 | int y = mMarginTop + (int) (Math.random() * (mUsableHeight - mClock.getHeight())); |
| 249 | mClock.setTranslationX(x); |
| 250 | mClock.setTranslationY(y); |
| 251 | mClock.postDelayed(mMoveTextRunnable, MOVE_CLOCK_TIMEOUT); |
| 252 | } |
| 253 | }; |
| 254 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 255 | KeyguardPresentation(Context context, Display display) { |
| 256 | super(context, display, R.style.keyguard_presentation_theme); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 257 | getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 258 | setCancelable(false); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Jim Miller | c90d645 | 2015-07-06 18:17:34 -0700 | [diff] [blame] | 261 | @Override |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 262 | public void onDetachedFromWindow() { |
| 263 | mClock.removeCallbacks(mMoveTextRunnable); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | protected void onCreate(Bundle savedInstanceState) { |
| 268 | super.onCreate(savedInstanceState); |
| 269 | |
| 270 | Point p = new Point(); |
| 271 | getDisplay().getSize(p); |
| 272 | mUsableWidth = VIDEO_SAFE_REGION * p.x/100; |
| 273 | mUsableHeight = VIDEO_SAFE_REGION * p.y/100; |
| 274 | mMarginLeft = (100 - VIDEO_SAFE_REGION) * p.x / 200; |
| 275 | mMarginTop = (100 - VIDEO_SAFE_REGION) * p.y / 200; |
| 276 | |
| 277 | setContentView(R.layout.keyguard_presentation); |
| 278 | mClock = findViewById(R.id.clock); |
| 279 | |
| 280 | // Avoid screen burn in |
| 281 | mClock.post(mMoveTextRunnable); |
| 282 | } |
| 283 | } |
| 284 | } |