【Android系统源码修改】Java层读写NvRAM的方法

本例将从NvRAM中读取Barcode,并写入到gsm.serial属性。

1. 找目标文件

需要用到系统中一个文件NvRAMAgent.java。可以使用命令 find -name “NvRAMAgent.java” 找到。
将该文件拷贝到自己需要用的包下。本例是拷贝到frameworks\base\telephony\java\android\telephony下。
然后打开该文件,修改包名为当前包名

package android.telephony;

2. 找到读写NvRAM的接口

有个内部类Proxy,这个类有读写NvRAM的方法

   public byte[] readFile(int fileLid) throws android.os.RemoteException
   {
       
   }

   public int writeFile(int fileLid, byte[] buff) throws android.os.RemoteException
   {
       
   }

3. 改写代码

在需要读写的地方添加java代码

下面代码getRobotSN()从NvRAM中读取由SN Writer烧录到机器中的BARCODE,并转换为String,由requestSN()方法写到属性"gsm.serial"中。

    /// Add by Bright.L @{
    private static final String PROPERTY_SERIAL_NUMBER = "gsm.serial"; // 定义一个key
    // Custom_NvRam_LID.h: AP_CFG_REEB_PRODUCT_INFO_LID, for Barcode and IMEI
    private static final int AP_CFG_REEB_PRODUCT_INFO_LID = 59;  /* Do not modify! */
    private static String gsmSerail = "";
 /// Add: modified by Bright.L @{
    /** Read robot serial number. */ /**@hide */
    private void requestSN() {
        if (gsmSerail.isEmpty()) {
            try {
                String nvRamReady = SystemProperties.get("service.nvram_init", ""); // 读取nvRam状态
                if (!nvRamReady.isEmpty() && nvRamReady.equals("Ready")) {
                    String prop = SystemProperties.get(PROPERTY_SERIAL_NUMBER, ""); // 读取系统属性中,gsm.serial值
                    if (prop.isEmpty()) {
                        /*new Thread(new Runnable() {
                            @Override
                            public void run() {*/
                                try {
                                    String sn = getRobotSN(); // 读取NvRAM值,即SN                                    
                                    SystemProperties.set(PROPERTY_SERIAL_NUMBER, sn); // 将SN设置到系统属性gsm.serial中
                                    gsmSerail = sn;
                                } catch (Exception e) {
                                    Log.d(TAG, "requestSN, set SN Error. " + e.getLocalizedMessage());
                                }
                           /* }
                        }).start();*/
                    }
                }
            } catch (Exception e) {
                Log.d(TAG, "requestSN, Error. " + e.getLocalizedMessage());
            }
        }
    }

    /** Read NvRAM. */ /**@hide */
    private String getRobotSN() {
        String sn = "";
        IBinder binder = ServiceManager.getService("NvRAMAgent"); // 通过binder机制获取NvRAM代理服务
        NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
        if (agent != null) {
            try {
                byte[] buff = null;
                buff = agent.readFile(AP_CFG_REEB_PRODUCT_INFO_LID); // 读目标
                sn = new String(buff);
                if (sn.length() > 32) {
                    sn = sn.substring(0, 32).trim(); // 根据业务需要,裁剪SN,过滤无效字符
                }
                if (sn != null) {
                    return sn;
                }
            } catch (RemoteException e) {
                Log.e(TAG, "NvRAMAgent read file fail " + e.getLocalizedMessage());
            }
        }
        return "";
    }
    /// End: modified by Bright.L @}

TelephonyManager()构造函数中调用requestSN()

  /** @hide */
    private TelephonyManager() {
        mContext = null;

        /// ADD: by Bright.L @{
        requestSN();  // 将SN写到系统属性gsm.serial中
        /// end @}

        try {
            mOnlyOwnerSimSupport = MPlugin.createInstance(IOnlyOwnerSimSupport.class.getName());
        } catch (Exception e) {
            Rlog.e(TAG, "Fail to create plug-in");
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Android系统源码修改)