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; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 31 | import android.view.LayoutInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.WindowManager; |
| 34 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 35 | import com.android.systemui.util.InjectionInflationController; |
| 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 | |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 42 | private final MediaRouter mMediaRouter; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 43 | private final DisplayManager mDisplayService; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 44 | private final InjectionInflationController mInjectableInflater; |
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; |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame] | 48 | private final DisplayInfo mTmpDisplayInfo = new DisplayInfo(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 49 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 50 | private final SparseArray<Presentation> mPresentations = new SparseArray<>(); |
| 51 | |
| 52 | private final DisplayManager.DisplayListener mDisplayListener = |
| 53 | new DisplayManager.DisplayListener() { |
| 54 | |
| 55 | @Override |
| 56 | public void onDisplayAdded(int displayId) { |
| 57 | final Display display = mDisplayService.getDisplay(displayId); |
| 58 | if (mShowing) { |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 59 | showPresentation(display); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onDisplayChanged(int displayId) { |
| 65 | if (displayId == DEFAULT_DISPLAY) return; |
| 66 | final Display display = mDisplayService.getDisplay(displayId); |
| 67 | if (display != null && mShowing) { |
| 68 | final Presentation presentation = mPresentations.get(displayId); |
| 69 | if (presentation != null && !presentation.getDisplay().equals(display)) { |
| 70 | hidePresentation(displayId); |
| 71 | showPresentation(display); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onDisplayRemoved(int displayId) { |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 78 | hidePresentation(displayId); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 82 | public KeyguardDisplayManager(Context context, |
| 83 | InjectionInflationController injectableInflater) { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 84 | mContext = context; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 85 | mInjectableInflater = injectableInflater; |
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) { |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 119 | presentation = new KeyguardPresentation(mContext, display, mInjectableInflater); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 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. |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 141 | */ |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 142 | private void hidePresentation(int displayId) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 143 | final Presentation presentation = mPresentations.get(displayId); |
| 144 | if (presentation != null) { |
| 145 | presentation.dismiss(); |
| 146 | mPresentations.remove(displayId); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 147 | } |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 150 | public void show() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 151 | if (!mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 152 | if (DEBUG) Log.v(TAG, "show"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 153 | mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY, |
| 154 | mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PASSIVE_DISCOVERY); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 155 | updateDisplays(true /* showing */); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 156 | } |
| 157 | mShowing = true; |
| 158 | } |
| 159 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 160 | public void hide() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 161 | if (mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 162 | if (DEBUG) Log.v(TAG, "hide"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 163 | mMediaRouter.removeCallback(mMediaRouterCallback); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 164 | updateDisplays(false /* showing */); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 165 | } |
| 166 | mShowing = false; |
| 167 | } |
| 168 | |
| 169 | private final MediaRouter.SimpleCallback mMediaRouterCallback = |
| 170 | new MediaRouter.SimpleCallback() { |
| 171 | @Override |
| 172 | public void onRouteSelected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 173 | if (DEBUG) Log.d(TAG, "onRouteSelected: 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 onRouteUnselected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 179 | if (DEBUG) Log.d(TAG, "onRouteUnselected: type=" + type + ", 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 | @Override |
| 184 | public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 185 | if (DEBUG) Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 186 | updateDisplays(mShowing); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 187 | } |
| 188 | }; |
| 189 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 190 | protected boolean updateDisplays(boolean showing) { |
| 191 | boolean changed = false; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 192 | if (showing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 193 | final Display[] displays = mDisplayService.getDisplays(); |
| 194 | for (Display display : displays) { |
| 195 | changed |= showPresentation(display); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 196 | } |
| 197 | } else { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 198 | changed = mPresentations.size() > 0; |
| 199 | for (int i = mPresentations.size() - 1; i >= 0; i--) { |
| 200 | mPresentations.valueAt(i).dismiss(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 201 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 202 | mPresentations.clear(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 203 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 204 | return changed; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | private final static class KeyguardPresentation extends Presentation { |
| 208 | private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height |
| 209 | private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 210 | private final InjectionInflationController mInjectableInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 211 | private View mClock; |
| 212 | private int mUsableWidth; |
| 213 | private int mUsableHeight; |
| 214 | private int mMarginTop; |
| 215 | private int mMarginLeft; |
| 216 | Runnable mMoveTextRunnable = new Runnable() { |
| 217 | @Override |
| 218 | public void run() { |
| 219 | int x = mMarginLeft + (int) (Math.random() * (mUsableWidth - mClock.getWidth())); |
| 220 | int y = mMarginTop + (int) (Math.random() * (mUsableHeight - mClock.getHeight())); |
| 221 | mClock.setTranslationX(x); |
| 222 | mClock.setTranslationY(y); |
| 223 | mClock.postDelayed(mMoveTextRunnable, MOVE_CLOCK_TIMEOUT); |
| 224 | } |
| 225 | }; |
| 226 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 227 | KeyguardPresentation(Context context, Display display, |
| 228 | InjectionInflationController injectionInflater) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 229 | super(context, display, R.style.keyguard_presentation_theme); |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 230 | mInjectableInflater = injectionInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 231 | getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 232 | setCancelable(false); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Jim Miller | c90d645 | 2015-07-06 18:17:34 -0700 | [diff] [blame] | 235 | @Override |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 236 | public void onDetachedFromWindow() { |
| 237 | mClock.removeCallbacks(mMoveTextRunnable); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | protected void onCreate(Bundle savedInstanceState) { |
| 242 | super.onCreate(savedInstanceState); |
| 243 | |
| 244 | Point p = new Point(); |
| 245 | getDisplay().getSize(p); |
| 246 | mUsableWidth = VIDEO_SAFE_REGION * p.x/100; |
| 247 | mUsableHeight = VIDEO_SAFE_REGION * p.y/100; |
| 248 | mMarginLeft = (100 - VIDEO_SAFE_REGION) * p.x / 200; |
| 249 | mMarginTop = (100 - VIDEO_SAFE_REGION) * p.y / 200; |
| 250 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame^] | 251 | LayoutInflater inflater = mInjectableInflater.injectable( |
| 252 | LayoutInflater.from(getContext())); |
| 253 | setContentView(inflater.inflate(R.layout.keyguard_presentation, null)); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 254 | mClock = findViewById(R.id.clock); |
| 255 | |
| 256 | // Avoid screen burn in |
| 257 | mClock.post(mMoveTextRunnable); |
| 258 | } |
| 259 | } |
| 260 | } |