首页 > 代码库 > RK平台Android4.4 添加一个新的遥控器支持以及添加特殊按键
RK平台Android4.4 添加一个新的遥控器支持以及添加特殊按键
瑞芯微平台
SDK:Android4.4
好久没写博客了,最近工作中需要在SDK中添加一个新的遥控器支持,由于自己对java代码比较头大,过程也是一波三折,整个流程其实分析下来并不难,这里做个简单的总结。也算是学习android的一个开端。
1. 遥控器红外键值到linux层的映射
安卓4.4后linux层和红外层的键值映射是在设备树中修改的,不需要在linux中修改驱动代码,直接在相应的dts文件中修改即可,首先每个遥控器都有一个usercode,按照瑞芯微方面提供的文档:
在终端中输入命令:echo 1 > sys/module/cmcc_pwm_remotectl/parameters/code_print
其中USERCODE=0xf601的遥控器用户码,用来标识是哪一个遥控器,RMC_GETDATA=http://www.mamicode.com/bb,这个是遥控器设备linux驱动中对应的按键上报的对应的键值。
文件目录\kernel\arch\arm\boot\dts\xxx.dts
在代码中先添加如下框架(文件里面照葫芦画瓢)(先把usercode这样加上,然后输入上面的命令才能记录相应按键DTS中对应的值):
我这上面是修改好的样子,0xbb和0xbc分别对应的是向上键和确定键。这些都是记录的键值,
<0xbcKEY_REPLY>, 其中0xbc在DTS文件中确定键对应的宏定义 KEY_REPLY这个要在input.h文件中去找
input.h文件路径:kernel\arch\arm\boot\dts\include\dt-bindings\input\input.h
DTS文件中每一按键的键值都可以通过打印来记录,每一个键值所对应的宏可以通过linux驱动层中input.h文件中对应的宏找到。名字一定要保持一致!其中上面确定键对应的232是linux层上报android层的键值。
就这样记录所有的按键,然后在DTS文件对号入座,第一步遥控器红外层到linux层的键值映射就搞定了
2. Linux层到android层的键值映射
同样通过终端输入命令:getevent 记录Linux层到android层的键值记录
对应的映射文件就是110b0030_pwm.kl 这个是可以在相关配置文件修改确定是哪个KL文件是linux层到android层键值映射,也可以修改为自己创建的KL文件。
文件目录:盒子根文件目录下的/system/usr/keylayout/110b0030_pwm.kl文件
这里232是确定键从Linux层上报到android层的键值,其中确定键对应的宏DPAD_CENTER 这个可以在keyevent.java文件中找到
一般的遥控器常规按键做到这两步了,基本都没问题了,但是特殊按键的添加还需要继续完善!
通常Linux驱动层基本不需要修改,第一步:我们只需要修改对应的KL文件即可。
已遥控器上的搜索按键为例:
由于DTS文件中:
<0xa7KEY_PROGRAM> a7是记录的搜索键的键值, 后边对应的宏是input.h文件中可以找到对应的,这个加进去就好了
KL文件中:
key 362 TV_SEARCH //362是getevent命令记录的搜索键的键值, TV_SEARCH是自定义的宏,这个名字自己可以随便定义。
第二个修改的地方是keycodes.h文件
文件目录:frameworks\native\include\android\keycodes.h
在文件中添加一个没有用到的键值
第三个修改的地方是KeycodeLables.h文件
文件目录:\frameworks\native\include\input\KeycodeLables.h
第四个修改attrs.xml文件
文件目录:\frameworks\base\core\res\res\values\arrrs.xml
第五个修改的文件:KeyEvent.java (这个文件修改的地方有两个)
如果添加的是最后一个别忘了LAST_KEYCODE 这个要和最后一个保持一致.下面还有一个地方也要添加:
至此特殊按键搜索按键从红键值外层到android层的键值映射就完成了。搜索按键到android应用层的键值映射就是257.
特殊按键应用层特殊处理可以修改PhoneWindowManager.java文件
文件目录:\frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java
特殊按键处理这次应用层的做法是修改PhoneWindowManager.java文件,同时在上面的目录中添加keyfunction.java文件。
package com.android.internal.policy.impl; import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Element; import org.w3c.dom.Node; import java.io.Serializable; import org.xml.sax.SAXException; import android.os.Looper; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ContextWrapper; import android.util.Log; import android.os.Message; import android.os.Messenger; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.UserHandle; import android.media.AudioService; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; //import com.hisilicon.android.HiDisplayManager; //import com.hisilicon.android.DispFmt; import android.widget.Toast; import android.view.KeyEvent; import android.os.SystemProperties; import android.content.res.Configuration; import android.app.ActivityManagerNative; import android.app.ActivityManager; import android.content.ComponentName; import android.content.pm.PackageInfo; //import android.view.VGAToast; public class Keyfunction { private static Keyfunction mInstance = null; private static final String KEYFUNTIONXLMNAME = "/system/etc/keyfunction.xml"; private static final int MSG_TO_SET_FORMAT = 111; private static Context mContext; // private HiDisplayManager mDisplayManager = null; private ToastUtil toast = null; private String mProduc_name = null; private final String TAG = "Keyfunction"; private boolean is4Cpu = true; private static ArrayList<Integer> settingFormatValueList = new ArrayList<Integer>(); private static ArrayList<String> settingFormatStringList = new ArrayList<String>(); private File avplay; private int[] threeD_ValueList = {19, 20, 21}; private String[] threeD_StringList = {"1080P 24Hz FramePacking", "720P 60Hz FramePacking", "720P 50Hz FramePacking"}; private int[] fourK_ValueList = {0x100, 0x101, 0x102, 0x103}; private String[] fourK_StringList = {"3840X2160 24Hz", "3840X2160 25Hz", "3840X2160 30Hz", "4096X2160 24Hz"}; private int nCurrentFormat; private boolean currFmtInSets = false; private int nCurrentFormatIndex; private String CurrentFormatIndexString; private boolean bEventWasHandled = true; private final long TIME_SPACE = 3000; private int mScreenCount = 0; private Keyfunction(Context context) { Log.e(TAG, "-------->Keyfunction <---------"); showStartMsg = false; mContext = context; loadKeyFuntion(mContext); toast = new ToastUtil(); mProduc_name = SystemProperties.get("ro.product.device"); is4Cpu = mProduc_name.substring(0, 5).equals("Hi379")?true:false; } public static class ToastUtil { private static Handler handler = new Handler(Looper.getMainLooper()); private static Toast toast = null; private static Object synObj = new Object(); public static void showMessage(final Context act, final String msg) { showMessage(act, msg, Toast.LENGTH_SHORT); } public static void showMessage(final Context act, final int msg) { showMessage(act, msg, Toast.LENGTH_SHORT); } public static void showMessage(final Context act, final String msg, final int len) { handler.post(new Runnable() { @Override public void run() { synchronized (synObj) { if (toast != null) { // toast.cancel(); toast.setText(msg); toast.setDuration(len); } else { toast = Toast.makeText(act, msg, len); } toast.show(); } } }); } public static void showMessage(final Context act, final int msg, final int len) { handler.post(new Runnable() { @Override public void run() { synchronized (synObj) { if (toast != null) { // toast.cancel(); toast.setText(msg); toast.setDuration(len); } else { toast = Toast.makeText(act, msg, len); } toast.show(); } } }); } } private static Keyfunction keyfunction; HashMap<String, AppInfoVO> keyFunctionMap = new HashMap<String, Keyfunction.AppInfoVO>(); private final boolean showStartMsg; public static Keyfunction getInstance(Context context) { if (keyfunction == null) { keyfunction = new Keyfunction(context); } return keyfunction; } static int parseToInt(String number) throws Exception { int n = -1; if (number.startsWith("0x") || number.startsWith("0X")) { n = Integer.parseInt(number.substring(2), 16); } else { n = Integer.parseInt(number); } return n; } static int parseToInt(String number, int def) { int n = def; try { n = parseToInt(number); } catch (Exception e) { return def; } return n; } private HashMap<String, AppInfoVO> loadKeyFuntion(Context mContext) { File file = null; file = new File(KEYFUNTIONXLMNAME); if ((!file.exists()) || (!file.canRead())) { keyFunctionMap.clear(); return keyFunctionMap; } try { keyFunctionMap.clear(); DocumentBuilderFactory xmlparser = DocumentBuilderFactory.newInstance(); DocumentBuilder xmlDOC; Document doc = null; xmlDOC = xmlparser.newDocumentBuilder(); doc = xmlDOC.parse(file); if (null == doc) return keyFunctionMap; NodeList appItems = doc.getElementsByTagName("key"); for (int i = 0; i < appItems.getLength(); i++) { Node appItem = appItems.item(i); if (!appItem.hasChildNodes()) continue; String actionType = ((Element) appItem).getAttribute("actionType"); NodeList appInfoItems = appItem.getChildNodes(); AppInfoVO appInfo = new AppInfoVO(); appInfo.actionType = ((actionType == null || actionType.length() == 0) ? AppInfoVO.ACTION_TYPE_ACTIVITY : actionType); for (int j = 0; j < appInfoItems.getLength(); j++) { Node appInfoItem = appInfoItems.item(j); String nodeName = appInfoItem.getNodeName(); String nodeTextContent = appInfoItem.getTextContent(); if (nodeName.equalsIgnoreCase("keyName")) { appInfo.keyName = nodeTextContent; } else if (nodeName.equalsIgnoreCase("keyValue")) { appInfo.keyValue = http://www.mamicode.com/nodeTextContent;>
同时在盒子根目录下的/system/etc目录下添加keyfunction.xml配置文件。<?xml version="1.0" encoding="utf-8"?> <Applications> <!-- <key> <keyName>VOD_SEARCH</keyName> <keyValue>84</keyValue> <packageName>com.ipanel.portal</packageName> <activityName>com.ipanel.portal.IPanel30PortalActivity</activityName> <action>android.intent.action.MAIN</action> <method>0</method> <flags>0x14008000</flags> <extras> <url type="string">http://vod.fjgdwl.com:80/gldb/NEW_UI/search/search_2.htm</url> </extras> <exclude> <packageName>com.ipanel.join.live.fj</packageName> <activityName>com.ipanel.portal.IPanel30PortalActivity</activityName> </exclude> </key> <key> <keyName>Huikan</keyName> <keyValue>67</keyValue> <packageName>com.ipanel.portal</packageName> <activityName>com.ipanel.portal.IPanel30PortalActivity</activityName> <action>android.intent.action.MAIN</action> <method>1</method> <flags>0x14008000</flags> <exclude> <packageName>com.ipanel.portal</packageName> </exclude> <extras> <url type="string">http://vod.fjgdwl.com:80/gldb/NEW_UI/lookBack/list.htm?typePos=0</url> </extras> </key> --> <key> <keyName>>Huikan</keyName> <keyValue>275</keyValue> <packageName>com.ipanel.join.vod.fj</packageName> <activityName>com.ipanel.join.vod.ui.BTVChannelListActivity</activityName> <action>android.intent.action.MAIN</action> <method>0</method> <flags>0x14008000 </flags> </key> <key> <keyName>SEARCH</keyName> <keyValue>257</keyValue> <packageName>com.ipanel.join.vod.fj</packageName> <activityName>com.ipanel.join.vod.fj.SearchActionActivity</activityName> <action>android.intent.action.MAIN</action> <method>0</method> <flags>0x14008000 </flags> <exclude> <packageName>com.ipanel.join.live.fj</packageName> </exclude> </key> <key> <keyName>HD</keyName> <keyValue>170</keyValue> <packageName>com.ipanel.join.live.fj</packageName> <activityName>com.jiangxi.apps.nclive.LiveActivity</activityName> <action>android.intent.action.MAIN</action> <flags>0x10200000</flags> <extras> <bouquet type="string">815</bouquet> <live_show_epg type="boolean">true</live_show_epg> </extras> </key> <key> <keyName>Love</keyName> <keyValue>256</keyValue> <packageName>com.ipanel.join.live.fj</packageName> <activityName>com.jiangxi.apps.nclive.LiveActivity</activityName> <action>android.intent.action.MAIN</action> <flags>0x10200000</flags> <extras> <bouquet type="string">2147483627</bouquet> <live_show_epg type="boolean">true</live_show_epg> </extras> </key> <key> <keyName>Settings</keyName> <keyValue>176</keyValue> <packageName>com.ipanel.join.cq.settings</packageName> <activityName>com.ipanel.join.cq.settings.SettingActivity</activityName> <action>android.intent.action.MAIN</action> </key> <key> <keyName>Email</keyName> <keyValue>65</keyValue> <packageName>com.ipanel.join.cq.settings</packageName> <activityName>com.ipanel.join.cq.settings.activity.MailListActivity</activityName> <action>android.intent.action.MAIN</action> </key> <key> <keyName>HiPQSetting</keyName> <keyValue>1185</keyValue> <packageName>com.hisilicon.android.pqsetting</packageName> <serviceName>com.hisilicon.android.pqsetting.MainService</serviceName> <action>android.intent.action.MAIN</action> </key> </Applications>
至此搜索按键从红外层到应用层的映射一直到安卓应用层的处理就全部完毕。这里搜索按键是跳到特殊的APP应用界面。
RK平台Android4.4 添加一个新的遥控器支持以及添加特殊按键