安卓本身并不支持双卡,在定制过之后,默认情况下两张卡的来电铃声是一样的,不能分别进行设置,这就需要添加一些相关代码。
基本思路是,找到来电时播放铃声的地方,修改为根据卡的ID播放相应的声音。(而整个来电流程比较复杂,可以参考其他一些分析来电流程的文章来进行了解,这里就不赘述)
以Android6.0为例,控制通话的上层代码在android\packages\services下,而控制来电铃声的在Telecomm里面。
有两个相关类:
Telecomm\src\com\android\server\telecom\Ringer.java
Telecomm\src\com\android\server\telecom\AsyncRingtonePlayer.java
AsyncRingtonePlayer封装了一个用来播放铃声的类,Ringer是用来控制播放铃声的。
在来电时会调用Ringer的startRingingOrCallWaiting方法播放铃声。
private void startRingingOrCallWaiting(Call call) {
Call foregroundCall = mCallsManager.getForegroundCall();
Log.v(this, "startRingingOrCallWaiting, foregroundCall: %s.", foregroundCall);
if (mRingingCalls.contains(foregroundCall) && (!mCallsManager.hasActiveOrHoldingCall())) {
// The foreground call is one of incoming calls so play the ringer out loud.
stopCallWaiting(call);
if (!shouldRingForContact(foregroundCall.getContactUri())) {
return;
}
AudioManager audioManager =
(AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) >= 0) {
if (mState != STATE_RINGING) {
Log.event(call, Log.Events.START_RINGER);
mState = STATE_RINGING;
}
mCallAudioManager.setIsRinging(call, true);
// Because we wait until a contact info query to complete before processing a
// call (for the purposes of direct-to-voicemail), the information about custom
// ringtones should be available by the time this code executes. We can safely
// request the custom ringtone from the call and expect it to be current.
mRingtonePlayer.play(foregroundCall.getRingtone());
} else {
Log.v(this, "startRingingOrCallWaiting, skipping because volume is 0");
}
if (shouldVibrate(mContext) && !mIsVibrating) {
mVibrator.vibrate(VIBRATION_PATTERN, VIBRATION_PATTERN_REPEAT,
VIBRATION_ATTRIBUTES);
mIsVibrating = true;
}
} else if (foregroundCall != null) {
...
}
}
源码中的注释还是比较清楚地,在正常状态下来电时,调用mRingtonePlayer.play(foregroundCall.getRingtone());播放铃声。所以我们要做的就是在调用这个方法前进行双卡的区分。
在AsyncRingtonePlayer中添加一个方法,来标识当前来电的卡ID:
private int id;
public void setID(int id){
this.cid = id;
}
之后在播放前调用这个方法:
try{
int id = SubscriptionManager.getPhoneId(Integer.valueOf(
foregroundCall.getTargetPhoneAccount().getId()));
mRingtonePlayer.setcCID(id);
}catch(Exception e){
mRingtonePlayer.setcCID(0);
}
mRingtonePlayer.play(foregroundCall.getRingtone());
不同平台的获取ID的方法可能有所不同,移植时注意。
然后主要看AsyncRingtonePlayer中的播放逻辑:
void play(Uri ringtone) {
Log.d(this, "Posting play.");
//调用postMessage进行通知
postMessage(EVENT_PLAY, true /* shouldCreateHandler */, ringtone);
}
private void postMessage(int messageCode, boolean shouldCreateHandler, Uri ringtone) {
synchronized(this) {
if (mHandler == null && shouldCreateHandler) {
mHandler = getNewHandler();
}
if (mHandler == null) {
Log.d(this, "Message %d skipped because there is no handler.", messageCode);
} else {
//发送消息
mHandler.obtainMessage(messageCode, ringtone).sendToTarget();
}
}
}
private Handler getNewHandler() {
Preconditions.checkState(mHandler == null);
HandlerThread thread = new HandlerThread("ringtone-player");
thread.start();
return new Handler(thread.getLooper()) {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case EVENT_PLAY:
//调用handlePlay播放
handlePlay((Uri) msg.obj);
break;
case EVENT_REPEAT:
handleRepeat();
break;
case EVENT_STOP:
handleStop();
break;
}
}
};
}
private void handlePlay(Uri ringtoneUri) {
// don't bother with any of this if there is an EVENT_STOP waiting.
if (mHandler.hasMessages(EVENT_STOP)) {
return;
}
ThreadUtil.checkNotOnMainThread();
Log.i(this, "Play ringtone.");
if (mRingtone == null) {
// 获取一个Ringtone对象
mRingtone = getRingtone(ringtoneUri);
// Cancel everything if there is no ringtone.
if (mRingtone == null) {
handleStop();
return;
}
}
handleRepeat();
}
private void handleRepeat() {
if (mRingtone == null) {
return;
}
if (mRingtone.isPlaying()) {
Log.d(this, "Ringtone already playing.");
} else {
//开始播放
mRingtone.play();
Log.i(this, "Repeat ringtone.");
}
// Repost event to restart ringer in {@link RESTART_RINGER_MILLIS}.
synchronized(this) {
if (!mHandler.hasMessages(EVENT_REPEAT)) {
mHandler.sendEmptyMessageDelayed(EVENT_REPEAT, RESTART_RINGER_MILLIS);
}
}
}
我们需要干预的就是获取Ringtone这一过程,以下时getRingtone:
private Ringtone getRingtone(Uri ringtoneUri) {
if (ringtoneUri == null) {
ringtoneUri = Settings.System.DEFAULT_RINGTONE_URI;
}
Ringtone ringtone = RingtoneManager.getRingtone(mContext, ringtoneUri);
if (ringtone != null) {
ringtone.setStreamType(AudioManager.STREAM_RING);
}
return ringtone;
}
可以看出在没有传递ringtoneUri(这个主要是在对一些号码单独设置铃声时才会有值,一般都为null)时,ringtoneUri取得是Settings.System.DEFAULT_RINGTONE_URI;而这个值:
public static final Uri DEFAULT_RINGTONE_URI = getUriFor(RINGTONE);
和Settings模块中设置的来电铃声是一个值。
所以,关键点就是根据之前设置的那个卡id,来动态的改变这里的ringtoneUri 。系统默认使用RINGTONE这个字段来保存来电铃声,添加双卡区分后,我们可以重新定义一个系统属性来保存卡二的铃声URI。不过Android已经为我们预置了额外的两个字段,可以直接拿来使用:
public static final String RINGTONE_2 = "ringtone_2";
public static final String RINGTONE_3 = "ringtone_3";
剩下的内容就很简单了,写一个方法getUriBySubID(通过id取uri)来替换原来方法中当参数为null时的逻辑即可。
最后不要忘了在Settings模块中添加设置卡二铃声的入口,可以参考原来默认的入口,只需改改名字,改改保存的字段即可。
关于未设置时的默认铃声问题,在我的平台上RINGTONE_2 和RINGTONE_3 的默认值是和RINGTONE一样的,当然也可以指定,和设置普通的系统属性默认值一样。如果是自己新定义的系统属性,一定要注意默认值的问题。
网友评论