From fe61ec8b8cea8f53c4c3081e60ee1968685d0660 Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Thu, 24 Oct 2013 10:41:55 -0700 Subject: [PATCH] fix unlock retry logic so we actually unlock (fix #1295) --- lib/devices/android/android-common.js | 30 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) 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();