diff --git a/lib/roles/device/plugins/input.js b/lib/roles/device/plugins/input.js index 3c9f5feb..a2d301f9 100644 --- a/lib/roles/device/plugins/input.js +++ b/lib/roles/device/plugins/input.js @@ -161,18 +161,60 @@ module.exports = syrup.serial() return callService(util.format("-a '%s'", apk.stopAction)) } - function sendInputEvent(event) { - agent.writer.write(new apk.agentProto.InputEvent(event).encodeNB()) + function keyEvent(data) { + return runAgentCommand( + apk.wire.RequestType.KEYEVENT + , new apk.wire.KeyEventRequest(data) + ) + } + + function type(text) { + return runAgentCommand( + apk.wire.RequestType.TYPE + , new apk.wire.TypeRequest(text) + ) + } + + function paste(text) { + return setClipboard(text) + .then(function() { + keyEvent({ + event: apk.wire.KeyEvent.PRESS + , keyCode: adb.Keycode.KEYCODE_V + , ctrlKey: true + }) + }) + } + + function wake() { + return runAgentCommand( + apk.wire.RequestType.WAKE + , new apk.wire.WakeRequest() + ) + } + + function freezeRotation(rotation) { + return runAgentCommand( + apk.wire.RequestType.SET_ROTATION + , new apk.wire.SetRotationRequest(rotation, true) + ) + } + + function thawRotation() { + return runAgentCommand( + apk.wire.RequestType.SET_ROTATION + , new apk.wire.SetRotationRequest(0, false) + ) } function version() { return runServiceCommand( - apk.serviceProto.RequestType.VERSION - , new apk.serviceProto.VersionRequest() + apk.wire.RequestType.VERSION + , new apk.wire.VersionRequest() ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.VersionResponse.decode(data) + var response = apk.wire.VersionResponse.decode(data) if (response.success) { return response.version } @@ -182,12 +224,12 @@ module.exports = syrup.serial() function unlock() { return runServiceCommand( - apk.serviceProto.RequestType.SET_KEYGUARD_STATE - , new apk.serviceProto.SetKeyguardStateRequest(false) + apk.wire.RequestType.SET_KEYGUARD_STATE + , new apk.wire.SetKeyguardStateRequest(false) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.SetKeyguardStateResponse.decode(data) + var response = apk.wire.SetKeyguardStateResponse.decode(data) if (!response.success) { throw new Error('Unable to unlock device') } @@ -196,12 +238,12 @@ module.exports = syrup.serial() function lock() { return runServiceCommand( - apk.serviceProto.RequestType.SET_KEYGUARD_STATE - , new apk.serviceProto.SetKeyguardStateRequest(true) + apk.wire.RequestType.SET_KEYGUARD_STATE + , new apk.wire.SetKeyguardStateRequest(true) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.SetKeyguardStateResponse.decode(data) + var response = apk.wire.SetKeyguardStateResponse.decode(data) if (!response.success) { throw new Error('Unable to lock device') } @@ -210,12 +252,12 @@ module.exports = syrup.serial() function acquireWakeLock() { return runServiceCommand( - apk.serviceProto.RequestType.SET_WAKE_LOCK - , new apk.serviceProto.SetWakeLockRequest(true) + apk.wire.RequestType.SET_WAKE_LOCK + , new apk.wire.SetWakeLockRequest(true) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.SetWakeLockResponse.decode(data) + var response = apk.wire.SetWakeLockResponse.decode(data) if (!response.success) { throw new Error('Unable to acquire WakeLock') } @@ -224,12 +266,12 @@ module.exports = syrup.serial() function releaseWakeLock() { return runServiceCommand( - apk.serviceProto.RequestType.SET_WAKE_LOCK - , new apk.serviceProto.SetWakeLockRequest(false) + apk.wire.RequestType.SET_WAKE_LOCK + , new apk.wire.SetWakeLockRequest(false) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.SetWakeLockResponse.decode(data) + var response = apk.wire.SetWakeLockResponse.decode(data) if (!response.success) { throw new Error('Unable to release WakeLock') } @@ -238,12 +280,12 @@ module.exports = syrup.serial() function identity() { return runServiceCommand( - apk.serviceProto.RequestType.IDENTIFY - , new apk.serviceProto.IdentifyRequest(options.serial) + apk.wire.RequestType.IDENTIFY + , new apk.wire.IdentifyRequest(options.serial) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.IdentifyResponse.decode(data) + var response = apk.wire.IdentifyResponse.decode(data) if (!response.success) { throw new Error('Unable to identify device') } @@ -252,15 +294,15 @@ module.exports = syrup.serial() function setClipboard(text) { return runServiceCommand( - apk.serviceProto.RequestType.SET_CLIPBOARD - , new apk.serviceProto.SetClipboardRequest( - apk.serviceProto.ClipboardType.TEXT + apk.wire.RequestType.SET_CLIPBOARD + , new apk.wire.SetClipboardRequest( + apk.wire.ClipboardType.TEXT , text ) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.SetClipboardResponse.decode(data) + var response = apk.wire.SetClipboardResponse.decode(data) if (!response.success) { throw new Error('Unable to set clipboard') } @@ -269,17 +311,17 @@ module.exports = syrup.serial() function getClipboard() { return runServiceCommand( - apk.serviceProto.RequestType.GET_CLIPBOARD - , new apk.serviceProto.GetClipboardRequest( - apk.serviceProto.ClipboardType.TEXT + apk.wire.RequestType.GET_CLIPBOARD + , new apk.wire.GetClipboardRequest( + apk.wire.ClipboardType.TEXT ) ) .timeout(10000) .then(function(data) { - var response = apk.serviceProto.GetClipboardResponse.decode(data) + var response = apk.wire.GetClipboardResponse.decode(data) if (response.success) { switch (response.type) { - case apk.serviceProto.ClipboardType.TEXT: + case apk.wire.ClipboardType.TEXT: return response.text } } @@ -289,12 +331,12 @@ module.exports = syrup.serial() function getBrowsers() { return runServiceCommand( - apk.serviceProto.RequestType.GET_BROWSERS - , new apk.serviceProto.GetBrowsersRequest() + apk.wire.RequestType.GET_BROWSERS + , new apk.wire.GetBrowsersRequest() ) .timeout(15000) .then(function(data) { - var response = apk.serviceProto.GetBrowsersResponse.decode(data) + var response = apk.wire.GetBrowsersResponse.decode(data) if (response.success) { delete response.success return response @@ -305,12 +347,12 @@ module.exports = syrup.serial() function getProperties(properties) { return runServiceCommand( - apk.serviceProto.RequestType.GET_PROPERTIES - , new apk.serviceProto.GetPropertiesRequest(properties) + apk.wire.RequestType.GET_PROPERTIES + , new apk.wire.GetPropertiesRequest(properties) ) .timeout(15000) .then(function(data) { - var response = apk.serviceProto.GetPropertiesResponse.decode(data) + var response = apk.wire.GetPropertiesResponse.decode(data) if (response.success) { var mapped = Object.create(null) response.properties.forEach(function(property) { @@ -324,7 +366,7 @@ module.exports = syrup.serial() function runServiceCommand(type, cmd) { var resolver = Promise.defer() - service.writer.write(new apk.serviceProto.RequestEnvelope( + service.writer.write(new apk.wire.RequestEnvelope( type , cmd.encodeNB() ).encodeNB()) @@ -332,6 +374,13 @@ module.exports = syrup.serial() return resolver.promise } + function runAgentCommand(type, cmd) { + agent.writer.write(new apk.wire.RequestEnvelope( + type + , cmd.encodeNB() + ).encodeNB()) + } + return openAgent() .then(openService) .then(function() { @@ -345,58 +394,47 @@ module.exports = syrup.serial() ]) }) .on(wire.KeyDownMessage, function(channel, message) { - sendInputEvent({ - action: apk.agentProto.InputAction.KEYDOWN + keyEvent({ + event: apk.wire.KeyEvent.DOWN , keyCode: keyutil.unwire(message.keyCode) }) }) .on(wire.KeyUpMessage, function(channel, message) { - sendInputEvent({ - action: apk.agentProto.InputAction.KEYUP + keyEvent({ + event: apk.wire.KeyEvent.UP , keyCode: keyutil.unwire(message.keyCode) }) }) .on(wire.KeyPressMessage, function(channel, message) { - sendInputEvent({ - action: apk.agentProto.InputAction.KEYPRESS + keyEvent({ + event: apk.wire.KeyEvent.PRESS , keyCode: keyutil.unwire(message.keyCode) }) }) .on(wire.TypeMessage, function(channel, message) { - sendInputEvent({ - action: apk.agentProto.InputAction.TYPE - , keyCode: 0 - , text: message.text - }) + type(message.text) }) .on(wire.RotateMessage, function(channel, message) { - sendInputEvent({ - action: message.rotation < 0 - ? apk.agentProto.InputAction.THAW_ROTATION - : apk.agentProto.InputAction.FREEZE_ROTATION - , rotation: message.rotation - }) + if (message.rotation >= 0) { + freezeRotation(message.rotation) + } + else { + thawRotation() + } }) return { - unlock: unlock - , lock: lock - , acquireWakeLock: acquireWakeLock - , releaseWakeLock: releaseWakeLock - , identity: identity - , paste: function(text) { - return setClipboard(text) - .then(function() { - sendInputEvent({ - action: apk.agentProto.InputAction.KEYPRESS - , keyCode: adb.Keycode.KEYCODE_V - , ctrlKey: true - }) - }) - } + acquireWakeLock: acquireWakeLock , copy: getClipboard , getBrowsers: getBrowsers , getProperties: getProperties + , identity: identity + , lock: lock + , paste: paste + , releaseWakeLock: releaseWakeLock + , unlock: unlock + , version: version + , wake: wake } }) }) diff --git a/lib/roles/device/resources/service.js b/lib/roles/device/resources/service.js index ece26c6a..6220682d 100644 --- a/lib/roles/device/resources/service.js +++ b/lib/roles/device/resources/service.js @@ -15,16 +15,13 @@ module.exports = syrup.serial() var log = logger.createLogger('device:resources:service') var resource = { - requiredVersion: '0.4.1' + requiredVersion: '0.5.0' , pkg: 'jp.co.cyberagent.stf' , main: 'jp.co.cyberagent.stf.Agent' , apk: pathutil.vendor('STFService/STFService.apk') - , agentProto: ProtoBuf.loadProtoFile( - pathutil.vendor('STFService/proto/agent.proto') - ).build().jp.co.cyberagent.stf.proto - , serviceProto: ProtoBuf.loadProtoFile( - pathutil.vendor('STFService/proto/service.proto') - ).build().jp.co.cyberagent.stf.proto + , wire: ProtoBuf.loadProtoFile( + pathutil.vendor('STFService/wire.proto') + ).build().jp.co.cyberagent.stf , startAction: 'jp.co.cyberagent.stf.ACTION_START' , stopAction: 'jp.co.cyberagent.stf.ACTION_STOP' } diff --git a/vendor/STFService/STFService.apk b/vendor/STFService/STFService.apk index 67b781b6..9da685e4 100644 Binary files a/vendor/STFService/STFService.apk and b/vendor/STFService/STFService.apk differ diff --git a/vendor/STFService/proto/agent.proto b/vendor/STFService/proto/agent.proto deleted file mode 100644 index 0756d5c0..00000000 --- a/vendor/STFService/proto/agent.proto +++ /dev/null @@ -1,29 +0,0 @@ -package jp.co.cyberagent.stf.proto; - -option java_outer_classname = "AgentProto"; - -enum InputAction { - KEYDOWN = 0; - KEYUP = 1; - KEYPRESS = 2; - TYPE = 3; - WAKE = 4; - FREEZE_ROTATION = 5; - THAW_ROTATION = 6; -} - -message InputEvent { - required InputAction action = 1; - optional int32 keyCode = 2; - optional bool shiftKey = 3; - optional bool ctrlKey = 4; - optional bool altKey = 5; - optional bool metaKey = 6; - optional bool symKey = 7; - optional bool functionKey = 8; - optional bool capsLockKey = 9; - optional bool scrollLockKey = 10; - optional bool numLockKey = 11; - optional string text = 12; - optional int32 rotation = 13; -} diff --git a/vendor/STFService/proto/service.proto b/vendor/STFService/wire.proto similarity index 70% rename from vendor/STFService/proto/service.proto rename to vendor/STFService/wire.proto index 7921e7c9..e0abfd57 100644 --- a/vendor/STFService/proto/service.proto +++ b/vendor/STFService/wire.proto @@ -1,6 +1,6 @@ -package jp.co.cyberagent.stf.proto; +package jp.co.cyberagent.stf; -option java_outer_classname = "ServiceProto"; +option java_outer_classname = "Wire"; enum RequestType { VERSION = 0; @@ -11,6 +11,10 @@ enum RequestType { GET_BROWSERS = 5; GET_PROPERTIES = 6; IDENTIFY = 7; + KEYEVENT = 8; + TYPE = 9; + WAKE = 10; + SET_ROTATION = 11; } message RequestEnvelope { @@ -18,6 +22,8 @@ message RequestEnvelope { required bytes request = 2; } +// Service + message VersionRequest { } @@ -102,3 +108,37 @@ message IdentifyRequest { message IdentifyResponse { required bool success = 1; } + +// Agent + +enum KeyEvent { + DOWN = 0; + UP = 1; + PRESS = 2; +} + +message KeyEventRequest { + required KeyEvent event = 1; + required int32 keyCode = 2; + optional bool shiftKey = 3; + optional bool ctrlKey = 4; + optional bool altKey = 5; + optional bool metaKey = 6; + optional bool symKey = 7; + optional bool functionKey = 8; + optional bool capsLockKey = 9; + optional bool scrollLockKey = 10; + optional bool numLockKey = 11; +} + +message TypeRequest { + required string text = 1; +} + +message SetRotationRequest { + required int32 rotation = 1; + required bool lock = 2; +} + +message WakeRequest { +}