前言
今天客戶提了個需求,因為我們的設(shè)備在正常情況下無法調(diào)節(jié)通話音量,只有在打電話過程中,按物理音量加減鍵才能出現(xiàn)調(diào)節(jié)通話音量seekBar,很不方便,于是乎需求就來了。需要優(yōu)化兩個地方
1、在正常情況下,按物理音量加減鍵都顯示 通話音量調(diào)節(jié)seekBar,可方便快速調(diào)節(jié)通話音量
2、在Settings中提示音界面點擊設(shè)置進入,增加通話音量調(diào)節(jié)seekBar
修改前
修改后
實現(xiàn)
第一個功能
先來完成第一個功能,還是通過Hierarchy View查看布局結(jié)構(gòu),查找到布局文件id為volume_dialog,通過在源碼中搜索找到位于SystemUI中,volume_dialog.xml
源碼位置 frameworks\base\packages\SystemUI\res\layout\volume_dialog.xml
對應(yīng)的java類為 frameworks\base\packages\SystemUI\src\com\android\systemui\volume\VolumeDialog.java
修改代碼
addRow(AudioManager.STREAM_VOICE_CALL,
R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, true);
原來的第四個參數(shù)為false,修改為true即可顯示通話音量seekBar
為了便于說明,我們跟進addRow()中查看
private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
final VolumeRow row = initRow(stream, iconRes, iconMuteRes, important);
if (!mRows.isEmpty()) {
final View v = new View(mContext);
v.setId(android.R.id.background);
final int h = mContext.getResources()
.getDimensionPixelSize(R.dimen.volume_slider_interspacing);
final LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, h);
mDialogContentView.addView(v, mDialogContentView.getChildCount() - 1, lp);
row.space = v;
}
...
}
傳遞的參數(shù)對應(yīng)important,從字面意思理解重要對應(yīng)顯示,繼續(xù)查看initRow都做了什么
private VolumeRow initRow(final int stream, int iconRes, int iconMuteRes, boolean important) {
final VolumeRow row = new VolumeRow();
row.stream = stream;
row.iconRes = iconRes;
row.iconMuteRes = iconMuteRes;
row.important = important;
row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
row.view.setTag(row);
row.header = (TextView) row.view.findViewById(R.id.volume_row_header);
mSpTexts.add(row.header);
row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider);
row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
// forward events above the slider into the slider
row.view.setOnTouchListener(new OnTouchListener() {
private final Rect mSliderHitRect = new Rect();
private boolean mDragging;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
row.slider.getHitRect(mSliderHitRect);
if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN
&& event.getY() < mSliderHitRect.top) {
mDragging = true;
}
if (mDragging) {
event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top);
row.slider.dispatchTouchEvent(event);
if (event.getActionMasked() == MotionEvent.ACTION_UP
|| event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
mDragging = false;
}
return true;
}
return false;
}
});
row.icon = (ImageButton) row.view.findViewById(R.id.volume_row_icon);
row.icon.setImageResource(iconRes);
row.icon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState);
mController.setActiveStream(row.stream);
if (row.stream == AudioManager.STREAM_RING) {
final boolean hasVibrator = mController.hasVibrator();
if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {
if (hasVibrator) {
mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);
} else {
final boolean wasZero = row.ss.level == 0;
mController.setStreamVolume(stream, wasZero ? row.lastAudibleLevel : 0);
}
} else {
mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
if (row.ss.level == 0) {
mController.setStreamVolume(stream, 1);
}
}
} else {
final boolean vmute = row.ss.level == 0;
mController.setStreamVolume(stream, vmute ? row.lastAudibleLevel : 0);
}
row.userAttempt = 0; // reset the grace period, slider should update immediately
}
});
row.settingsButton = (ImageButton) row.view.findViewById(R.id.volume_settings_button);
row.settingsButton.setOnClickListener(mClickSettings);
return row;
}
從上面可看出,將一些變量都保存到了VolumeRow中,設(shè)置了icon的點擊事件,將當(dāng)前對應(yīng)的音量類型設(shè)置為最低(禁音), 設(shè)置seekBar的改變事件。通過過濾日志,查找到控制音量類型的顯示和隱藏的代碼塊updateRowsH()
private boolean isVisibleH(VolumeRow row, boolean isActive) {
return mExpanded && row.view.getVisibility() == View.VISIBLE
|| (mExpanded && (row.important || isActive))
|| !mExpanded && isActive;
}
private void updateRowsH() {
if (D.BUG) Log.d(TAG, "updateRowsH");
final VolumeRow activeRow = getActiveRow();
updateFooterH();
updateExpandButtonH();
if (!mShowing) {
trimObsoleteH();
}
// apply changes to all rows
for (VolumeRow row : mRows) {
final boolean isActive = row == activeRow;
final boolean visible = isVisibleH(row, isActive);
Log.e(TAG, "row==" + row.stream + " isActive=="+isActive + " visible="+visible);
Util.setVisOrGone(row.view, visible);
Util.setVisOrGone(row.space, visible && mExpanded);
final int expandButtonRes = mExpanded ? R.drawable.ic_volume_settings : 0;
if (expandButtonRes != row.cachedExpandButtonRes) {
row.cachedExpandButtonRes = expandButtonRes;
if (expandButtonRes == 0) {
row.settingsButton.setImageDrawable(null);
} else {
row.settingsButton.setImageResource(expandButtonRes);
}
}
Util.setVisOrInvis(row.settingsButton, false);
updateVolumeRowHeaderVisibleH(row);
row.header.setAlpha(mExpanded && isActive ? 1 : 0.5f);
updateVolumeRowSliderTintH(row, isActive);
}
}
遍歷已經(jīng)添加的音量類型集合mRows,依次判斷是否處于活動狀態(tài),再和開始設(shè)置的important屬性比較。mExpanded是否展開,默認(rèn)只顯示鈴聲音量控制,點擊下拉的按鈕,才完全顯示其它的音量控制
mExpanded && row.view.getVisibility() == View.VISIBLE || (mExpanded && (row.important || isActive)) || !mExpanded && isActive
true && false || (true && (true || false)) || false && true --->true
好了,至此分析完畢,重新mmm push SystemUI.apk 查看效果
第二個功能
源碼位置
Settings\res_ext\xml\edit_profile_prefs.xml
Settings\src\com\mediatek\audioprofile\Editprofile.java
Settings\src\com\mediatek\audioprofile\VolumeSeekBarPreference.java
在edit_profile_prefs.xml中仿照原來的Alarm volume和Ring volume,新增加一個Call volume
<!-- Media volume -->
<com.mediatek.audioprofile.VolumeSeekBarPreference
android:key="media_volume"
android:icon="@*android:drawable/ic_audio_vol"
android:title="@string/media_volume_option_title" />
<!-- Alarm volume -->
<com.mediatek.audioprofile.VolumeSeekBarPreference
android:key="alarm_volume"
android:icon="@*android:drawable/ic_audio_alarm"
android:title="@string/alarm_volume_option_title" />
<!-- Ring volume -->
<com.mediatek.audioprofile.VolumeSeekBarPreference
android:key="ring_volume"
android:icon="@*android:drawable/ic_audio_ring_notif"
android:title="@string/ring_volume_option_title" />
<!-- Call volume -->
<com.mediatek.audioprofile.VolumeSeekBarPreference
android:key="call_volume"
android:icon="@drawable/ic_volume_voice"
android:title="@string/call_volume_option_title" />
對應(yīng)的drawable文件時從SystemUI中拷貝過來的,ic_volume_voice.xml
<vector xmlns:android="http://schemas./apk/res/android"
android:height="24.0dp"
android:viewportHeight="48.0"
android:viewportWidth="48.0"
android:width="24.0dp" >
<path
android:fillColor="#ff727272"
android:pathData="M13.25,21.59c2.88,5.66 7.51,10.29 13.18,13.17l4.4,-4.41c0.55,-0.55 1.34,-0.71 2.03,-0.49C35.1,30.6 37.51,31.0 40.0,31.0c1.11,0.0 2.0,0.89 2.0,2.0l0.0,7.0c0.0,1.11 -0.89,2.0 -2.0,2.0C21.22,42.0 6.0,26.78 6.0,8.0c0.0,-1.1 0.9,-2.0 2.0,-2.0l7.0,0.0c1.11,0.0 2.0,0.89 2.0,2.0 0.0,2.4 0.4,4.9 1.14,7.1 0.2,0.6 0.06,1.48 -0.49,2.03l-4.4,4.42z" />
</vector>
接下來對應(yīng)到 Editprofile.java 文件中,可以看到 KEY_ALARM_VOLUME 對應(yīng)的preference初始化,依舊照葫蘆畫瓢,添加 KEY_CALL_VOLUME
private void initVolume(PreferenceScreen parent) {
initVolumePreference(KEY_MEDIA_VOLUME, AudioManager.STREAM_MUSIC);
initVolumePreference(KEY_ALARM_VOLUME, AudioManager.STREAM_ALARM);
initVolumePreference(KEY_CALL_VOLUME, AudioManager.STREAM_VOICE_CALL);
if (mVoiceCapable) {
mVolume = initVolumePreference(KEY_RING_VOLUME, AudioManager.STREAM_RING);
parent.removePreference(parent.findPreference(KEY_NOTIFICATION_VOLUME));
} else {
mVolume = initVolumePreference(KEY_NOTIFICATION_VOLUME,
AudioManager.STREAM_NOTIFICATION);
parent.removePreference(parent.findPreference(KEY_RING_VOLUME));
}
}
重新編譯,push替換后發(fā)現(xiàn),UI倒是出來了,但是無法滑動,事情果然沒那么簡單,繼續(xù)查看 initVolumePreference()
private VolumeSeekBarPreference initVolumePreference(String key, int stream) {
Log.d("@M_" + TAG, "Init volume preference, key = " + key + ",stream = " + stream);
final VolumeSeekBarPreference volumePref = (VolumeSeekBarPreference) findPreference(key);
volumePref.setStream(stream);
volumePref.setCallback(mVolumeCallback);
volumePref.setProfile(mKey);
return volumePref;
}
保存了當(dāng)前的音量調(diào)節(jié)類型,設(shè)置seekBar回調(diào)事件,接下來看看回調(diào)處理了什么
private final class VolumePreferenceCallback implements VolumeSeekBarPreference.Callback {
private SeekBarVolumizer mCurrent;
@Override
public void onSampleStarting(SeekBarVolumizer sbv) {
if (mCurrent != null && mCurrent != sbv) {
mCurrent.stopSample();
}
mCurrent = sbv;
if (mCurrent != null) {
mHandler.removeMessages(H.STOP_SAMPLE);
mHandler.sendEmptyMessageDelayed(H.STOP_SAMPLE, SAMPLE_CUTOFF);
}
}
public void onStreamValueChanged(int stream, int progress) {
if (stream == AudioManager.STREAM_RING) {
mHandler.removeMessages(H.UPDATE_RINGER_ICON);
mHandler.obtainMessage(H.UPDATE_RINGER_ICON, progress, 0).sendToTarget();
}
}
public void stopSample() {
if (mCurrent != null) {
mCurrent.stopSample();
}
}
public void ringtoneChanged() {
if (mCurrent != null) {
mCurrent.ringtoneChanged();
} else {
mVolume.getSeekBar().ringtoneChanged();
}
}
};
當(dāng)我們點擊或者是滑動seekBar時,會根據(jù)當(dāng)前設(shè)置的音量大小播放一段短暫的默認(rèn)鈴音,當(dāng)鈴音未播放完成時,再次點擊將不進行播放。繼續(xù)跟進 VolumeSeekBarPreference.java 中
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (mStream == 0) {
Log.w(TAG, "No stream found, not binding volumizer ");
return;
}
getPreferenceManager().registerOnActivityStopListener(this);
final SeekBar seekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
if (seekBar == mSeekBar) {
return;
}
mSeekBar = seekBar;
final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
@Override
public void onSampleStarting(SeekBarVolumizer sbv) {
if (mCallback != null) {
mCallback.onSampleStarting(sbv);
}
}
};
final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
if (mVolumizer == null) {
mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc, mKey);
}
//mVolumizer.setProfile(mKey);
mVolumizer.setSeekBar(mSeekBar);
}
mStream == 0, 直接return,會不會和這有關(guān)系呢,來看下各個音量調(diào)節(jié)類型對應(yīng)的int值
/** Used to identify the volume of audio streams for phone calls */
public static final int STREAM_VOICE_CALL = 0;
/** Used to identify the volume of audio streams for the phone ring and message alerts */
public static final int STREAM_RING = 2;
/** Used to identify the volume of audio streams for music playback */
public static final int STREAM_MUSIC = 3;
/** Used to identify the volume of audio streams for alarms */
public static final int STREAM_ALARM = 4;
我們新增的 STREAM_VOICE_CALL對應(yīng)的mStream正好為0,直接給return掉了,所以修改為 mStream < 0 即可,重新編譯 push 發(fā)現(xiàn)成功了。
具體的音量調(diào)節(jié)邏輯在 packages\apps\Settings\src\com\mediatek\audioprofile\SeekBarVolumizer.java 中,感興趣的可繼續(xù)深究,肯定離不開調(diào)用 .setStreamVolume()方法,大概看了一眼,主要是onProgressChanged()回調(diào),通過postSetVolume(progress)方法,發(fā)送MSG_SET_STREAM_VOLUME消息,最終調(diào)用saveVolume()
|