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.experimentalcar; 18 19 import android.annotation.MainThread; 20 import android.annotation.Nullable; 21 import android.car.IExperimentalCar; 22 import android.car.IExperimentalCarHelper; 23 import android.car.experimental.CarDriverDistractionManager; 24 import android.car.experimental.CarTestDemoExperimentalFeatureManager; 25 import android.car.experimental.ExperimentalCar; 26 import android.content.Context; 27 import android.content.pm.PackageManager; 28 import android.os.Binder; 29 import android.os.Handler; 30 import android.os.IBinder; 31 import android.os.Looper; 32 import android.os.Process; 33 import android.os.RemoteException; 34 import android.util.Log; 35 36 import com.android.car.CarServiceBase; 37 import com.android.car.internal.util.IndentingPrintWriter; 38 import com.android.internal.annotations.GuardedBy; 39 40 import java.io.FileDescriptor; 41 import java.io.PrintWriter; 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.List; 45 46 /** 47 * Implements IExperimentalCar for experimental features. 48 */ 49 public final class IExperimentalCarImpl extends IExperimentalCar.Stub { 50 51 private static final String TAG = "CAR.EXPIMPL"; 52 53 private static final List<String> ALL_AVAILABLE_FEATURES = Arrays.asList( 54 ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE, 55 ExperimentalCar.DRIVER_DISTRACTION_EXPERIMENTAL_FEATURE_SERVICE 56 ); 57 58 private final Context mContext; 59 60 private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); 61 62 private final Object mLock = new Object(); 63 64 @GuardedBy("mLock") 65 private boolean mReleased; 66 67 @GuardedBy("mLock") 68 private IExperimentalCarHelper mHelper; 69 70 @GuardedBy("mLock") 71 private ArrayList<CarServiceBase> mRunningServices = new ArrayList<>(); 72 IExperimentalCarImpl(Context context)73 public IExperimentalCarImpl(Context context) { 74 mContext = context; 75 } 76 77 @Override init(IExperimentalCarHelper helper, List<String> enabledFeatures)78 public void init(IExperimentalCarHelper helper, List<String> enabledFeatures) { 79 // From car service or unit testing only 80 assertCallingFromSystemProcessOrSelf(); 81 82 // dispatch to main thread as release is always done in main. 83 mMainThreadHandler.post(() -> { 84 synchronized (mLock) { 85 if (mReleased) { 86 Log.w(TAG, "init binder call after onDestroy, will ignore"); 87 return; 88 } 89 } 90 ArrayList<CarServiceBase> services = new ArrayList<>(); 91 ArrayList<String> startedFeatures = new ArrayList<>(); 92 ArrayList<String> classNames = new ArrayList<>(); 93 ArrayList<IBinder> binders = new ArrayList<>(); 94 95 // This cannot address inter-dependency. That requires re-ordering this in dependency 96 // order. 97 // That should be done when we find such needs. For now, each feature inside here should 98 // not have inter-dependency as they are all optional. 99 for (String feature : enabledFeatures) { 100 CarServiceBase service = constructServiceForFeature(feature); 101 if (service == null) { 102 Log.e(TAG, "Failed to construct requested feature:" + feature); 103 continue; 104 } 105 service.init(); 106 services.add(service); 107 startedFeatures.add(feature); 108 // If it is not IBinder, then it is internal feature. 109 if (service instanceof IBinder) { 110 binders.add((IBinder) service); 111 } else { 112 binders.add(null); 113 } 114 classNames.add(getClassNameForFeature(feature)); 115 } 116 try { 117 helper.onInitComplete(ALL_AVAILABLE_FEATURES, startedFeatures, classNames, binders); 118 } catch (RemoteException e) { 119 Log.w(TAG, "Car service crashed?", e); 120 // will be destroyed soon. Just continue and register services for possible cleanup. 121 } 122 synchronized (mLock) { 123 mHelper = helper; 124 mRunningServices.addAll(services); 125 } 126 }); 127 } 128 129 // should be called in Service.onDestroy 130 @MainThread release()131 void release() { 132 // Copy to handle call release without lock 133 ArrayList<CarServiceBase> services; 134 synchronized (mLock) { 135 if (mReleased) { 136 return; 137 } 138 mReleased = true; 139 services = new ArrayList<>(mRunningServices); 140 mRunningServices.clear(); 141 } 142 for (CarServiceBase service : services) { 143 service.release(); 144 } 145 } 146 147 /** dump */ dump(FileDescriptor fd, PrintWriter writer, String[] args)148 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 149 try (IndentingPrintWriter pw = new IndentingPrintWriter(writer)) { 150 ArrayList<CarServiceBase> services; 151 synchronized (mLock) { 152 pw.printf("mReleased: %b\n", mReleased); 153 pw.printf("ALL_AVAILABLE_FEATURES: %s\n", ALL_AVAILABLE_FEATURES); 154 services = new ArrayList<>(mRunningServices); 155 } 156 pw.printf(" Number of running services: %d\n", services.size()); 157 int i = 0; 158 159 for (CarServiceBase service : services) { 160 writer.printf("%d:\n", i); 161 pw.increaseIndent(); 162 service.dump(pw); 163 pw.decreaseIndent(); 164 i++; 165 } 166 } 167 } 168 169 @Nullable getClassNameForFeature(String featureName)170 private String getClassNameForFeature(String featureName) { 171 switch (featureName) { 172 case ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE: 173 return CarTestDemoExperimentalFeatureManager.class.getName(); 174 case ExperimentalCar.DRIVER_DISTRACTION_EXPERIMENTAL_FEATURE_SERVICE: 175 return CarDriverDistractionManager.class.getName(); 176 default: 177 return null; 178 } 179 } 180 181 @Nullable constructServiceForFeature(String featureName)182 private CarServiceBase constructServiceForFeature(String featureName) { 183 switch (featureName) { 184 case ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE: 185 return new TestDemoExperimentalFeatureService(); 186 case ExperimentalCar.DRIVER_DISTRACTION_EXPERIMENTAL_FEATURE_SERVICE: 187 return new DriverDistractionExperimentalFeatureService(mContext); 188 default: 189 return null; 190 } 191 } 192 assertCallingFromSystemProcessOrSelf()193 private static void assertCallingFromSystemProcessOrSelf() { 194 int uid = Binder.getCallingUid(); 195 int pid = Binder.getCallingPid(); 196 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) { 197 throw new SecurityException("Only allowed from system or self, uid:" + uid 198 + " pid:" + pid); 199 } 200 } 201 202 /** 203 * Assert that a permission has been granted for the current context. 204 */ assertPermission(Context context, String permission)205 public static void assertPermission(Context context, String permission) { 206 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { 207 throw new SecurityException("requires " + permission); 208 } 209 } 210 } 211