PersistentDataBlockManager.java revision 68d4acd205e8c2da524e62734ca42847306cc029
1package android.service.persistentdata;
2
3import android.os.RemoteException;
4import android.util.Slog;
5
6/**
7 * Interface for reading and writing data blocks to a persistent partition.
8 *
9 * Allows writing one block at a time. Namely, each time
10 * {@link android.service.persistentdata.PersistentDataBlockManager}.write(byte[] data)
11 * is called, it will overwite the data that was previously written on the block.
12 *
13 * Clients can query the size of the currently written block via
14 * {@link android.service.persistentdata.PersistentDataBlockManager}.getTotalDataSize().
15 *
16 * Clients can any number of bytes from the currently written block up to its total size by invoking
17 * {@link android.service.persistentdata.PersistentDataBlockManager}.read(byte[] data).
18 *
19 * @hide
20 */
21public class PersistentDataBlockManager {
22    private static final String TAG = PersistentDataBlockManager.class.getSimpleName();
23    private IPersistentDataBlockService sService;
24
25    public PersistentDataBlockManager(IPersistentDataBlockService service) {
26        sService = service;
27    }
28
29    /**
30     * Writes {@code data} to the persistent partition. Previously written data
31     * will be overwritten. This data will persist across factory resets.
32     *
33     * @param data the data to write
34     */
35    public void write(byte[] data) {
36        try {
37            sService.write(data);
38        } catch (RemoteException e) {
39            onError("writing data");
40        }
41    }
42
43    /**
44     * Tries to read {@code data.length} bytes into {@code data}. Call {@code getDataBlockSize()}
45     * to determine the total size of the block currently residing in the persistent partition.
46     *
47     * @param data the buffer in which to read the data
48     * @return the actual number of bytes read
49     */
50    public int read(byte[] data) {
51        try {
52            return sService.read(data);
53        } catch (RemoteException e) {
54            onError("reading data");
55            return -1;
56        }
57    }
58
59    /**
60     * Retrieves the size of the block currently written to the persistent partition.
61     */
62    public int getDataBlockSize() {
63        try {
64            return sService.getDataBlockSize();
65        } catch (RemoteException e) {
66            onError("getting data block size");
67            return 0;
68        }
69    }
70
71    /**
72     * Writes a byte enabling or disabling the ability to "OEM unlock" the device.
73     */
74    public void setOemUnlockEnabled(boolean enabled) {
75        try {
76            sService.setOemUnlockEnabled(enabled);
77        } catch (RemoteException e) {
78            onError("setting OEM unlock enabled to " + enabled);
79        }
80    }
81
82    /**
83     * Returns whether or not "OEM unlock" is enabled or disabled on this device.
84     */
85    public boolean getOemUnlockEnabled() {
86        try {
87            return sService.getOemUnlockEnabled();
88        } catch (RemoteException e) {
89            onError("getting OEM unlock enabled bit");
90            return false;
91        }
92    }
93
94    private void onError(String msg) {
95        Slog.v(TAG, "Remote exception while " + msg);
96    }
97}
98