"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var os_1 = require("os");
var application_manager_base_1 = require("../application-manager-base");
var AndroidApplicationManager = (function (_super) {
    __extends(AndroidApplicationManager, _super);
    function AndroidApplicationManager(adb, identifier, $staticConfig, $options, $logcatHelper) {
        _super.call(this);
        this.adb = adb;
        this.identifier = identifier;
        this.$staticConfig = $staticConfig;
        this.$options = $options;
        this.$logcatHelper = $logcatHelper;
    }
    AndroidApplicationManager.prototype.getInstalledApplications = function () {
        var _this = this;
        return (function () {
            var result = _this.adb.executeShellCommand(["pm", "list", "packages"]).wait() || "";
            var regex = /package:(.+)/;
            return result.split(os_1.EOL)
                .map(function (packageString) {
                var match = packageString.match(regex);
                return match ? match[1] : null;
            })
                .filter(function (parsedPackage) { return parsedPackage !== null; });
        }).future()();
    };
    AndroidApplicationManager.prototype.installApplication = function (packageFilePath) {
        return this.adb.executeCommand(["install", "-r", ("" + packageFilePath)]);
    };
    AndroidApplicationManager.prototype.uninstallApplication = function (appIdentifier) {
        return this.adb.executeShellCommand(["pm", "uninstall", ("" + appIdentifier)]);
    };
    AndroidApplicationManager.prototype.startApplication = function (appIdentifier) {
        var _this = this;
        return (function () {
            _this.adb.executeShellCommand(["am", "start",
                "-a", "android.intent.action.MAIN",
                "-n", (appIdentifier + "/" + _this.$staticConfig.START_PACKAGE_ACTIVITY_NAME),
                "-c", "android.intent.category.LAUNCHER"]).wait();
            if (!_this.$options.justlaunch) {
                _this.$logcatHelper.start(_this.identifier);
            }
        }).future()();
    };
    AndroidApplicationManager.prototype.stopApplication = function (appIdentifier) {
        return this.adb.executeShellCommand(["am", "force-stop", ("" + appIdentifier)]);
    };
    AndroidApplicationManager.prototype.canStartApplication = function () {
        return true;
    };
    return AndroidApplicationManager;
})(application_manager_base_1.ApplicationManagerBase);
exports.AndroidApplicationManager = AndroidApplicationManager;
