天天看點

Android6.0 源碼修改之Settings音量調節界面增加通話音量調節

Android6.0 源碼修改之Settings音量調節界面增加通話音量調節

前言

今天客戶提了個需求,因為我們的裝置在正常情況下無法調節通話音量,隻有在打電話過程中,按實體音量加減鍵才能出現調節通話音量seekBar,很不友善,于是乎需求就來了。需要優化兩個地方

1、在正常情況下,按實體音量加減鍵都顯示 通話音量調節seekBar,可友善快速調節通話音量

2、在Settings中提示音界面點選設定進入,增加通話音量調節seekBar

在這裡插入圖檔描述

修改前

修改後

實作

第一個功能

先來完成第一個功能,還是通過Hierarchy View檢視布局結構,查找到布局檔案id為volume_dialog,通過在源碼中搜尋找到位于SystemUI中,volume_dialog.xml

源碼位置 frameworksbasepackagesSystemUIreslayoutvolume_dialog.xml

對應的java類為 frameworksbasepackagesSystemUIsrccomandroidsystemuivolumeVolumeDialog.java

修改代碼

addRow(AudioManager.STREAM_VOICE_CALL,

R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, true);           

原來的第四個參數為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;
}
...           

}

傳遞的參數對應important,從字面意思了解重要對應顯示,繼續檢視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中,設定了icon的點選事件,将目前對應的音量類型設定為最低(禁音), 設定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);
}           

周遊已經添加的音量類型集合mRows,依次判斷是否處于活動狀态,再和開始設定的important屬性比較。mExpanded是否展開,預設隻顯示鈴聲音量控制,點選下拉的按鈕,才完全顯示其它的音量控制

mExpanded && row.view.getVisibility() == View.VISIBLE || (mExpanded && (row.important || isActive)) || !mExpanded && isActive

true && false || (true && (true || false)) || false && true --->true

好了,至此分析完畢,重新mmm push SystemUI.apk 檢視效果

第二個功能

源碼位置

Settingsres_extxmledit_profile_prefs.xml

SettingssrccommediatekaudioprofileEditprofile.java

SettingssrccommediatekaudioprofileVolumeSeekBarPreference.java

在edit_profile_prefs.xml中仿照原來的Alarm volume和Ring volume,新增加一個Call volume

android:key="media_volume"
    android:icon="@*android:drawable/ic_audio_vol"
    android:title="@string/media_volume_option_title" />
           
android:key="alarm_volume"
    android:icon="@*android:drawable/ic_audio_alarm"
    android:title="@string/alarm_volume_option_title" />
           
android:key="ring_volume"
    android:icon="@*android:drawable/ic_audio_ring_notif"
    android:title="@string/ring_volume_option_title" />
           
android:key="call_volume"
    android:icon="@drawable/ic_volume_voice"
    android:title="@string/call_volume_option_title" />           

對應的drawable檔案時從SystemUI中拷貝過來的,ic_volume_voice.xml

android:height="24.0dp"

android:viewportHeight="48.0"

android:viewportWidth="48.0"

android:width="24.0dp" >

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" />
           

接下來對應到 Editprofile.java 檔案中,可以看到 KEY_ALARM_VOLUME 對應的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替換後發現,UI倒是出來了,但是無法滑動,事情果然沒那麼簡單,繼續檢視 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;           

儲存了目前的音量調節類型,設定seekBar回調事件,接下來看看回調處理了什麼

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

};

當我們點選或者是滑動seekBar時,會根據目前設定的音量大小播放一段短暫的預設鈴音,當鈴音未播放完成時,再次點選将不進行播放。繼續跟進 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,會不會和這有關系呢,來看下各個音量調節類型對應的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對應的mStream正好為0,直接給return掉了,是以修改為 mStream < 0 即可,重新編譯 push 發現成功了。

具體的音量調節邏輯在 packagesappsSettingssrccommediatekaudioprofileSeekBarVolumizer.java 中,感興趣的可繼續深究,肯定離不開調用 .setStreamVolume()方法,大概看了一眼,主要是onProgressChanged()回調,通過postSetVolume(progress)方法,發送MSG_SET_STREAM_VOLUME消息,最終調用saveVolume()

原文位址

https://www.cnblogs.com/cczheng-666/p/10757358.html

繼續閱讀