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 package com.android.car.pm; 17 18 import android.car.builtin.content.pm.PackageManagerHelper; 19 import android.car.builtin.util.Slogf; 20 import android.car.content.pm.CarAppBlockingPolicy; 21 import android.car.content.pm.ICarAppBlockingPolicy; 22 import android.car.content.pm.ICarAppBlockingPolicySetter; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.ServiceConnection; 27 import android.content.pm.ServiceInfo; 28 import android.os.Handler; 29 import android.os.IBinder; 30 import android.os.RemoteException; 31 import android.os.UserHandle; 32 33 import com.android.car.CarLog; 34 import com.android.internal.annotations.GuardedBy; 35 36 public class AppBlockingPolicyProxy implements ServiceConnection { 37 38 private static final String TAG = CarLog.tagFor(AppBlockingPolicyProxy.class); 39 40 private final CarPackageManagerService mService; 41 private final Context mContext; 42 private final ServiceInfo mServiceInfo; 43 private final ICarAppBlockingPolicySetterImpl mSetter; 44 private final Object mLock = new Object(); 45 46 @GuardedBy("mLock") 47 private ICarAppBlockingPolicy mPolicyService; 48 49 /** 50 * policy not set within this time after binding will be treated as failure and will be 51 * ignored. 52 */ 53 private static final long TIMEOUT_MS = 5000; 54 private static final int MAX_CRASH_RETRY = 2; 55 @GuardedBy("mLock") 56 private int mCrashCount; 57 @GuardedBy("mLock") 58 private boolean mBound; 59 60 private final Handler mHandler; 61 private final Runnable mTimeoutRunnable = new Runnable() { 62 @Override 63 public void run() { 64 Slogf.w(TAG, "Timeout for policy setting for service:" + mServiceInfo); 65 disconnect(); 66 mService.onPolicyConnectionFailure(AppBlockingPolicyProxy.this); 67 } 68 }; 69 AppBlockingPolicyProxy(CarPackageManagerService service, Context context, ServiceInfo serviceInfo)70 public AppBlockingPolicyProxy(CarPackageManagerService service, Context context, 71 ServiceInfo serviceInfo) { 72 mService = service; 73 mContext = context; 74 mServiceInfo = serviceInfo; 75 mSetter = new ICarAppBlockingPolicySetterImpl(); 76 mHandler = new Handler(mService.getLooper()); 77 } 78 getPackageName()79 public String getPackageName() { 80 return mServiceInfo.packageName; 81 } 82 connect()83 public void connect() { 84 Intent intent = new Intent(); 85 intent.setComponent(PackageManagerHelper.getComponentName(mServiceInfo)); 86 mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, 87 UserHandle.CURRENT); 88 synchronized (mLock) { 89 mBound = true; 90 } 91 mHandler.postDelayed(mTimeoutRunnable, TIMEOUT_MS); 92 } 93 disconnect()94 public void disconnect() { 95 synchronized (mLock) { 96 if (!mBound) { 97 return; 98 } 99 mBound = false; 100 mPolicyService = null; 101 } 102 mHandler.removeCallbacks(mTimeoutRunnable); 103 try { 104 mContext.unbindService(this); 105 } catch (IllegalArgumentException e) { 106 Slogf.w(TAG, "unbind", e); 107 } 108 } 109 110 @Override onServiceConnected(ComponentName name, IBinder service)111 public void onServiceConnected(ComponentName name, IBinder service) { 112 ICarAppBlockingPolicy policy = null; 113 boolean failed = false; 114 synchronized (mLock) { 115 mPolicyService = ICarAppBlockingPolicy.Stub.asInterface(service); 116 policy = mPolicyService; 117 if (policy == null) { 118 failed = true; 119 } 120 } 121 if (failed) { 122 Slogf.w(TAG, "Policy service connected with null binder:" + name); 123 mService.onPolicyConnectionFailure(this); 124 return; 125 } 126 try { 127 policy.setAppBlockingPolicySetter(mSetter); 128 } catch (RemoteException e) { 129 // let retry handle this 130 } 131 } 132 133 @Override onServiceDisconnected(ComponentName name)134 public void onServiceDisconnected(ComponentName name) { 135 boolean failed = false; 136 synchronized (mLock) { 137 mCrashCount++; 138 if (mCrashCount > MAX_CRASH_RETRY) { 139 mPolicyService = null; 140 failed = true; 141 } 142 } 143 if (failed) { 144 Slogf.w(TAG, "Policy service keep crashing, giving up:" + name); 145 mService.onPolicyConnectionFailure(this); 146 } 147 } 148 149 @Override toString()150 public String toString() { 151 synchronized (mLock) { 152 return "AppBlockingPolicyProxy [mServiceInfo=" + mServiceInfo + ", mCrashCount=" 153 + mCrashCount + "]"; 154 } 155 } 156 157 private class ICarAppBlockingPolicySetterImpl extends ICarAppBlockingPolicySetter.Stub { 158 159 @Override setAppBlockingPolicy(CarAppBlockingPolicy policy)160 public void setAppBlockingPolicy(CarAppBlockingPolicy policy) { 161 mHandler.removeCallbacks(mTimeoutRunnable); 162 if (policy == null) { 163 Slogf.w(TAG, "setAppBlockingPolicy null policy from policy service:" 164 + mServiceInfo); 165 } 166 mService.onPolicyConnectionAndSet(AppBlockingPolicyProxy.this, policy); 167 } 168 } 169 } 170