先看实现后效果
这是来电效果如下:
这是接听来电效果如下:
所有来电都会自动在通话记录里留下数据
实际效果可以在没有SIM卡的机器上也能接收来电,实现思路来源于系统源码 packages/services/Telecomm/testapps, 这个模块是用来测试通话功能的,基于这个测试代码我们稍加修改即可达到以上效果.以下大部分操作需要在有完整的系统源码和机器的root权限的前提下执行.
1. 单独编译testapps,即可在out目录下得到TelecomTestApps.apk
mmm packages/services/Telecomm/testapps
2. 将TelecomTestApps.apk push到机器的/system/app目录下,该操作需要root权限以及挂载system目录,然后再重启机器.
adb push TelecomTestApps.apk /system/app
3. 重启机器后在桌面可以看到多出很多APP,我们只需要启动其中的Test Connection Service App即可.
该界面启动后添加了两个通知交互UI,效果如下:
展开第一个通知可以选择添加PhoneAccount,这一步也是必须.
展开第二个通知可以选择Add Call,点击后即可在上方显示一个虚拟的来电.
在操作第一步添加PhoneAccount的时候可能在logcat上出现如下错误:
这说明系统中缺少android.software.connectionservice feature,在系统源码中找到frameworks/native/data/etc/android.software.connectionservice.xml文件,导入到机器的/vendor/etc/permissions目录下然后重启下系统即可.
4.修改testapps2源码,以契合自己的业务需求.
目前业务需求是
1.不显示任何多余的通知
2.在桌面不显示任何testapps相关的APP
3.使用广播来启动来电
我们先在packages/services/Telecomm目录下复制一份testapps代码命名为testapps2,z这样可以保留一份原始的系统源码.
我们现在查看下程序入口com.android.server.telecom.testapps.TestCallActivity内容.
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
final Intent intent = getIntent();
final String action = intent != null ? intent.getAction() : null;
final Uri data = intent != null ? intent.getData() : null;
if (ACTION_NEW_INCOMING_CALL.equals(action) && data != null) {
CallNotificationReceiver.sendIncomingCallIntent(this, data,
VideoProfile.STATE_AUDIO_ONLY);
} else if (ACTION_NEW_UNKNOWN_CALL.equals(action) && data != null) {
CallNotificationReceiver.addNewUnknownCall(this, data, intent.getExtras());
} else if (ACTION_HANGUP_CALLS.equals(action)) {
CallNotificationReceiver.hangupCalls(this);
} else if (ACTION_RTT_CALL.equals(action)) {
CallNotificationReceiver.sendIncomingRttCallIntent(
this, data, VideoProfile.STATE_AUDIO_ONLY);
} else if (ACTION_SEND_UPGRADE_REQUEST.equals(action)) {
CallNotificationReceiver.sendUpgradeRequest(this, data);
} else if (ACTION_REMOTE_RTT_UPGRADE.equals(action)) {
CallNotificationReceiver.remoteRttUpgrade(this);
} else {
CallServiceNotifier.getInstance().updateNotification(this);
}
finish();
}
这部分代码是显示通知以及响应通知回调,这块不用注重查看.关键位置在com.android.server.telecom.testapps.CallNotificationReceiver中,查看下核心代码:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_CALL_SERVICE_EXIT.equals(action)) {
CallServiceNotifier.getInstance().cancelNotifications(context);
} else if (ACTION_REGISTER_PHONE_ACCOUNT.equals(action)) {
CallServiceNotifier.getInstance().registerPhoneAccount(context);
} else if (ACTION_SHOW_ALL_PHONE_ACCOUNTS.equals(action)) {
CallServiceNotifier.getInstance().showAllPhoneAccounts(context);
} else if (ACTION_ONE_WAY_VIDEO_CALL.equals(action)) {
sendIncomingCallIntent(context, null, VideoProfile.STATE_RX_ENABLED);
} else if (ACTION_TWO_WAY_VIDEO_CALL.equals(action)) {
sendIncomingCallIntent(context, null, VideoProfile.STATE_BIDIRECTIONAL);
} else if (ACTION_RTT_CALL.equals(action)) {
sendIncomingRttCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
} else if (ACTION_AUDIO_CALL.equals(action)) {
sendIncomingCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
}
}
public static void sendIncomingCallIntent(Context context, Uri handle, int videoState) {
PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
new ComponentName(context, TestConnectionService.class),
CallServiceNotifier.SIM_SUBSCRIPTION_ID);
// For the purposes of testing, indicate whether the incoming call is a video call by
// stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
Bundle extras = new Bundle();
extras.putInt(TestConnectionService.EXTRA_START_VIDEO_STATE, videoState);
if (handle != null) {
extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
}
TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
}
以上只贴出了SIM卡的语音通话响应,其他类型可自行研究,由以上代码可知添加PhoneAccount和响应Add Call是由
CallServiceNotifier.getInstance().registerPhoneAccount(context);
sendIncomingCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
这两个函数实现的,那么事情就简单了,我们可以在testapps2中添加一个广播接收器MyBroadcastReceiver.java
用于接收我们自定义的广播,当接受到自定义广播后调用以上两个函数即可触发来电显示.
关于如何处理去掉多余的桌面APP和移除通知,我们只需将APP的清单文件中包含<action android:name="android.intent.action.MAIN"/>的组件全部注释掉即可.
如何在系统源码全量编译时自动将我们修改的APP编译进镜像中:
1.修改testapps/android.bp 中的android_test { .... } 为 android_app { .... },并且注释掉APP清单文件中的<uses-library android:name="android.test.runner"/>,修改android_app {} 中的模块名称为TelecomTestApps2,这是为了防止编译时与之前备份的testapp出现同名模块冲突.
2.在device/厂商/厂商.mk中添加如下
PRODUCT_PACKAGES += \
TelecomTestApps2
PRODUCT_COPY_FILES += \
frameworks/native/data/etc/android.software.connectionservice.xml:vendor/etc/permissions/android.software.connectionservice.xml
完成以上步骤后进行全量编译即可.