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

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

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;
     }
 
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现 Android 11 监测两路 HDMI 输出信号的代码如下: ``` private final BroadcastReceiver mHdmi1Receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_HDMI_PLUGGED)) { int state = intent.getIntExtra(HdmiManager.EXTRA_HDMI_PLUG_STATE, -1); if (state == HdmiManager.HDMI_PLUGGED) { // HDMI 1 is plugged in } else if (state == HdmiManager.HDMI_UNPLUGGED) { // HDMI 1 is unplugged } } } }; private final BroadcastReceiver mHdmi2Receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_HDMI_PLUGGED)) { int state = intent.getIntExtra(HdmiManager.EXTRA_HDMI_PLUG_STATE, -1); if (state == HdmiManager.HDMI_PLUGGED) { // HDMI 2 is plugged in } else if (state == HdmiManager.HDMI_UNPLUGGED) { // HDMI 2 is unplugged } } } }; private void registerHdmiReceivers() { IntentFilter hdmi1Filter = new IntentFilter(Intent.ACTION_HDMI_PLUGGED); IntentFilter hdmi2Filter = new IntentFilter(Intent.ACTION_HDMI_PLUGGED); hdmi1Filter.addAction(Intent.ACTION_HDMI_PLUGGED); hdmi2Filter.addAction(Intent.ACTION_HDMI_PLUGGED); hdmi1Filter.addCategory(Intent.CATEGORY_DEFAULT); hdmi2Filter.addCategory(Intent.CATEGORY_DEFAULT); hdmi1Filter.addDataScheme("hdmi"); hdmi2Filter.addDataScheme("hdmi"); registerReceiver(mHdmi1Receiver, hdmi1Filter); registerReceiver(mHdmi2Receiver, hdmi2Filter); } ``` 以上代码实现了注册两个 BroadcastReceiver 监听 HDMI 1 和 HDMI 2 的插拔事件,当 HDMI 1 或 HDMI 2 被插入或拔出时,会触发相应的回调函数,您可以在回调函数中执行相应的操作。请注意,此代码适用于 Android 11 及以上版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术求索者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值