• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.server.telecom;
18 
19 import android.annotation.IntRange;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.app.UiModeManager;
23 import android.telecom.Log;
24 import android.util.LocalLog;
25 
26 import com.android.internal.util.IndentingPrintWriter;
27 
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.Optional;
32 import java.util.PriorityQueue;
33 import java.util.function.Function;
34 import java.util.stream.Collectors;
35 
36 /**
37  * Tracks the package names of apps which enter end exit car mode.
38  */
39 public class CarModeTracker {
40     /**
41      * Data class holding information about apps which have requested to enter car mode.
42      */
43     private class CarModeApp {
44         private final boolean mAutomotiveProjection;
45         private final @IntRange(from = 0) int mPriority;
46         private @NonNull String mPackageName;
47 
CarModeApp(@onNull String packageName)48         public CarModeApp(@NonNull String packageName) {
49             this(true, 0, packageName);
50         }
51 
CarModeApp(int priority, @NonNull String packageName)52         public CarModeApp(int priority, @NonNull String packageName) {
53             this(false, priority, packageName);
54         }
55 
CarModeApp(boolean automotiveProjection, int priority, @NonNull String packageName)56         private CarModeApp(boolean automotiveProjection, int priority, @NonNull String packageName) {
57             mAutomotiveProjection = automotiveProjection;
58             mPriority = priority;
59             mPackageName = Objects.requireNonNull(packageName);
60         }
61 
hasSetAutomotiveProjection()62         public boolean hasSetAutomotiveProjection() {
63             return mAutomotiveProjection;
64         }
65 
66         /**
67          * The priority at which the app requested to enter car mode.
68          * Will be the same as the one specified when {@link UiModeManager#enableCarMode(int, int)}
69          * was called, or {@link UiModeManager#DEFAULT_PRIORITY} if no priority was specified.
70          * @return The priority.
71          */
getPriority()72         public int getPriority() {
73             return mPriority;
74         }
75 
76         /**
77          * @return The package name of the app which requested to enter car mode/set projection.
78          */
getPackageName()79         public String getPackageName() {
80             return mPackageName;
81         }
82 
setPackageName(String packageName)83         public void setPackageName(String packageName) {
84             mPackageName = packageName;
85         }
86 
toString()87         public String toString() {
88             return String.format("[%s, %s]",
89                     mAutomotiveProjection ? "PROJECTION SET" : mPriority,
90                     mPackageName);
91         }
92     }
93 
94     /**
95      * Priority list of apps which have entered or exited car mode, ordered first by whether the app
96      * has set automotive projection, and then by highest priority.  Where items have the same
97      * priority, order is arbitrary, but we only allow one item in the queue per priority.
98      */
99     private PriorityQueue<CarModeApp> mCarModeApps = new PriorityQueue<>(2,
100             // Natural ordering of booleans is False, True. Natural ordering of ints is increasing.
101             Comparator.comparing(CarModeApp::hasSetAutomotiveProjection)
102                     .thenComparing(CarModeApp::getPriority)
103                     .reversed());
104 
105     private final LocalLog mCarModeChangeLog = new LocalLog(20);
106 
107     /**
108      * Handles a request to enter car mode by a package name.
109      * @param priority The priority at which car mode is entered.
110      * @param packageName The package name of the app entering car mode.
111      */
handleEnterCarMode(@ntRangefrom = 0) int priority, @NonNull String packageName)112     public void handleEnterCarMode(@IntRange(from = 0) int priority, @NonNull String packageName) {
113         if (mCarModeApps.stream().anyMatch(c -> c.getPriority() == priority)) {
114             Log.w(this, "handleEnterCarMode: already in car mode at priority %d (apps: %s)",
115                     priority, getCarModePriorityString());
116             return;
117         }
118 
119         if (mCarModeApps.stream().anyMatch(c -> c.getPackageName().equals(packageName))) {
120             Log.w(this, "handleEnterCarMode: %s is already in car mode (apps: %s)",
121                     packageName, getCarModePriorityString());
122             return;
123         }
124 
125         Log.i(this, "handleEnterCarMode: packageName=%s, priority=%d", packageName, priority);
126         mCarModeChangeLog.log("enterCarMode: packageName=" + packageName + ", priority="
127                 + priority);
128         mCarModeApps.add(new CarModeApp(priority, packageName));
129     }
130 
131     /**
132      * Handles a request to exist car mode at a priority level.
133      * @param priority The priority level.
134      * @param packageName The packagename of the app requesting the change.
135      */
handleExitCarMode(@ntRangefrom = 0) int priority, @NonNull String packageName)136     public void handleExitCarMode(@IntRange(from = 0) int priority, @NonNull String packageName) {
137         if (!mCarModeApps.stream().anyMatch(c -> c.getPriority() == priority)) {
138             Log.w(this, "handleExitCarMode: not in car mode at priority %d (apps=%s)",
139                     priority, getCarModePriorityString());
140             return;
141         }
142 
143         if (priority != UiModeManager.DEFAULT_PRIORITY && !mCarModeApps.stream().anyMatch(
144                 c -> c.getPackageName().equals(packageName) && c.getPriority() == priority)) {
145             Log.w(this, "handleExitCarMode: %s didn't enter car mode at priority %d (apps=%s)",
146                     packageName, priority, getCarModePriorityString());
147             return;
148         }
149 
150         Log.i(this, "handleExitCarMode: packageName=%s, priority=%d", packageName, priority);
151         mCarModeChangeLog.log("exitCarMode: packageName=" + packageName + ", priority="
152                 + priority);
153         mCarModeApps.removeIf(c -> c.getPriority() == priority);
154     }
155 
handleSetAutomotiveProjection(@onNull String packageName)156     public void handleSetAutomotiveProjection(@NonNull String packageName) {
157         Optional<CarModeApp> projectingApp = mCarModeApps.stream()
158                 .filter(CarModeApp::hasSetAutomotiveProjection)
159                 .findAny();
160         // No app with automotive projection? Easy peasy, just add it.
161         if (!projectingApp.isPresent()) {
162             Log.i(this, "handleSetAutomotiveProjection: %s", packageName);
163             mCarModeChangeLog.log("setAutomotiveProjection: packageName=" + packageName);
164             mCarModeApps.add(new CarModeApp(packageName));
165             return;
166         }
167         // Otherwise an app already has automotive projection set. Is it the same app?
168         if (packageName.equals(projectingApp.get().getPackageName())) {
169             Log.w(this, "handleSetAutomotiveProjection: %s already the automotive projection app",
170                     packageName);
171             return;
172         }
173         // We have a new app for automotive projection. As a shortcut just reuse the same object by
174         // overwriting the package name.
175         Log.i(this, "handleSetAutomotiveProjection: %s replacing %s as automotive projection app",
176                 packageName, projectingApp.get().getPackageName());
177         mCarModeChangeLog.log("setAutomotiveProjection: " + packageName + " replaces "
178                 + projectingApp.get().getPackageName());
179         projectingApp.get().setPackageName(packageName);
180     }
181 
handleReleaseAutomotiveProjection()182     public void handleReleaseAutomotiveProjection() {
183         Optional<String> projectingPackage = mCarModeApps.stream()
184                 .filter(CarModeApp::hasSetAutomotiveProjection)
185                 .map(CarModeApp::getPackageName)
186                 .findAny();
187         if (!projectingPackage.isPresent()) {
188             Log.w(this, "handleReleaseAutomotiveProjection: no current automotive projection app");
189             return;
190         }
191         Log.i(this, "handleReleaseAutomotiveProjection: %s", projectingPackage.get());
192         mCarModeChangeLog.log("releaseAutomotiveProjection: packageName="
193                 + projectingPackage.get());
194         mCarModeApps.removeIf(CarModeApp::hasSetAutomotiveProjection);
195     }
196 
197     /**
198      * Force-removes a package from the car mode tracking list, no matter at which priority.
199      *
200      * This handles the case where packages are disabled or uninstalled. In those case, remove them
201      * from the tracking list so they don't cause a leak.
202      * @param packageName Package name of the app to force-remove
203      */
forceRemove(@onNull String packageName)204     public void forceRemove(@NonNull String packageName) {
205         // We must account for the possibility that the app has set both car mode AND projection.
206         List<CarModeApp> forcedApp = mCarModeApps.stream()
207                 .filter(c -> c.getPackageName().equals(packageName))
208                 .collect(Collectors.toList());
209         if (forcedApp.isEmpty()) {
210             Log.i(this, "Package %s is not tracked.", packageName);
211             return;
212         }
213         for (CarModeApp app : forcedApp) {
214             String logString = "forceRemove: " + app;
215             Log.i(this, logString);
216             mCarModeChangeLog.log(logString);
217         }
218         mCarModeApps.removeIf(c -> c.getPackageName().equals(packageName));
219     }
220 
221     /**
222      * Retrieves a list of the apps which are currently in car mode, ordered by priority such that
223      * the highest priority app is first.
224      * @return List of apps in car mode.
225      */
getCarModeApps()226     public @NonNull List<String> getCarModeApps() {
227         return mCarModeApps
228                 .stream()
229                 .sorted(mCarModeApps.comparator())
230                 .map(CarModeApp::getPackageName)
231                 .collect(Collectors.toList());
232     }
233 
getCarModePriorityString()234     private @NonNull String getCarModePriorityString() {
235         return mCarModeApps
236                 .stream()
237                 .sorted(mCarModeApps.comparator())
238                 .map(CarModeApp::toString)
239                 .collect(Collectors.joining(", "));
240     }
241 
242     /**
243      * Gets the app which is currently in car mode.  This is the highest priority app which has
244      * entered car mode.
245      * @return The app which is in car mode.
246      */
getCurrentCarModePackage()247     public @Nullable String getCurrentCarModePackage() {
248         CarModeApp app = mCarModeApps.peek();
249         return app == null ? null : app.getPackageName();
250     }
251 
252     /**
253      * @return {@code true} if the device is in car mode, {@code false} otherwise.
254      */
isInCarMode()255     public boolean isInCarMode() {
256         return !mCarModeApps.isEmpty();
257     }
258 
259     /**
260      * Dumps the state of the car mode tracker to the specified print writer.
261      * @param pw
262      */
dump(IndentingPrintWriter pw)263     public void dump(IndentingPrintWriter pw) {
264         pw.println("CarModeTracker:");
265         pw.increaseIndent();
266 
267         pw.println("Current car mode apps:");
268         pw.increaseIndent();
269         for (CarModeApp app : mCarModeApps) {
270             pw.print("[");
271             pw.print(app.hasSetAutomotiveProjection() ? "PROJECTION SET" : app.getPriority());
272             pw.print("] ");
273             pw.println(app.getPackageName());
274         }
275         pw.decreaseIndent();
276 
277         pw.println("Car mode history:");
278         pw.increaseIndent();
279         mCarModeChangeLog.dump(pw);
280         pw.decreaseIndent();
281 
282         pw.decreaseIndent();
283     }
284 }
285