Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.systemui.pip.phone; |
| 18 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame^] | 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 22 | import android.app.IActivityManager; |
| 23 | import android.content.Context; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame^] | 24 | import android.content.pm.ParceledListSlice; |
| 25 | import android.os.Handler; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | import android.view.IPinnedStackController; |
| 29 | import android.view.IPinnedStackListener; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 30 | import android.view.IWindowManager; |
| 31 | import android.view.WindowManagerGlobal; |
| 32 | |
| 33 | /** |
| 34 | * Manages the picture-in-picture (PIP) UI and states for Phones. |
| 35 | */ |
| 36 | public class PipManager { |
| 37 | private static final String TAG = "PipManager"; |
| 38 | |
| 39 | private static PipManager sPipController; |
| 40 | |
| 41 | private Context mContext; |
| 42 | private IActivityManager mActivityManager; |
| 43 | private IWindowManager mWindowManager; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame^] | 44 | private Handler mHandler = new Handler(); |
| 45 | |
| 46 | private final PinnedStackListener mPinnedStackListener = new PinnedStackListener(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 47 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 48 | private PipMenuActivityController mMenuController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 49 | private PipTouchHandler mTouchHandler; |
| 50 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame^] | 51 | /** |
| 52 | * Handler for messages from the PIP controller. |
| 53 | */ |
| 54 | private class PinnedStackListener extends IPinnedStackListener.Stub { |
| 55 | |
| 56 | @Override |
| 57 | public void onListenerRegistered(IPinnedStackController controller) { |
| 58 | mHandler.post(() -> { |
| 59 | mTouchHandler.setPinnedStackController(controller); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onBoundsChanged(boolean adjustedForIme) { |
| 65 | // Do nothing |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void onActionsChanged(ParceledListSlice actions) { |
| 70 | mHandler.post(() -> { |
| 71 | mMenuController.setActions(actions); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onMinimizedStateChanged(boolean isMinimized) { |
| 77 | mHandler.post(() -> { |
| 78 | mTouchHandler.onMinimizedStateChanged(isMinimized); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onSnapToEdgeStateChanged(boolean isSnapToEdge) { |
| 84 | mHandler.post(() -> { |
| 85 | mTouchHandler.onSnapToEdgeStateChanged(isSnapToEdge); |
| 86 | }); |
| 87 | } |
| 88 | } |
| 89 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 90 | private PipManager() {} |
| 91 | |
| 92 | /** |
| 93 | * Initializes {@link PipManager}. |
| 94 | */ |
| 95 | public void initialize(Context context) { |
| 96 | mContext = context; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 97 | mActivityManager = ActivityManager.getService(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 98 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
| 99 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame^] | 100 | try { |
| 101 | mWindowManager.registerPinnedStackListener(DEFAULT_DISPLAY, mPinnedStackListener); |
| 102 | } catch (RemoteException e) { |
| 103 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 104 | } |
| 105 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 106 | mMenuController = new PipMenuActivityController(context, mActivityManager, mWindowManager); |
| 107 | mTouchHandler = new PipTouchHandler(context, mMenuController, mActivityManager, |
| 108 | mWindowManager); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Updates the PIP per configuration changed. |
| 113 | */ |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 114 | public void onConfigurationChanged() { |
| 115 | mTouchHandler.onConfigurationChanged(); |
| 116 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Gets an instance of {@link PipManager}. |
| 120 | */ |
| 121 | public static PipManager getInstance() { |
| 122 | if (sPipController == null) { |
| 123 | sPipController = new PipManager(); |
| 124 | } |
| 125 | return sPipController; |
| 126 | } |
| 127 | } |