PersistentDataBlockManager.java revision 963295ea105314e28e4ca9563aa09cb7440de4c3
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.os.RemoteException; 20import android.util.Slog; 21 22/** 23 * Interface for reading and writing data blocks to a persistent partition. 24 * 25 * Allows writing one block at a time. Namely, each time 26 * {@link PersistentDataBlockManager#write(byte[])} 27 * is called, it will overwite the data that was previously written on the block. 28 * 29 * Clients can query the size of the currently written block via 30 * {@link PersistentDataBlockManager#getDataBlockSize()}. 31 * 32 * Clients can query the maximum size for a block via 33 * 34 * Clients can read the currently written block by invoking 35 * {@link PersistentDataBlockManager#read()}. 36 * 37 * @hide 38 */ 39public class PersistentDataBlockManager { 40 private static final String TAG = PersistentDataBlockManager.class.getSimpleName(); 41 private IPersistentDataBlockService sService; 42 43 public PersistentDataBlockManager(IPersistentDataBlockService service) { 44 sService = service; 45 } 46 47 /** 48 * Writes {@code data} to the persistent partition. Previously written data 49 * will be overwritten. This data will persist across factory resets. 50 * 51 * Returns the number of bytes written or -1 on error. If the block is too big 52 * to fit on the partition, returns -MAX_BLOCK_SIZE. 53 * 54 * @param data the data to write 55 */ 56 public int write(byte[] data) { 57 try { 58 return sService.write(data); 59 } catch (RemoteException e) { 60 onError("writing data"); 61 return -1; 62 } 63 } 64 65 /** 66 * Returns the data block stored on the persistent partition. 67 */ 68 public byte[] read() { 69 try { 70 return sService.read(); 71 } catch (RemoteException e) { 72 onError("reading data"); 73 return null; 74 } 75 } 76 77 /** 78 * Retrieves the size of the block currently written to the persistent partition. 79 * 80 * Return -1 on error. 81 */ 82 public int getDataBlockSize() { 83 try { 84 return sService.getDataBlockSize(); 85 } catch (RemoteException e) { 86 onError("getting data block size"); 87 return -1; 88 } 89 } 90 91 /** 92 * Retrieves the maximum size allowed for a data block. 93 * 94 * Returns -1 on error. 95 */ 96 public long getMaximumDataBlockSize() { 97 try { 98 return sService.getMaximumDataBlockSize(); 99 } catch (RemoteException e) { 100 onError("getting maximum data block size"); 101 return -1; 102 } 103 } 104 105 /** 106 * Zeroes the previously written block in its entirety. Calling this method 107 * will erase all data written to the persistent data partition. 108 */ 109 public void wipe() { 110 try { 111 sService.wipe(); 112 } catch (RemoteException e) { 113 onError("wiping persistent partition"); 114 } 115 } 116 117 /** 118 * Writes a byte enabling or disabling the ability to "OEM unlock" the device. 119 */ 120 public void setOemUnlockEnabled(boolean enabled) { 121 try { 122 sService.setOemUnlockEnabled(enabled); 123 } catch (RemoteException e) { 124 onError("setting OEM unlock enabled to " + enabled); 125 } 126 } 127 128 /** 129 * Returns whether or not "OEM unlock" is enabled or disabled on this device. 130 */ 131 public boolean getOemUnlockEnabled() { 132 try { 133 return sService.getOemUnlockEnabled(); 134 } catch (RemoteException e) { 135 onError("getting OEM unlock enabled bit"); 136 return false; 137 } 138 } 139 140 private void onError(String msg) { 141 Slog.v(TAG, "Remote exception while " + msg); 142 } 143} 144