public void startDownloadFromJson(GetApkInfoJson json, long id, Download download) { ArrayList<DownloadModel> filesToDownload = new ArrayList<DownloadModel>(); if (json.getObb() != null) { DownloadModel mainObbDownload = new DownloadModel( json.getObb().getMain().getPath(), OBB_DESTINATION + download.getPackageName() + "/" + json.getObb().getMain().getFilename(), json.getObb().getMain().getMd5sum(), json.getObb().getMain().getFilesize().longValue()); filesToDownload.add(mainObbDownload); if (json.getObb().getPatch() != null) { DownloadModel patchObbDownload = new DownloadModel( json.getObb().getPatch().getPath(), OBB_DESTINATION + download.getPackageName() + "/" + json.getObb().getPatch().getFilename(), json.getObb().getPatch().getMd5sum(), json.getObb().getPatch().getFilesize().longValue()); filesToDownload.add(patchObbDownload); } } String path = Aptoide.getConfiguration().getPathCacheApks(); if (json.getApk().getMd5sum() != null) { download.setId(json.getApk().getMd5sum().hashCode()); } DownloadModel downloadModel = new DownloadModel( json.getApk().getPath(), path + json.getApk().getMd5sum() + ".apk", json.getApk().getMd5sum(), json.getApk().getSize().longValue()); downloadModel.setAutoExecute(true); downloadModel.setFallbackUrl(json.getApk().getAltPath()); filesToDownload.add(downloadModel); FinishedApk apk = new FinishedApk( download.getName(), download.getPackageName(), download.getVersion(), id, download.getIcon(), path + json.getApk().getMd5sum() + ".apk", new ArrayList<String>(json.getApk().getPermissions())); apk.setId(json.getApk().getId().longValue()); download(download.getId(), download, apk, filesToDownload); }
private NotificationCompat.Builder setNotification(final long id) { DownloadInfoRunnable info = getDownload(id); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); // TODO Intent onClick = new Intent(); onClick.setClassName( getPackageName(), /*Aptoide.getConfiguration().*/ getStartActivityClass().getName()); onClick.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); onClick.setAction(Intent.ACTION_VIEW); onClick.putExtra("fromDownloadNotification", true); // The PendingIntent to launch our activity if the user selects this notification PendingIntent onClickAction = PendingIntent.getActivity( getApplicationContext(), 0, onClick, PendingIntent.FLAG_UPDATE_CURRENT); int size = DownloadUtils.dpToPixels(getApplicationContext(), 36); Bitmap icon = null; try { // icon = // DownloadUtils.decodeSampledBitmapFromResource(ImageLoader.getInstance().getDiscCache().get(info.getDownload().getIcon()).getAbsolutePath(), size, size); icon = GlideUtils.downloadOnlyFromCache( Aptoide.getContext(), info.getDownload().getIcon(), size, size); } catch (Exception e) { e.printStackTrace(); } mBuilder.setOngoing(true); mBuilder.setContentTitle( getString(R.string.aptoide_downloading, Aptoide.getConfiguration().getMarketName())); mBuilder.setContentText(info.getDownload().getName()); if (icon != null) mBuilder.setLargeIcon(icon); mBuilder.setSmallIcon(android.R.drawable.stat_sys_download); mBuilder.setProgress(0, 0, true); mBuilder.setContentIntent(onClickAction); // Log.d("download-trace", "ETA: " + info.getEta()); if (info.getEta() > 0) { String remaining = DownloadUtils.formatEta(info.getEta(), ""); mBuilder.setContentInfo("ETA: " + (!remaining.equals("") ? remaining : "0s")); } return mBuilder; }
private NotificationCompat.Builder createDefaultNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); Intent onClick = new Intent(); onClick.setClassName( getPackageName(), /*Aptoide.getConfiguration().*/ getStartActivityClass().getName()); // TODO dependency injection onClick.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); onClick.setAction(Intent.ACTION_VIEW); onClick.putExtra("fromDownloadNotification", true); // The PendingIntent to launch our activity if the user selects this notification PendingIntent onClickAction = PendingIntent.getActivity( getApplicationContext(), 0, onClick, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setOngoing(true); mBuilder .setContentTitle( getString(R.string.aptoide_downloading, Aptoide.getConfiguration().getMarketName())) .setSmallIcon(R.drawable.stat_sys_download) .setProgress(0, 0, true) .setContentIntent(onClickAction); mBuilder.setProgress(100, 0, true); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)) .notify(-3, mBuilder.build()); return mBuilder; }
public void startDownloadFromV6(Download download, UpdatesResponse.UpdateApk apk) { ArrayList<DownloadModel> filesToDownload = new ArrayList<>(); String path = Aptoide.getConfiguration().getPathCacheApks(); if (apk.md5sum != null) { download.setId(apk.md5sum.hashCode()); } DownloadModel downloadModel = new DownloadModel( apk.apk.path, path + apk.md5sum + ".apk", apk.md5sum, apk.apk.filesize.longValue()); downloadModel.setAutoExecute(true); downloadModel.setFallbackUrl(apk.apk.path_alt); filesToDownload.add(downloadModel); FinishedApk fapk = new FinishedApk( download.getName(), download.getPackageName(), download.getVersion(), apk.md5sum.hashCode(), download.getIcon(), path + apk.md5sum + ".apk", new ArrayList<String>()); fapk.setId(apk.md5sum.hashCode()); download(download.getId(), download, fapk, filesToDownload); }
@Override public void onDestroy() { super.onDestroy(); if (timer != null) { timer.cancel(); timer.purge(); } Log.d("Aptoide-RabbitMqService", "RabbitMqService Destroyed!"); try { isRunning = false; if (channel != null && channel.isOpen()) { channel.close(); } if (connection != null && connection.isOpen()) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } catch (ShutdownSignalException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } Aptoide.setWebInstallServiceRunning(false); }
private void download( long id, Download download, FinishedApk apk, ArrayList<DownloadModel> filesToDownload) { DownloadInfoRunnable info = getDownload(id); if (download.getCpiUrl() != null) { apk.setCpiUrl(download.getCpiUrl()); } if (download.getReferrer() != null) { apk.setReferrer(download.getReferrer()); } else { Log.d("AptoideDownloadService", "Creating download with no referrer"); } info.setDownloadExecutor(new DownloadExecutor(apk)); info.setDownload(download); info.setFilesToDownload(filesToDownload); boolean update; try { Aptoide.getContext().getPackageManager().getPackageInfo(download.getPackageName(), 0); update = true; } catch (PackageManager.NameNotFoundException e) { update = false; } info.setUpdate(update); downloads.put(info.getId(), info); NotificationCompat.Builder builder = setNotification(info.getId()); info.setmBuilder(builder); info.download(); if (mBuilder == null) mBuilder = createDefaultNotification(); startForeground(-3, mBuilder.build()); startService(new Intent(getApplicationContext(), DownloadService.class)); startIfStopped(); Toast.makeText( getApplicationContext(), getApplicationContext().getString(R.string.starting_download), Toast.LENGTH_LONG) .show(); }
public void startDownloadFromUrl( String remotePath, String md5, long id, Download download, String repoName) { ArrayList<DownloadModel> filesToDownload = new ArrayList<DownloadModel>(); String path = Aptoide.getConfiguration().getPathCacheApks(); DownloadModel downloadModel = new DownloadModel(remotePath, path + md5 + ".apk", md5, 0); downloadModel.setAutoExecute(true); filesToDownload.add(downloadModel); FinishedApk apk = new FinishedApk( download.getName(), download.getPackageName(), download.getVersion(), id, download.getIcon(), path + md5 + ".apk", new ArrayList<String>()); apk.setRepoName(repoName); download(id, download, apk, filesToDownload); }
@Override public int onStartCommand(final Intent intent, int flags, int startId) { if (!isRunning) { // Toast.makeText(getApplicationContext(), "Starting amqp service", Toast.LENGTH_LONG).show(); Aptoide.setWebInstallServiceRunning(true); // new Thread(new Runnable() { // @Override // public void run() { // // // try { // // AccountManager manager = // AccountManager.get(getApplicationContext()); // // if // (manager.getAccountsByType(Aptoide.getConfiguration().getAccountType()).length > 0) { // // final Account account = // AccountManager.get(getApplicationContext()).getAccountsByType(Aptoide.getConfiguration().getAccountType())[0]; // // SharedPreferences sharedPreferences = // PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // String queueName = sharedPreferences.getString("queueName", // null); // ContentResolver.setIsSyncable(account, // Constants.WEBINSTALL_SYNC_AUTHORITY, 1); // ContentResolver.setSyncAutomatically(account, // Constants.WEBINSTALL_SYNC_AUTHORITY, true); // // if (Build.VERSION.SDK_INT >= 8) { // ContentResolver.addPeriodicSync(account, // Constants.WEBINSTALL_SYNC_AUTHORITY, new Bundle(), // Constants.WEBINSTALL_SYNC_POLL_FREQUENCY); // } // // String host = Constants.WEBINSTALL_HOST; // isRunning = true; // try { // ConnectionFactory factory = new ConnectionFactory(); // factory.setHost(host); // factory.setUsername("public"); // factory.setPassword("public"); // factory.setConnectionTimeout(20000); // // factory.setVirtualHost("webinstall"); // connection = (AMQConnection) factory.newConnection(); // newChannel(queueName, new AMQHandler() { // @Override // void handleMessage(String body) { // // try { // JSONObject object = new JSONObject(body); // // Intent i = new Intent(getApplicationContext(), // appViewClass); // SharedPreferences securePreferences = // SecurePreferences.getInstance(); // String authToken = // securePreferences.getString("devtoken", ""); // String repo = object.getString("repo"); // long id = object.getLong("id"); // String md5sum = object.getString("md5sum"); // i.putExtra("fromMyapp", true); // i.putExtra("repoName", repo); // i.putExtra("id", id); // i.putExtra("download_from", "webinstall"); // i.putExtra("md5sum", md5sum); // // String deviceId = // Settings.Secure.getString(getApplicationContext().getContentResolver(), // Settings.Secure.ANDROID_ID); // // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // String hmac = object.getString("hmac"); // String calculatedHmac = // AptoideUtils.Algorithms.computeHmacSha1(repo + id + md5sum, authToken + deviceId); // if (hmac.equals(calculatedHmac)) { // getApplicationContext().startActivity(i); // } else { // Log.d("Aptoide-WebInstall", "Error // validating message: received: " + hmac + " calculated:" + calculatedHmac); // } // // } catch (Exception e) { // e.printStackTrace(); // } // // // } // }); // } catch (IOException e) { // // PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(Constants.WEBINSTALL_QUEUE_EXCLUDED, true).commit(); // e.printStackTrace(); // try { // if (channel != null && channel.isOpen()) { // channel.close(); // } // // if (connection != null && connection.isOpen()) { // connection.close(); // } // } catch (IOException e1) { // e1.printStackTrace(); // } catch (ShutdownSignalException e1) { // e1.printStackTrace(); // } // isRunning = false; // // } // } // }catch (Exception e){ // e.printStackTrace(); // } // } // }).start(); } return START_STICKY; }