Home | History | Annotate | Download | only in persistentdata
      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 
     17 package android.service.persistentdata;
     18 
     19 import android.annotation.IntDef;
     20 import android.annotation.RequiresPermission;
     21 import android.annotation.SuppressLint;
     22 import android.annotation.SystemApi;
     23 import android.annotation.SystemService;
     24 import android.content.Context;
     25 import android.os.RemoteException;
     26 import android.service.oemlock.OemLockManager;
     27 
     28 import java.lang.annotation.Retention;
     29 import java.lang.annotation.RetentionPolicy;
     30 
     31 /**
     32  * Interface for reading and writing data blocks to a persistent partition.
     33  *
     34  * Allows writing one block at a time. Namely, each time
     35  * {@link PersistentDataBlockManager#write(byte[])}
     36  * is called, it will overwite the data that was previously written on the block.
     37  *
     38  * Clients can query the size of the currently written block via
     39  * {@link PersistentDataBlockManager#getDataBlockSize()}.
     40  *
     41  * Clients can query the maximum size for a block via
     42  * {@link PersistentDataBlockManager#getMaximumDataBlockSize()}
     43  *
     44  * Clients can read the currently written block by invoking
     45  * {@link PersistentDataBlockManager#read()}.
     46  *
     47  * @hide
     48  */
     49 @SystemApi
     50 @SystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE)
     51 public class PersistentDataBlockManager {
     52     private static final String TAG = PersistentDataBlockManager.class.getSimpleName();
     53     private IPersistentDataBlockService sService;
     54 
     55     /**
     56      * Indicates that the device's bootloader lock state is UNKNOWN.
     57      */
     58     public static final int FLASH_LOCK_UNKNOWN = -1;
     59     /**
     60      * Indicates that the device's bootloader is UNLOCKED.
     61      */
     62     public static final int FLASH_LOCK_UNLOCKED = 0;
     63     /**
     64      * Indicates that the device's bootloader is LOCKED.
     65      */
     66     public static final int FLASH_LOCK_LOCKED = 1;
     67 
     68     @IntDef({
     69         FLASH_LOCK_UNKNOWN,
     70         FLASH_LOCK_LOCKED,
     71         FLASH_LOCK_UNLOCKED,
     72     })
     73     @Retention(RetentionPolicy.SOURCE)
     74     public @interface FlashLockState {}
     75 
     76     /** @hide */
     77     public PersistentDataBlockManager(IPersistentDataBlockService service) {
     78         sService = service;
     79     }
     80 
     81     /**
     82      * Writes {@code data} to the persistent partition. Previously written data
     83      * will be overwritten. This data will persist across factory resets.
     84      *
     85      * Returns the number of bytes written or -1 on error. If the block is too big
     86      * to fit on the partition, returns -MAX_BLOCK_SIZE.
     87      *
     88      * {@link #wipe} will block any further {@link #write} operation until reboot,
     89      * in which case -1 will be returned.
     90      *
     91      * @param data the data to write
     92      */
     93     @SuppressLint("Doclava125")
     94     public int write(byte[] data) {
     95         try {
     96             return sService.write(data);
     97         } catch (RemoteException e) {
     98             throw e.rethrowFromSystemServer();
     99         }
    100     }
    101 
    102     /**
    103      * Returns the data block stored on the persistent partition.
    104      */
    105     @SuppressLint("Doclava125")
    106     public byte[] read() {
    107         try {
    108             return sService.read();
    109         } catch (RemoteException e) {
    110             throw e.rethrowFromSystemServer();
    111         }
    112     }
    113 
    114     /**
    115      * Retrieves the size of the block currently written to the persistent partition.
    116      *
    117      * Return -1 on error.
    118      */
    119     @RequiresPermission(android.Manifest.permission.ACCESS_PDB_STATE)
    120     public int getDataBlockSize() {
    121         try {
    122             return sService.getDataBlockSize();
    123         } catch (RemoteException e) {
    124             throw e.rethrowFromSystemServer();
    125         }
    126     }
    127 
    128     /**
    129      * Retrieves the maximum size allowed for a data block.
    130      *
    131      * Returns -1 on error.
    132      */
    133     public long getMaximumDataBlockSize() {
    134         try {
    135             return sService.getMaximumDataBlockSize();
    136         } catch (RemoteException e) {
    137             throw e.rethrowFromSystemServer();
    138         }
    139     }
    140 
    141     /**
    142      * Zeroes the previously written block in its entirety. Calling this method
    143      * will erase all data written to the persistent data partition.
    144      * It will also prevent any further {@link #write} operation until reboot,
    145      * in order to prevent a potential race condition. See b/30352311.
    146      */
    147     @RequiresPermission(android.Manifest.permission.OEM_UNLOCK_STATE)
    148     public void wipe() {
    149         try {
    150             sService.wipe();
    151         } catch (RemoteException e) {
    152             throw e.rethrowFromSystemServer();
    153         }
    154     }
    155 
    156     /**
    157      * Writes a byte enabling or disabling the ability to "OEM unlock" the device.
    158      *
    159      * @deprecated use {@link OemLockManager#setOemUnlockAllowedByUser(boolean)} instead.
    160      */
    161     @RequiresPermission(android.Manifest.permission.OEM_UNLOCK_STATE)
    162     public void setOemUnlockEnabled(boolean enabled) {
    163         try {
    164             sService.setOemUnlockEnabled(enabled);
    165         } catch (RemoteException e) {
    166             throw e.rethrowFromSystemServer();
    167         }
    168     }
    169 
    170     /**
    171      * Returns whether or not "OEM unlock" is enabled or disabled on this device.
    172      *
    173      * @deprecated use {@link OemLockManager#isOemUnlockAllowedByUser()} instead.
    174      */
    175     @RequiresPermission(anyOf = {
    176             android.Manifest.permission.READ_OEM_UNLOCK_STATE,
    177             android.Manifest.permission.OEM_UNLOCK_STATE
    178     })
    179     public boolean getOemUnlockEnabled() {
    180         try {
    181             return sService.getOemUnlockEnabled();
    182         } catch (RemoteException e) {
    183             throw e.rethrowFromSystemServer();
    184         }
    185     }
    186 
    187     /**
    188      * Retrieves available information about this device's flash lock state.
    189      *
    190      * @return {@link #FLASH_LOCK_LOCKED} if device bootloader is locked,
    191      * {@link #FLASH_LOCK_UNLOCKED} if device bootloader is unlocked, or {@link #FLASH_LOCK_UNKNOWN}
    192      * if this information cannot be ascertained on this device.
    193      */
    194     @RequiresPermission(anyOf = {
    195             android.Manifest.permission.READ_OEM_UNLOCK_STATE,
    196             android.Manifest.permission.OEM_UNLOCK_STATE
    197     })
    198     @FlashLockState
    199     public int getFlashLockState() {
    200         try {
    201             return sService.getFlashLockState();
    202         } catch (RemoteException e) {
    203             throw e.rethrowFromSystemServer();
    204         }
    205     }
    206 }
    207