1 /*
2  * Copyright (C) 2019 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.car.ui.paintbooth.overlays;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.content.om.IOverlayManager;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 
25 import androidx.annotation.NonNull;
26 
27 import static java.util.stream.Collectors.toList;
28 import static java.util.stream.Collectors.toMap;
29 
30 import java.util.List;
31 import java.util.Map;
32 
33 /**
34  * OverlayManager implementation. Exclude this file when building Paintbooth outside of the system
35  * image.
36  */
37 public class OverlayManagerImpl implements OverlayManager {
38     private final IOverlayManager mOverlayManager;
39 
OverlayManagerImpl(Context context)40     public OverlayManagerImpl(Context context) {
41         mOverlayManager = IOverlayManager.Stub.asInterface(
42                 ServiceManager.getService(Context.OVERLAY_SERVICE));
43     }
44 
convertOverlayInfo(android.content.om.OverlayInfo info)45     private OverlayInfo convertOverlayInfo(android.content.om.OverlayInfo info) {
46         return new OverlayInfo() {
47             @NonNull
48             @Override
49             public String getPackageName() {
50                 return info.packageName;
51             }
52 
53             @Override
54             public boolean isEnabled() {
55                 return info.state == android.content.om.OverlayInfo.STATE_ENABLED;
56             }
57         };
58     }
59 
60     @Override
61     @NonNull
62     public Map<String, List<OverlayManager.OverlayInfo>> getOverlays() throws RemoteException {
63         Map<String, List<android.content.om.OverlayInfo>> overlays =
64                 mOverlayManager.getAllOverlays(ActivityManager.getCurrentUser());
65         return overlays.entrySet()
66                 .stream()
67                 .collect(toMap(Map.Entry::getKey, e -> e.getValue()
68                         .stream()
69                         .map(this::convertOverlayInfo)
70                         .collect(toList())));
71     }
72 
73     @Override
74     public void applyOverlay(@NonNull String packageName, boolean enable) throws RemoteException {
75         mOverlayManager.setEnabled(packageName, enable, ActivityManager.getCurrentUser());
76     }
77 }
78