FileDocCategorySizeDatePackage
UpdateLock.javaAPI DocAndroid 5.1 API5161Thu Mar 12 22:22:10 GMT 2015android.os

UpdateLock

public class UpdateLock extends Object
Advisory wakelock-like mechanism by which processes that should not be interrupted for OTA/update purposes can so advise the OS. This is particularly relevant for headless or kiosk-like operation.
hide

Fields Summary
private static final boolean
DEBUG
private static final String
TAG
private static IUpdateLock
sService
IBinder
mToken
int
mCount
boolean
mRefCounted
boolean
mHeld
final String
mTag
public static final String
UPDATE_LOCK_CHANGED
Broadcast Intent action sent when the global update lock state changes, i.e. when the first locker acquires an update lock, or when the last locker releases theirs. The broadcast is sticky but is sent only to registered receivers.
public static final String
NOW_IS_CONVENIENT
Boolean Intent extra on the UPDATE_LOCK_CHANGED sticky broadcast, indicating whether now is an appropriate time to interrupt device activity with an update operation. True means that updates are okay right now; false indicates that perhaps later would be a better time.
public static final String
TIMESTAMP
Long Intent extra on the UPDATE_LOCK_CHANGED sticky broadcast, marking the wall-clock time [in UTC] at which the broadcast was sent. Note that this is in the System.currentTimeMillis() time base, which may be non-monotonic especially around reboots.
Constructors Summary
public UpdateLock(String tag)
Construct an UpdateLock instance.

param
tag An arbitrary string used to identify this lock instance in dump output.


                           
       
        mTag = tag;
        mToken = new Binder();
    
Methods Summary
public voidacquire()
Acquire an update lock.

        if (DEBUG) {
            Log.v(TAG, "acquire() : " + this, new RuntimeException("here"));
        }
        checkService();
        synchronized (mToken) {
            acquireLocked();
        }
    
private voidacquireLocked()

        if (!mRefCounted || mCount++ == 0) {
            if (sService != null) {
                try {
                    sService.acquireUpdateLock(mToken, mTag);
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to contact service to acquire");
                }
            }
            mHeld = true;
        }
    
private static voidcheckService()

        
        if (sService == null) {
            sService = IUpdateLock.Stub.asInterface(
                    ServiceManager.getService(Context.UPDATE_LOCK_SERVICE));
        }
    
protected voidfinalize()

        synchronized (mToken) {
            // if mHeld is true, sService must be non-null
            if (mHeld) {
                Log.wtf(TAG, "UpdateLock finalized while still held");
                try {
                    sService.releaseUpdateLock(mToken);
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to contact service to release");
                }
            }
        }
    
public booleanisHeld()
Is this lock currently held?

        synchronized (mToken) {
            return mHeld;
        }
    
public voidrelease()
Release this update lock.

        if (DEBUG) Log.v(TAG, "release() : " + this, new RuntimeException("here"));
        checkService();
        synchronized (mToken) {
            releaseLocked();
        }
    
private voidreleaseLocked()

        if (!mRefCounted || --mCount == 0) {
            if (sService != null) {
                try {
                    sService.releaseUpdateLock(mToken);
                } catch (RemoteException e) {
                    Log.e(TAG, "Unable to contact service to release");
                }
            }
            mHeld = false;
        }
        if (mCount < 0) {
            throw new RuntimeException("UpdateLock under-locked");
        }
    
public voidsetReferenceCounted(boolean isRefCounted)
Change the refcount behavior of this update lock.

        if (DEBUG) {
            Log.v(TAG, "setting refcounted=" + isRefCounted + " : " + this);
        }
        mRefCounted = isRefCounted;