device_api-android 1.2.8 → 1.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc299fd6553b73df6928d551ceb30f7bbfc43e87
4
- data.tar.gz: 18327f25f6fed8d4c9401ec2ff7d42a75800058d
3
+ metadata.gz: c09f0413dc5506bd026fdaa5f8bf85af6a18a404
4
+ data.tar.gz: 2bc91a6a0affe29634f881f5592d3be10bde1efb
5
5
  SHA512:
6
- metadata.gz: 1c7268f823bd12658fff01b5c186f83a62a05d5d68e937eaf06bc0245f0777bdafd4672a24365fc3d35681532a29dc9a05441753acbf9296b6e35ded3d58dfb4
7
- data.tar.gz: fd850b7e8544c44cba97ddbbee27260590164b5fd448c67db0d2c49a6def61b44f1bac420e7db2e90d982dcec2d83de16ec307eabe0cde89050af75012e1ff76
6
+ metadata.gz: 5247d30f5ce2233891a6f3770c5283b753466f6e9fd81249e1184ec14ea9a9064843228f924c01ea07325449b95a76c3e119b933634d95f81558068188438856
7
+ data.tar.gz: 9fb2718ad39a1ceceaefa7ce4e1b2251d0b39d5afbbb109819f299b635d0d75b1438f8d76d7f28f8d4e48f3462775aaf1c2cfef578ffbc2fac41e97054c30855
data/README.md CHANGED
@@ -25,6 +25,34 @@ Try connecting an android device with usb, and run:
25
25
 
26
26
  You might need to set your device to developer mode, and turn on usb debugging so that the android debug bridge can detect your device.
27
27
 
