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