安卓11 SysteUI添加按钮以及下拉状态栏的色温调节按钮

最近客户想要做一个台灯产品,需要实现 串口调节台灯功能 ,其中包括

亮度调节

色温调节

开关

三个功能

话不多说,贴代码

java 复制代码
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
old mode 100644
new mode 100755
index 17f92ee..71fefa0
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -790,5 +790,8 @@
             </intent-filter>
         </receiver>
 
+        <service android:name=".junjie.MySerialPortService"
+            android:exported="true"/>
+
     </application>
 </manifest>
diff --git a/packages/SystemUI/res/drawable/ic_qs_td_button.xml b/packages/SystemUI/res/drawable/ic_qs_td_button.xml
new file mode 100755
index 0000000..8a81bc2
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_qs_td_button.xml
@@ -0,0 +1,18 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"^M
+    android:width="108dp"^M
+    android:height="108dp"^M
+    android:viewportWidth="1024"^M
+    android:viewportHeight="1024">^M
+  <path^M
+      android:fillColor="#FFFFFFFF"^M
+      android:pathData="M4.3,603.5a1.7,1.7 0,0 0,0 -3.4,1.7 1.7,0 0,0 0,3.4zM904.9,560.7a19.9,19.9 0,0 1,-14.9 -6.7l-315.2,-356.5a19.9,19.9 0,1 1,29.9 -26.4l315.2,356.5a19.9,19.9 0,0 1,-14.9 33.1zM684.2,882.9a19.9,19.9 0,0 1,-14.3 -33.8l216.2,-222a19.9,19.9 0,0 1,28.5 27.8l-216.2,222a19.9,19.9 0,0 1,-14.3 6z"/>^M
+  <path^M
+      android:fillColor="#FFFFFFFF"^M
+      android:pathData="M545.9,235.2a19.9,19.9 0,0 1,-13.7 -34.4l60.3,-56.7a60.3,60.3 0,0 0,-82.5 -87.8l-60.3,56.7a19.9,19.9 0,0 1,-27.3 -29l60.3,-56.7a100.1,100.1 0,0 1,137.2 145.9l-60.3,56.7a19.9,19.9 0,0 1,-13.7 5.4z"/>^M
+  <path^M
+      android:fillColor="#FFFFFFFF"^M
+      android:pathData="M482.2,478.3h-0.6a19.9,19.9 0,0 1,-13.9 -6.3L174.9,160.4a19.9,19.9 0,0 1,0.9 -28.2,233.7 233.7,0 0,1 320.1,340.6 19.9,19.9 0,0 1,-13.6 5.4zM218.3,148.4l264.1,281.1a193.9,193.9 0,0 0,-264.1 -281.1zM1001.5,1024h-725.1a19.9,19.9 0,0 1,-19.9 -19.9c0,-88.8 72.3,-161.1 161.1,-161.1h442.9c88.8,0 161.1,72.3 161.1,161.1a19.9,19.9 0,0 1,-19.9 19.9zM297.8,984.2h682a121.8,121.8 0,0 0,-119.5 -101.3h-442.9a121.8,121.8 0,0 0,-119.5 101.3zM926.9,666.5a72.3,72.3 0,1 1,72.3 -72.3,72.4 72.4,0 0,1 -72.3,72.3zM926.9,561.8a32.5,32.5 0,1 0,32.5 32.5,32.5 32.5,0 0,0 -32.5,-32.5z"/>^M
+  <path^M
+      android:fillColor="#FFFFFFFF"^M
+      android:pathData="M321.7,428.5a101.1,101.1 0,0 1,-52.5 -187.6,19.9 19.9,0 1,1 20.7,34.1 61.3,61.3 0,1 0,82.6 86.6,19.9 19.9,0 1,1 33,22.3 101.1,101.1 0,0 1,-83.9 44.6z"/>^M
+</vector>^M
diff --git a/packages/SystemUI/res/layout/brightness_mirror.xml b/packages/SystemUI/res/layout/brightness_mirror.xml
old mode 100644
new mode 100755
index e3440b5..288947e
--- a/packages/SystemUI/res/layout/brightness_mirror.xml
+++ b/packages/SystemUI/res/layout/brightness_mirror.xml
@@ -18,7 +18,7 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/brightness_mirror"
     android:layout_width="@dimen/qs_panel_width"