28
+ ### Connecting and disconnecting from Remote Devices
29
+
30
+ You can connect to devices via their IP address and port number. The syntax is:
31
+
32
+ DeviceAPI::Android.connect(<IP address>,<port number>=5555)
33
+
34
+ This should add a device to the already connected devices, which you can query with DeviceAPI::Android.devices. You can disconnect from a device like so:
35
+
36
+ DeviceAPI::Android.disconnect(<IP address>,<port number>=5555)
37
+
38
+ Once connected, the IP address and port number combination becomes the serial for the device, and you can execute commands such as adb shell through specifying the IP address/port number instead of the serial number. For both Android.connect and Android.disconnect, if port number is not specified, and ip address is only specified, port number defaults to 5555. (Note that Android.disconnect doesn't automagically disconnect you from a connection with a port number that is not 5555 when it is called without a port argument)
39
+
40
+ You can also use the disconnect method on a Android device object, without any arguments to disconnect a device. It will throw an error if the device is not connected.
41
+
42
+ device.disconnect
43
+
44
+ You can use device.is_remote? to determine if the device is a remote device, e.g. it has a ipaddress and port as an adb serial, and can attempt to be connected to.
45
+
46
+ device.is_remote?
47
+
48
+ DeviceAPI::Android::ADB::DeviceAlreadyConnectedError is raised when DeviceAPI::Android.connect is called on an already connected device.
49
+
50
+ DeviceAPI::Android::DeviceDisconnectedWhenNotARemoteDevice is raised when we are attempting to call disconnect on a device that is not a remote device, e.g. it has no hope of being disconnected from.
51
+
52
+ DeviceAPI::Android::ADBCommandError is raised when we cannot connect to a device, e.g. adb times out.
53
+
54
+ DeviceAPI::Android::ADBCommandError is raised when we cannot disconnect from a device, e.g. adb times out or we were already disconnected.
55
+
28
56
  ### Detecting devices
29
57
 
30
58
  There are two methods for detecting devices:
@@ -105,7 +133,7 @@ You should be presented with something similar to this:
105
133
  Bus 001 Device 011: ID 05c6:6765 Qualcomm, Inc.
106
134
  Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
107
135
 
108
- The important thing to note here is the Vendor ID and Product ID for the device. In the case of the above, the device is a Tesco Hudl (showing as an Archos device) with the combinded ID of 0e79:5009 - 0e79 is the Vendor ID while 5009 is the Product ID. Open the 51-android.rules file and add the following line:
136
+ The important thing to note here is the Vendor ID and Product ID for the device. In the case of the above, the device is a Tesco Hudl (showing as an Archos device) with the combined ID of 0e79:5009 - 0e79 is the Vendor ID while 5009 is the Product ID. Open the 51-android.rules file and add the following line:
109
137
 
110
138
  SUBSYSTEM=="usb", ATTR{idVendor}=="0e79", ATTR{idProduct}=="5009", MODE="0666", OWNER="hive"
111
139
 
@@ -115,4 +143,4 @@ Change the Vendor and Product IDs where appropriate, also check that the owner m
115
143
 
116
144
  *DeviceAPI-Android* is available to everyone under the terms of the MIT open source licence. Take a look at the LICENSE file in the code.
117
145
 
118
- Copyright (c) 2015 BBC
146
+ Copyright (c) 2016 BBC
@@ -233,6 +233,30 @@ module DeviceAPI
233
233
  shell(serial, cmd)
234
234
  end
235
235
 
236
+ def self.connect(ipaddress, port = 5555)
237
+ ipaddressandport = "#{ipaddress}:#{port}"
238
+ check_ip_address(ipaddressandport)
239
+ cmd = "adb connect #{ipaddressandport}"
240
+ result = execute(cmd)
241
+ if result.stdout.to_s =~ /.*already connected to.*/
242
+ raise DeviceAlreadyConnectedError.new("Device #{ipaddressandport} already connected")
243
+ else
244
+ unless result.stdout.to_s =~ /.*connected to.*/
245
+ raise ADBCommandError.new("Unable to adb connect to #{ipaddressandport} result was: #{result.stdout}")
246
+ end
247
+ end
248
+ end
249
+
250
+ def self.disconnect(ipaddress, port = 5555)
251
+ ipaddressandport = "#{ipaddress}:#{port}"
252
+ check_ip_address(ipaddressandport)
253
+ cmd = "adb disconnect #{ipaddressandport}"
254
+ result = execute(cmd)
255
+ unless result.exit == 0
256
+ raise ADBCommandError.new("Unable to adb disconnect to #{ipaddressandport} result was: #{result.stdout}")
257
+ end
258
+ end
259
+
236
260
  # Returns wifi status and access point name
237
261
  # @param serial serial number of device
238
262
  # @example
@@ -254,8 +278,7 @@ module DeviceAPI
254
278
  # @param [String] serial serial number of device
255
279
  # @param [String] command command to execute
256
280
  def self.shell(serial, command)
257
- result = execute("adb -s #{serial} shell #{command}")
258
-
281
+ result = execute("adb -s '#{serial}' shell #{command}")
259
282
  case result.stderr
260
283
  when /^error: device unauthorized./
261
284
  raise DeviceAPI::UnauthorizedDevice, result.stderr
@@ -275,8 +298,8 @@ module DeviceAPI
275
298
  # @option coords [String] :x_to (0) Coordinate to end on on the X axis
276
299
  # @option coords [String] :y_from (0) Coordinate to start from on the Y axis
277
300
  # @option coords [String] :y_to (0) Coordinate to end on on the Y axis
278
- def self.swipe(serial, coords = {x_from: 0, x_to: 0, y_from: 0, y_to: 0 })
279
- shell(serial, "input swipe #{coords[:x_from]} #{coords[:x_to]} #{coords[:y_from]} #{coords[:y_to]}").stdout
301
+ def self.swipe(serial, coords = {x_from: 0, y_from: 0, x_to: 0, y_to: 0 })
302
+ shell(serial, "input swipe #{coords[:x_from]} #{coords[:y_from]} #{coords[:x_to]} #{coords[:y_to]}").stdout
280
303
  end
281
304
 
282
305
  # Starts intent using adb
@@ -315,7 +338,11 @@ module DeviceAPI
315
338
  result.include?('true')
316
339
  end
317
340
 
318
-
341
+ def self.check_ip_address(ipaddressandport)
342
+ unless ipaddressandport =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}):[0-9]+\Z/
343
+ raise ADBCommandError.new("Invalid IP address and port #{ipaddressandport}")
344
+ end
345
+ end
319
346
  end
320
347
 
321
348
  # ADB Error class
@@ -324,6 +351,11 @@ module DeviceAPI
324
351
  super(msg)
325
352
  end
326
353
  end
354
+ class DeviceAlreadyConnectedError < ADBCommandError
355
+ def initialize(msg)
356
+ super(msg)
357
+ end
358
+ end
327
359
 
328
360
  end
329
361
  end
@@ -7,7 +7,11 @@ module DeviceAPI
7
7
  # work due to Amazons implementation of the Keyguard.
8
8
  def unlock
9
9
  ADB.keyevent(serial, '26') unless screen_on?
10
- ADB.swipe(serial, { x_from: 900, x_to: 300, y_from: 100, y_to: 100 } )
10
+ if orientation == :landscape
11
+ ADB.swipe(serial, { x_from: 500, y_from: 575, x_to: 500, y_to: 250 } )
12
+ else
13
+ ADB.swipe(serial, { x_from: 300, y_from: 900, x_to: 300, y_to: 600 } )
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -27,6 +27,11 @@ module DeviceAPI
27
27
  def initialize(options = {})
28
28
  @serial = options[:serial]
29
29
  @state = options[:state]
