diff --git a/lib/devices/android/android-common.js b/lib/devices/android/android-common.js index 42104d99a24..a12a8179cea 100644 --- a/lib/devices/android/android-common.js +++ b/lib/devices/android/android-common.js @@ -211,21 +211,27 @@ androidCommon.unlockScreen = function(cb) { this.adb.isScreenLocked(function(err, isLocked) { if (err) return cb(err); if (isLocked) { - this.adb.startApp("io.appium.unlock", ".Unlock", function(err) { - if (err) return cb(err); - var intervalRepeat = 500; - var interval = function() { + var timeoutMs = 10000; + var start = Date.now(); + var unlockAndCheck = function() { + logger.debug("Screen is locked, trying to unlock"); + this.adb.startApp("io.appium.unlock", ".Unlock", function(err) { + if (err) return cb(err); this.adb.isScreenLocked(function(err, isLocked) { if (err) return cb(err); - if (isLocked) { - setTimeout(interval, intervalRepeat); - } else { - cb(); + if (!isLocked) { + logger.debug("Screen is unlocked, continuing"); + return cb(); } - }); - }.bind(this); - setTimeout(interval, intervalRepeat); - }.bind(this)); + if ((Date.now() - timeoutMs) > start) { + return cb(new Error("Screen did not unlock")); + } else { + setTimeout(unlockAndCheck, 1000); + } + }.bind(this)); + }.bind(this)); + }.bind(this); + unlockAndCheck(); } else { logger.debug('Screen already unlocked, continuing.'); cb();