-    android:layout_height="@dimen/brightness_mirror_height"
+    android:layout_height="@dimen/brightness_mirror_height1"
     android:layout_gravity="@integer/notification_panel_layout_gravity"
     android:visibility="invisible">
     <FrameLayout
diff --git a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
old mode 100644
new mode 100755
index 12127f5..039f633
--- a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
+++ b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
@@ -17,17 +17,69 @@
     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:orientation="vertical"
     style="@style/BrightnessDialogContainer">
 
+
     <com.android.systemui.settings.ToggleSliderView
         android:id="@+id/brightness_slider"
-        android:layout_width="0dp"
-        android:layout_height="48dp"
-        android:layout_gravity="center_vertical"
-        android:layout_weight="1"
+        android:layout_width="match_parent"
+        android:layout_height="40dp"
         android:contentDescription="@string/accessibility_brightness"
         android:importantForAccessibility="no"
-        systemui:text="@string/status_bar_settings_auto_brightness_label" />
+        systemui:text="@string/status_bar_settings_auto_brightness_label"
+        android:visibility="gone"/>
+    <!--liujunjie add -->
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="40dp"
+        android:orientation="horizontal">
+        <SeekBar
+            android:layout_width="0dp"
+            android:layout_weight="5"
+            android:layout_height="40dp"
+            android:id="@+id/seekbar3" />
+        <TextView
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:text="@string/str1"
+            android:gravity="center"/>
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="40dp"
+        android:orientation="horizontal">
+        <SeekBar
+            android:layout_width="0dp"
+            android:layout_weight="5"
+            android:layout_height="40dp"
+            android:id="@+id/seekbar1" />
+        <TextView
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:text="@string/str2"
+            android:gravity="center"/>
+    </LinearLayout>
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="40dp"
+        android:orientation="horizontal">
+        <SeekBar
+            android:layout_width="0dp"
+            android:layout_weight="5"
+            android:layout_height="40dp"
+            android:id="@+id/seekbar2" />
+        <TextView
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:text="@string/str3"
+            android:gravity="center"/>
+    </LinearLayout>
+    <!--liujunjie add end -->
+
 
 </LinearLayout>
diff --git a/packages/SystemUI/res/values-zh-rCN/strings.xml b/packages/SystemUI/res/values-zh-rCN/strings.xml
index a60b9d0..6a864e08 100755
--- a/packages/SystemUI/res/values-zh-rCN/strings.xml
+++ b/packages/SystemUI/res/values-zh-rCN/strings.xml
@@ -20,6 +20,10 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <string name="app_label" msgid="4811759950673118541">"系统界面"</string>
+    <string name="str1" msgid="4811759950673118544">"屏幕亮度"</string>
+    <string name="str2" msgid="4811759950673118545">"台灯亮度"</string>
+    <string name="str3" msgid="4811759950673118547">"台灯色温"</string>
+    <string name="quick_settings_dtbutton_unlocked_label" msgid="4811759950673118542">"台灯"</string>
     <string name="status_bar_clear_all_button" msgid="2491321682873657397">"清除"</string>
     <string name="status_bar_no_notifications_title" msgid="7812479124981107507">"无通知"</string>
     <string name="status_bar_ongoing_events_title" msgid="3986169317496615446">"正在进行的"</string>
diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml
old mode 100644
new mode 100755
index fcd47d4..6a32d03
--- a/packages/SystemUI/res/values/config.xml
+++ b/packages/SystemUI/res/values/config.xml
@@ -106,7 +106,7 @@
 
     <!-- The default tiles to display in QuickSettings -->
     <string name="quick_settings_tiles_default" translatable="false">
