一:如何添加快捷开关

源码路径:frameworks/base/packages/SystemUI/res/values/config.xml

添加headset快捷开关,参考如下修改。

Index: res/values/config.xml
===================================================================
--- res/values/config.xml (版本 6870)
+++ res/values/config.xml (工作副本)
@@ -101,7 +101,7 @@
<!-- The default tiles to display in QuickSettings -->
<!-- M: add hotknot tile -->
<string name="quick_settings_tiles_default" translatable="false">
- wifi,cell,battery,autobringht,custom(com.bullitt_group.night/nightsight.bullitt_group.com.night.quicksettings.QuickSettingsService),flashlight,dnd,rotation,bt,airplane,hotknot,nfc,location
+ wifi,cell,battery,autobringht,custom(com.bullitt_group.night/nightsight.bullitt_group.com.night.quicksettings.QuickSettingsService),flashlight,dnd,rotation,bt,airplane,hotknot,nfc,location,headset
</string> <!-- Tiles native to System UI. Order should match "quick_settings_tiles_default" -->

Android版本7.1修改方式

源码路径:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java

创建HeadsetTile 对象,实现快捷开关主要功能。

Index: src/com/android/systemui/statusbar/phone/QSTileHost.java
===================================================================
--- src/com/android/systemui/statusbar/phone/QSTileHost.java (版本 6870)
+++ src/com/android/systemui/statusbar/phone/QSTileHost.java (工作副本)
@@ -41,6 +41,7 @@
import com.android.systemui.qs.external.TileLifecycleManager;
import com.android.systemui.qs.external.TileServices;
import com.android.systemui.qs.tiles.AirplaneModeTile;
+import com.android.systemui.qs.tiles.HeadsetTile;
import com.android.systemui.qs.tiles.BatteryTile;
import com.android.systemui.qs.tiles.BluetoothTile;
import com.android.systemui.qs.tiles.CastTile;
@@ -501,6 +502,7 @@
else if (tileSpec.equals("battery")) return new BatteryTile(this);
else if (tileSpec.equals("saver")) return new DataSaverTile(this);
else if (tileSpec.equals("night")) return new NightDisplayTile(this);
+ else if (tileSpec.equals("headset")) return new HeadsetTile(this); /// M: Add extra tiles in quicksetting @{
else if (tileSpec.equals("hotknot") && SIMHelper.isMtkHotKnotSupport())

Android8.0修改方式,原理一样,只是代码路径不同,这里不做过多介绍。

源码路径:frameworks/base/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java

public class QSFactoryImpl implements QSFactory {

    private static final String TAG = "QSFactory";
private final QSTileHost mHost; public QSFactoryImpl(QSTileHost host) {
mHost = host;
} public QSTile createTile(String tileSpec) {
if (tileSpec.equals("wifi")) return new WifiTile(mHost);
else if (tileSpec.equals("bt")) return new BluetoothTile(mHost);
else if (tileSpec.equals("cell")) return new CellularTile(mHost);
else if (tileSpec.equals("dnd")) return new DndTile(mHost);
else if (tileSpec.equals("inversion")) return new ColorInversionTile(mHost);
else if (tileSpec.equals("airplane")) return new AirplaneModeTile(mHost);
else if (tileSpec.equals("work")) return new WorkModeTile(mHost);
else if (tileSpec.equals("rotation")) return new RotationLockTile(mHost);
else if (tileSpec.equals("flashlight")) return new FlashlightTile(mHost);
else if (tileSpec.equals("location")) return new LocationTile(mHost);
else if (tileSpec.equals("cast")) return new CastTile(mHost);
else if (tileSpec.equals("hotspot")) return new HotspotTile(mHost);
else if (tileSpec.equals("user")) return new UserTile(mHost);
else if (tileSpec.equals("battery")) return new BatterySaverTile(mHost);
else if (tileSpec.equals("saver")) return new DataSaverTile(mHost);
else if (tileSpec.equals("night")) return new NightDisplayTile(mHost);
else if (tileSpec.equals("nfc")) return new NfcTile(mHost);
// Intent tiles.
else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(mHost, tileSpec);
else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(mHost, tileSpec);
else {
Log.w(TAG, "Bad tile spec: " + tileSpec);
return null;
}
}
二:快捷开关功能实现

这里主要实现,打开开关弹出Notification(不可删除通知),关闭开关才能关闭通知。

源码路径:frameworks/base/packages/SystemUI/src/com/android/systemui/qs/tiles/HeadsetTile.java

