涉及到的语言:java
,C++/C
,xml
等。
涉及到的编译部分:makefile.mk
,android.bp
。
进程间通信 (IPC) 部分 :AIDL
+‘JNI’
+ HIDL
。
Demo 来自Google官方提供的ApiDemos。ConsumerIrService是红外相关的服务。
由于篇幅过长,本篇只会涉及到APP层
的调用和部分Frameworks层
。
应用层调用:
development/samples/ApiDemos/src/com/example/android/apis/hardware/ConsumerIr.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 通过Context获取系统服务
mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);
setContentView(R.layout.consumer_ir);
...
}
Frameworks层:
getSystemService()方法的定义在Context.java中,并在Context中定义了ConsumerIr的服务名称。
frameworks/base/core/java/android/content/Context.java
public abstract class Context {
...
public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name);
...
public static final String CONSUMER_IR_SERVICE = "consumer_ir";
...
}
可以看到是个抽象的方法,接下来我们看看他的实现。
frameworks/base/core/java/android/app/ContextImpl.java
class ContextImpl extends Context { {
...
@Override
public Object getSystemService(String name) {
...
return SystemServiceRegistry.getSystemService(this, name);
}
...
}
frameworks/base/core/java/android/app/SystemServiceRegistry.java
public static Object getSystemService(ContextImpl ctx, String name) {
if (name == null) {
return null;
}
// 获取提取器
final ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
if (fetcher == null) {
if (sEnableServiceNotFoundWtf) {
Slog.wtf(TAG, "Unknown manager requested: " + name);
}
return null;
}
// 获取我们目标服务
final Object ret = fetcher.getService(ctx);
if (sEnableServiceNotFoundWtf && ret == null) {
switch (name) {
case Context.CONTENT_CAPTURE_MANAGER_SERVICE:
case Context.APP_PREDICTION_SERVICE:
case Context.INCREMENTAL_SERVICE:
return null;
}
Slog.wtf(TAG, "Manager wrapper not available: " + name);
return null;
}
return ret;
}
在源码中我们能够看到SYSTEM_SERVICE_FETCHERS.get(name)
, 我们在应用层传递的服务名称到这里被使用。那我们看看 SYSTEM_SERVICE_FETCHERS
是什么。
@SystemApi
public final class SystemServiceRegistry {
...
private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
new ArrayMap<String, ServiceFetcher<?>>();
private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
new ArrayMap<Class<?>, String>();
private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
new ArrayMap<String, ServiceFetcher<?>>();
private static final Map<String, String> SYSTEM_SERVICE_CLASS_NAMES = new ArrayMap<>();
...
static {
... //在这里ConsumerIrManager 被注册,并被加入到全局缓存中。
registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
new CachedServiceFetcher<ConsumerIrManager>() {
@Override
public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
return new ConsumerIrManager(ctx);
}});
...
}
...
private static <T> void registerService(@NonNull String serviceName,
@NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
SYSTEM_SERVICE_CLASS_NAMES.put(serviceName, serviceClass.getSimpleName());
}
}
这样看来,SYSTEM_SERVICE_FETCHERS.get(name)
拿到的是 ServiceFetcher
的对象并 fetcher.getService(ctx)
拿到我们的目标服务。我们简单看一下。还是在SystemServiceRegistry.java
中。
@SystemApi
public final class SystemServiceRegistry {
...
static abstract interface ServiceFetcher<T> {
T getService(ContextImpl ctx);
}
...
static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
...
@Override
@SuppressWarnings("unchecked")
public final T getService(ContextImpl ctx) {
...
for (;;) {
boolean doInitialize = false;
synchronized (cache) {
...
// 这里有一些判断, 比如是否被建立。检查缓存中是否存在,可能在期间被清理了。是否需要重新开始。以及线程安全问题。
// 有兴趣的可以自己查阅。
...
}
if (doInitialize) {
T service = null;
@ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
try {
// 这里就是我们在注册期间所实现的方法。
service = createService(ctx);
newState = ContextImpl.STATE_READY;
} catch (ServiceNotFoundException e) {
onServiceNotFound(e);
} finally {
synchronized (cache) {
// 这里添加缓存,避免重复实例。
cache[mCacheIndex] = service;
gates[mCacheIndex] = newState;
cache.notifyAll();
}
}
ret = service;
break; // exit the for (;;)
}
... // 对其他等待的线程进行处理
}
return ret;
}
// 注册是, 我们需要实现的
public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
}
...
}
我们在应用层所拿到的ConsumerIrManager
就是这样被提供的。
接下来我们继续看,我们通过ConsumerIrManager
如何调用的,以及它的实现。
frameworks/base/core/java/android/hardware/ConsumerIrManager.java
// Manager类供应用调用
@SystemService(Context.CONSUMER_IR_SERVICE)
@RequiresFeature(PackageManager.FEATURE_CONSUMER_IR)
public final class ConsumerIrManager {
...
// 在构造函数我们能看到aidl用法
public ConsumerIrManager(Context context) throws ServiceNotFoundException {
mPackageName = context.getPackageName();
// 也就是这里,的转换操作。
mService = IConsumerIrService.Stub.asInterface(
ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE));
}
...
public boolean hasIrEmitter() {
...
return mService.hasIrEmitter();
...
}
...
public void transmit(int carrierFrequency, int[] pattern) {
...
mService.transmit(mPackageName, carrierFrequency, pattern);
...
}
...
public CarrierFrequencyRange[] getCarrierFrequencies() {
...
int[] freqs = mService.getCarrierFrequencies();
...
CarrierFrequencyRange[] range = new CarrierFrequencyRange[freqs.length / 2];
for (int i = 0; i < freqs.length; i += 2) {
range[i / 2] = new CarrierFrequencyRange(freqs[i], freqs[i+1]);
}
return range;
...
}
...
}
在ConsumerIrManager
提供的方法都是通过aidl 返还的实例调用的。hasIrEmitter
,transmit
,getCarrierFrequencies
都是在aidl
中所定义的。
接下来请移步至:通过ConsumerIrService看完整的系统服务调用流程(二)。来看Hal/JNI/驱动层的部分。
如有错误请指出,谢谢~