Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui; |
| 16 | |
| 17 | import android.os.RemoteException; |
| 18 | import android.util.Log; |
| 19 | import android.view.IDockedStackListener; |
| 20 | import android.view.WindowManagerGlobal; |
| 21 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 22 | import java.lang.ref.WeakReference; |
| 23 | import java.util.ArrayList; |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 24 | import java.util.function.Consumer; |
| 25 | |
| 26 | /** |
| 27 | * Utility wrapper to listen for whether or not a docked stack exists, to be |
| 28 | * used for things like the different overview icon in that mode. |
| 29 | */ |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 30 | public class DockedStackExistsListener { |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 31 | |
| 32 | private static final String TAG = "DockedStackExistsListener"; |
| 33 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 34 | private static ArrayList<WeakReference<Consumer<Boolean>>> sCallbacks = new ArrayList<>(); |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 35 | private static boolean mLastExists; |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 36 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 37 | static { |
| 38 | try { |
| 39 | WindowManagerGlobal.getWindowManagerService().registerDockedStackListener( |
| 40 | new IDockedStackListener.Stub() { |
| 41 | @Override |
| 42 | public void onDividerVisibilityChanged(boolean b) throws RemoteException { |
| 43 | |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void onDockedStackExistsChanged(boolean exists) |
| 48 | throws RemoteException { |
| 49 | DockedStackExistsListener.onDockedStackExistsChanged(exists); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void onDockedStackMinimizedChanged(boolean b, long l, boolean b1) |
| 54 | throws RemoteException { |
| 55 | |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onAdjustedForImeChanged(boolean b, long l) |
| 60 | throws RemoteException { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onDockSideChanged(int i) throws RemoteException { |
| 66 | |
| 67 | } |
| 68 | }); |
| 69 | } catch (RemoteException e) { |
| 70 | Log.e(TAG, "Failed registering docked stack exists listener", e); |
| 71 | } |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 74 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 75 | private static void onDockedStackExistsChanged(boolean exists) { |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 76 | mLastExists = exists; |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 77 | synchronized (sCallbacks) { |
Jason Monk | 32e3bb5 | 2017-07-27 12:35:41 -0400 | [diff] [blame] | 78 | sCallbacks.removeIf(wf -> { |
| 79 | Consumer<Boolean> l = wf.get(); |
| 80 | if (l != null) l.accept(exists); |
| 81 | return l == null; |
| 82 | }); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 83 | } |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public static void register(Consumer<Boolean> callback) { |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 87 | callback.accept(mLastExists); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 88 | synchronized (sCallbacks) { |
| 89 | sCallbacks.add(new WeakReference<>(callback)); |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |