Remote wakelock service

Remote wakelock service is built above the DataTransactor that acrosses the machine remote service.It is used for controlling the remote device screen.

Basic concept

RemoteWakeLockService

It is used for remote wakelock. It provides the function of initializing the remote wakelock and listening it's status. 

RemoteWakeLockManager

It represents the remote wakelock manager. It is the interface provided by the remote wakelock service. The remote wakelock service operation can be performed by the user as long as the device object is acquierd.

RemoteWakeLockCallBack

Defines the interface that includes the remote wakelock callback. RemoteRouteSearch

RemoteWakeLock

Provides the function that used for remote wakelock controlling. 

How to use

Remote wakelock service is a subservice of the IWDS system service, which is used in accordance with the IWDS service framework.

1.Uses IWDS service framework for getting remote wakelock manager.  Registeres remote wakelock callback. Uses remote wakelock manager for initializing remote wakelock instance. 

ServiceClient mServiceClient = new ServiceClient(mContext,
                RemoteWakeLockManager.SERVICE_REMOTE_WAKELOCK, mConnectionCallbacks);
mServiceClient.connect();

//在 mConnectionCallbacks 的回调函数onConnected中获取RemoteWakeLockManager实例
public void onConnected(ServiceClient serviceClient) {
    mRemoteWakeLockManager = (RemoteWakeLockManager) serviceClient.getServiceManagerContext();

    //注册远程唤醒锁回调接口,监听远程唤醒锁的状态。
    mRemoteWakeLockManager.registerRemoteWakeLockCallback(mRemoteWakeLockCallback);
    ... ...
    //获取RemoteWakeLock实例。
    mRemoteWakeLock = mRemoteWakeLockManager.newRemoteWakeLock(levelAndFlags, tag);
    ... ...
}

2.Uses remote wakelock instance for remote device screen controlling.

//长时持锁。
mRemoteWakeLock.acquire();
//限时持锁。
mRemoteWakeLock.acquire(timeout);
//释放持锁。
mRemoteWakeLock.release();
//判断持锁情况。
mRemoteWakeLock.isHeld();

3.Unregisteres remote wakelock callback when using finished or before exting applications.

mRemoteWakeLockManager.registerRemoteWakeLockCallback(null);

Interface

1.Remote wakelock service interface.

/**
 * 注册远程锁回调接口,一个管理器只能注册一个回调接口,后注册的接口会替换掉之前注册的接口。传入{@code null}为注销之前注册的回调接口。
 * 
 * @param callback 远程锁回调接口
 */
public void registerRemoteWakeLockCallback(RemoteWakeLockCallback callback);

/**
 * 获得一个新的远程锁
 * <p>
 * 注意:获得一个远程锁不代表持该远程锁,持远程锁应在获取远程锁得到远程锁实例后调用{@link RemoteWakeLock#acquire()}或
 * {@link RemoteWakeLock#acquire(long)}方法。远程锁的接口参考{@link PowerManager.WakeLock}的接口设计
 * 
 * @param levelAndFlags 远程锁的等级和标志,为了方便开发者使用,该标志保持与{@link PowerManager#newWakeLock(int, String)}
 *        使用的标志以及使用方法一致。如果标志使用不当,将与{@link PowerManager#newWakeLock(int, String)}一样抛出异常。
 * @param tag 远程锁的标签,为了方便开发者使用,该标签的要求与{@link PowerManager#newWakeLock(int, String)}的要求一致。如果传入
 *        <code>null</code>将抛出异常。
 * @return 新的远程锁对象
 * 
 * @see PowerManager#newWakeLock(int, String)
 */
public RemoteWakeLock newRemoteWakeLock(int levelAndFlags, String tag);

2.Remote wakelock interface.

/**
 * 持锁。长时间持锁,需要调用{@link #release()}释放锁。
 * 
 * @see PowerManager.WakeLock#acquire()
 * @see #acquire(long)
 * @see #release()
 * @see RemoteWakeLockCallback#onAcquireResult(RemoteWakeLock, long, int)
 */
 public void acquire();

/**
 * 持锁,在timeout时间内持锁,超时后将自动释放。
 * 
 * @param timeout 持锁超时,超时后自动释放锁
 * 
 * @see PowerManager.WakeLock#acquire(long)
 * @see #acquire()
 * @see #release()
 * @see RemoteWakeLockCallback#onAcquireResult(RemoteWakeLock, long, int)
 */
 public void acquire(long timeout);

/**
 * 释放之前的持锁。
 * 
 * @see PowerManager.WakeLock#release()
 * @see #acquire()
 * @see #acquire(long)
 * @see #isHeld()
 */
 public void release();

/**
 * 是否正在持锁。
 * <p>
 * 注意:是否正在持锁的值将在调用了{@link #acquire()}或{@link #acquire(long)}方法返回结果为成功之后(即触发
 * {@link RemoteWakeLockCallback#onAcquireResult(RemoteWakeLock, long, int)}回调方法并且返回结果为
 * {@link RemoteWakeLockManager#RESULT_OK})或者调用了{@link #release()}
 * 方法之后会更新,可以简单表明远程设备的持锁状态,却不是实时与对端设备的持锁状态相关联。
 * 
 * @return 正在持锁返回true,否则返回false。
 * 
 * @see PowerManager.WakeLock#isHeld()
 * @see #release()
 * @see RemoteWakeLockCallback#onAcquireResult(RemoteWakeLock, long, int)
 */
 public boolean isHeld();

3.Remote wakelock callback interface.

/**
 * 远程持锁结果回调函数,调用{@link RemoteWakeLock#acquire()}或{@link RemoteWakeLock#acquire(long)}
 * 方法产生结果后通过此回调函数返回。
 * 
 * @param wakeLock 产生持锁结果的远程锁
 * @param timeout 之前持锁用户给定的超时,无超时为-1。
 * @param resultCode 此次持锁的结果。{@link RemoteWakeLockManager#RESULT_OK}、
 *        {@link RemoteWakeLockManager#RESULT_FAILED_CHANNEL_UNAVAILABLE}、
 *        {@link RemoteWakeLockManager#RESULT_FAILED_LINK_DISCONNECTED}或
 *        {@link RemoteWakeLockManager#RESULT_FAILED_IWDS_CRASH}
 * 
 * @see RemoteWakeLock#release()
 * @see RemoteWakeLock#isHeld()
 */
 public void onAcquireResult(RemoteWakeLock wakeLock, long timeout, int resultCode);

Demo

Demo:elf/samples/RemoteWakeLockTest