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