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 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 17 | import android.os.IBinder; |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 18 | import android.os.RemoteException; |
| 19 | import android.util.Log; |
| 20 | import android.view.IDockedStackListener; |
| 21 | import android.view.WindowManagerGlobal; |
| 22 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 23 | import java.lang.ref.WeakReference; |
| 24 | import java.util.ArrayList; |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 25 | import java.util.function.Consumer; |
| 26 | |
| 27 | /** |
| 28 | * Utility wrapper to listen for whether or not a docked stack exists, to be |
| 29 | * used for things like the different overview icon in that mode. |
| 30 | */ |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 31 | public class DockedStackExistsListener { |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 32 | |
| 33 | private static final String TAG = "DockedStackExistsListener"; |
| 34 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 35 | private static ArrayList<WeakReference<Consumer<Boolean>>> sCallbacks = new ArrayList<>(); |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 36 | private static boolean mLastExists; |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 37 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 38 | static { |
| 39 | try { |
| 40 | WindowManagerGlobal.getWindowManagerService().registerDockedStackListener( |
| 41 | new IDockedStackListener.Stub() { |
| 42 | @Override |
| 43 | public void onDividerVisibilityChanged(boolean b) throws RemoteException { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void onDockedStackExistsChanged(boolean exists) |
| 49 | throws RemoteException { |
| 50 | DockedStackExistsListener.onDockedStackExistsChanged(exists); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onDockedStackMinimizedChanged(boolean b, long l, boolean b1) |
| 55 | throws RemoteException { |
| 56 | |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void onAdjustedForImeChanged(boolean b, long l) |
| 61 | throws RemoteException { |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void onDockSideChanged(int i) throws RemoteException { |
| 67 | |
| 68 | } |
| 69 | }); |
| 70 | } catch (RemoteException e) { |
| 71 | Log.e(TAG, "Failed registering docked stack exists listener", e); |
| 72 | } |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 75 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 76 | private static void onDockedStackExistsChanged(boolean exists) { |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 77 | mLastExists = exists; |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 78 | synchronized (sCallbacks) { |
Jason Monk | 32e3bb5 | 2017-07-27 12:35:41 -0400 | [diff] [blame] | 79 | sCallbacks.removeIf(wf -> { |
| 80 | Consumer<Boolean> l = wf.get(); |
| 81 | if (l != null) l.accept(exists); |
| 82 | return l == null; |
| 83 | }); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 84 | } |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public static void register(Consumer<Boolean> callback) { |
Jason Monk | aa74bf8 | 2017-08-15 11:34:33 -0400 | [diff] [blame] | 88 | callback.accept(mLastExists); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 89 | synchronized (sCallbacks) { |
| 90 | sCallbacks.add(new WeakReference<>(callback)); |
Jason Monk | f85fc96 | 2017-04-19 17:13:41 -0400 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |