Search in sources :

Example 1 with InstallResult

use of com.lody.virtual.remote.InstallResult in project VirtualXposed by android-hacker.

the class Installd method addApp.

public static void addApp(AppInfoLite info, UpdateListener refreshListener) {
    class AddResult {

        private PackageAppData appData;

        private int userId;

        private boolean justEnableHidden;
    }
    AddResult addResult = new AddResult();
    VUiKit.defer().when(() -> {
        InstalledAppInfo installedAppInfo = VirtualCore.get().getInstalledAppInfo(info.packageName, 0);
        addResult.justEnableHidden = installedAppInfo != null;
        if (info.disableMultiVersion) {
            addResult.justEnableHidden = false;
        }
        if (addResult.justEnableHidden) {
            int[] userIds = installedAppInfo.getInstalledUsers();
            int nextUserId = userIds.length;
            /*
                  Input : userIds = {0, 1, 3}
                  Output: nextUserId = 2
                 */
            for (int i = 0; i < userIds.length; i++) {
                if (userIds[i] != i) {
                    nextUserId = i;
                    break;
                }
            }
            addResult.userId = nextUserId;
            if (VUserManager.get().getUserInfo(nextUserId) == null) {
                // user not exist, create it automatically.
                String nextUserName = "Space " + (nextUserId + 1);
                VUserInfo newUserInfo = VUserManager.get().createUser(nextUserName, VUserInfo.FLAG_ADMIN);
                if (newUserInfo == null) {
                    throw new IllegalStateException();
                }
            }
            boolean success = VirtualCore.get().installPackageAsUser(nextUserId, info.packageName);
            if (!success) {
                throw new IllegalStateException();
            }
        } else {
            PackageInfo pkgInfo = null;
            try {
                pkgInfo = XApp.getApp().getPackageManager().getPackageArchiveInfo(info.path, 0);
                pkgInfo.applicationInfo.sourceDir = info.path;
                pkgInfo.applicationInfo.publicSourceDir = info.path;
            } catch (Exception e) {
            }
            if (pkgInfo != null) {
                PackageAppData data = PackageAppDataStorage.get().acquire(pkgInfo.applicationInfo);
                addResult.appData = data;
                data.isInstalling = true;
                data.isFirstOpen = false;
                if (refreshListener != null) {
                    refreshListener.update(data);
                }
            }
            InstallResult res = addVirtualApp(info);
            if (!res.isSuccess) {
                if (addResult.appData != null) {
                // mView.removeAppToLauncher(addResult.appData);
                }
                throw new IllegalStateException();
            }
        }
    }).then((res) -> {
        if (addResult.appData == null) {
            addResult.appData = PackageAppDataStorage.get().acquire(info.packageName);
        }
    }).done(res -> {
        boolean multipleVersion = addResult.justEnableHidden && addResult.userId != 0;
        if (!multipleVersion) {
            PackageAppData data = addResult.appData;
            data.isInstalling = false;
            data.isLoading = true;
            if (refreshListener != null) {
                refreshListener.update(data);
            }
            handleOptApp(data, info.packageName, true, refreshListener);
        } else {
            MultiplePackageAppData data = new MultiplePackageAppData(addResult.appData, addResult.userId);
            data.isInstalling = false;
            data.isLoading = true;
            if (refreshListener != null) {
                refreshListener.update(data);
            }
            handleOptApp(data, info.packageName, false, refreshListener);
        }
    });
}
Also used : VUserInfo(com.lody.virtual.os.VUserInfo) XApp(io.virtualapp.XApp) PackageAppDataStorage(io.virtualapp.home.repo.PackageAppDataStorage) Context(android.content.Context) InstallResult(com.lody.virtual.remote.InstallResult) PackageManager(android.content.pm.PackageManager) VirtualCore(com.lody.virtual.client.core.VirtualCore) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) DeviceUtil(com.lody.virtual.helper.utils.DeviceUtil) VUserManager(com.lody.virtual.os.VUserManager) PackageAppData(io.virtualapp.home.models.PackageAppData) AppData(io.virtualapp.home.models.AppData) Intent(android.content.Intent) IOException(java.io.IOException) VCommends(io.virtualapp.VCommends) PackageInfo(android.content.pm.PackageInfo) AppInfoLite(io.virtualapp.home.models.AppInfoLite) ArrayList(java.util.ArrayList) InstallStrategy(com.lody.virtual.client.core.InstallStrategy) InstalledAppInfo(com.lody.virtual.remote.InstalledAppInfo) VUiKit(io.virtualapp.abs.ui.VUiKit) PackageInfo(android.content.pm.PackageInfo) InstalledAppInfo(com.lody.virtual.remote.InstalledAppInfo) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) PackageAppData(io.virtualapp.home.models.PackageAppData) IOException(java.io.IOException) InstallResult(com.lody.virtual.remote.InstallResult) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) VUserInfo(com.lody.virtual.os.VUserInfo)

