Exemplo n.º 1
0
def configure_wifi_and_airplane_mode(wifi_enabled=False):
  """Configure airplane mode and wifi on device."""
  # Airplane mode should be disabled in all cases. This can get inadvertently
  # turned on via gestures.
  adb.disable_airplane_mode()

  # Need to disable wifi before changing configuration.
  adb.disable_wifi()

  # Check if wifi needs to be enabled. If not, then no need to modify the
  # supplicant file.
  wifi_enabled = wifi_enabled or environment.get_value('WIFI', True)
  if not wifi_enabled:
    # No more work to do, we already disabled it at start.
    return

  if adb.is_gce():
    wifi_ssid = 'VirtWifi'
    wifi_password = ''
  else:
    config = db_config.get()
    if not config.wifi_ssid:
      logs.log('No wifi ssid is set, skipping wifi config.')
      return
    wifi_ssid = config.wifi_ssid
    wifi_password = config.wifi_password or ''

  adb.enable_wifi()

  # Wait 2 seconds to allow the wifi to be enabled.
  time.sleep(2)

  wifi_util_apk_path = os.path.join(
      environment.get_platform_resources_directory(), 'wifi_util.apk')
  if not adb.is_package_installed(WIFI_UTIL_PACKAGE_NAME):
    adb.install_package(wifi_util_apk_path)

  connect_wifi_command = (
      'am instrument -e method connectToNetwork -e ssid {ssid} ')
  if wifi_password:
    connect_wifi_command += '-e psk {password} '
  connect_wifi_command += '-w {call_path}'

  output = adb.run_adb_shell_command(
      connect_wifi_command.format(
          ssid=quote(wifi_ssid),
          password=quote(wifi_password),
          call_path=WIFI_UTIL_CALL_PATH))
  if 'result=true' not in output:
    logs.log_error('Failed to connect to wifi.', output=output)
Exemplo n.º 2
0
def wait_for_battery_charge_if_needed():
    """Check device battery and make sure it is charged beyond minimum level and
  temperature thresholds."""
    # Battery levels are not applicable on GCE.
    if adb.is_gce():
        return

    # Make sure device is online.
    adb.wait_for_device()

    # Skip battery check if done recently.
    last_battery_check_time = persistent_cache.get_value(
        LAST_BATTERY_CHECK_TIME_KEY,
        constructor=datetime.datetime.utcfromtimestamp)
    if last_battery_check_time and not dates.time_has_expired(
            last_battery_check_time, seconds=BATTERY_CHECK_INTERVAL):
        return

    # Initialize variables.
    battery_level_threshold = environment.get_value(
        'LOW_BATTERY_LEVEL_THRESHOLD', LOW_BATTERY_LEVEL_THRESHOLD)
    battery_temperature_threshold = environment.get_value(
        'MAX_BATTERY_TEMPERATURE_THRESHOLD', MAX_BATTERY_TEMPERATURE_THRESHOLD)
    device_restarted = False

    while 1:
        battery_information = get_battery_information()
        if battery_information is None:
            logs.log_error(
                'Failed to get battery information, skipping check.')
            return

        battery_level = battery_information['level']
        battery_temperature = battery_information['temperature']
        logs.log(
            'Battery information: level (%d%%), temperature (%.1f celsius).' %
            (battery_level, battery_temperature))
        if (battery_level >= battery_level_threshold
                and battery_temperature <= battery_temperature_threshold):
            persistent_cache.set_value(LAST_BATTERY_CHECK_TIME_KEY,
                                       time.time())
            return

        logs.log('Battery in bad battery state, putting device in sleep mode.')

        if not device_restarted:
            reboot()
            adb.disable_wifi()
            device_restarted = True

        # Change thresholds to expected levels (only if they were below minimum
        # thresholds).
        if battery_level < battery_level_threshold:
            battery_level_threshold = EXPECTED_BATTERY_LEVEL
        if battery_temperature > battery_temperature_threshold:
            battery_temperature_threshold = EXPECTED_BATTERY_TEMPERATURE

        # Stopping shell should help with shutting off a lot of services that would
        # otherwise use up the battery. However, we need to turn it back on to get
        # battery status information. Also, turn off display explicitly (needed for
        # Nexus 9s).
        turn_off_display_if_needed()
        adb.stop_shell()
        time.sleep(BATTERY_CHARGE_INTERVAL)
        adb.start_shell()