javahttpnanohttpd

How download an installed app with nanohttpd


Hello i want to make a http server with nanohttpd that shows installed apps, and convert them to apk and download. I can list but how can i download the app i select

List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA;
for (ApplicationInfo packageInfo : packages) {

        String name = String.valueOf(packageManager.getApplicationLabel(packageInfo));
        if (name.isEmpty()) {
            name = packageInfo.packageName;
        }
        Drawable icon = packageManager.getApplicationIcon(packageInfo);
        String apkPath = packageInfo.sourceDir;
        apps.add(new App(name, apkPath, icon, packageInfo.packageName));
}

if (method.equals(Method.GET)) {
    for (App file : apps) {

        answer += "<a href=\"" + file.getAppName()
                + "\" alt = \"\">" + file.getAppName()
                + "</a><br>";
    }

     

Solution

  • Ok, I basicly take the url after list the apps and check which appname equal to url after that i start to download it like that

     if (method.equals(Method.GET)) {
                if (a) {
                    answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " +
                            "charset=utf-8\"><title> HTTP File Browser</title>";
                    for (App file : appShare) {
                        answer += "<a href=\"" + file.getAppName()
                                + "\" alt = \"\">" + file.getAppName()
                                + "</a><br>";
                    }
                    answer += "</head></html>";
                    if(blabla.equals(header.get("referer"))){
                        a=false;
                    }
    
                } else {
                    for (int i = 0; i < appShare.size(); i++) {
                        if (appShare.get(i).getAppName().equals(appnames))
                            arrayC = i;
                    }
                    answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " +
                            "charset=utf-8\"><title> Download " + appShare.get(arrayC).getAppName() + "</title>";
                    // serve file download
                    InputStream inputStream;
                    Response response = null;
    
                    if (mStatusUpdateListener != null) {
                        mStatusUpdateListener.onDownloadingFile(
                                new File(appShare.get(arrayC).getApkPath()), false);
                    }
    
                    try {
                        inputStream = new FileInputStream(
    
                                new File(appShare.get(arrayC).getApkPath()));
                        response = new DownloadResponse(
    
                                new File(appShare.get(arrayC).getApkPath()), inputStream);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }            response.addHeader(
                            "Content-Disposition", "attachment; filename=" + appShare.get(arrayC).getAppName()+".apk");
                    return response;
    
    
                }