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(),
相关推荐
阿巴斯甜16 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker17 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952718 小时前
Andorid Google 登录接入文档
android
黄林晴19 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android