Android 7.0模拟来电
有时为了测试的需要,在没SIM卡的机器上测试来电,需要一种一种模拟技术。这篇文章***模拟来电的实现***给出了实现的方式,但说的比较概要。本篇文章则根据自己的实践步骤整理而成,对于具体实现给出了参考。正如原文章所说,Android 5.0版本以前和以后(包括5.0)的实现方式是不同的。本文则是在Android 7.0上实践而来。
本文章是在lineageos 14.1的版本上测试的。其源码中已经包括了模拟来电的完整代码,我的工作就是编译其测试用的应用程序即可。 代码路径在:packages/services/Telecomm/testapps。
先把源码编译一遍,再用如下命令单独编译此app: ninja -f out/build-lineage_thea.ninja TelecomTestApps。
编译之后,生成TelecomTestApps.apk, 把它push 到 /system/app/目录中,重启机器,在应用程序列表中就可以看到TestConnectionService, Test Dialer, Test InCall UI三个图标。测试来电要用的就是TestConnectionService。
测试之前,先在设置-应用程序中找到TelecomTestApps, 在权限中打开Phone权限。
然后点击TestConnectionService, 在通知栏中先找到"Test Phone Accounts", 展开此通知,点击“REG. ACCT.” , 先注册一个PhoneAccount, 否则会有权限错误:This PhoneAccountHandle is not registered for this user!
最后展开"Test Connection Service"通知,点击“ADD CALL”, 来电就来了。主要是通知 TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
来实现来电的。
关于在Android 4.4上模拟来电,原文中也有介绍,我的理解如下:
if(simulated_phone == 0){
phone = new PhoneProxy(new GSMPhone(context,
ci, sPhoneNotifier, false, i), i);
} else {
CommandsInterface simulated_ci = new SimulatedCommands();
Rlog.i(LOG_TAG, "Creating Simulated GSMPhone " + i);
phone = new PhoneProxy(new GSMPhone(context,
simulated_ci, sPhoneNotifier, true, i), i);
}
Phone ph = PhoneFactory.getDefaultPhone(link_id);
SimulatedRadioControl mRadioControl = ph.getSimulatedRadioControl();
mRadioControl.triggerRing(phoneNumber);
主要代码如上,还未测试,后续更新结果.
====================
更新,有两个问题需要说明:
一、PhoneFactory.getDefaultPhone调用有异常java.lang.RuntimeException: PhoneFactory.getDefaultPhone must be called from Looper thread, 这个问题的原因是创建Phone的进程是com.android.phone,创建的代码在packages/service/telephony/src/com/android/phone/PhoneApp.java:
public void onCreate() {
mPhoneGlobals = new PhoneGlobals(PhoneApp.this);
mPhoneGlobals.onCreate();
…
}
当调用getDefaultPhone()时对比Looper对象,调用者与Phone有不一样就抛出此异常。所以必须调用者也设置运行在com.android.phone,具体可参考网上的文章PhoneFactory.getDefaultPhone must be called from Looper thread
二、经测试,在Android 4.4上成功模拟来电。但是有一个小问题,在来电界面上,不能显示来电手机号码。
对于此问题,跟踪了一下流程,大致过程如下:
DriverCall dc = null;
…
dc = (DriverCall) polledCalls.get(curDC);
mConnections[i] = new GsmConnection(mPhone.getContext(), dc, this, i);
…
// Number presentation
final int newNumberPresentation = connection.getNumberPresentation();
if (call.getNumberPresentation() != newNumberPresentation) {
call.setNumberPresentation(newNumberPresentation);
changed = true;
}
public void setNumberPresentation(int presentation) {public void setNumberPresentation(int presentation) {
mIdentification.setNumberPresentation(presentation);
}
public void init(Context context, Call call) {
final CallIdentification identification = call.getIdentification();
…
startContactInfoSearch(identification, true,
call.getState() == Call.State.INCOMING);
}
public static ContactCacheEntry buildCacheEntryFromCall(Context context,
CallIdentification identification, boolean isIncoming) {
final ContactCacheEntry entry = new ContactCacheEntry();
// TODO: get rid of caller info.
final CallerInfo info = CallerInfoUtils.buildCallerInfo(context, identification);
ContactInfoCache.populateCacheEntry(context, info, entry,
identification.getNumberPresentation(), isIncoming);
return entry;
}
…
} else if (presentation != Call.PRESENTATION_ALLOWED) {
// This case should never happen since the network should never send a phone #
// AND a restricted presentation. However we leave it here in case of weird
// network behavior
displayName = getPresentationString(context, presentation);
Log.d(TAG, " ==> presentation not allowed! displayName = " + displayName);
DriverCall
toDriverCall(int index) {
DriverCall ret;
ret = new DriverCall();
ret.index = index;
ret.isMT = mIsMT;
ret.numberPresentation = 1;
…
}