  1. 继承 QSTile<QSTile.BooleanState>
  2. newTileState方法中return new BooleanState()
  3. handleClick方法处理点击事件
  4. handleUpdateState方法更新状态信息
/*
* Copyright (c) 2016, 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.systemui.qs.tiles; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import android.widget.Switch; import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.systemui.R;
import com.android.systemui.SystemUI;
import com.android.systemui.qs.QSTile; public class HeadsetTile extends QSTile<QSTile.BooleanState> { private Notification notification = new Notification();
private NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
private boolean headsetState; public boolean isHeadsetState() {
return headsetState;
} public void setHeadsetState(boolean headsetState) {
this.headsetState = headsetState;
} public HeadsetTile(Host host) {
super(host);
} @Override
public BooleanState newTileState() {
return new BooleanState();
} @Override
protected void handleClick() {
//default mState.value is false
final boolean activated = !mState.value;
Log.d("jasun", "========activated========" + activated);
MetricsLogger.action(mContext, getMetricsCategory(), activated);
if (activated == true) {
SendNotification("Disable the headphone jack.");
setHeadsetState(activated);
} else {
mNotificationManager.cancel(1);
setHeadsetState(activated);
}
refreshState();
} @Override
protected void handleUpdateState(BooleanState state, Object arg) {
final boolean isActivated = isHeadsetState();
state.value = isActivated;
state.label = mContext.getString(R.string.quick_settings_headset_label);
state.icon = ResourceIcon.get(isActivated ? R.drawable.ic_qs_headset_on
: R.drawable.ic_qs_headset_off);
state.contentDescription = mContext.getString(isActivated
? R.string.quick_settings_headset_summary_on
: R.string.quick_settings_headset_summary_off);
state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
= Switch.class.getName();
} @Override
public int getMetricsCategory() {
return MetricsEvent.QS_headset;
} @Override
public Intent getLongClickIntent() {
return new Intent(Settings.ACTION_headset_SETTINGS);
} @Override
protected void setListening(boolean listening) { } private void sendNotification(String message) {
Intent intent = new Intent(mContext, SystemUI.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
intent, 0);
notification.icon = R.drawable.ic_qs_headset_on;
notification.tickerText = "HeadSet can not used";
notification.when = System.currentTimeMillis();
notification.defaults = Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;// set default sound
// notification.flags = Notification.FLAG_AUTO_CANCEL;// click auto disappeared
notification.flags = Notification.FLAG_NO_CLEAR;
notification.setLatestEventInfo(mContext, "Headset can not used", message, pendingIntent); mNotificationManager.notify(1, notification); } @Override
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_headset_label);
}
}

喜欢源码分析系列可参考其他文章:

Android源码分析(一)-----如何快速掌握Android编译文件

Android源码分析(二)-----如何编译修改后的framework资源文件

Android源码分析(三)-----系统框架设计思想

Android源码分析(四)-----Android源码编译及刷机步骤

Android源码分析(五)-----如何从架构师的角度去设计Framework框架

Android源码分析(十三)----SystemUI下拉状态栏如何添加快捷开关的更多相关文章

  1. Android源码分析(十七)----init.rc文件添加脚本代码

    一:init.rc文件修改 开机后运行一次: chmod 777 /system/bin/bt_config.sh service bt_config /system/bin/bt_config.sh ...

  2. Android源码分析(八)-----系统启动流程&IPC简述

    一 :系统启动流程图 从下往上依次启动linux kernel -->zygote-->SystemServer-->NativeService-->AndroidServic ...

  3. Android源码分析-全面理解Context

    前言 Context在android中的作用不言而喻,当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context,而这个Context到底是什么呢,这个问题好像很好回答又好像 ...

  4. Android源码分析(十)-----关机菜单中如何添加飞行模式选项

    一:关机菜单添加飞行模式选项 源码路径:frameworks/base/core/res/res/values/config.xml 增加<item>airplane</item&g ...

  5. Android源码分析(二)-----如何编译修改后的framework资源文件

    一 : 编译framework资源文件 如果修改android framework资源文件,需要先编译资源文件,然后再编译framework才可以正常引用, 进入项目目录 cd work/source ...

  6. QTimer源码分析(以Windows下实现为例)

    QTimer源码分析(以Windows下实现为例) 分类: Qt2011-04-13 21:32 5026人阅读 评论(0) 收藏 举报 windowstimerqtoptimizationcallb ...

  7. Android源码分析(六)-----蓝牙Bluetooth源码目录分析

    一 :Bluetooth 的设置应用 packages\apps\Settings\src\com\android\settings\bluetooth* 蓝牙设置应用及设置参数,蓝牙状态,蓝牙设备等 ...

  8. Android源码分析(十六)----adb shell 命令进行OTA升级

    一: 进入shell命令界面 adb shell 二:创建目录/cache/recovery mkdir /cache/recovery 如果系统中已有此目录,则会提示已存在. 三: 修改文件夹权限 ...

  9. Android源码分析(十五)----GPS冷启动实现原理分析

    一:原理分析 主要sendExtraCommand方法中传递两个参数, 根据如下源码可以知道第一个参数传递delete_aiding_data,第二个参数传递null即可. @Override pub ...

随机推荐

  1. keil mdk+stm32的ac5和 ac6两个编译器下的字节对齐操作方法

    最近在使用ac6.9的编译器,编译速度是真的很快,使用stm32的hal库编译速度也比ac5的编译器快很多.本文试验stm32中字节对齐的代码测试,主要是结构体,因为结构体中实际项目中用到最多,同时在 ...

  2. Identity入门2:AuthenticationManager【转】

    在 上篇文章 中讲了关于 Identity 需要了解的单词以及相对应的几个知识点,并且知道了Identity处在整个登入流程中的位置,本篇主要是在 .NET 整个认证系统中比较重要的一个环节,就是 认 ...

  3. 两个开源的 Spring Boot + Vue 前后端分离项目

    折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...

  4. java基础JDK jvm path环境变量

    JDk=JRE +java的开发工具(javac.exe java.exe javadoc.exe)JRE =JVM +Java核心类库 2.为什么 要配置 path环境变量 ?如何配置?JAVA_H ...

  5. sed和awk练习及知识点

    一.针对/etc/passwd操作 1.sed操作,将文件中的第九行至第十五行复制到第16行下. [root@ns1 lianxi]# sed '9,15H;16G' /etc/passwd 2.用a ...

  6. POJ2718Smallest Difference(暴力全排列)

    传送门 题目大意:升序输入十进制数 没有重复 分成两个非空集合 每个集合组成一个数(不能有前导零) 求两个数差的最小值. 题解:全排列...我数组从1开始怎么一直WA...还有这个输入值得学习. 代码 ...

  7. Web数据交互技术

    作者 | Jeskson 来源 | 达达前端小酒馆 web的概念 web叫全球广域网,可以叫做万维网,是一种分布式结构,建立在Internet上的网络服务.万维网共享分布在网络上的各个服务器中的所有互 ...

  8. Redis数据结构及常用命令(草稿)

    通用命令 数据类型 string 字符 list 列表 set 集合 zset 有序集合 hash 散列(字典中的字典) bitmap 位图 hyperloglog

  9. ASP.NET MVC 下使用支付宝支付接口 以及 ASP.NET Core 下相关改造支付

    通过nuget首先引用AopSdk.dll 包 下面写的是 Asp.Net MVC 下相关的支付接口 APP支付 配置客户端相关的参数,配置成自己的代码就可以了 private string APPI ...

  10. jenkins pipeline 复杂的发布流程

    1.参数化构建界面 2.交付流水线界面 3.脚本详解 #!groovy pipeline { //在任何可用的代理上执行Pipeline agent any //参数化变量,目前只支持[boolean ...