Mady Mellor | c3d6f7d | 2018-11-07 09:36:56 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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.bubbles; |
| 18 | |
| 19 | import static android.view.View.GONE; |
| 20 | import static android.view.View.VISIBLE; |
| 21 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 22 | |
| 23 | import static com.android.systemui.bubbles.BubbleMovementHelper.EDGE_OVERLAP; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.graphics.Point; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.view.WindowManager; |
| 29 | import android.widget.FrameLayout; |
| 30 | |
| 31 | import com.android.systemui.Dependency; |
| 32 | import com.android.systemui.R; |
| 33 | import com.android.systemui.statusbar.notification.NotificationData; |
| 34 | import com.android.systemui.statusbar.phone.StatusBarWindowController; |
| 35 | |
| 36 | import java.util.HashMap; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | /** |
| 40 | * Bubbles are a special type of content that can "float" on top of other apps or System UI. |
| 41 | * Bubbles can be expanded to show more content. |
| 42 | * |
| 43 | * The controller manages addition, removal, and visible state of bubbles on screen. |
| 44 | */ |
| 45 | public class BubbleController { |
| 46 | private static final int MAX_BUBBLES = 5; // TODO: actually enforce this |
| 47 | |
| 48 | private static final String TAG = "BubbleController"; |
| 49 | |
| 50 | private Context mContext; |
| 51 | |
| 52 | private Map<String, BubbleView> mBubbles = new HashMap<>(); |
| 53 | private BubbleStackView mStackView; |
| 54 | private Point mDisplaySize; |
| 55 | |
| 56 | // Bubbles get added to the status bar view |
| 57 | private StatusBarWindowController mStatusBarWindowController; |
| 58 | |
| 59 | public BubbleController(Context context) { |
| 60 | mContext = context; |
| 61 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 62 | mDisplaySize = new Point(); |
| 63 | wm.getDefaultDisplay().getSize(mDisplaySize); |
| 64 | mStatusBarWindowController = Dependency.get(StatusBarWindowController.class); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Whether or not there are bubbles present, regardless of them being visible on the |
| 69 | * screen (e.g. if on AOD). |
| 70 | */ |
| 71 | public boolean hasBubbles() { |
| 72 | return mBubbles.size() > 0; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Whether the stack of bubbles is expanded or not. |
| 77 | */ |
| 78 | public boolean isStackExpanded() { |
| 79 | return mStackView != null && mStackView.isExpanded(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tell the stack of bubbles to collapse. |
| 84 | */ |
| 85 | public void collapseStack() { |
| 86 | if (mStackView != null) { |
| 87 | mStackView.animateExpansion(false); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tell the stack of bubbles to be dismissed, this will remove all of the bubbles in the stack. |
| 93 | */ |
| 94 | public void dismissStack() { |
| 95 | mStackView.setVisibility(GONE); |
| 96 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 97 | // Reset the position of the stack (TODO - or should we save / respect last user position?) |
| 98 | mStackView.setPosition(startPoint.x, startPoint.y); |
| 99 | for (String key: mBubbles.keySet()) { |
| 100 | removeBubble(key); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Adds a bubble associated with the provided notification entry or updates it if it exists. |
| 106 | */ |
| 107 | public void addBubble(NotificationData.Entry notif) { |
| 108 | if (mBubbles.containsKey(notif.key)) { |
| 109 | // It's an update |
| 110 | BubbleView bubble = mBubbles.get(notif.key); |
| 111 | mStackView.updateBubble(bubble, notif); |
| 112 | } else { |
| 113 | // It's new |
| 114 | BubbleView bubble = new BubbleView(mContext); |
| 115 | bubble.setNotif(notif); |
| 116 | mBubbles.put(bubble.getKey(), bubble); |
| 117 | |
| 118 | boolean setPosition = false; |
| 119 | if (mStackView == null) { |
| 120 | setPosition = true; |
| 121 | mStackView = new BubbleStackView(mContext); |
| 122 | ViewGroup sbv = (ViewGroup) mStatusBarWindowController.getStatusBarView(); |
| 123 | // XXX: Bug when you expand the shade on top of expanded bubble, there is no scrim |
| 124 | // between bubble and the shade |
| 125 | int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1; |
| 126 | sbv.addView(mStackView, bubblePosition, |
| 127 | new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 128 | } |
| 129 | mStackView.setVisibility(VISIBLE); |
| 130 | mStackView.addBubble(bubble); |
| 131 | |
| 132 | if (setPosition) { |
| 133 | // Need to add the bubble to the stack before we can know the width |
| 134 | Point startPoint = getStartPoint(mStackView.getStackWidth(), mDisplaySize); |
| 135 | mStackView.setPosition(startPoint.x, startPoint.y); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Removes the bubble associated with the {@param uri}. |
| 142 | */ |
| 143 | public void removeBubble(String key) { |
| 144 | BubbleView bv = mBubbles.get(key); |
| 145 | if (bv != null) { |
| 146 | mStackView.removeBubble(bv); |
| 147 | bv.getEntry().setBubbleDismissed(true); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TODO: factor in PIP location / maybe last place user had it |
| 152 | /** |
| 153 | * Gets an appropriate starting point to position the bubble stack. |
| 154 | */ |
| 155 | public static Point getStartPoint(int size, Point displaySize) { |
| 156 | final int x = displaySize.x - size + EDGE_OVERLAP; |
| 157 | final int y = displaySize.y / 4; |
| 158 | return new Point(x, y); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Gets an appropriate position for the bubble when the stack is expanded. |
| 163 | */ |
| 164 | public static Point getExpandPoint(BubbleStackView view, int size, Point displaySize) { |
| 165 | // Same place for now.. |
| 166 | return new Point(EDGE_OVERLAP, size); |
| 167 | } |
| 168 | |
| 169 | } |