FileDocCategorySizeDatePackage
SetAlarm.javaAPI DocAndroid 5.1 API5203Thu Mar 12 22:22:44 GMT 2015com.android.testing.sleephelper

SetAlarm

public class SetAlarm extends android.app.Instrumentation

Fields Summary
private static final String
COMMAND
private static final String
PARAM
private static final String
CMD_PREPARE
private static final String
CMD_SET
private static final String
CMD_DONE
private static final String
SERVICE_ACTION
private static final String
SERVICE_PKG
private static final String
LOG_TAG
private com.android.testing.alarmservice.Alarm
mAlarmService
private android.os.Bundle
mArgs
private String
mCommand
private android.content.Intent
mServceIntent
private android.content.ServiceConnection
mConn
Constructors Summary
Methods Summary
private voidcallDone()

        try {
            mAlarmService.done();
        } catch (RemoteException e) {
            errorFinish("RemoteExeption in prepare()");
        } finally {
            getContext().unbindService(mConn);
        }
        // explicitly stop the service (started in prepare()) so that the service is now free
        // to be reclaimed
        getContext().stopService(mServceIntent);
    
private voidcallPrepare()

        try {
            mAlarmService.prepare();
        } catch (RemoteException e) {
            errorFinish("RemoteExeption in prepare()");
        } finally {
            getContext().unbindService(mConn);
        }
    
private voidcallSetAndWait(long timeoutMills)

        try {
            mAlarmService.setAlarmAndWait(timeoutMills);
        } catch (RemoteException e) {
            errorFinish("RemoteExeption in setAlarmAndWait()");
        } finally {
            getContext().unbindService(mConn);
        }
    
private voidconnectToAlarmService()

        // start the service with an intent, this ensures the service keeps running after unbind
        ComponentName cn = getContext().startService(mServceIntent);
        if (cn == null) {
            errorFinish("failed to start service");
        }
        if (!getContext().bindService(mServceIntent, mConn, Context.BIND_AUTO_CREATE)) {
            errorFinish("failed to bind service");
        }
    
private voiderrorFinish(java.lang.String msg)

        Bundle ret = new Bundle();
        ret.putString("error", msg);
        finish(Activity.RESULT_CANCELED, ret);
    
private voidhandleCommands()



       
        if (CMD_PREPARE.equals(mCommand)) {
            callPrepare();
        } else if (CMD_SET.equals(mCommand)) {
            String paramString = mArgs.getString(PARAM);
            if (paramString == null) {
                errorFinish("argument expected for alarm time");
            }
            long timeout = -1;
            try {
                timeout = Long.parseLong(paramString);
            } catch (NumberFormatException nfe) {
                errorFinish("a number argument is expected");
            }
            callSetAndWait(timeout);
        } else if (CMD_DONE.equals(mCommand)) {
            callDone();
        } else {
            errorFinish("Unrecognized command: " + mCommand);
        }
        finish(Activity.RESULT_OK, new Bundle());
    
public voidonCreate(android.os.Bundle arguments)

        super.onCreate(arguments);
        mCommand = arguments.getString(COMMAND);
        if ("true".equals(arguments.getString("debug"))) {
            Debug.waitForDebugger();
        }
        if (mCommand == null) {
            errorFinish("No command specified");
        }
        mArgs = arguments;
        connectToAlarmService();