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     * @param data the data to write
58     */
59    public int write(byte[] data) {
60        try {
61            return sService.write(data);
62        } catch (RemoteException e) {
63            onError("writing data");
64            return -1;
65        }
66    }
67
68    /**
69     * Returns the data block stored on the persistent partition.
70     */
71    public byte[] read() {
72        try {
73            return sService.read();
74        } catch (RemoteException e) {
75            onError("reading data");
76            return null;
77        }
78    }
79
80    /**
81     * Retrieves the size of the block currently written to the persistent partition.
82     *
83     * Return -1 on error.
84     */
85    public int getDataBlockSize() {
86        try {
87            return sService.getDataBlockSize();
88        } catch (RemoteException e) {
89            onError("getting data block size");
90            return -1;
91        }
92    }
93
94    /**
95     * Retrieves the maximum size allowed for a data block.
96     *
97     * Returns -1 on error.
98     */
99    public long getMaximumDataBlockSize() {
100        try {
101            return sService.getMaximumDataBlockSize();
102        } catch (RemoteException e) {
103            onError("getting maximum data block size");
104            return -1;
105        }
106    }
107
108    /**
109     * Zeroes the previously written block in its entirety. Calling this method
110     * will erase all data written to the persistent data partition.
111     */
112    public void wipe() {
113        try {
114            sService.wipe();
115        } catch (RemoteException e) {
116            onError("wiping persistent partition");
117        }
118    }
119
120    /**
121     * Writes a byte enabling or disabling the ability to "OEM unlock" the device.
122     */
123    public void setOemUnlockEnabled(boolean enabled) {
124        try {
125            sService.setOemUnlockEnabled(enabled);
126        } catch (RemoteException e) {
127            onError("setting OEM unlock enabled to " + enabled);
128        }
129    }
130
131    /**
132     * Returns whether or not "OEM unlock" is enabled or disabled on this device.
133     */
134    public boolean getOemUnlockEnabled() {
135        try {
136            return sService.getOemUnlockEnabled();
137        } catch (RemoteException e) {
138            onError("getting OEM unlock enabled bit");
139            return false;
140        }
141    }
142
143    private void onError(String msg) {
144        Slog.v(TAG, "Remote exception while " + msg);
145    }
146}
147