Example 2 with InstallResult

use of com.lody.virtual.remote.InstallResult in project VirtualXposed by android-hacker.

the class CmdReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!ACTION.equalsIgnoreCase(action)) {
        return;
    }
    String cmd = intent.getStringExtra(KEY_CMD);
    if (TextUtils.isEmpty(cmd)) {
        showTips(context, "No cmd found!");
        return;
    }
    if (CMD_REBOOT.equalsIgnoreCase(cmd)) {
        VirtualCore.get().killAllApps();
        showTips(context, "Reboot Success!!");
        return;
    }
    if (CMD_UPDATE.equalsIgnoreCase(cmd)) {
        String pkg = intent.getStringExtra(KEY_PKG);
        if (TextUtils.isEmpty(pkg)) {
            showTips(context, "Please tell me the update package!!");
            return;
        }
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            showTips(context, "system error, update failed!");
            return;
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(pkg, 0);
            String apkPath = applicationInfo.sourceDir;
            InstallResult installResult = VirtualCore.get().installPackage(apkPath, InstallStrategy.UPDATE_IF_EXIST);
            if (installResult.isSuccess) {
                if (installResult.isUpdate) {
                    showTips(context, "Update " + pkg + " Success!!");
                }
            } else {
                showTips(context, "Update " + pkg + " failed: " + installResult.error);
            }
        } catch (PackageManager.NameNotFoundException e) {
            showTips(context, "Can not found " + pkg + " outside!");
        }
    } else if (CMD_LAUNCH.equalsIgnoreCase(cmd)) {
        String pkg = intent.getStringExtra(KEY_PKG);
        if (TextUtils.isEmpty(pkg)) {
            showTips(context, "Please tell me the launch package!!");
            return;
        }
        LoadingActivity.launch(context, pkg, 0);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) InstallResult(com.lody.virtual.remote.InstallResult)

Example 3 with InstallResult

use of com.lody.virtual.remote.InstallResult in project VirtualApp by asLody.

the class HomePresenterImpl method addApp.

@Override
public void addApp(AppInfoLite info) {
    class AddResult {

        private PackageAppData appData;

        private int userId;

        private boolean justEnableHidden;
    }
    AddResult addResult = new AddResult();
    VUiKit.defer().when(() -> {
        InstalledAppInfo installedAppInfo = VirtualCore.get().getInstalledAppInfo(info.packageName, 0);
        addResult.justEnableHidden = installedAppInfo != null;
        if (addResult.justEnableHidden) {
            int[] userIds = installedAppInfo.getInstalledUsers();
            int nextUserId = userIds.length;
            /*
                  Input : userIds = {0, 1, 3}
                  Output: nextUserId = 2
                 */
            for (int i = 0; i < userIds.length; i++) {
                if (userIds[i] != i) {
                    nextUserId = i;
                    break;
                }
            }
            addResult.userId = nextUserId;
            if (VUserManager.get().getUserInfo(nextUserId) == null) {
                // user not exist, create it automatically.
                String nextUserName = "Space " + (nextUserId + 1);
                VUserInfo newUserInfo = VUserManager.get().createUser(nextUserName, VUserInfo.FLAG_ADMIN);
                if (newUserInfo == null) {
                    throw new IllegalStateException();
                }
            }
            boolean success = VirtualCore.get().installPackageAsUser(nextUserId, info.packageName);
            if (!success) {
                throw new IllegalStateException();
            }
        } else {
            InstallResult res = mRepo.addVirtualApp(info);
            if (!res.isSuccess) {
                throw new IllegalStateException();
            }
        }
    }).then((res) -> {
        addResult.appData = PackageAppDataStorage.get().acquire(info.packageName);
    }).done(res -> {
        boolean multipleVersion = addResult.justEnableHidden && addResult.userId != 0;
        if (!multipleVersion) {
            PackageAppData data = addResult.appData;
            data.isLoading = true;
            mView.addAppToLauncher(data);
            handleOptApp(data, info.packageName, true);
        } else {
            MultiplePackageAppData data = new MultiplePackageAppData(addResult.appData, addResult.userId);
            data.isLoading = true;
            mView.addAppToLauncher(data);
            handleOptApp(data, info.packageName, false);
        }
    });
}
Also used : VUserInfo(com.lody.virtual.os.VUserInfo) PackageAppDataStorage(io.virtualapp.home.repo.PackageAppDataStorage) InstallResult(com.lody.virtual.remote.InstallResult) VirtualCore(com.lody.virtual.client.core.VirtualCore) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) VUserManager(com.lody.virtual.os.VUserManager) PackageAppData(io.virtualapp.home.models.PackageAppData) AppData(io.virtualapp.home.models.AppData) IOException(java.io.IOException) AppRepository(io.virtualapp.home.repo.AppRepository) VCommends(io.virtualapp.VCommends) AppInfoLite(io.virtualapp.home.models.AppInfoLite) GmsSupport(com.lody.virtual.GmsSupport) Bitmap(android.graphics.Bitmap) InstalledAppInfo(com.lody.virtual.remote.InstalledAppInfo) VUiKit(io.virtualapp.abs.ui.VUiKit) Once(jonathanfinerty.once.Once) Activity(android.app.Activity) InstalledAppInfo(com.lody.virtual.remote.InstalledAppInfo) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) MultiplePackageAppData(io.virtualapp.home.models.MultiplePackageAppData) PackageAppData(io.virtualapp.home.models.PackageAppData) InstallResult(com.lody.virtual.remote.InstallResult) VUserInfo(com.lody.virtual.os.VUserInfo)

