Rockchip系列之VendorStorage 浅浅的介绍(1)-CSDN博客
Rockchip系列之VendorStorage uboot/kernel/user space 阶段接口使用介绍(2)_一歲抬頭的博客-CSDN博客
Rockchip系列之VendorStorage 新增framework系统jni+service接口访问(3)-CSDN博客
Rockchip系列之VendorStorage 新增framework封装VendorStorageManager访问(4)-CSDN博客
Rockchip 自定义vendorstorages数据再u-boot通过cmdline给kernel传递数据_一歲抬頭的博客-CSDN博客
在Rockchip系列中,基于前几篇文章,在本文中,我将介绍在framework新增封装VendorStorageManager类提供了对VendorStorage服务的访问和管理功能 方面上层APP和客户使用 (也方便自己调试。
VendorStorageManager类
辅助方法
- hexStringToByteArray(String hex): 将十六进制字符串转换为字节数组的辅助方法。
- byteArrayToHexString(byte[] data): 将字节数组转换为十六进制字符串的辅助方法。
- isValidVendorId(int id): 检查供应商ID的有效性的辅助方法。
- isValidDataLength(int id, int len): 检查数据长度的有效性的辅助方法。
- padData(int id, byte[] data): 在必要时用零填充数据的辅助方法。
- trimData(int id, byte[] data): 在必要时裁剪数据的辅助方法。
访问接口
getInstance()
: 获取VendorStorageManager
的实例。readMacAddress(int id)
: 通过指定的ID从供应商存储中读取MAC地址。writeMacAddress(int id, String mac)
: 将指定的MAC地址写入供应商存储。readSN()
: 从供应商存储中读取序列号。writeSN(String sn)
: 将序列号写入供应商存储。readHDCP2xKey()
: 从供应商存储中读取HDCP 2x密钥。writeHDCP2xKey(byte[] key)
: 将HDCP 2x密钥写入供应商存储。readCustomData(int id)
: 从供应商存储中读取自定义数据。writeCustomData(int id, byte[] data)
: 将自定义数据写入供应商存储。readDRMKey()
: 从供应商存储中读取DRM密钥。writeDRMKey(byte[] key)
: 将DRM密钥写入供应商存储。readPlayReadyCert()
: 从供应商存储中读取PlayReady证书。writePlayReadyCert(byte[] cert)
: 将PlayReady证书写入供应商存储。readPlayReadyRootKey(int index)
: 从供应商存储中读取PlayReady根密钥。writePlayReadyRootKey(int index, byte[] key)
: 将PlayReady根密钥写入供应商存储。readSensorCalibration()
: 从供应商存储中读取传感器校准数据。writeSensorCalibration(byte[] data)
: 将传感器校准数据写入供应商存储。readIMEI()
: 从供应商存储中读取IMEI。writeIMEI(String imei)
: 将IMEI写入供应商存储。readEinkVcom()
: 从供应商存储中读取e-ink VCOM。writeEinkVcom(int vcom)
: 将e-ink VCOM写入供应商存储。readHDCP14HDMIKey()
: 从供应商存储中读取HDCP 1.4 HDMI密钥。writeHDCP14HDMIKey(byte[] key)
: 将HDCP 1.4 HDMI密钥写入供应商存储。readHDCP14DPKey()
: 从供应商存储中读取HDCP 1.4 DP密钥。writeHDCP14DPKey(byte[] key)
: 将HDCP 1.4 DP密钥写入供应商存储。readAttentionKey()
: 从供应商存储中读取attention密钥。writeAttentionKey(byte[] key)
: 将attention密钥写入供应商存储。readReservedData(int index)
: 从供应商存储中读取保留数据。writeReservedData(int index, byte[] data)
: 将保留数据写入供应商存储。readAllCommonIds()
: 读取所有常用的供应商ID并打印它们。readCustomIdByHex(String hex)
: 通过十六进制字符串读取自定义供应商ID。writeCustomIdByHex(String hex, String type, String input)
: 通过十六进制字符串和输入类型写入自定义供应商ID。writeJsonString(int id, String json)
: 将JSON字符串写入供应商存储。readJsonString(int id)
: 从供应商存储中读取JSON字符串。writeBinFile(int id, String filePath)
: 将二进制文件写入供应商存储。readBinFile(int id, String filePath)
: 从供应商存储中读取二进制文件。write2048Bytes(int id1, int id2, byte[] data)
: 将2048字节的数据分别写入两个供应商ID。read2048Bytes(int id1, int id2)
: 从两个供应商ID读取2048字节的数据。
这些方法提供了对供应商存储的封装访问,可以方便地读取和写入不同类型的供应商特定数据。你可以根据需要调用相应的方法来操作供应商存储中的数据。
VendorStorageManager类是一个单例类,用于管理对VendorStorageService服务的访问。我实现了读取和写入不同类型数据的方法,并处理了数据的裁剪和填充。以下是新增的framework封装manager访问功能的代码:
package android.xxxx; import android.xxxx.IVendorStorageManager; import android.xxxx.util.MethodUtil; import android.os.IBinder; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.lang.reflect.Method; import java.util.Arrays; public class VendorStorageManager { private static final String TAG = "VendorStorageManager"; private static final String SERVICE_NAME = "vendor_storage"; // 添加了一些常量 vendor_test.h public static final int VENDOR_SN_ID = 1; public static final int VENDOR_WIFI_MAC_ID = 2; public static final int VENDOR_LAN_MAC_ID = 3; public static final int VENDOR_BT_MAC_ID = 4; public static final int VENDOR_HDCP_14_HDMI_ID = 5; public static final int VENDOR_HDCP_14_DP_ID = 6; public static final int VENDOR_HDCP_2x_ID = 7; public static final int VENDOR_DRM_KEY_ID = 8; public static final int VENDOR_PLAYREADY_Cert_ID = 9; public static final int VENDOR_ATTENTION_KEY_ID = 10; public static final int VENDOR_PLAYREADY_ROOT_KEY_0_ID = 11; public static final int VENDOR_PLAYREADY_ROOT_KEY_1_ID = 12; public static final int VENDOR_SENSOR_CALIBRATION_ID = 13; public static final int VENODR_RESERVE_ID_14 = 14; public static final int VENDOR_IMEI_ID = 15; public static final int VENDOR_CUSTOM_ID = 16; public static final int EINK_VCOM_ID = 17; public static final int VENDOR_USER1 = 18; public static final int VENDOR_USER2 = 19; public static final int MAX_DATA_LENGTH = 1024; // 数据的最大长度(字节) public static final int MAC_ADDRESS_LENGTH = 6; // MAC地址的长度(字节) public static final int SN_LENGTH = 32; // 序列号的长度(字节) private IVendorStorageManager mService; private VendorStorageManager() { Method method = null; try { method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null, new Object[]{"vendor_storage"}); mService = IVendorStorageManager.Stub.asInterface(binder); if (mService == null) { Log.d(TAG, "获取vendor storage服务失败"); } } catch (Exception e) { e.printStackTrace(); } /* mService = IVendorStorageManager.Stub.asInterface(ServiceManager.getService(SERVICE_NAME)); if (mService == null) { Log.e(TAG, "获取vendor storage服务失败"); }*/ } private static class SingletonHolder { private static VendorStorageManager sInstance = new VendorStorageManager(); } public static VendorStorageManager getInstance() { return SingletonHolder.sInstance; } // 辅助方法,用于将十六进制字符串转换为字节数组 private byte[] hexStringToByteArray(String hex) { if (hex == null || hex.length() % 2 != 0) { return null; } byte[] result = new byte[hex.length() / 2]; for (int i = 0; i < result.length; i++) { result[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16); } return result; } // 辅助方法,用于将字节数组转换为十六进制字符串 private String byteArrayToHexString(byte[] data) { if (data == null) { return null; } StringBuilder sb = new StringBuilder(); for (byte b : data) { sb.append(String.format("%02x", b)); } return sb.toString(); } // 辅助方法,用于检查vendor id的有效性 private boolean isValidVendorId(int id) { return id >= VENDOR_SN_ID && id <= VENDOR_USER2; } // 辅助方法,用于检查数据长度的有效性 private boolean isValidDataLength(int id, int len) { switch (id) { case VENDOR_SN_ID: return len == SN_LENGTH; case VENDOR_WIFI_MAC_ID: case VENDOR_LAN_MAC_ID: case VENDOR_BT_MAC_ID: return len == MAC_ADDRESS_LENGTH; default: return len > 0 && len <= MAX_DATA_LENGTH; } } // 辅助方法,用于在必要时用零填充数据 private byte[] padData(int id, byte[] data) { if (data == null) { return null; } int len = data.length; if (len == MAX_DATA_LENGTH) { return data; } byte[] padded = new byte[MAX_DATA_LENGTH]; System.arraycopy(data, 0, padded, 0, len); return padded; } // 辅助方法,用于在必要时裁剪数据 private byte[] trimData(int id, byte[] data) { if (data == null) { return null; } int len = data.length; switch (id) { case VENDOR_SN_ID: return Arrays.copyOf(data, SN_LENGTH); case VENDOR_WIFI_MAC_ID: case VENDOR_LAN_MAC_ID: case VENDOR_BT_MAC_ID: return Arrays.copyOf(data, MAC_ADDRESS_LENGTH); default: // 找到最后非零字节 int last = len - 1; while (last >= 0 && data[last] == 0) { last--; } return Arrays.copyOf(data, last + 1); } } // 方法,用于通过id从vendor storage读取数据 public byte[] vendorStorageRead(int id) { if (mService == null) { Log.e(TAG, "Vendor storage服务不可用"); return null; } if (!isValidVendorId(id)) { Log.e(TAG, "无效的vendor id: " + id); return null; } try { byte[] data = mService.vendorStorageRead(id); if (data != null) { data = trimData(id, data); // 如果需要,裁剪数据 Log.d(TAG, "Vendor storage读取成功: id = " + id + ", data = " + byteArrayToHexString(data)); } else { Log.e(TAG, "Vendor storage读取失败: id = " + id); } return data; } catch (Exception e) { Log.e(TAG, "Vendor storage读取错误: id = " + id, e); return null; } } // 方法,用于通过id向vendor storage写入数据 public boolean vendorStorageWrite(int id, byte[] data) { if (mService == null) { Log.e(TAG, "Vendor storage服务不可用"); return false; } if (!isValidVendorId(id)) { Log.e(TAG, "无效的vendor id: " + id); return false; } if (data == null || !isValidDataLength(id, data.length)) { Log.e(TAG, "无效的数据长度: " + (data == null ? 0 : data.length)); return false; } try { data = padData(id, data); // 如果需要,用零填充数据 int ret = mService.vendorStorageWrite(id, data); boolean result = ret > 0; Log.d(TAG, "Vendor storage写入 :" + ret); /*if (result) { Log.d(TAG, "Vendor storage写入成功: id = " + id + ", data = " + byteArrayToHexString(data)); } else { Log.e(TAG, "Vendor storage写入失败: id = " + id); }*/ return result; } catch (Exception e) { Log.e(TAG, "Vendor storage写入错误: id = " + id, e); return false; } } // 方法,用于通过id从vendor storage读取MAC地址 public String readMacAddress(int id) { if (id != VENDOR_WIFI_MAC_ID && id != VENDOR_LAN_MAC_ID && id != VENDOR_BT_MAC_ID) { Log.e(TAG, "无效的MAC地址id: " + id); return null; } byte[] data = vendorStorageRead(id); if (data != null && data.length == MAC_ADDRESS_LENGTH || (id == VENDOR_LAN_MAC_ID && data.length == 12)) { String mac = byteArrayToHexString(data); mac = mac.substring(0, 2) + ":" + mac.substring(2, 4) + ":" + mac.substring(4, 6) + ":" + mac.substring(6, 8) + ":" + mac.substring(8, 10) + ":" + mac.substring(10, 12); // 在字节之间添加冒号 return mac.toUpperCase(); // 转换为大写 } else { return null; } } public boolean writeMacAddress(int id, String mac) { if (id != VENDOR_WIFI_MAC_ID && id != VENDOR_LAN_MAC_ID && id != VENDOR_BT_MAC_ID) { Log.e(TAG, "无效的MAC地址id: " + id); return false; } if (mac == null || !mac.matches("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) { Log.e(TAG, "无效的MAC地址格式: " + mac); return false; } mac = mac.replace(":", ""); // 移除冒号 byte[] data = hexStringToByteArray(mac); return vendorStorageWrite(id, data); } // 方法,用于从vendor storage读取序列号 public String readSN() { byte[] data = vendorStorageRead(VENDOR_SN_ID); if (data != null /*&& data.length == SN_LENGTH*/) { return new String(data); // 将字节转换为字符串 } else { return null; } } // 方法,用于向vendor storage写入序列号 public boolean writeSN(String sn) { if (sn == null /*|| sn.length() != SN_LENGTH*/) { Log.e(TAG, "无效的序列号长度: " + (sn == null ? 0 : sn.length())); return false; } byte[] data = sn.getBytes(); // 将字符串转换为字节 return vendorStorageWrite(VENDOR_SN_ID, data); } // 方法,用于从vendor storage读取HDCP 2x密钥 public byte[] readHDCP2xKey() { return vendorStorageRead(VENDOR_HDCP_2x_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入HDCP 2x密钥 public boolean writeHDCP2xKey(byte[] key) { if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的HDCP 2x密钥长度: " + (key == null ? 0 : key.length)); return false; } return vendorStorageWrite(VENDOR_HDCP_2x_ID, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取自定义数据 public byte[] readCustomData(int id) { if (id != VENDOR_CUSTOM_ID && id != VENDOR_USER1 && id != VENDOR_USER2) { Log.e(TAG, "无效的自定义数据id: " + id); return null; } return vendorStorageRead(id); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入自定义数据 public boolean writeCustomData(int id, byte[] data) { if (id != VENDOR_CUSTOM_ID && id != VENDOR_USER1 && id != VENDOR_USER2) { Log.e(TAG, "无效的自定义数据id: " + id); return false; } if (data == null || data.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的自定义数据长度: " + (data == null ? 0 : data.length)); return false; } return vendorStorageWrite(id, data); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取DRM密钥 public byte[] readDRMKey() { return vendorStorageRead(VENDOR_DRM_KEY_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入DRM密钥 public boolean writeDRMKey(byte[] key) { if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的DRM密钥长度: " + (key == null ? 0 : key.length)); return false; } return vendorStorageWrite(VENDOR_DRM_KEY_ID, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取PlayReady证书 public byte[] readPlayReadyCert() { return vendorStorageRead(VENDOR_PLAYREADY_Cert_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入PlayReady证书 public boolean writePlayReadyCert(byte[] cert) { if (cert == null || cert.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的PlayReady证书长度: " + (cert == null ? 0 : cert.length)); return false; } return vendorStorageWrite(VENDOR_PLAYREADY_Cert_ID, cert); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取PlayReady根密钥 public byte[] readPlayReadyRootKey(int index) { if (index != 0 && index != 1) { Log.e(TAG, "无效的PlayReady根密钥索引: " + index); return null; } int id = index == 0 ? VENDOR_PLAYREADY_ROOT_KEY_0_ID : VENDOR_PLAYREADY_ROOT_KEY_1_ID; return vendorStorageRead(id); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入PlayReady根密钥 public boolean writePlayReadyRootKey(int index, byte[] key) { if (index != 0 && index != 1) { Log.e(TAG, "无效的PlayReady根密钥索引: " + index); return false; } if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的PlayReady根密钥长度: " + (key == null ? 0 : key.length)); return false; } int id = index == 0 ? VENDOR_PLAYREADY_ROOT_KEY_0_ID : VENDOR_PLAYREADY_ROOT_KEY_1_ID; return vendorStorageWrite(id, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取传感器校准数据 public byte[] readSensorCalibration() { return vendorStorageRead(VENDOR_SENSOR_CALIBRATION_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入传感器校准数据 public boolean writeSensorCalibration(byte[] data) { if (data == null || data.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的传感器校准数据长度: " + (data == null ? 0 : data.length)); return false; } return vendorStorageWrite(VENDOR_SENSOR_CALIBRATION_ID, data); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取IMEI public String readIMEI() { byte[] data = vendorStorageRead(VENDOR_IMEI_ID); if (data != null) { return new String(data); // 将字节转换为字符串 } else { return null; } } // 方法,用于向vendor storage写入IMEI public boolean writeIMEI(String imei) { if (imei == null || imei.length() > MAX_DATA_LENGTH) { Log.e(TAG, "无效的IMEI长度: " + (imei == null ? 0 : imei.length())); return false; } byte[] data = imei.getBytes(); // 将字符串转换为字节 return vendorStorageWrite(VENDOR_IMEI_ID, data); } // 方法,用于从vendor storage读取e-ink VCOM public int readEinkVcom() { byte[] data = vendorStorageRead(EINK_VCOM_ID); if (data != null && data.length >= 4) { // 假设数据是以小端序存储的 int vcom = data[0] & 0xFF | (data[1] & 0xFF) << 8 | (data[2] & 0xFF) << 16 | (data[3] & 0xFF) << 24; return vcom; }else { return -1; // 无效的值 } } // 方法,用于向vendor storage写入e-ink VCOM public boolean writeEinkVcom(int vcom) { byte[] data = new byte[4]; // 以小端序存储数据 data[0] = (byte) (vcom & 0xFF); data[1] = (byte) ((vcom >> 8) & 0xFF); data[2] = (byte) ((vcom >> 16) & 0xFF); data[3] = (byte) ((vcom >> 24) & 0xFF); return vendorStorageWrite(EINK_VCOM_ID, data); } // 方法,用于从vendor storage读取HDCP 1.4 HDMI密钥 public byte[] readHDCP14HDMIKey() { return vendorStorageRead(VENDOR_HDCP_14_HDMI_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入HDCP 1.4 HDMI密钥 public boolean writeHDCP14HDMIKey(byte[] key) { if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的HDCP 1.4 HDMI密钥长度: " + (key == null ? 0 : key.length)); return false; } return vendorStorageWrite(VENDOR_HDCP_14_HDMI_ID, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取HDCP 1.4 DP密钥 public byte[] readHDCP14DPKey() { return vendorStorageRead(VENDOR_HDCP_14_DP_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入HDCP 1.4 DP密钥 public boolean writeHDCP14DPKey(byte[] key) { if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的HDCP 1.4 DP密钥长度: " + (key == null ? 0 : key.length)); return false; } return vendorStorageWrite(VENDOR_HDCP_14_DP_ID, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取attention密钥 public byte[] readAttentionKey() { return vendorStorageRead(VENDOR_ATTENTION_KEY_ID); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入attention密钥 public boolean writeAttentionKey(byte[] key) { if (key == null || key.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的attention密钥长度: " + (key == null ? 0 : key.length)); return false; } return vendorStorageWrite(VENDOR_ATTENTION_KEY_ID, key); // 不需要裁剪或填充数据 } // 方法,用于从vendor storage读取保留数据 public byte[] readReservedData(int index) { if (index != 14) { Log.e(TAG, "无效的保留数据索引: " + index); return null; } int id = VENODR_RESERVE_ID_14; return vendorStorageRead(id); // 不需要裁剪或填充数据 } // 方法,用于向vendor storage写入保留数据 public boolean writeReservedData(int index, byte[] data) { if (index != 14) { Log.e(TAG, "无效的保留数据索引: " + index); return false; } if (data == null || data.length > MAX_DATA_LENGTH) { Log.e(TAG, "无效的保留数据长度: " + (data == null ? 0 : data.length)); return false; } int id = VENODR_RESERVE_ID_14; return vendorStorageWrite(id, data); // 不需要裁剪或填充数据 } // 方法,用于读取所有常用的vendor id并打印它们 public void readAllCommonIds() { int[] ids = new int[]{ VENDOR_SN_ID, VENDOR_WIFI_MAC_ID, VENDOR_LAN_MAC_ID, VENDOR_BT_MAC_ID, VENDOR_HDCP_14_HDMI_ID, VENDOR_HDCP_14_DP_ID, VENDOR_HDCP_2x_ID, VENDOR_DRM_KEY_ID, VENDOR_PLAYREADY_Cert_ID, VENDOR_ATTENTION_KEY_ID, VENDOR_PLAYREADY_ROOT_KEY_0_ID, VENDOR_PLAYREADY_ROOT_KEY_1_ID, VENDOR_SENSOR_CALIBRATION_ID }; for (int id : ids) { byte[] data = vendorStorageRead(id); if (data != null) { Log.d(TAG, "Vendor id: " + id + ", data: " + byteArrayToHexString(data)); } else { Log.e(TAG, "Vendor id: " + id + ", data: null"); } } } // 方法,用于通过十六进制字符串读取自定义vendor id public byte[] readCustomIdByHex(String hex) { if (hex == null || !hex.matches("([0-9A-Fa-f]{2})+")) { Log.e(TAG, "无效的自定义id十六进制: " + hex); return null; } byte[] data = hexStringToByteArray(hex); int id = data[0] & 0xFF; return vendorStorageRead(id); } // 方法,用于通过十六进制字符串和输入类型写入自定义vendor id public boolean writeCustomIdByHex(String hex, String type, String input) { if (hex == null || !hex.matches("([0-9A-Fa-f]{2})+")) { Log.e(TAG, "无效的自定义id十六进制: " + hex); return false; } if (type == null || !(type.equals("hex") || type.equals("string") || type.equals("file"))) { Log.e(TAG, "无效的输入类型: " + type); return false; } if (input == null) { Log.e(TAG, "无效的输入: null"); return false; } byte[] data = hexStringToByteArray(hex); int id = data[0] & 0xFF; Log.d("MainActivity", "id: " + id + "," + data[0]); if (type.equals("hex")) { data = hexStringToByteArray(input); } else if (type.equals("string")) { data = input.getBytes(); } else if (type.equals("file")) { // 读取文件内容作为字节 try { File file = new File(input); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { bos.write(buffer, 0, len); } fis.close(); bos.close(); data = bos.toByteArray(); } catch (Exception e) { Log.e(TAG, "读取文件失败: " + input, e); return false; } } return vendorStorageWrite(id, data); } // 方法,用于向vendor storage写入json字符串 public boolean writeJsonString(int id, String json) { if (json == null) { Log.e(TAG, "无效的json字符串: null"); return false; } // 将json字符串转换为字节数组 byte[] data = json.getBytes(); // 将数据写入vendor storage return writeCustomData(id, data); } // 方法,用于从vendor storage读取json字符串 public String readJsonString(int id) { // 从vendor storage读取数据 byte[] data = readCustomData(id); // 检查数据 if (data != null) { // 将字节数组转换为json字符串 return new String(data); } else { Log.e(TAG, "读取json字符串失败: id = " + id); return null; } } // 方法,用于向vendor storage写入bin文件 public boolean writeBinFile(int id, String filePath) { if (filePath == null) { Log.e(TAG, "无效的文件路径: null"); return false; } // 读取文件内容作为字节数组 byte[] data = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { bos.write(buffer, 0, len); } fis.close(); bos.close(); data = bos.toByteArray(); } catch (Exception e) { Log.e(TAG, "读取文件失败: " + filePath, e); return false; } // 将数据写入vendor storage return writeCustomData(id, data); } // 方法,用于从vendor storage读取bin文件 public boolean readBinFile(int id, String filePath) { if (filePath == null) { Log.e(TAG, "无效的文件路径: null"); return false; } // 从vendor storage读取数据 byte[] data = readCustomData(id); // 检查数据 if (data != null) { // 将字节数组写入文件 try { File file = new File(filePath); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); return true; } catch (Exception e) { Log.e(TAG, "写入文件失败: " + filePath, e); return false; } } else { Log.e(TAG, "读取bin文件失败: id = " + id); return false; } } // to write 2048 bytes to vendor storage by two ids and a byte array public boolean write2048Bytes(int id1, int id2, byte[] data) { if (data == null || data.length != 2048) { Log.e(TAG, "Invalid data: " + data); return false; } // split the data into two parts byte[] data1 = new byte[1024]; byte[] data2 = new byte[1024]; System.arraycopy(data, 0, data1, 0, 1024); System.arraycopy(data, 1024, data2, 0, 1024); // write the data to vendor storage by two ids boolean result1 = writeCustomData(id1, data1); boolean result2 = writeCustomData(id2, data2); // check the results return result1 && result2; } // to read 2048 bytes from vendor storage by two ids public byte[] read2048Bytes(int id1, int id2) { // read the data from vendor storage by two ids byte[] data1 = readCustomData(id1); byte[] data2 = readCustomData(id2); // check the data if (data1 != null && data2 != null) { // merge the data into one byte array byte[] data = new byte[2048]; System.arraycopy(data1, 0, data, 0, 1024); System.arraycopy(data2, 0, data, 1024, 1024); return data; } else { Log.e(TAG, "Read 2048 bytes failed: id1 = " + id1 + ", id2 = " + id2); return null; } } }
//调用示例: VendorStorageManager vstManager = VendorStorageManager.getInstance(); vstManager.writeSN("SN202207110000000000000000000001"); String sn = vstManager.readSN();
总结
在本篇博客中,我介绍了新增的framework封装manager访问功能,通过VendorStorageManager类提供了对VendorStorageService服务的访问和管理。这些新增的方法可以方便地读取和写入供应商特定的数据。可以根据自己的需求使用这些方法来访问VendorStorage服务,并进行相应的数据处理。
希望本文对您有所帮助!如有任何疑问,请随时向我提问。