android5.0源码开发之NVRam存储,恢复出厂设置数据不被擦除--下篇

这篇,我们来说说上层对NVRam的使用

NVRam的存取是以byte的形式进行的

    private byte callValue[] = new byte[16];//定义16个字节,用以读取存储两个long型
    private long[] callinout = new long[2];

    private synchronized void WriteData() {//写入NVRam
        IBinder binder = ServiceManager.getService("NvRAMAgent");
        NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);    
        try {
            int flag = agent.writeFile(AP_CFG_CUSTOM_FILE_CALL, callValue);
            Log.v("dial","write success flag="+flag);    
        } catch (RemoteException e) {
            e.printStackTrace();
            Log.v("dial","write failed"+e);
        }
    }

    //Read NvRAM
    private synchronized void ReadData() {
        IBinder binder = ServiceManager.getService("NvRAMAgent");
        NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
byte buffs[] = new byte[16];
        try {
             buffs = agent.readFile(AP_CFG_CUSTOM_FILE_CALL);// read buffer from nvram
        } catch (RemoteException e) {
            e.printStackTrace();
        }

//backup NvRAM 每次xieruNVRam,必须要备份
     public void NvRAMBackupData() {
        int ret = 0;
        IBinder binder = ServiceManager.getService("NvRAMBackupAgent");
        
        if(binder != null){
           NvRAMBackupAgent agent = NvRAMBackupAgent.Stub.asInterface(binder);
           try {
          ret = agent.testBackup();
  Log.v("dial", "NvRAMBackupAgent succuss");
           } catch (RemoteException e) {
          e.printStackTrace();
  Log.v("dial", "NvRAMBackupAgent fail" + e);
           }
        }
        else
        {
           Log.v(TAG, "NvRAMBackupAgent service = "+binder);
        }
}
        for(int i = 0; i < buffs.length; i ++){
            Log.i("dial","read_buff["+i+"]="+buffs[i]);  
        }
        System.arraycopy(buffs, 0, callValue, 0, buffs.length);
    }


private byte[] longToByte(long l,long n){
long temp1 = l;
long temp2 = n;
byte[] b = new byte[16];
for (int i = 0; i < 8; i++) {
b[i] = new Long(temp1 & 0xff).byteValue();
temp1 = temp1 >> 8;
}
for (int i = 8; i < 16; i++) {
b[i] = new Long(temp2 & 0xff).byteValue();
temp2 = temp2 >> 8;
}

return b;
}

private long[] byteToLong(byte[] b){
long[] l = new long[2];
long l0 = b[0] & 0xff;
long l1 = b[1] & 0xff;
long l2 = b[2] & 0xff;
long l3 = b[3] & 0xff;
long l4 = b[4] & 0xff;
long l5 = b[5] & 0xff;
long l6 = b[6] & 0xff;
long l7 = b[7] & 0xff;
l1 <<= 8;
l2 <<= 16;
l3 <<= 24;
l4 <<= 8 * 4;
l5 <<= 8 * 5;
l6 <<= 8 * 6;
l7 <<= 8 * 7;
l[0] = l0 | l1 | l2 | l3 | l4 | l5 | l6 | l7;
long l8 = b[8] & 0xff;
long l9 = b[9] & 0xff;
long l10 = b[10] & 0xff;
long l11 = b[11] & 0xff;
long l12 = b[12] & 0xff;
long l13 = b[13] & 0xff;
long l14 = b[14] & 0xff;
long l15 = b[15] & 0xff;
l9 <<= 8;
l10 <<= 16;
l11 <<= 8 * 3;
l12 <<= 8 * 4;
l13 <<= 8 * 5;
l14 <<= 8 * 6;
l15 <<= 8 * 7;
l[1] = l8 | l9 | l10 | l11 | l12 | l13 | l14 | l15;
return l;
}


你可能感兴趣的:(android5.0源码开发之NVRam存储,恢复出厂设置数据不被擦除--下篇)