1.xml
\packages\apps\Settings\res\layout\preference_volume_slider.xml
+ <TextView
+ android:layout_marginStart="1550dp"
+ android:id="@+id/seek_bar_volume"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/rtc_id"
+ android:textSize="30sp" />
2.添加代码
packages/apps/Settings/src/com/android/settings/notification/VolumeSeekBarPreference.java
private String mSuppressionText;
+ private TextView mVolText;
private boolean mMuted;
......
protected void onBindView(View view) {
super.onBindView(view);
if (mStream == 0) {
Log.w(TAG, "No stream found, not binding volumizer");
return;
}
mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text);
+ mVolText= (TextView) view.findViewById(R.id.seek_bar_volume);
......
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
if (mCallback != null) {
mCallback.onStreamValueChanged(mStream, progress);
+Progress=progress;
+ mVolText.setText(String.valueOf((100*Progress)/15));
+ Log.w(TAG, "onProgressChanged="+Progress);
......
public void onMuted(boolean muted) {
if (mMuted == muted) return;
mMuted = muted;
updateIconView();
}
};
final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
if (mVolumizer == null) {
mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
}
mVolumizer.start();
mVolumizer.setSeekBar(mSeekBar);
updateIconView();
mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress());
+ mVolText.setText(String.valueOf((100*mSeekBar.getProgress())/15));
+ Log.w(TAG, "onProgressChanged1111="+Progress);
updateSuppressionText();
}
......
3.全部文件
//packages/apps/Settings/src/com/android/settings/notification/VolumeSeekBarPreference.java
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.preference.SeekBarPreference;
import android.preference.SeekBarVolumizer;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.android.settings.R;
import java.util.Objects;
/** A slider preference that directly controls an audio stream volume (no dialog) **/
public class VolumeSeekBarPreference extends SeekBarPreference
implements PreferenceManager.OnActivityStopListener {
private static final String TAG = "VolumeSeekBarPreference";
private int mStream;
private SeekBar mSeekBar;
private SeekBarVolumizer mVolumizer;
private Callback mCallback;
private ImageView mIconView;
private TextView mSuppressionTextView;
private String mSuppressionText;
//add long
private TextView mVolText;
//end long
private boolean mMuted;
private int mIconResId;
private int mMuteIconResId;
private boolean mStopped;
int Progress;
public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setLayoutResource(R.layout.preference_volume_slider);
}
public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public VolumeSeekBarPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VolumeSeekBarPreference(Context context) {
this(context, null);
}
public void setStream(int stream) {
mStream = stream;
//add long
Log.w(TAG, "setStream"+stream);
//end long
}
public void setCallback(Callback callback) {
mCallback = callback;
}
public void onActivityResume() {
if (mStopped) {
init();
}
}
@Override
public void onActivityStop() {
mStopped = true;
if (mVolumizer != null) {
mVolumizer.stop();
}
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (mStream == 0) {
Log.w(TAG, "No stream found, not binding volumizer");
return;
}
mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text);
//add long
mVolText= (TextView) view.findViewById(R.id.seek_bar_volume);
//end long
/* if(SystemProperties.get("persist.audio.volune",1).equals(1)) Progress=1 ;
else if(SystemProperties.get("persist.audio.volune",2).equals(2)) Progress=2 ;
else if(SystemProperties.get("persist.audio.volune",3).equals(3)) Progress=3 ;
else if(SystemProperties.get("persist.audio.volune",4).equals(4)) Progress=4 ;
else if(SystemProperties.get("persist.audio.volune",5).equals(5)) Progress=5 ;
else if(SystemProperties.get("persist.audio.volune",6).equals(6)) Progress=6 ;
else if(SystemProperties.get("persist.audio.volune",7).equals(7)) Progress=7 ;
else if(SystemProperties.get("persist.audio.volune",8).equals(8)) Progress=8 ;
else if(SystemProperties.get("persist.audio.volune",9).equals(9)) Progress=9 ;
else if(SystemProperties.get("persist.audio.volune",10).equals(10)) Progress=10 ;
else if(SystemProperties.get("persist.audio.volune",11).equals(11)) Progress=11 ;
else if(SystemProperties.get("persist.audio.volune",12).equals(12)) Progress=12 ;
else if(SystemProperties.get("persist.audio.volune",13).equals(13)) Progress=13 ;
else if(SystemProperties.get("persist.audio.volune",14).equals(14)) Progress=14 ;
else if(SystemProperties.get("persist.audio.volune",15).equals(15)) Progress=15 ;
else if(SystemProperties.get("persist.audio.volune",0).equals(0)) Progress=0 ;
*/
init();
}
private void init() {
if (mSeekBar == null) return;
getPreferenceManager().registerOnActivityStopListener(this);
final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
@Override
public void onSampleStarting(SeekBarVolumizer sbv) {
if (mCallback != null) {
mCallback.onSampleStarting(sbv);
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
if (mCallback != null) {
mCallback.onStreamValueChanged(mStream, progress);
//add long
Progress=progress;
mVolText.setText(String.valueOf((100*Progress)/15));
Log.w(TAG, "onProgressChanged="+Progress);
// SystemProperties.set("persist.audio.volune",Progress);
//end long
}
}
@Override
public void onMuted(boolean muted) {
if (mMuted == muted) return;
mMuted = muted;
updateIconView();
}
};
final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
if (mVolumizer == null) {
mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
}
mVolumizer.start();
mVolumizer.setSeekBar(mSeekBar);
updateIconView();
mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress());
//add long
mVolText.setText(String.valueOf((100*mSeekBar.getProgress())/15));
Log.w(TAG, "onProgressChanged1111="+Progress);
// SystemProperties.set("persist.audio.volune",Progress);
//end long
updateSuppressionText();
}
// during initialization, this preference is the SeekBar listener
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
super.onProgressChanged(seekBar, progress, fromTouch);
mCallback.onStreamValueChanged(mStream, progress);
}
private void updateIconView() {
if (mIconView == null) return;
if (mIconResId != 0) {
mIconView.setImageResource(mIconResId);
} else if (mMuteIconResId != 0 && mMuted) {
mIconView.setImageResource(mMuteIconResId);
} else {
mIconView.setImageDrawable(getIcon());
}
//add long
Log.w(TAG, "mprogress=="+Progress);
//end long
}
public void showIcon(int resId) {
// Instead of using setIcon, which will trigger listeners, this just decorates the
// preference temporarily with a new icon.
if (mIconResId == resId) return;
mIconResId = resId;
updateIconView();
}
public void setMuteIcon(int resId) {
if (mMuteIconResId == resId) return;
mMuteIconResId = resId;
updateIconView();
}
private Uri getMediaVolumeUri() {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ getContext().getPackageName()
+ "/" + R.raw.media_volume);
}
public void setSuppressionText(String text) {
if (Objects.equals(text, mSuppressionText)) return;
mSuppressionText = text;
updateSuppressionText();
}
private void updateSuppressionText() {
if (mSuppressionTextView != null && mSeekBar != null) {
mSuppressionTextView.setText(mSuppressionText);
final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText);
mSuppressionTextView.setVisibility(showSuppression ? View.VISIBLE : View.INVISIBLE);
mSeekBar.setVisibility(showSuppression ? View.INVISIBLE : View.VISIBLE);
}
}
public interface Callback {
void onSampleStarting(SeekBarVolumizer sbv);
void onStreamValueChanged(int stream, int progress);
}
}