安卓11菜单实现hdmi-4K动态切换

客户要求系统实现动态hdmi-4K与普通分辨率直接热切换,先在菜单中做个试验,设置中加个切换开关,点击开关就可以直接切到hdmi-4K,这个功能实现后可以通过插拔hdmi那个状态(sys/class/drm/card0-HDMI-A-1/status)直接实现切换,测试代码也很简单,方法如下:

bash 复制代码
Index: packages/apps/Settings/src/com/android/settings/display/HdmiSettings.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/display/HdmiSettings.java	(revision 907)
+++ packages/apps/Settings/src/com/android/settings/display/HdmiSettings.java	(working copy)
@@ -27,6 +27,7 @@
 import androidx.preference.SwitchPreference;
 import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.HdmiListPreference;
@@ -39,6 +40,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import android.os.SystemProperties;
+import java.io.*;
 
 import static com.android.settings.display.DrmDisplaySetting.DPY_STATUS_CONNECTED;
 
@@ -53,6 +56,7 @@
     private static final String KEY_PRE_CATE = "Display";
     private static final String KEY_PRE_RESOLUTION = "Resolution";
     private static final String KEY_PRE_SCREEN_SCALE = "ScreenScale";
+    private static final String KEY_PRE_HDMI_4K = "HDMIswitch4K";
     private static final String KEY_AUX_CATEGORY = "aux_category";
     private static final String KEY_AUX_SCREEN_VH = "aux_screen_vh";
     private static final String KEY_AUX_SCREEN_VH_LIST = "aux_screen_vhlist";
@@ -87,6 +91,7 @@
     private PreferenceCategory mAuxCategory;
     private CheckBoxPreference mAuxScreenVH;
     private ListPreference mAuxScreenVHList;
+    private SwitchPreference hdmi4kPreference;
     private Context mContext;
     private DisplayInfo mSelectDisplayInfo;
     private DisplayManager mDisplayManager;
@@ -114,6 +119,10 @@
             if (mDestory && MSG_SWITCH_DEVICE_STATUS != msg.what) {
                 return;
             }
+			
+			Log.d(TAG, "fan msg.what ="+msg.what);
+			
+			Log.d(TAG, "fan  switch control ="+(ITEM_CONTROL) msg.obj);
             if (MSG_UPDATE_STATUS == msg.what) {
                 final ITEM_CONTROL control = (ITEM_CONTROL) msg.obj;
                 new Thread() {
@@ -177,6 +186,7 @@
                                 if (SWITCH_STATUS_OFF_ON == msg.arg1) {
                                     sendSwitchDeviceOffOnMsg(control, SWITCH_STATUS_ON);
                                 } else {
+                                    Log.d(TAG, "fan control ="+control);
                                     sendUpdateStateMsg(control, 2000);
                                 }
                             }
@@ -379,6 +389,7 @@
             scalePreference.setOnPreferenceClickListener(this);
             category.addPreference(scalePreference);
             category.setEnabled(false);
+			
             DisplayInfo displayInfo = new DisplayInfo();
             displayInfo.setDisplayNo(display);
             mDisplayInfoList.put(display, displayInfo);
@@ -394,6 +405,13 @@
         mAuxScreenVHList.setOnPreferenceChangeListener(this);
         mAuxScreenVHList.setOnPreferenceClickListener(this);
         mAuxCategory.removePreference(mAuxScreenVHList);
+	   //add hdmi4kPreference 
+
+ 	   hdmi4kPreference = (SwitchPreference) findPreference(KEY_PRE_HDMI_4K);
+	   hdmi4kPreference.setChecked(SystemProperties.getBoolean("persist.sys.hdmi4k", false));
+	   hdmi4kPreference.setOnPreferenceChangeListener(this);
+
+		
     }
 
     private void sendSwitchDeviceOffOnMsg(ITEM_CONTROL control, int status) {
@@ -583,6 +601,7 @@
                 cate.setEnabled(false);
             }
         } else if (key.startsWith(KEY_PRE_RESOLUTION)) {
+			Log.i("fan","key.replace="+key.replace(KEY_PRE_SCREEN_SCALE, ""));
             for (Map.Entry<Integer, DisplayInfo> entry : mDisplayInfoList.entrySet()) {
                 int display = Integer.parseInt(key.replace(KEY_PRE_RESOLUTION, ""));
                 if (display == entry.getKey()) {
@@ -597,6 +616,23 @@
         }
         return true;
     }
+	 public static	void exe_cmd(String cmd){
+	
+	 Process process2 = null;
+	 DataOutputStream dos = null;
+	 try{
+	 process2 = Runtime.getRuntime().exec("su");
+	 dos = new DataOutputStream(process2.getOutputStream());
+	 dos.writeBytes(cmd);
+	 dos.flush();
+	 dos.close();
+	 }
+	 catch (Exception e) { 
+		 
+			e.printStackTrace(); 
+	}  
+	
+	 }
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object obj) {
@@ -662,6 +698,30 @@
             //mDisplayManager.forceScheduleTraversalLocked();
             sendSwitchDeviceOffOnMsg(ITEM_CONTROL.REFRESH_DISPLAY_STATUS_INFO, SWITCH_STATUS_OFF_ON);
         }
+		
+		else if(preference == hdmi4kPreference){
+ 			 for (Map.Entry<Integer, DisplayInfo> entry : mDisplayInfoList.entrySet()) {
+ 	           // int display = Integer.parseInt(key.replace(KEY_PRE_RESOLUTION, ""));
+ 	            if (0 == entry.getKey()) {
+ 	                mSelectDisplayInfo = entry.getValue();
+ 	            }
+ 	        }
+	          if (obj == Boolean.TRUE) 
+			  {
+				Log.d(TAG, "hdmi 4k is select") ;
+				exe_cmd("setprop persist.vendor.resolution.aux 3840x2160@60");
+			 // exe_cmd("setprop persist.vendor.resolution.aux 1920x1080@60");
+			  exe_cmd("setprop vendor.display.timeline 1");
+			   exe_cmd("setprop persist.sys.hdmi4k true");
+			  }
+	  	  	  else{
+	  
+			 	 Log.d(TAG, "hdmi 4k is close" );
+			   exe_cmd("setprop persist.vendor.resolution.aux 720x480@59.94-736-798-858-489-495-525-a");
+			   exe_cmd("setprop vendor.display.timeline 1");
+			   exe_cmd("setprop persist.sys.hdmi4k false");
+			  }
+			}
         return true;
     }
 
相关推荐
雨白2 小时前
Android 快捷方式实战指南:静态、动态与固定快捷方式详解
android
hqk3 小时前
鸿蒙项目实战:手把手带你实现 WanAndroid 布局与交互
android·前端·harmonyos
LING3 小时前
RN容器启动优化实践
android·react native
恋猫de小郭6 小时前
Flutter 发布官方 Skills ,Flutter 在 AI 领域再添一助力
android·前端·flutter
Kapaseker11 小时前
一杯美式搞懂 Any、Unit、Nothing
android·kotlin
黄林晴11 小时前
你的 Android App 还没接 AI?Gemini API 接入全攻略
android
恋猫de小郭21 小时前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
冬奇Lab1 天前
PowerManagerService(上):电源状态与WakeLock管理
android·源码阅读
BoomHe1 天前
Now in Android 架构模式全面分析
android·android jetpack
二流小码农1 天前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos