Home | History | Annotate | Download | only in pm
      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.car.CarApiUtil;
     22 import android.car.CarManagerBase;
     23 import android.car.CarNotConnectedException;
     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 class CarPackageManager implements CarManagerBase {
     36 
     37     /**
     38      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
     39      * flag is set, the call will be blocked until policy is set to system. This can take time
     40      * and the flag cannot be used in main thread.
     41      * @hide
     42      */
     43     @SystemApi
     44     public static final int FLAG_SET_POLICY_WAIT_FOR_CHANGE = 0x1;
     45     /**
     46      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
     47      * flag is set, passed policy is added to existing policy set from the current package.
     48      * If none of {@link #FLAG_SET_POLICY_ADD} or {@link #FLAG_SET_POLICY_REMOVE} is set, existing
     49      * policy is replaced. Note that policy per each package is always replaced and will not be
     50      * added.
     51      * @hide
     52      */
     53     @SystemApi
     54     public static final int FLAG_SET_POLICY_ADD = 0x2;
     55     /**
     56      * Flag for {@link #setAppBlockingPolicy(String, CarAppBlockingPolicy, int)}. When this
     57      * flag is set, passed policy is removed from existing policy set from the current package.
     58      * If none of {@link #FLAG_SET_POLICY_ADD} or {@link #FLAG_SET_POLICY_REMOVE} is set, existing
     59      * policy is replaced.
     60      * @hide
     61      */
     62     @SystemApi
     63     public static final int FLAG_SET_POLICY_REMOVE = 0x4;
     64 
     65     /** @hide */
     66     @IntDef(flag = true,
     67             value = {FLAG_SET_POLICY_WAIT_FOR_CHANGE, FLAG_SET_POLICY_ADD, FLAG_SET_POLICY_REMOVE})
     68     @Retention(RetentionPolicy.SOURCE)
     69     public @interface SetPolicyFlags {}
     70 
     71     private final ICarPackageManager mService;
     72     private final Context mContext;
     73 
     74     /** @hide */
     75     public CarPackageManager(IBinder service, Context context) {
     76         mService = ICarPackageManager.Stub.asInterface(service);
     77         mContext = context;
     78     }
     79 
     80     /** @hide */
     81     @Override
     82     public void onCarDisconnected() {
     83         // nothing to do
     84     }
     85 
     86     /**
     87      * Set Application blocking policy for system app. {@link #FLAG_SET_POLICY_ADD} or
     88      * {@link #FLAG_SET_POLICY_REMOVE} flag allows adding or removing from already set policy. When
     89      * none of these flags are set, it will completely replace existing policy for each package
     90      * specified.
     91      * When {@link #FLAG_SET_POLICY_WAIT_FOR_CHANGE} flag is set, this call will be blocked
     92      * until the policy is set to system and become effective. Otherwise, the call will start
     93      * changing the policy but it will be completed asynchronously and the call will return
     94      * without waiting for system level policy change.
     95      *
     96      * @param packageName Package name of the client. If wrong package name is passed, exception
     97      *        will be thrown. This name is used to update the policy.
     98      * @param policy
     99      * @param flags
    100      * @throws SecurityException if caller has no permission.
    101      * @throws IllegalArgumentException For wrong or invalid arguments.
    102      * @throws IllegalStateException If {@link #FLAG_SET_POLICY_WAIT_FOR_CHANGE} is set while
    103      *         called from main thread.
    104      * @hide
    105      */
    106     @SystemApi
    107     public void setAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy,
    108             @SetPolicyFlags int flags) throws CarNotConnectedException, SecurityException,
    109             IllegalArgumentException {
    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 (IllegalStateException e) {
    118             CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
    119         } catch (RemoteException e) {
    120             //ignore as CarApi will handle disconnection anyway.
    121         }
    122     }
    123 
    124     /**
    125      * Check if given activity is allowed while driving.
    126      * @param packageName
    127      * @param className
    128      * @return
    129      */
    130     public boolean isActivityAllowedWhileDriving(String packageName, String className)
    131             throws CarNotConnectedException{
    132         try {
    133             return mService.isActivityAllowedWhileDriving(packageName, className);
    134         } catch (IllegalStateException e) {
    135             CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
    136         } catch (RemoteException e) {
    137             //ignore as CarApi will handle disconnection anyway.
    138         }
    139         return false;
    140     }
    141 
    142     /**
    143      * Check if given service is allowed while driving.
    144      * @param packageName
    145      * @param className
    146      * @return
    147      */
    148     public boolean isServiceAllowedWhileDriving(String packageName, String className)
    149             throws CarNotConnectedException {
    150         try {
    151             return mService.isServiceAllowedWhileDriving(packageName, className);
    152         } catch (IllegalStateException e) {
    153             CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
    154         } catch (RemoteException e) {
    155             //ignore as CarApi will handle disconnection anyway.
    156         }
    157         return false;
    158     }
    159 }
    160