Search in sources :

Example 1 with AppLauncherInfo

use of org.robovm.libimobiledevice.util.AppLauncherCallback.AppLauncherInfo in project robovm by robovm.

the class AppLauncher method launchInternal.

private int launchInternal() throws Exception {
    install();
    int lockedRetriesLeft = launchOnLockedRetries;
    while (true) {
        IDeviceConnection conn = null;
        String appPath = null;
        try (LockdowndClient lockdowndClient = new LockdowndClient(device, getClass().getSimpleName(), true)) {
            appPath = getAppPath(lockdowndClient, appId);
            // E.g. 7.0.2
            String productVersion = lockdowndClient.getValue(null, "ProductVersion").toString();
            // E.g. 11B508
            String buildVersion = lockdowndClient.getValue(null, "BuildVersion").toString();
            if (appLauncherCallback != null) {
                appLauncherCallback.setAppLaunchInfo(new AppLauncherInfo(device, appPath, productVersion, buildVersion));
            }
            LockdowndServiceDescriptor debugService = null;
            try {
                debugService = lockdowndClient.startService(DEBUG_SERVER_SERVICE_NAME);
            } catch (LibIMobileDeviceException e) {
                if (e.getErrorCode() == LockdowndError.LOCKDOWN_E_INVALID_SERVICE.swigValue()) {
                    // This happens when the developer image hasn't been mounted.
                    // Mount and try again.
                    mountDeveloperImage(lockdowndClient);
                    debugService = lockdowndClient.startService(DEBUG_SERVER_SERVICE_NAME);
                } else {
                    throw e;
                }
            }
            conn = device.connect(debugService.getPort());
            log("Debug server port: " + debugService.getPort());
            if (localPort != -1) {
                String exe = ((NSDictionary) PropertyListParser.parse(new File(localAppPath, "Info.plist"))).objectForKey("CFBundleExecutable").toString();
                log("launchios \"" + new File(localAppPath, exe).getAbsolutePath() + "\" \"" + appPath + "\" " + localPort);
                StringBuilder argsString = new StringBuilder();
                for (String arg : args) {
                    if (argsString.length() > 0) {
                        argsString.append(' ');
                    }
                    argsString.append(arg);
                }
                log("process launch -- " + argsString);
            }
        }
        if (lockedRetriesLeft == launchOnLockedRetries) {
            // First try
            log("Remote app path: " + appPath);
            log("Launching app...");
        } else {
            log("Launching app (retry %d of %d)...", (launchOnLockedRetries - lockedRetriesLeft), launchOnLockedRetries);
        }
        try {
            // otherwise perform port forwarding and stdout piping
            if (localPort == -1) {
                return pipeStdOut(conn, appPath);
            } else {
                return forward(conn, appPath);
            }
        } catch (RuntimeException e) {
            if (!e.getMessage().contains("Locked") || lockedRetriesLeft == 0) {
                throw e;
            }
            lockedRetriesLeft--;
            log("Device locked. Retrying launch in %d seconds...", secondsBetweenLaunchOnLockedRetries);
            Thread.sleep(secondsBetweenLaunchOnLockedRetries * 1000);
        } finally {
            conn.dispose();
        }
    }
}
Also used : LibIMobileDeviceException(org.robovm.libimobiledevice.LibIMobileDeviceException) IDeviceConnection(org.robovm.libimobiledevice.IDeviceConnection) AppLauncherInfo(org.robovm.libimobiledevice.util.AppLauncherCallback.AppLauncherInfo) LockdowndServiceDescriptor(org.robovm.libimobiledevice.LockdowndServiceDescriptor) NSString(com.dd.plist.NSString) LockdowndClient(org.robovm.libimobiledevice.LockdowndClient) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

NSString (com.dd.plist.NSString)1 File (java.io.File)1 ZipFile (java.util.zip.ZipFile)1 IDeviceConnection (org.robovm.libimobiledevice.IDeviceConnection)1 LibIMobileDeviceException (org.robovm.libimobiledevice.LibIMobileDeviceException)1 LockdowndClient (org.robovm.libimobiledevice.LockdowndClient)1 LockdowndServiceDescriptor (org.robovm.libimobiledevice.LockdowndServiceDescriptor)1 AppLauncherInfo (org.robovm.libimobiledevice.util.AppLauncherCallback.AppLauncherInfo)1