Android 7.0模拟来电

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上模拟来电,原文中也有介绍,我的理解如下:

  1. 修改frameworks中的frameworks/opt/telephony/src/java/com/android/internal/telephony/PhoneFactory.java
    在创建一个GSMPhone对象的时候,创建一个虚拟的BaseCommands:

    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);
    }

  2. 写一个测试程序,调用如下接口,触发来电:

    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上成功模拟来电。但是有一个小问题,在来电界面上,不能显示来电手机号码。
对于此问题,跟踪了一下流程,大致过程如下:

  1. frameworks/opt/telephony/src/java/com/android/internal/telephony/test/SimulatedCommands.java中调用triggerRing()之后的调用到:
    simulatedCallState.triggerRing(number);
  2. 调用到 telephony/src/java/com/android/internal/telephony/test/SimulatedGsmCallState.java中triggerRing()。此方法中生成CallInfo对象。
  3. SimulatedCommands 中public void getCurrentCalls方法被调用得到来电,此方法会调用simulatedCallState.getDriverCalls(), 把CallInfo对象转换成DriverCall对象,传递出去。
  4. ./telephony/src/java/com/android/internal/telephony/gsm/GsmCallTracker.java中的 handlePollCalls(AsyncResult ar)方法中,从DriverCall对象构造Connection对象:

DriverCall dc = null;

dc = (DriverCall) polledCalls.get(curDC);
mConnections[i] = new GsmConnection(mPhone.getContext(), dc, this, i);

  1. 经过一系列调用,走到packages/services/Telephony/src/com/android/phone/CallModeler.java中的private boolean updateCallFromConnection(Call call, Connection connection,
    boolean isForConference)方法,此方法从Connection提取信息来构造Call对象:


// Number presentation
final int newNumberPresentation = connection.getNumberPresentation();
if (call.getNumberPresentation() != newNumberPresentation) {
call.setNumberPresentation(newNumberPresentation);
changed = true;
}

  1. Call对象中设置presentation到CallIdentification对象mIdentification:

public void setNumberPresentation(int presentation) {public void setNumberPresentation(int presentation) {
mIdentification.setNumberPresentation(presentation);
}

  1. 最后apps/InCallUI/src/com/android/incallui/CallCardPresenter.java中显示来电信息,其初始化中会提取出CallIdentification对象, 并搜索联系人中是否保存此号码对应的信息等:

public void init(Context context, Call call) {
final CallIdentification identification = call.getIdentification();

startContactInfoSearch(identification, true,
call.getState() == Call.State.INCOMING);
}

  1. 在apps/InCallUI/src/com/android/incallui/ContactInfoCache.java中把CallIdentification对象转换成CallerInfo对象:

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;
}

  1. 上述public static void populateCacheEntry(Context context, CallerInfo info, ContactCacheEntry cce,
    int presentation, boolean isIncoming) 方法中根据CallerInfo来构造联系人查找结果对象ContactCacheEntry, 其中有如下判断, 根据传入的presentation来决定来电界面显示的内容。


    } 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);

  2. 问题到这就明了了,来电界面显示号码的地方总是“未知”,是因为上述displayNmae的值"未知", 原因是presentation值设置的不对,这里的值是0。此值的源头是在telephony/src/java/com/android/internal/telephony/test/SimulatedGsmCallState.java中的 DriverCall中设置的。在
    toDriverCall(int index)方法中,进行如下粗体修改,成功显示号码:

DriverCall
toDriverCall(int index) {
DriverCall ret;
ret = new DriverCall();
ret.index = index;
ret.isMT = mIsMT;
ret.numberPresentation = 1;

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值