-        wifi,bt,dnd,flashlight,rotation,battery,cell,airplane,cast,screenrecord
+        wifi,bt,tdbutton,airplane
     </string>
 
     <!-- The minimum number of tiles to display in QuickSettings -->
@@ -114,7 +114,7 @@
 
     <!-- Tiles native to System UI. Order should match "quick_settings_tiles_default" -->
     <string name="quick_settings_tiles_stock" translatable="false">
-        wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,dark,work,cast,night,screenrecord,reverse
+        wifi,cell,tdbutton,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,dark,work,cast,night,screenrecord,reverse
     </string>
 
     <!-- The tiles to display in QuickSettings -->
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
old mode 100644
new mode 100755
index f002a27..ae0fcca
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -426,6 +426,7 @@
     <dimen name="notification_panel_width">@dimen/match_parent</dimen>
 
     <dimen name="brightness_mirror_height">48dp</dimen>
+    <dimen name="brightness_mirror_height1">80dp</dimen>
 
     <!-- The width of the panel that holds the quick settings. -->
     <dimen name="qs_panel_width">@dimen/notification_panel_width</dimen>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index 7f2fbba..e65bdb2 100755
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -19,6 +19,11 @@
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Name of the status bar as seen in the applications info settings page. [CHAR LIMIT=12] -->
     <string name="app_label">System UI</string>
+    <string name="str1" >Screen brightness</string>
+    <string name="str2" >Lamp brightness</string>
+    <string name="str3" >Lamp color temperature</string>
+    <!--台灯-->
+    <string name="quick_settings_dtbutton_unlocked_label">DeskLamp</string>
 
     <!-- The text for the button in the notification window-shade that clears
          all of the currently visible notifications. [CHAR LIMIT=10]-->
