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