Example 4 with InstallResult

use of com.lody.virtual.remote.InstallResult in project VirtualApp by asLody.

the class MyAppRequestListener method onRequestInstall.

@Override
public void onRequestInstall(String path) {
    Toast.makeText(context, "Installing: " + path, Toast.LENGTH_SHORT).show();
    InstallResult res = VirtualCore.get().installPackage(path, InstallStrategy.UPDATE_IF_EXIST);
    if (res.isSuccess) {
        try {
            VirtualCore.get().preOpt(res.packageName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (res.isUpdate) {
            Toast.makeText(context, "Update: " + res.packageName + " success!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "Install: " + res.packageName + " success!", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(context, "Install failed: " + res.error, Toast.LENGTH_SHORT).show();
    }
}
Also used : IOException(java.io.IOException) InstallResult(com.lody.virtual.remote.InstallResult)

Example 5 with InstallResult

use of com.lody.virtual.remote.InstallResult in project VirtualXposed by android-hacker.

the class VAppManagerService method installPackage.

public synchronized InstallResult installPackage(String path, int flags, boolean notify) {
    long installTime = System.currentTimeMillis();
    if (path == null) {
        return InstallResult.makeFailure("path = NULL");
    }
    File packageFile = new File(path);
    if (!packageFile.exists() || !packageFile.isFile()) {
        return InstallResult.makeFailure("Package File is not exist.");
    }
    VPackage pkg = null;
    try {
        pkg = PackageParserEx.parsePackage(packageFile);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    if (pkg == null || pkg.packageName == null) {
        return InstallResult.makeFailure("Unable to parse the package.");
    }
    InstallResult res = new InstallResult();
    res.packageName = pkg.packageName;
    // PackageCache holds all packages, try to check if we need to update.
    VPackage existOne = PackageCacheManager.get(pkg.packageName);
    PackageSetting existSetting = existOne != null ? (PackageSetting) existOne.mExtras : null;
    if (existOne != null) {
        if ((flags & InstallStrategy.IGNORE_NEW_VERSION) != 0) {
            res.isUpdate = true;
            return res;
        }
        if (!canUpdate(existOne, pkg, flags)) {
            return InstallResult.makeFailure("Can not update the package (such as version downrange).");
        }
        res.isUpdate = true;
    }
    File appDir = VEnvironment.getDataAppPackageDirectory(pkg.packageName);
    File libDir = new File(appDir, "lib");
    if (res.isUpdate) {
        FileUtils.deleteDir(libDir);
        VEnvironment.getOdexFile(pkg.packageName).delete();
        VActivityManagerService.get().killAppByPkg(pkg.packageName, VUserHandle.USER_ALL);
    }
    if (!libDir.exists() && !libDir.mkdirs()) {
        return InstallResult.makeFailure("Unable to create lib dir.");
    }
    boolean dependSystem = (flags & InstallStrategy.DEPEND_SYSTEM_IF_EXIST) != 0 && VirtualCore.get().isOutsideInstalled(pkg.packageName);
    if (existSetting != null && existSetting.dependSystem) {
        dependSystem = false;
    }
    NativeLibraryHelperCompat.copyNativeBinaries(new File(path), libDir);
    if (!dependSystem) {
        File privatePackageFile = new File(appDir, "base.apk");
        File parentFolder = privatePackageFile.getParentFile();
        if (!parentFolder.exists() && !parentFolder.mkdirs()) {
            VLog.w(TAG, "Warning: unable to create folder : " + privatePackageFile.getPath());
        } else if (privatePackageFile.exists() && !privatePackageFile.delete()) {
            VLog.w(TAG, "Warning: unable to delete file : " + privatePackageFile.getPath());
        }
        try {
            FileUtils.copyFile(packageFile, privatePackageFile);
        } catch (IOException e) {
            privatePackageFile.delete();
            return InstallResult.makeFailure("Unable to copy the package file.");
        }
        packageFile = privatePackageFile;
    }
    if (existOne != null) {
        PackageCacheManager.remove(pkg.packageName);
    }
    chmodPackageDictionary(packageFile);
    PackageSetting ps;
    if (existSetting != null) {
        ps = existSetting;
    } else {
        ps = new PackageSetting();
    }
    ps.dependSystem = dependSystem;
    ps.apkPath = packageFile.getPath();
    ps.libPath = libDir.getPath();
    ps.packageName = pkg.packageName;
    ps.appId = VUserHandle.getAppId(mUidSystem.getOrCreateUid(pkg));
    if (res.isUpdate) {
        ps.lastUpdateTime = installTime;
    } else {
        ps.firstInstallTime = installTime;
        ps.lastUpdateTime = installTime;
        for (int userId : VUserManagerService.get().getUserIds()) {
            boolean installed = userId == 0;
            ps.setUserState(userId, false, /*launched*/
            false, /*hidden*/
            installed);
        }
    }
    PackageParserEx.savePackageCache(pkg);
    PackageCacheManager.put(pkg, ps);
    mPersistenceLayer.save();
    if (!dependSystem) {
        boolean runDexOpt = false;
        if (VirtualRuntime.isArt()) {
            try {
                ArtDexOptimizer.compileDex2Oat(ps.apkPath, VEnvironment.getOdexFile(ps.packageName).getPath());
            } catch (IOException e) {
                e.printStackTrace();
                runDexOpt = true;
            }
        } else {
            runDexOpt = true;
        }
        if (runDexOpt) {
            try {
                DexFile.loadDex(ps.apkPath, VEnvironment.getOdexFile(ps.packageName).getPath(), 0).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    BroadcastSystem.get().startApp(pkg);
    if (notify) {
        notifyAppInstalled(ps, -1);
    }
    res.isSuccess = true;
    return res;
}
Also used : VPackage(com.lody.virtual.server.pm.parser.VPackage) IOException(java.io.IOException) DexFile(dalvik.system.DexFile) File(java.io.File) InstallResult(com.lody.virtual.remote.InstallResult)

Aggregations

InstallResult (com.lody.virtual.remote.InstallResult)7 IOException (java.io.IOException)6 VPackage (com.lody.virtual.server.pm.parser.VPackage)3 DexFile (dalvik.system.DexFile)3 File (java.io.File)3 PackageManager (android.content.pm.PackageManager)2 VirtualCore (com.lody.virtual.client.core.VirtualCore)2 VUserInfo (com.lody.virtual.os.VUserInfo)2 VUserManager (com.lody.virtual.os.VUserManager)2 InstalledAppInfo (com.lody.virtual.remote.InstalledAppInfo)2 VCommends (io.virtualapp.VCommends)2 VUiKit (io.virtualapp.abs.ui.VUiKit)2 AppData (io.virtualapp.home.models.AppData)2 AppInfoLite (io.virtualapp.home.models.AppInfoLite)2 MultiplePackageAppData (io.virtualapp.home.models.MultiplePackageAppData)2 PackageAppData (io.virtualapp.home.models.PackageAppData)2 PackageAppDataStorage (io.virtualapp.home.repo.PackageAppDataStorage)2 Activity (android.app.Activity)1 Context (android.content.Context)1 Intent (android.content.Intent)1