diff --git a/packages/SystemUI/src/com/android/systemui/junjie/MySerialPortService.java b/packages/SystemUI/src/com/android/systemui/junjie/MySerialPortService.java
new file mode 100755
index 0000000..0df1efb
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/junjie/MySerialPortService.java
@@ -0,0 +1,204 @@
+package com.android.systemui.junjie;
+
+import android.app.Service;
+import android.content.Intent;
+import android.hardware.SerialPort;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class MySerialPortService extends Service {
+
+    public static final String port = "/dev/ttyS9";
+    private SerialPort serialPort;
+    private ParcelFileDescriptor parcelFileDescriptor;
+    private ByteBuffer dates = ByteBuffer.allocate(10);
+    public static final String TAG = "MySerialPortService";
+    private FileOutputStream mFileOutputStream;
+    private final IBinder mBinder = new MyBinder();
+
+    public void setStatusChange(StatusChange statusChange) {
+        this.statusChange = statusChange;
+    }
+
+    private StatusChange statusChange;
+
+    public MySerialPortService() {
+
+    }
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        Log.d(TAG, "启动service");
+        thread.start();
+
+    }
+
+    public byte[] getRealData(byte[] src){
+        byte[] res = new byte[src.length+1];
+        byte checkSum = 0x0 & 0xff;
+        for (int i = 0; i < src.length; i++) {
+            byte ui = src[i];
+            checkSum +=(ui&0xff);
+            res[i] = src[i];
+        }
+        checkSum = (byte) (checkSum%0x100);
+        res[src.length] = checkSum;
+        Log.d(TAG, "getRealData: checkSun" +checkSum);
+        return res;
+    }
+
+    /**
+     * 打开灯
+     * */
+    public void setPortOff() {
+        Log.d(TAG, "onReceive: 串口关闭");
+        byte[] setui = new byte[]{0x55,(byte) 0xaa,0x1,0x3,0x1,0x0} ;
+        Log.d(TAG, "setPortOff==" + mFileOutputStream);
+        setCommd(setui);
+
+    }
+    /**
+     * 关闭灯
+     * */
+    public void setPortON() {
+        byte[] setui = new byte[]{0x55,(byte) 0xaa,0x1,0x3,0x1,0x1};
+               Log.d(TAG, "setPortON==" + mFileOutputStream);
+        setCommd(setui);
+    }
+    /**
+     * 配置色温
+     */
+    public void  setColorTemperature(int value){
+        //setPortON();
+        byte[] setui = new byte[]{0x55,(byte) 0xaa,0x1,0x5,0x1, (byte) (value&0xff)};
+        setCommd(setui);
+        Log.d(TAG, "setColorTemperature: "+value);
+
+
+    }
+
+    /**
+     * 配置亮度
+     */
+    public void  setBrightness(int value){
+        //setPortON();
+        byte[] setui = new byte[]{0x55,(byte) 0xaa,0x1,0x4,0x1, (byte) (value&0xff)};
+        setCommd(setui);
+        Log.d(TAG, "setColorTemperature: "+value);
+       // queryState();
+    }
+
+    /**
+     * 查询状态
+     * */
+    public void queryState(){
+        byte[] setui = new byte[]{0x55,(byte) 0xaa,0x1,0x1,0x0} ;
+        setCommd(setui);
+
+
+    }
+
+    private void setCommd(byte[] su){
+        try {
+            if (mFileOutputStream != null) {
+                mFileOutputStream.write(getRealData(su));
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+
+
+
+    private final Thread thread = new Thread(new Runnable() {
+        @Override
+        public void run() {
+            serialPort = new SerialPort(port);
+            try {
+                parcelFileDescriptor = ParcelFileDescriptor.open(new File(port), ParcelFileDescriptor.MODE_READ_WRITE);
+                serialPort.open(parcelFileDescriptor, 9600);
+                mFileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
+                Log.d(TAG,"main Thread start "+mFileOutputStream);
+                writeThread.start();
+                while (true) {
+                    Log.d(TAG, "run: 读取线程阻塞:");
+                    int read = serialPort.read(dates);
+                    readDate();
+                }
+
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+
+        }
+    });
+
+    private final Thread writeThread = new Thread(new Runnable() {
+        @Override
+        public void run() {
+            Log.d(TAG,"writeThread start "+mFileOutputStream);
+            if (mFileOutputStream != null){
+                //setColorTemperature(10);
+                //queryState();
+            }
+        }
+    });
+
+    private void readDate() {
+        byte[] by = dates.array();
+        /*for (byte b:
+             by) {
+            Log.d(TAG, "readDate: sts:"+b);
+        }*/
+        if (statusChange != null && by[3] == 0x2){
+            Log.d(TAG, "readDate: 读取数据 命令字段:"+by[3]+", 数据长度:"+by[4] +"开关状态:"+by[5]+" ,亮度值:"+by[6]+" ,色温值 :"+by[7]);
+            statusChange.dateSet(by[5]==1,by[6],by[7]);
+        }
+    }
+
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        // TODO: Return the communication channel to the service.
+        return mBinder;
+    }
+    public class MyBinder extends Binder {
+        public MySerialPortService getService() {
+            return MySerialPortService.this;
+        }
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        try {
+            mFileOutputStream.close();
+            serialPort.close();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+
+        return super.onStartCommand(intent, START_STICKY, startId);
+    }
+
+    public interface StatusChange{
+        void dateSet(boolean status,int br,int color);
+    }
+
+
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index a87a13c..3ce1e82 100755
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -46,6 +46,7 @@ import com.android.systemui.Dumpable;
 import com.android.systemui.R;
 import com.android.systemui.broadcast.BroadcastDispatcher;
 import com.android.systemui.dump.DumpManager;
+import com.android.systemui.junjie.MySerialPortService;
 import com.android.systemui.media.MediaHierarchyManager;
 import com.android.systemui.media.MediaHost;
 import com.android.systemui.plugins.qs.DetailAdapter;
@@ -63,6 +64,7 @@ import com.android.systemui.tuner.TunerService;
 import com.android.systemui.tuner.TunerService.Tunable;
 import com.android.systemui.util.animation.DisappearParameters;
 
+
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -82,6 +84,14 @@ import android.content.IntentFilter;
 import android.content.BroadcastReceiver;
 import android.util.Log;
 
+import android.widget.Button;
+import android.widget.SeekBar;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.database.ContentObserver;
+import android.provider.Settings;
+
+
 /** View that represents the quick settings tile panel (when expanded/pulled down). **/
 public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener,
         Dumpable {
@@ -160,6 +170,205 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne
     private int mMediaTotalBottomMargin;
     private int mFooterMarginStartHorizontal;
     private Consumer<Boolean> mMediaVisibilityChangedListener;
+    // liujunjie add
+
+    private static MySerialPortService mySerialPortService;
+    private SeekBar seekBar1,seekBar2,seekBar3;
+    private static boolean isOn;
+    private int br;
+    private int color;
+    public static StatusChange statusChange2;
+
+    private final  MySerialPortService.StatusChange statusChange = new MySerialPortService.StatusChange() {
+        @Override
+        public void dateSet(boolean status, int br1, int color1) {
+
+            br = br1;
+            color = color1;
+            seekBar1.setProgress(br1);
+            seekBar2.setProgress(color1);
+            if (isOn != status){
+                statusChange2.statusChange(status);
+            }
+            isOn = status;
+            //updateSeekStatus();
+
+
+        }
+    };
+    private void updateSeekStatus(){
+        if (isOn){
+            if (seekBar1.getVisibility() != View.VISIBLE){
+                seekBar1.setVisibility(View.VISIBLE);
+                seekBar2.setVisibility(View.VISIBLE);
+            }
+
+        }else {
+            if (seekBar1.getVisibility() != View.GONE){
+                seekBar1.setVisibility(View.GONE);
+                seekBar2.setVisibility(View.GONE);
+            }
+
+            }
+
+
+    }
+
+    public static void setStatusChange(QSPanel.StatusChange statusChange) {
+        statusChange2 = statusChange;
+    }
+
+    public interface StatusChange {
+        void statusChange(boolean status);
+    }
+
+    class BrightnessObserver extends ContentObserver {
+        private Context mContext;
+
+        public BrightnessObserver(Context context) {
+            super(null);
+            mContext = context;
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            super.onChange(selfChange);
+            if (!isMeTouch){
+
+                int brightness = Settings.System.getInt(
+                        mContext.getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS,
+                        0
+                );
+                handleBrightnessChange(brightness);
+
+            }
+
+
+
+        }
+        private void handleBrightnessChange(int brightness) {
+            int st = Math.round((float) brightness /255 * 100);
+            if (seekBar3 != null){
+                seekBar3.setProgress(st);
+            }
+        }
+    }
+    boolean isMeTouch = false;
+    private void junjieInit() {
+        Intent intent = new Intent(mContext, MySerialPortService.class);
+        mContext.bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
+        seekBar1 = mBrightnessView.findViewById(R.id.seekbar1);
+        seekBar2 = mBrightnessView.findViewById(R.id.seekbar2);
+        seekBar3 = mBrightnessView.findViewById(R.id.seekbar3); // 屏幕亮度
+        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                if (mySerialPortService != null && fromUser  && progress != 0){
+                    mySerialPortService.setBrightness(progress);
+                }
+
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {
+
+            }
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {
+
+            }
+        });
+        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                if (mySerialPortService != null && fromUser){
+                    mySerialPortService.setColorTemperature(progress);
+                }
+
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {
+
+            }
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {
+
+            }
+        });
+
+        seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                if (isMeTouch){
+                    int st = Math.round((float) progress /100 * 255);
+                    Settings.System.putInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,st);
+                }
+
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {
+                isMeTouch = true;
+
+            }
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {
+                isMeTouch = false;
+
+            }
+        });
+        initStatus();
+        int sy = Settings.System.getInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,0);
+        seekBar3.setProgress(sy);
+    }
+
+    private void initStatus() {
+        BrightnessObserver brightnessObserver = new BrightnessObserver(getContext());
+        getContext().getContentResolver().registerContentObserver(
+                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
+                true,
+                brightnessObserver
+        );
+
+    }
+
+    public static void liujunjieClick() {
+        if (mySerialPortService != null){
+            if (isOn){
+                mySerialPortService.setPortOff();
+
+            }else {
+                mySerialPortService.setPortON();
+            }
+            isOn = !isOn;
+        }
+    }
+
+    public static boolean getDTStatus() {
+       return isOn;
+    }
+
+    private final ServiceConnection mConnection = new ServiceConnection() {
+        @Override
+        public void onServiceConnected(ComponentName name, IBinder service) {
+            MySerialPortService.MyBinder binder = (MySerialPortService.MyBinder) service;
+            mySerialPortService = binder.getService();
+            mySerialPortService.setStatusChange(statusChange);
+            mySerialPortService.queryState();
+
+        }
+        @Override
+        public void onServiceDisconnected(ComponentName name) {
+            mySerialPortService = null;
+
+        }
+    };
+
        
 
 
