• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 android.car.content.pm;
18 
19 import android.annotation.IntDef;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.car.CarManagerBase;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.os.IBinder;
26 import android.os.Looper;
27 import android.os.RemoteException;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * Provides car specific API related with package management.
34  */
35 public final class CarPackageManager implements CarManagerBase {
36     private static final String TAG = "CarPackageManager";
37 
38     /**
39      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
40      * flag is set, the call will be blocked until policy is set to system. This can take time
41      * and the flag cannot be used in main thread.
42      * @hide
43      */
44     @SystemApi
45     public static final int FLAG_SET_POLICY_WAIT_FOR_CHANGE = 0x1;
46     /**
47      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
48      * flag is set, passed policy is added to existing policy set from the current package.
49      * If none of {@link #FLAG_SET_POLICY_ADD} or {@link #FLAG_SET_POLICY_REMOVE} is set, existing
50      * policy is replaced. Note that policy per each package is always replaced and will not be
51      * added.
52      * @hide
53      */
54     @SystemApi
55     public static final int FLAG_SET_POLICY_ADD = 0x2;
56     /**
57      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
58      * flag is set, passed policy is removed from existing policy set from the current package.
59      * If none of {@link #FLAG_SET_POLICY_ADD} or {@link #FLAG_SET_POLICY_REMOVE} is set, existing
60      * policy is replaced.
61      * @hide
62      */
63     @SystemApi
64     public static final int FLAG_SET_POLICY_REMOVE = 0x4;
65 
66     /** @hide */
67     @IntDef(flag = true,
68             value = {FLAG_SET_POLICY_WAIT_FOR_CHANGE, FLAG_SET_POLICY_ADD, FLAG_SET_POLICY_REMOVE})
69     @Retention(RetentionPolicy.SOURCE)
70     public @interface SetPolicyFlags {}
71 
72     private final ICarPackageManager mService;
73     private final Context mContext;
74 
75     /** @hide */
CarPackageManager(IBinder service, Context context)76     public CarPackageManager(IBinder service, Context context) {
77         mService = ICarPackageManager.Stub.asInterface(service);
78         mContext = context;
79     }
80 
81     /** @hide */
82     @Override
onCarDisconnected()83     public void onCarDisconnected() {
84         // nothing to do
85     }
86 
87     /**
88      * Set Application blocking policy for system app. {@link #FLAG_SET_POLICY_ADD} or
89      * {@link #FLAG_SET_POLICY_REMOVE} flag allows adding or removing from already set policy. When
90      * none of these flags are set, it will completely replace existing policy for each package
91      * specified.
92      * When {@link #FLAG_SET_POLICY_WAIT_FOR_CHANGE} flag is set, this call will be blocked
93      * until the policy is set to system and become effective. Otherwise, the call will start
94      * changing the policy but it will be completed asynchronously and the call will return
95      * without waiting for system level policy change.
96      *
97      * @param packageName Package name of the client. If wrong package name is passed, exception
98      *        will be thrown. This name is used to update the policy.
99      * @param policy
100      * @param flags
101      * @throws SecurityException if caller has no permission.
102      * @throws IllegalArgumentException For wrong or invalid arguments.
103      * @throws IllegalStateException If {@link #FLAG_SET_POLICY_WAIT_FOR_CHANGE} is set while
104      *         called from main thread.
105      * @hide
106      */
107     @SystemApi
setAppBlockingPolicy( String packageName, CarAppBlockingPolicy policy, @SetPolicyFlags int flags)108     public void setAppBlockingPolicy(
109             String packageName, CarAppBlockingPolicy policy, @SetPolicyFlags int flags) {
110         if ((flags & FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0 &&
111                 Looper.getMainLooper().isCurrentThread()) {
112             throw new IllegalStateException(
113                     "FLAG_SET_POLICY_WAIT_FOR_CHANGE cannot be used in main thread");
114         }
115         try {
116             mService.setAppBlockingPolicy(packageName, policy, flags);
117         } catch (RemoteException e) {
118             throw e.rethrowFromSystemServer();
119         }
120     }
121 
122     /**
123      * Restarts the requested task. If task with {@code taskId} does not exist, do nothing.
124      *
125      * @hide
126      */
restartTask(int taskId)127     public void restartTask(int taskId) {
128         try {
129             mService.restartTask(taskId);
130         } catch (RemoteException e) {
131             throw e.rethrowFromSystemServer();
132         }
133     }
134 
135     /**
136      * Check if finishing Activity will lead into safe Activity (=allowed Activity) to be shown.
137      * This can be used by unsafe activity blocking Activity to check if finishing itself can
138      * lead into being launched again due to unsafe activity shown. Note that checking this does not
139      * guarantee that blocking will not be done as driving state can change after this call is made.
140      *
141      * @param activityName
142      * @return true if there is a safe Activity (or car is stopped) in the back of task stack
143      *         so that finishing the Activity will not trigger another Activity blocking. If
144      *         the given Activity is not in foreground, then it will return true as well as
145      *         finishing the Activity will not make any difference.
146      *
147      * @hide
148      */
149     @SystemApi
isActivityBackedBySafeActivity(ComponentName activityName)150     public boolean isActivityBackedBySafeActivity(ComponentName activityName) {
151         try {
152             return mService.isActivityBackedBySafeActivity(activityName);
153         } catch (RemoteException e) {
154             throw e.rethrowFromSystemServer();
155         }
156     }
157 
158     /**
159      * Enable/Disable Activity Blocking.  This is to provide an option for toggling app blocking
160      * behavior for development purposes.
161      * @hide
162      */
163     @TestApi
setEnableActivityBlocking(boolean enable)164     public void setEnableActivityBlocking(boolean enable) {
165         try {
166             mService.setEnableActivityBlocking(enable);
167         } catch (RemoteException e) {
168             throw e.rethrowFromSystemServer();
169         }
170     }
171 
172     /**
173      * Check if given activity is distraction optimized, i.e, allowed in a
174      * restricted driving state
175      *
176      * @param packageName
177      * @param className
178      * @return
179      */
isActivityDistractionOptimized(String packageName, String className)180     public boolean isActivityDistractionOptimized(String packageName, String className) {
181         try {
182             return mService.isActivityDistractionOptimized(packageName, className);
183         } catch (RemoteException e) {
184             throw e.rethrowFromSystemServer();
185         }
186     }
187 
188     /**
189      * Check if given service is distraction optimized, i.e, allowed in a restricted
190      * driving state.
191      *
192      * @param packageName
193      * @param className
194      * @return
195      */
isServiceDistractionOptimized(String packageName, String className)196     public boolean isServiceDistractionOptimized(String packageName, String className) {
197         try {
198             return mService.isServiceDistractionOptimized(packageName, className);
199         } catch (RemoteException e) {
200             throw e.rethrowFromSystemServer();
201         }
202     }
203 }
204