android下拉栏添加媒体音量调节

参考下拉栏的屏幕亮度调节,添加媒体音量调节。

平台信息:

MSM8909.LA.3.1.2_AOSP

PLATFORM_VERSION=9

BUILD_ID=PKQ1.190903.001

效果图

布局文件与音量图标

java 复制代码
Index: frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(版本 12429)
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 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.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:systemui="http://schemas.android.com/apk/res-auto"
+    android:layout_height="wrap_content"
+    android:layout_width="match_parent"
+    android:layout_gravity="center_vertical"
+    android:paddingLeft="16dp"
+    android:paddingRight="16dp">
+
+    <ImageView
+        android:id="@+id/media_volume_icon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginEnd="8dp"
+        android:src="@drawable/ic_media_stream"
+        android:contentDescription="@null"
+        android:visibility="visible" />
+
+    <SeekBar
+        android:id="@+id/media_volume_seekbar"
+        android:layout_width="0dp"
+        android:layout_height="48dp"
+        android:layout_gravity="center_vertical"
+        android:layout_weight="1" />
+
+</LinearLayout>
Index: frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(版本 12429)
@@ -0,0 +1,25 @@
+<!--
+    Copyright (C) 2017 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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        android:tint="?android:attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,3l0.01,10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55C7.79,13 6,14.79 6,17c0,2.21 1.79,4 4.01,4S14,19.21 14,17V7h4V3H12zM10.01,19c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C12.01,18.1 11.11,19 10.01,19z"/>
+</vector>

实现

java 复制代码
Index: frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
===================================================================
--- frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12424)
+++ frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12429)
@@ -20,10 +20,14 @@
 import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
 
 import android.annotation.Nullable;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.media.AudioManager;
 import android.metrics.LogMaker;
 import android.os.Handler;
 import android.os.Message;
@@ -32,6 +36,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
+import android.widget.SeekBar;
+import android.widget.SeekBar.OnSeekBarChangeListener;
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -62,7 +68,7 @@
 
     protected final Context mContext;
     protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
-    protected final View mBrightnessView;
+    protected final View mBrightnessView, mMediaVolumeView;
     private final H mHandler = new H();
     private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
     private final QSTileRevealController mQsTileRevealController;
@@ -87,6 +93,9 @@
     private BrightnessMirrorController mBrightnessMirrorController;
     private View mDivider;
 
+    private AudioManager mAudioManager;
+    private SeekBar mMediaVolumeSeekBar;
+
     public QSPanel(Context context) {
         this(context, null);
     }
@@ -101,6 +110,12 @@
             R.layout.quick_settings_brightness_dialog, this, false);
         addView(mBrightnessView);
 
+        mMediaVolumeView = LayoutInflater.from(mContext).inflate(
+            R.layout.quick_settings_media_volume_dialog, this, false);
+        addView(mMediaVolumeView);
+
         mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
                 R.layout.qs_paged_tile_layout, this, false);
         mTileLayout.setListening(mListening);
@@ -123,9 +138,42 @@
                 findViewById(R.id.brightness_icon),
                 findViewById(R.id.brightness_slider));
 
+        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+        mMediaVolumeSeekBar = findViewById(R.id.media_volume_seekbar);
+        mMediaVolumeSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setOnSeekBarChangeListener(mSeekListener);
+        IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
+        mContext.registerReceiver(mMediaVolumeReceiver, filter);
+
         updateResources();
     }
 
+    private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
+        @Override
+        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
+        }
+
+        @Override
+        public void onStartTrackingTouch(SeekBar seekBar) { }
+
+        @Override
+        public void onStopTrackingTouch(SeekBar seekBar) { }
+    };
+
+    private final BroadcastReceiver mMediaVolumeReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int currentMediaVolumeValue = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+            mMediaVolumeSeekBar.setProgress(currentMediaVolumeValue);
+        }
+    };
+
     protected void addDivider() {
         mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
         mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
相关推荐
qq_3938282220 分钟前
苹果MAC 安卓模拟器
android·软件需求·模拟器
恋猫de小郭1 小时前
用 AI 把一个五年前的 RN 项目,从 0.61.3 升级到 0.74.0 是一种什么样的体验
android·前端·react native
Kapaseker1 小时前
如果你还不懂 Kotlin Flow,这里有一万字
android·kotlin
啊森要自信2 小时前
【QT】Qt 信号与槽的使用详解&&连接方式&&Lambda表达式定义槽函数
android·开发语言·c++·qt·qt5
智江鹏2 小时前
Android 之 MVVM架构
android·架构
lincats2 小时前
一步一步学习使用LiveBindings(7) 实现对JSON数据的绑定
android·delphi·livebindings·delphi 12.3·firedac
Gracker9 小时前
Android Weekly #202520
android
weixin_4111918410 小时前
原生安卓与flutter混编的实现
android·flutter
呼啸长风10 小时前
记一次未成功的 MMKV Pull Request
android·ios·开源
小墙程序员12 小时前
Android 性能优化(六)使用 Callstacks Sample 和 Java/Kotlin Method Recording 分析方法的耗时
android·性能优化·android studio