@@ -234,6 +443,8 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne
             addView(mHorizontalLinearLayout, lp);
 
             initMediaHostState();
+            //junjieInit(); // liujunjie add
+            android.util.Log.d("liujunjie","========== 1111 junjieInit  ========");
         }
         addSecurityFooter();
         if (mRegularTileLayout instanceof PagedTileLayout) {
@@ -261,6 +472,7 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne
         addView(mBrightnessView);
         mBrightnessController = new BrightnessController(getContext(),
                 findViewById(R.id.brightness_slider), mBroadcastDispatcher);
+        junjieInit();
     }
 
     protected QSTileLayout createRegularTileLayout() {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java
index f9bd43d..e235fb7 100755
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java
@@ -41,6 +41,7 @@ import com.android.systemui.qs.tiles.NfcTile;
 import com.android.systemui.qs.tiles.NightDisplayTile;
 import com.android.systemui.qs.tiles.RotationLockTile;
 import com.android.systemui.qs.tiles.ScreenRecordTile;
+import com.android.systemui.qs.tiles.TDButtonTile;
 import com.android.systemui.qs.tiles.UiModeNightTile;
 import com.android.systemui.qs.tiles.UserTile;
 import com.android.systemui.qs.tiles.WifiTile;
@@ -83,6 +84,8 @@ public class QSFactoryImpl implements QSFactory {
 
     private final Lazy<QSHost> mQsHostLazy;
 
+    private final Provider<TDButtonTile> mTDButtonTileProvider;
+
     @Inject
     public QSFactoryImpl(Lazy<QSHost> qsHostLazy,
             Provider<WifiTile> wifiTileProvider,
@@ -105,7 +108,9 @@ public class QSFactoryImpl implements QSFactory {
             Provider<GarbageMonitor.MemoryTile> memoryTileProvider,
             Provider<UiModeNightTile> uiModeNightTileProvider,
             Provider<ScreenRecordTile> screenRecordTileProvider,
-                       Provider<EyeHealthTile> eyeHealthTileProvider) {
+                       Provider<EyeHealthTile> eyeHealthTileProvider,
+                       Provider<TDButtonTile> tDButtonTileProvider
+            ) {
         mQsHostLazy = qsHostLazy;
         mWifiTileProvider = wifiTileProvider;
         mBluetoothTileProvider = bluetoothTileProvider;
@@ -128,6 +133,7 @@ public class QSFactoryImpl implements QSFactory {
         mUiModeNightTileProvider = uiModeNightTileProvider;
         mScreenRecordTileProvider = screenRecordTileProvider;
                mEyeHealthTileProvider=eyeHealthTileProvider;
+        mTDButtonTileProvider = tDButtonTileProvider;
     }
 
     public QSTile createTile(String tileSpec) {
@@ -180,6 +186,8 @@ public class QSFactoryImpl implements QSFactory {
                 return mUiModeNightTileProvider.get();
             case "screenrecord":
                 return mScreenRecordTileProvider.get();
+            case "tdbutton":
+                return mTDButtonTileProvider.get();
                
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/TDButtonTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/TDButtonTile.java
new file mode 100755
index 0000000..76581e3
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/TDButtonTile.java
@@ -0,0 +1,104 @@
+/*
+ * 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.systemui.qs.tiles;
+
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.provider.Settings;
+import android.service.quicksettings.Tile;
+import android.widget.Switch;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.systemui.R;
+import com.android.systemui.plugins.qs.QSTile.BooleanState;
+import com.android.systemui.qs.QSHost;
+import com.android.systemui.qs.QSPanel;
+import com.android.systemui.qs.tileimpl.QSTileImpl;
+
+import javax.inject.Inject;
+
+/** Quick settings tile: Rotation **/
+public class TDButtonTile extends QSTileImpl<BooleanState> {
+
+    private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_td_button);
+
+    @Inject
+    public TDButtonTile(QSHost host) {
+        super(host);
+        QSPanel.setStatusChange(new QSPanel.StatusChange(){
+            @Override
+            public void statusChange(boolean status){
+
+                refreshState();
+
+            }
+
+        });
+    }
+
+    @Override
+    public BooleanState newTileState() {
+        return new BooleanState();
+    }
+
+
+    @Override
+    protected void handleClick() {
+        final boolean newState = !mState.value;
+        QSPanel.liujunjieClick();
+        refreshState(newState);
+    }
+
+
+    @Override
+    public CharSequence getTileLabel() {
+        return getState().label;
+    }
+
+    @Override
+    protected void handleUpdateState(BooleanState state, Object arg) {
+
+        state.value = QSPanel.getDTStatus();
+        state.label = mContext.getString(R.string.quick_settings_dtbutton_unlocked_label);
+        state.icon = mIcon;
+        //tate.contentDescription = getAccessibilityString(rotationLocked);
+        //state.expandedAccessibilityClassName = Switch.class.getName();
+        state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return 0;
+    }
+
+
+    @Override
+    protected void handleSetListening(boolean listening) {
+
+
+    }
+
+    @Override
+    public Intent getLongClickIntent() {
+        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
+        homeIntent.addCategory(Intent.CATEGORY_HOME);xieuy
+        homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        return homeIntent;
+    }
+
+}

协议链接

bash 复制代码
链接:https://pan.baidu.com/s/1kOmVpb5fEKO9FAkN4JsnhA?pwd=s56b 
提取码:s56b 
相关推荐
喻师傅14 分钟前
SQL—替换字符串—replace函数用法详解
android·数据库·sql
zhangjiaofa28 分钟前
深入理解 Android 中的 ConfigurationInfo
android
knighthood20011 小时前
xml格式化(1):使用python的xml库实现自闭合标签
android·xml·python
lichong9511 小时前
【Flutter&Dart】 拖动边界线改变列宽并且有边界高亮和鼠标效果(12 /100)
android·flutter·计算机外设·api·postman·smartapi·postapi
lichong9512 小时前
【Flutter&Dart】 拖动边界线改变列宽类似 vscode 那种拖动改变编辑框窗口大小(11 /100)
android·ide·vscode·flutter·api·postman·smartapi
狗狗显卡4 小时前
PHP进阶-php文件读取与连接数据库基础部分
android·数据库·php
AirDroid_cn4 小时前
手机投屏到电视的3种选择:无线本地投屏,无线远程投屏,AirPlay投屏
android·智能手机·手机投屏·无线投屏·投屏电视
beefreesky4 小时前
安卓漏洞学习(十八):Android加固基本原理
android·学习
牛晓伟已占用4 小时前
一文彻底搞懂Android广播的所有知识--四大组件系统
android
林鸿群5 小时前
Android配件应用默认启动与USB权限申请区别
android