30
+ @remote = options[:remote] ? true : false
31
+ end
32
+
33
+ def is_remote?
34
+ @remote || false
30
35
  end
31
36
 
32
37
  # Mapping of device status - used to provide a consistent status across platforms
@@ -41,6 +46,13 @@ module DeviceAPI
41
46
  }[@state]
42
47
  end
43
48
 
49
+ def disconnect
50
+ unless is_remote?
51
+ raise DeviceAPI::Android::DeviceDisconnectedWhenNotARemoteDevice.new("Asked to disconnect device #{serial} when it is not a remote device")
52
+ end
53
+ ADB.disconnect(serial)
54
+ end
55
+
44
56
  # Return the device range
45
57
  # @return (String) device range string
46
58
  def range
@@ -278,7 +290,7 @@ module DeviceAPI
278
290
  wifi[:mac] unless wifi.nil?
279
291
  end
280
292
  end
281
-
293
+
282
294
  private
283
295
 
284
296
  def get_network_info
@@ -323,7 +335,6 @@ module DeviceAPI
323
335
  ADB.getpowerinfo(serial)
324
336
  end
325
337
 
326
-
327
338
  def get_phoneinfo
328
339
  ADB.getphoneinfo(serial)
329
340
  end
@@ -340,5 +351,12 @@ module DeviceAPI
340
351
  ADB.get_device_dpi(serial)
341
352
  end
342
353
  end
354
+
355
+ class DeviceDisconnectedWhenNotARemoteDevice < StandardError
356
+ def initialize(msg)
357
+ super(msg)
358
+ end
359
+ end
360
+
343
361
  end
344
362
  end
@@ -19,7 +19,9 @@ module DeviceAPI
19
19
  def self.devices
20
20
  ADB.devices.map do |d|
21
21
  if d.keys.first && !d.keys.first.include?('?')
22
- DeviceAPI::Android::Device.create( self.get_device_type(d), { serial: d.keys.first, state: d.values.first } )
22
+ serial = d.keys.first
23
+ remote = check_if_remote_device?(serial)
24
+ DeviceAPI::Android::Device.create( self.get_device_type(d), { serial: serial, state: d.values.first, remote: remote} )
23
25
  end
24
26
  end.compact
25
27
  end
@@ -30,18 +32,38 @@ module DeviceAPI
30
32
  raise DeviceAPI::BadSerialString.new("serial was '#{serial.nil? ? 'nil' : serial}'")
31
33
  end
32
34
  state = ADB.get_state(serial)
33
- DeviceAPI::Android::Device.create( self.get_device_type({ :"#{serial}" => state}), { serial: serial, state: state })
35
+ remote = check_if_remote_device?(serial)
36
+ DeviceAPI::Android::Device.create( self.get_device_type({ :"#{serial}" => state}), { serial: serial, state: state, remote: remote })
37
+ end
38
+
39
+ def self.connect(ipaddress,port=5555)
40
+ ADB.connect(ipaddress,port)
41
+ end
42
+
43
+ def self.disconnect(ipaddress,port=5555)
44
+ ADB.disconnect(ipaddress,port)
45
+ end
46
+
47
+ def self.check_if_remote_device?(serial)
48
+ begin
49
+ ADB::check_ip_address(serial)
50
+ true
51
+ rescue ADBCommandError
52
+ false
53
+ end
34
54
  end
35
55
 
36
56
  # Return the device type used in determining which Device Object to create
37
57
  def self.get_device_type(options)
38
58
  return :default if options.values.first == 'unauthorized'
59
+ serial = options.keys.first
60
+ state = ADB.get_state(serial)
39
61
  begin
40
- man = Device.new(serial: options.keys.first, state: options.values.first).manufacturer
62
+ man = Device.new(serial: serial, state: state).manufacturer
41
63
  rescue DeviceAPI::DeviceNotFound
42
64
  return :default
43
65
  rescue => e
44
- puts "Unrecognised exception whilst finding device '#{options.keys.first}' (state: #{options.values.first})"
66
+ puts "Unrecognised exception whilst finding device '#{serial}' (state: #{options.values.first})"
45
67
  puts e.message
46
68
  puts e.backtrace.inspect
47
69
  return :default
@@ -57,9 +79,9 @@ module DeviceAPI
57
79
  end
58
80
  type
59
81
  end
60
- end
61
82
 
62
83
  # Serial error class
63
- class BadSerialString < StandardError
84
+ class BadSerialString < StandardError
85
+ end
64
86
  end
65
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: device_api-android
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.8
4
+ version: 1.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Buckhurst
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-04-07 00:00:00.000000000 Z
14
+ date: 2016-08-09 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: device_api
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 2.4.8
82
+ rubygems_version: 2.6.6
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: Android Device Management API