PersistentDataBlockManager.java revision a9437bd1caeeb38780d920a81bde8cc7ca280fe0
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.service.persistentdata; 18 19import android.annotation.SystemApi; 20import android.os.RemoteException; 21import android.util.Slog; 22 23/** 24 * Interface for reading and writing data blocks to a persistent partition. 25 * 26 * Allows writing one block at a time. Namely, each time 27 * {@link PersistentDataBlockManager#write(byte[])} 28 * is called, it will overwite the data that was previously written on the block. 29 * 30 * Clients can query the size of the currently written block via 31 * {@link PersistentDataBlockManager#getDataBlockSize()}. 32 * 33 * Clients can query the maximum size for a block via 34 * {@link PersistentDataBlockManager#getMaximumDataBlockSize()} 35 * 36 * Clients can read the currently written block by invoking 37 * {@link PersistentDataBlockManager#read()}. 38 * 39 * @hide 40 */ 41@SystemApi 42public class PersistentDataBlockManager { 43 private static final String TAG = PersistentDataBlockManager.class.getSimpleName(); 44 private IPersistentDataBlockService sService; 45 46 public PersistentDataBlockManager(IPersistentDataBlockService service) { 47 sService = service; 48 } 49 50 /** 51 * Writes {@code data} to the persistent partition. Previously written data 52 * will be overwritten. This data will persist across factory resets. 53 * 54 * Returns the number of bytes written or -1 on error. If the block is too big 55 * to fit on the partition, returns -MAX_BLOCK_SIZE. 56 * 57 * {@link #wipe} will block any further {@link #write} operation until reboot, 58 * in which case -1 will be returned. 59 * 60 * @param data the data to write 61 */ 62 public int write(byte[] data) { 63 try { 64 return sService.write(data); 65 } catch (RemoteException e) { 66 onError("writing data"); 67 return -1; 68 } 69 } 70 71 /** 72 * Returns the data block stored on the persistent partition. 73 */ 74 public byte[] read() { 75 try { 76 return sService.read(); 77 } catch (RemoteException e) { 78 onError("reading data"); 79 return null; 80 } 81 } 82 83 /** 84 * Retrieves the size of the block currently written to the persistent partition. 85 * 86 * Return -1 on error. 87 */ 88 public int getDataBlockSize() { 89 try { 90 return sService.getDataBlockSize(); 91 } catch (RemoteException e) { 92 onError("getting data block size"); 93 return -1; 94 } 95 } 96 97 /** 98 * Retrieves the maximum size allowed for a data block. 99 * 100 * Returns -1 on error. 101 */ 102 public long getMaximumDataBlockSize() { 103 try { 104 return sService.getMaximumDataBlockSize(); 105 } catch (RemoteException e) { 106 onError("getting maximum data block size"); 107 return -1; 108 } 109 } 110 111 /** 112 * Zeroes the previously written block in its entirety. Calling this method 113 * will erase all data written to the persistent data partition. 114 * It will also prevent any further {@link #write} operation until reboot, 115 * in order to prevent a potential race condition. See b/30352311. 116 */ 117 public void wipe() { 118 try { 119 sService.wipe(); 120 } catch (RemoteException e) { 121 onError("wiping persistent partition"); 122 } 123 } 124 125 /** 126 * Writes a byte enabling or disabling the ability to "OEM unlock" the device. 127 */ 128 public void setOemUnlockEnabled(boolean enabled) { 129 try { 130 sService.setOemUnlockEnabled(enabled); 131 } catch (RemoteException e) { 132 onError("setting OEM unlock enabled to " + enabled); 133 } 134 } 135 136 /** 137 * Returns whether or not "OEM unlock" is enabled or disabled on this device. 138 */ 139 public boolean getOemUnlockEnabled() { 140 try { 141 return sService.getOemUnlockEnabled(); 142 } catch (RemoteException e) { 143 onError("getting OEM unlock enabled bit"); 144 return false; 145 } 146 } 147 148 private void onError(String msg) { 149 Slog.v(TAG, "Remote exception while " + msg); 150 } 151} 152