Search in sources :

Example 1 with AttnScriptUtil

use of com.att.aro.core.util.AttnScriptUtil in project VideoOptimzer by attdevsupport.

the class NorootedAndroidCollectorImpl method startCollector.

/**
 * Start collector in background and returns result which indicates success or error and detail data.
 *
 * @param folderToSaveTrace
 *            directory to save trace to
 * @param videoOption
 *            optional flag to capture video of device. default is false
 * @param isLiveViewVideo
 *            ignored here
 * @param androidId
 *            optional id of device to capture. default is the connected device.
 * @param extraParams
 *            optional data to pass to collectors. required by some collectors.
 * @return a StatusResult to hold result and success or failure
 */
@Override
public StatusResult startCollector(boolean isCommandLine, String folderToSaveTrace, VideoOption videoOption_deprecated, boolean isLiveViewVideo, String deviceId, Hashtable<String, Object> extraParams, String password) {
    LOG.info("startCollector() for non-rooted-android-collector");
    if (deviceChangeListener == null) {
        initDeviceChangeListener();
    }
    AttenuatorModel atnr = new AttenuatorModel();
    int throttleDL = -1;
    int throttleUL = -1;
    boolean atnrProfile = false;
    String location = "";
    String selectedAppName = "";
    Orientation videoOrientation = Orientation.PORTRAIT;
    if (extraParams != null) {
        atnr = (AttenuatorModel) getOrDefault(extraParams, "AttenuatorModel", atnr);
        videoOption = (VideoOption) getOrDefault(extraParams, "video_option", VideoOption.NONE);
        videoOrientation = (Orientation) getOrDefault(extraParams, "videoOrientation", Orientation.PORTRAIT);
        selectedAppName = (String) getOrDefault(extraParams, "selectedAppName", StringUtils.EMPTY);
        traceDesc = (String) getOrDefault(extraParams, "traceDesc", StringUtils.EMPTY);
        traceType = (String) getOrDefault(extraParams, "traceType", StringUtils.EMPTY);
        targetedApp = (String) getOrDefault(extraParams, "targetedApp", StringUtils.EMPTY);
        appProducer = (String) getOrDefault(extraParams, "appProducer", StringUtils.EMPTY);
    }
    int bitRate = videoOption.getBitRate();
    String screenSize = videoOption.getScreenSize();
    StatusResult result = new StatusResult();
    // find the device by the id
    result = findDevice(deviceId, result);
    if (!result.isSuccess()) {
        return result;
    }
    this.running = isCollectorRunning();
    // avoid running it twice
    if (this.running) {
        LOG.error("unknown collection still running on device");
        result.setError(ErrorCodeRegistry.getCollectorAlreadyRunning());
        result.setSuccess(false);
        return result;
    }
    if (filemanager.directoryExistAndNotEmpty(folderToSaveTrace)) {
        result.setError(ErrorCodeRegistry.getTraceDirExist());
        return result;
    }
    // there might be permission issue to creating dir to save trace
    filemanager.mkDir(folderToSaveTrace);
    if (!filemanager.directoryExist(folderToSaveTrace)) {
        result.setError(ErrorCodeRegistry.getFailedToCreateLocalTraceDirectory());
        result.setSuccess(false);
        return result;
    }
    aroDevice = new AROAndroidDevice(device, false);
    cleanARO();
    if (device.isEmulator()) {
        if (!aroDevice.getAbi().equals("x86_64")) {
            String message = "Emulator ABI:" + aroDevice.getAbi() + " does not support VPN collection! use an x86_64 instead.";
            LOG.error(message);
            result.setError(ErrorCodeRegistry.getNotSupported(message));
            return result;
        }
    }
    // Is this required?????
    LOG.debug("check VPN");
    if (isVpnActivated()) {
        LOG.error("unknown collection still running on device");
        result.setError(ErrorCodeRegistry.getCollectorAlreadyRunning());
        result.setSuccess(false);
        return result;
    }
    this.localTraceFolder = folderToSaveTrace;
    // there might be an instance of vpn_collector running
    // to be sure it is not in memory
    this.haltCollectorInDevice();
    new LogcatCollector(adbService, device.getSerialNumber()).clearLogcat();
    atnrProfile = atnr.isLoadProfile();
    if (atnrProfile) {
        // default
        int apiNumber = AndroidApiLevel.K19.levelNumber();
        try {
            apiNumber = Integer.parseInt(aroDevice.getApi());
        } catch (Exception e) {
            LOG.error("unknown device api number");
        }
        location = (String) atnr.getLocalPath();
        AttnScriptUtil util = new AttnScriptUtil(apiNumber);
        boolean scriptResult = util.scriptGenerator(location);
        if (!scriptResult) {
            result.setError(ErrorCodeRegistry.getScriptAdapterError(location));
            result.setSuccess(false);
            return result;
        }
        this.attnrScriptRun = true;
    } else if (atnr.isConstantThrottle()) {
        if (atnr.isThrottleDLEnabled()) {
            throttleDL = atnr.getThrottleDL();
        }
        if (atnr.isThrottleULEnabled()) {
            throttleUL = atnr.getThrottleUL();
        }
    }
    if (pushApk(this.device)) {
        try {
            // This is a temporary fix, the real fix needs to be investigated. Follow up with Bug # ARO22945-1571
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            LOG.error("Something went wrong while trying to install the apk", e);
        }
        if (apkInstalled && (boolean) getOrDefault(extraParams, "assignPermission", false)) {
            applyPermissionsOverADB();
            apkInstalled = false;
        }
        String cmd;
        cmd = "am start -n com.att.arocollector/com.att.arocollector.AROCollectorActivity" + // + " --ei delayDL " + delayTimeDL + " --ei delayUL " + delayTimeUL
        " --ei throttleDL " + throttleDL + " --ei throttleUL " + throttleUL + (atnrProfile ? (" --ez profile " + atnrProfile + " --es profilename '" + location + "'") : "") + " --es video " + videoOption.toString() + " --ei bitRate " + bitRate + " --es screenSize " + screenSize + " --es videoOrientation " + videoOrientation.toString() + " --es selectedAppName " + (StringUtils.isEmpty(selectedAppName) ? "EMPTY" : selectedAppName) + " --activity-single-top --activity-clear-top";
        LOG.info(cmd);
        if (!android.runApkInDevice(this.device, cmd)) {
            result.setError(ErrorCodeRegistry.getFaildedToRunVpnApk());
            result.setSuccess(false);
            return result;
        }
    } else {
        result.setError(ErrorCodeRegistry.getFailToInstallAPK());
        result.setSuccess(false);
        return result;
    }
    if (!isTrafficCaptureRunning(MILLISECONDSFORTIMEOUT)) {
        // timeout while waiting for VPN to activate within 15 seconds
        timeOutShutdown();
        result.setError(ErrorCodeRegistry.getTimeoutVpnActivation());
        result.setSuccess(false);
        return result;
    } else {
        // Write environment details
        try {
            EnvironmentDetails environmentDetails = new EnvironmentDetails(folderToSaveTrace);
            environmentDetails.populateDeviceInfo(aroDevice.getOS(), aroDevice.isRooted(), aroDevice.getPlatform().name());
            FileWriter writer = new FileWriter(folderToSaveTrace + "/environment_details.json");
            writer.append(new ObjectMapper().writeValueAsString(environmentDetails));
            writer.close();
        } catch (IOException e) {
            LOG.error("Error while writing environment details", e);
        }
    }
    new Thread(() -> {
        GoogleAnalyticsUtil.getGoogleAnalyticsInstance().sendAnalyticsEvents(GoogleAnalyticsUtil.getAnalyticsEvents().getNonRootedCollector(), GoogleAnalyticsUtil.getAnalyticsEvents().getStartTrace(), aroDevice != null && aroDevice.getApi() != null ? aroDevice.getApi() : "Unknown");
        GoogleAnalyticsUtil.getGoogleAnalyticsInstance().sendAnalyticsEvents(GoogleAnalyticsUtil.getAnalyticsEvents().getNonRootedCollector(), GoogleAnalyticsUtil.getAnalyticsEvents().getVideoCheck(), videoOption != null ? videoOption.name() : "Unknown");
    }).start();
    if (isAndroidVersionNougatOrHigher(this.device) == true) {
        startCpuTraceCapture();
    }
    if (isVideo()) {
        startVideoCapture();
    }
    startUserInputCapture();
    startUiXmlCapture();
    if (atnrProfile) {
        // wait for vpn collection start
        startAttnrProfile(location);
    }
    result.setSuccess(true);
    this.running = true;
    LOG.info("Trace collection started for the non-rooted-android-collector");
    return result;
}
Also used : EnvironmentDetails(com.att.aro.core.datacollector.pojo.EnvironmentDetails) FileWriter(java.io.FileWriter) AROAndroidDevice(com.att.aro.core.mobiledevice.pojo.AROAndroidDevice) AttenuatorModel(com.att.aro.core.peripheral.pojo.AttenuatorModel) IOException(java.io.IOException) Orientation(com.att.aro.core.video.pojo.Orientation) AttnScriptUtil(com.att.aro.core.util.AttnScriptUtil) AdbCommandRejectedException(com.android.ddmlib.AdbCommandRejectedException) InstallException(com.android.ddmlib.InstallException) TimeoutException(com.android.ddmlib.TimeoutException) IOException(java.io.IOException) StatusResult(com.att.aro.core.datacollector.pojo.StatusResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AdbCommandRejectedException (com.android.ddmlib.AdbCommandRejectedException)1 InstallException (com.android.ddmlib.InstallException)1 TimeoutException (com.android.ddmlib.TimeoutException)1 EnvironmentDetails (com.att.aro.core.datacollector.pojo.EnvironmentDetails)1 StatusResult (com.att.aro.core.datacollector.pojo.StatusResult)1 AROAndroidDevice (com.att.aro.core.mobiledevice.pojo.AROAndroidDevice)1 AttenuatorModel (com.att.aro.core.peripheral.pojo.AttenuatorModel)1 AttnScriptUtil (com.att.aro.core.util.AttnScriptUtil)1 Orientation (com.att.aro.core.video.pojo.Orientation)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1