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; |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 22 | import android.graphics.Color; |
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; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.WindowManager; |
| 35 | |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 36 | import com.android.systemui.Dependency; |
| 37 | import com.android.systemui.statusbar.NavigationBarController; |
| 38 | import com.android.systemui.statusbar.phone.NavigationBarView; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 39 | import com.android.systemui.util.InjectionInflationController; |
| 40 | |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 41 | public class KeyguardDisplayManager { |
| 42 | protected static final String TAG = "KeyguardDisplayManager"; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 43 | private static boolean DEBUG = KeyguardConstants.DEBUG; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 44 | |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 45 | private final MediaRouter mMediaRouter; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 46 | private final DisplayManager mDisplayService; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 47 | private final InjectionInflationController mInjectableInflater; |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 48 | private final Context mContext; |
| 49 | |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 50 | private boolean mShowing; |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame] | 51 | private final DisplayInfo mTmpDisplayInfo = new DisplayInfo(); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 52 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 53 | private final SparseArray<Presentation> mPresentations = new SparseArray<>(); |
| 54 | |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 55 | private final NavigationBarController mNavBarController = |
| 56 | Dependency.get(NavigationBarController.class); |
| 57 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 58 | private final DisplayManager.DisplayListener mDisplayListener = |
| 59 | new DisplayManager.DisplayListener() { |
| 60 | |
| 61 | @Override |
| 62 | public void onDisplayAdded(int displayId) { |
| 63 | final Display display = mDisplayService.getDisplay(displayId); |
| 64 | if (mShowing) { |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 65 | updateNavigationBarVisibility(displayId, false /* navBarVisible */); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 66 | showPresentation(display); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onDisplayChanged(int displayId) { |
| 72 | if (displayId == DEFAULT_DISPLAY) return; |
| 73 | final Display display = mDisplayService.getDisplay(displayId); |
| 74 | if (display != null && mShowing) { |
| 75 | final Presentation presentation = mPresentations.get(displayId); |
| 76 | if (presentation != null && !presentation.getDisplay().equals(display)) { |
| 77 | hidePresentation(displayId); |
| 78 | showPresentation(display); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onDisplayRemoved(int displayId) { |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 85 | hidePresentation(displayId); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 89 | public KeyguardDisplayManager(Context context, |
| 90 | InjectionInflationController injectableInflater) { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 91 | mContext = context; |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 92 | mInjectableInflater = injectableInflater; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 93 | mMediaRouter = mContext.getSystemService(MediaRouter.class); |
| 94 | mDisplayService = mContext.getSystemService(DisplayManager.class); |
| 95 | mDisplayService.registerDisplayListener(mDisplayListener, null /* handler */); |
| 96 | } |
| 97 | |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame] | 98 | private boolean isKeyguardShowable(Display display) { |
| 99 | if (display == null) { |
| 100 | if (DEBUG) Log.i(TAG, "Cannot show Keyguard on null display"); |
| 101 | return false; |
| 102 | } |
| 103 | if (display.getDisplayId() == DEFAULT_DISPLAY) { |
| 104 | if (DEBUG) Log.i(TAG, "Do not show KeyguardPresentation on the default display"); |
| 105 | return false; |
| 106 | } |
| 107 | display.getDisplayInfo(mTmpDisplayInfo); |
| 108 | if ((mTmpDisplayInfo.flags & Display.FLAG_PRIVATE) != 0) { |
| 109 | if (DEBUG) Log.i(TAG, "Do not show KeyguardPresentation on a private display"); |
| 110 | return false; |
| 111 | } |
| 112 | return true; |
| 113 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 114 | /** |
| 115 | * @param display The display to show the presentation on. |
| 116 | * @return {@code true} if a presentation was added. |
| 117 | * {@code false} if the presentation cannot be added on that display or the presentation |
| 118 | * was already there. |
| 119 | */ |
| 120 | private boolean showPresentation(Display display) { |
wilsonshih | b5d1ab9 | 2018-12-06 12:52:31 +0800 | [diff] [blame] | 121 | if (!isKeyguardShowable(display)) return false; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 122 | if (DEBUG) Log.i(TAG, "Keyguard enabled on display: " + display); |
| 123 | final int displayId = display.getDisplayId(); |
| 124 | Presentation presentation = mPresentations.get(displayId); |
| 125 | if (presentation == null) { |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 126 | presentation = new KeyguardPresentation(mContext, display, mInjectableInflater); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 127 | presentation.setOnDismissListener(dialog -> { |
| 128 | if (null != mPresentations.get(displayId)) { |
| 129 | mPresentations.remove(displayId); |
| 130 | } |
| 131 | }); |
| 132 | try { |
| 133 | presentation.show(); |
| 134 | } catch (WindowManager.InvalidDisplayException ex) { |
| 135 | Log.w(TAG, "Invalid display:", ex); |
| 136 | presentation = null; |
| 137 | } |
| 138 | if (presentation != null) { |
| 139 | mPresentations.append(displayId, presentation); |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param displayId The id of the display to hide the presentation off. |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 148 | */ |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 149 | private void hidePresentation(int displayId) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 150 | final Presentation presentation = mPresentations.get(displayId); |
| 151 | if (presentation != null) { |
| 152 | presentation.dismiss(); |
| 153 | mPresentations.remove(displayId); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 154 | } |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 157 | public void show() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 158 | if (!mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 159 | if (DEBUG) Log.v(TAG, "show"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 160 | mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY, |
| 161 | mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PASSIVE_DISCOVERY); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 162 | updateDisplays(true /* showing */); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 163 | } |
| 164 | mShowing = true; |
| 165 | } |
| 166 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 167 | public void hide() { |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 168 | if (mShowing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 169 | if (DEBUG) Log.v(TAG, "hide"); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 170 | mMediaRouter.removeCallback(mMediaRouterCallback); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 171 | updateDisplays(false /* showing */); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 172 | } |
| 173 | mShowing = false; |
| 174 | } |
| 175 | |
| 176 | private final MediaRouter.SimpleCallback mMediaRouterCallback = |
| 177 | new MediaRouter.SimpleCallback() { |
| 178 | @Override |
| 179 | public void onRouteSelected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 180 | if (DEBUG) Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 181 | updateDisplays(mShowing); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 186 | if (DEBUG) Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 187 | updateDisplays(mShowing); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 192 | if (DEBUG) Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info); |
wilsonshih | 177261f | 2019-02-22 12:02:18 +0800 | [diff] [blame] | 193 | updateDisplays(mShowing); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 194 | } |
| 195 | }; |
| 196 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 197 | protected boolean updateDisplays(boolean showing) { |
| 198 | boolean changed = false; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 199 | if (showing) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 200 | final Display[] displays = mDisplayService.getDisplays(); |
| 201 | for (Display display : displays) { |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 202 | int displayId = display.getDisplayId(); |
| 203 | updateNavigationBarVisibility(displayId, false /* navBarVisible */); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 204 | changed |= showPresentation(display); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 205 | } |
| 206 | } else { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 207 | changed = mPresentations.size() > 0; |
| 208 | for (int i = mPresentations.size() - 1; i >= 0; i--) { |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 209 | int displayId = mPresentations.keyAt(i); |
| 210 | updateNavigationBarVisibility(displayId, true /* navBarVisible */); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 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 | |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 218 | // TODO(b/127878649): this logic is from |
| 219 | // {@link StatusBarKeyguardViewManager#updateNavigationBarVisibility}. Try to revisit a long |
| 220 | // term solution in R. |
| 221 | private void updateNavigationBarVisibility(int displayId, boolean navBarVisible) { |
| 222 | // Leave this task to {@link StatusBarKeyguardViewManager} |
| 223 | if (displayId == DEFAULT_DISPLAY) return; |
| 224 | |
| 225 | NavigationBarView navBarView = mNavBarController.getNavigationBarView(displayId); |
| 226 | // We may not have nav bar on a display. |
| 227 | if (navBarView == null) return; |
| 228 | |
| 229 | if (navBarVisible) { |
| 230 | navBarView.getRootView().setVisibility(View.VISIBLE); |
| 231 | } else { |
| 232 | navBarView.getRootView().setVisibility(View.GONE); |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 237 | private final static class KeyguardPresentation extends Presentation { |
| 238 | private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height |
| 239 | private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 240 | private final InjectionInflationController mInjectableInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 241 | private View mClock; |
| 242 | private int mUsableWidth; |
| 243 | private int mUsableHeight; |
| 244 | private int mMarginTop; |
| 245 | private int mMarginLeft; |
| 246 | Runnable mMoveTextRunnable = new Runnable() { |
| 247 | @Override |
| 248 | public void run() { |
| 249 | int x = mMarginLeft + (int) (Math.random() * (mUsableWidth - mClock.getWidth())); |
| 250 | int y = mMarginTop + (int) (Math.random() * (mUsableHeight - mClock.getHeight())); |
| 251 | mClock.setTranslationX(x); |
| 252 | mClock.setTranslationY(y); |
| 253 | mClock.postDelayed(mMoveTextRunnable, MOVE_CLOCK_TIMEOUT); |
| 254 | } |
| 255 | }; |
| 256 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 257 | KeyguardPresentation(Context context, Display display, |
| 258 | InjectionInflationController injectionInflater) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 259 | super(context, display, R.style.keyguard_presentation_theme); |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 260 | mInjectableInflater = injectionInflater; |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 261 | getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 262 | setCancelable(false); |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Jim Miller | c90d645 | 2015-07-06 18:17:34 -0700 | [diff] [blame] | 265 | @Override |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 266 | public void onDetachedFromWindow() { |
| 267 | mClock.removeCallbacks(mMoveTextRunnable); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | protected void onCreate(Bundle savedInstanceState) { |
| 272 | super.onCreate(savedInstanceState); |
| 273 | |
| 274 | Point p = new Point(); |
| 275 | getDisplay().getSize(p); |
| 276 | mUsableWidth = VIDEO_SAFE_REGION * p.x/100; |
| 277 | mUsableHeight = VIDEO_SAFE_REGION * p.y/100; |
| 278 | mMarginLeft = (100 - VIDEO_SAFE_REGION) * p.x / 200; |
| 279 | mMarginTop = (100 - VIDEO_SAFE_REGION) * p.y / 200; |
| 280 | |
Robert Snoeberger | be35b76 | 2019-03-15 14:33:02 -0400 | [diff] [blame] | 281 | LayoutInflater inflater = mInjectableInflater.injectable( |
| 282 | LayoutInflater.from(getContext())); |
| 283 | setContentView(inflater.inflate(R.layout.keyguard_presentation, null)); |
Charles Chen | 64172bb | 2019-04-22 17:30:29 +0800 | [diff] [blame^] | 284 | |
| 285 | // Logic to make the lock screen fullscreen |
| 286 | getWindow().getDecorView().setSystemUiVisibility( |
| 287 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 288 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 289 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 290 | getWindow().setNavigationBarContrastEnforced(false); |
| 291 | getWindow().setNavigationBarColor(Color.TRANSPARENT); |
| 292 | |
Jim Miller | 3192148 | 2013-11-06 20:43:55 -0800 | [diff] [blame] | 293 | mClock = findViewById(R.id.clock); |
| 294 | |
| 295 | // Avoid screen burn in |
| 296 | mClock.post(mMoveTextRunnable); |
| 297 | } |
| 298 | } |
| 299 | } |