Search in sources :

Example 1 with VtFileReportScanItem

use of io.github.muntashirakon.AppManager.scanner.vt.VtFileReportScanItem in project AppManager by MuntashirAkon.

the class ScannerActivity method onAuthenticated.

@Override
protected void onAuthenticated(Bundle savedInstanceState) {
    setContentView(R.layout.activity_scanner);
    setSupportActionBar(findViewById(R.id.toolbar));
    model = new ViewModelProvider(this).get(ScannerViewModel.class);
    mActionBar = getSupportActionBar();
    Intent intent = getIntent();
    isExternalApk = intent.getBooleanExtra(EXTRA_IS_EXTERNAL, true);
    mProgressIndicator = findViewById(R.id.progress_linear);
    mProgressIndicator.setVisibilityAfterHide(View.GONE);
    showProgress(true);
    apkUri = IntentCompat.getDataUri(intent);
    if (apkUri == null) {
        Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    File apkFile = null;
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        if (!FmProvider.AUTHORITY.equals(apkUri.getAuthority())) {
            try {
                fd = getContentResolver().openFileDescriptor(apkUri, "r");
                if (fd == null) {
                    throw new FileNotFoundException("FileDescription cannot be null");
                }
                apkFile = FileUtils.getFileFromFd(fd);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    } else {
        String path = apkUri.getPath();
        if (path != null)
            apkFile = new File(path);
    }
    model.loadSummary(apkFile, apkUri);
    model.getApkChecksums().observe(this, checksums -> {
        SpannableStringBuilder sb = new SpannableStringBuilder(apkUri.toString()).append("\n");
        sb.append(getPrimaryText(this, getString(R.string.checksums)));
        for (Pair<String, String> digest : checksums) {
            sb.append("\n").append(getPrimaryText(this, digest.first + ": ")).append(getMonospacedText(digest.second));
        }
        ((TextView) findViewById(R.id.apk_title)).setText(R.string.source_dir);
        ((TextView) findViewById(R.id.apk_description)).setText(sb);
    });
    File finalApkFile = apkFile;
    model.getPackageInfo().observe(this, packageInfo -> {
        if (packageInfo != null) {
            String archiveFilePath = model.getApkFile().getAbsolutePath();
            mPackageName = packageInfo.packageName;
            final ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            applicationInfo.publicSourceDir = archiveFilePath;
            applicationInfo.sourceDir = archiveFilePath;
            mAppName = applicationInfo.loadLabel(getPackageManager());
        } else {
            mAppName = finalApkFile != null ? finalApkFile.getName() : apkUri.getLastPathSegment();
        }
        if (mActionBar != null) {
            mActionBar.setTitle(mAppName);
            mActionBar.setSubtitle(R.string.scanner);
        }
    });
    model.getApkVerifierResult().observe(this, result -> {
        TextView checksumDescription = findViewById(R.id.checksum_description);
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(PackageUtils.getApkVerifierInfo(result, this));
        List<X509Certificate> certificates = result.getSignerCertificates();
        if (certificates != null && certificates.size() > 0) {
            builder.append(getCertificateInfo(certificates));
        }
        checksumDescription.setText(builder);
    });
    model.getAllClasses().observe(this, allClasses -> {
        classListAll = allClasses;
        ((TextView) findViewById(R.id.classes_title)).setText(getResources().getQuantityString(R.plurals.classes, classListAll.size(), classListAll.size()));
        findViewById(R.id.classes).setOnClickListener(v -> {
            Intent intent1 = new Intent(this, ClassListingActivity.class);
            intent1.putExtra(ClassListingActivity.EXTRA_APP_NAME, mAppName);
            intent1.putExtra(ClassListingActivity.EXTRA_DEX_VFS_ID, model.getDexVfsId());
            startActivity(intent1);
        });
        // Fetch tracker info
        new Thread(() -> {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            try {
                setTrackerInfo();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        // Fetch library info
        new Thread(() -> {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            try {
                setLibraryInfo();
                // Progress is dismissed here because this will take the largest time
                runOnUiThread(() -> showProgress(false));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    });
    // VirusTotal
    View vtView = findViewById(R.id.vt);
    if (!FeatureController.isInternetEnabled() || AppPref.getVtApiKey() == null) {
        vtView.setVisibility(View.GONE);
        findViewById(R.id.vt_disclaimer).setVisibility(View.GONE);
    }
    TextView vtTitle = findViewById(R.id.vt_title);
    TextView vtDescription = findViewById(R.id.vt_description);
    model.getVtFileScanMeta().observe(this, vtFileScanMeta -> {
        if (vtFileScanMeta == null) {
            // Uploading
            vtTitle.setText(R.string.vt_uploading);
        } else {
            // Upload completed and queued
            vtTitle.setText(R.string.vt_queued);
            vtDescription.setText(vtFileScanMeta.getPermalink());
        }
    });
    model.getVtFileReport().observe(this, vtFileReport -> {
        if (vtFileReport == null) {
            // Failed
            vtTitle.setText(R.string.vt_failed);
            vtDescription.setText(null);
            vtView.setOnClickListener(null);
        } else {
            // Successful or still queued
            int positives = vtFileReport.getPositives() == null ? -1 : vtFileReport.getPositives();
            if (positives < 0) {
                // Still checking
                vtTitle.setText(R.string.vt_queued);
                vtDescription.setText(vtFileReport.getPermalink());
                return;
            }
            CharSequence resultSummary = getString(R.string.vt_success, positives, vtFileReport.getTotal());
            @ColorRes int color;
            if (positives == 0) {
                color = R.color.stopped;
            } else if (positives <= 5) {
                color = R.color.tracker;
            } else
                color = R.color.electric_red;
            DialogTitleBuilder titleBuilder = new DialogTitleBuilder(this).setTitle(getString(R.string.vt_success, positives, vtFileReport.getTotal())).setSubtitle(getString(R.string.vt_scan_date, vtFileReport.getScanDate())).setEndIcon(R.drawable.ic_vt, v -> {
                Uri vtPermalink = Uri.parse(vtFileReport.getPermalink());
                Intent linkIntent = new Intent(Intent.ACTION_VIEW, vtPermalink);
                if (linkIntent.resolveActivity(getPackageManager()) != null) {
                    startActivity(linkIntent);
                }
            }).setEndIconContentDescription(R.string.vt_permalink);
            Spanned result;
            Map<String, VtFileReportScanItem> vtFileReportScanItems = vtFileReport.getScans();
            if (vtFileReportScanItems != null) {
                @ColorInt int colorRed = ContextCompat.getColor(this, R.color.electric_red);
                @ColorInt int colorGreen = ContextCompat.getColor(this, R.color.stopped);
                ArrayList<Spannable> detectedList = new ArrayList<>();
                ArrayList<Spannable> undetectedList = new ArrayList<>();
                for (String avName : vtFileReportScanItems.keySet()) {
                    VtFileReportScanItem item = Objects.requireNonNull(vtFileReportScanItems.get(avName));
                    if (item.isDetected()) {
                        detectedList.add(new SpannableStringBuilder(getColoredText(getPrimaryText(this, avName), colorRed)).append(getSmallerText(" (" + item.getVersion() + ")")).append("\n").append(item.getMalware()));
                    } else {
                        undetectedList.add(new SpannableStringBuilder(getColoredText(getPrimaryText(this, avName), colorGreen)).append(getSmallerText(" (" + item.getVersion() + ")")));
                    }
                }
                detectedList.addAll(undetectedList);
                result = getOrderedList(detectedList);
            } else
                result = null;
            vtTitle.setText(getColoredText(resultSummary, ContextCompat.getColor(this, color)));
            if (result != null) {
                vtDescription.setText(R.string.tap_to_see_details);
                vtView.setOnClickListener(v -> new MaterialAlertDialogBuilder(this).setCustomTitle(titleBuilder.build()).setMessage(result).setPositiveButton(R.string.ok, null).setNeutralButton(R.string.copy, (dialog, which) -> {
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText(getString(R.string.scan_in_vt), result);
                    clipboard.setPrimaryClip(clip);
                    Snackbar.make(vtView, R.string.copied_to_clipboard, Snackbar.LENGTH_SHORT).show();
                }).show());
            }
        }
    });
}
Also used : X509Certificate(java.security.cert.X509Certificate) VtFileReportScanItem(io.github.muntashirakon.AppManager.scanner.vt.VtFileReportScanItem) PackageUtils(io.github.muntashirakon.AppManager.utils.PackageUtils) Bundle(android.os.Bundle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FeatureController(io.github.muntashirakon.AppManager.settings.FeatureController) NonNull(androidx.annotation.NonNull) Spannable(android.text.Spannable) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) Uri(android.net.Uri) ColorInt(androidx.annotation.ColorInt) ActionBar(androidx.appcompat.app.ActionBar) Map(java.util.Map) ClipboardManager(android.content.ClipboardManager) View(android.view.View) ContextCompat(androidx.core.content.ContextCompat) R(io.github.muntashirakon.AppManager.R) ArrayMap(androidx.collection.ArrayMap) StaticDataset(io.github.muntashirakon.AppManager.StaticDataset) LinearProgressIndicator(com.google.android.material.progressindicator.LinearProgressIndicator) Collection(java.util.Collection) Set(java.util.Set) FileUtils(io.github.muntashirakon.AppManager.utils.FileUtils) FileNotFoundException(java.io.FileNotFoundException) Objects(java.util.Objects) List(java.util.List) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) SearchableMultiChoiceDialogBuilder(io.github.muntashirakon.AppManager.types.SearchableMultiChoiceDialogBuilder) UIUtils.getMonospacedText(io.github.muntashirakon.AppManager.utils.UIUtils.getMonospacedText) UIUtils.getColoredText(io.github.muntashirakon.AppManager.utils.UIUtils.getColoredText) TextUtils(com.android.internal.util.TextUtils) Pattern(java.util.regex.Pattern) PackageInstallerActivity(io.github.muntashirakon.AppManager.apk.installer.PackageInstallerActivity) ApplicationInfo(android.content.pm.ApplicationInfo) Snackbar(com.google.android.material.snackbar.Snackbar) ColorRes(androidx.annotation.ColorRes) CertificateEncodingException(java.security.cert.CertificateEncodingException) Context(android.content.Context) Spanned(android.text.Spanned) MaterialCardView(com.google.android.material.card.MaterialCardView) BaseActivity(io.github.muntashirakon.AppManager.BaseActivity) Pair(android.util.Pair) Intent(android.content.Intent) HashMap(java.util.HashMap) UIUtils.getPrimaryText(io.github.muntashirakon.AppManager.utils.UIUtils.getPrimaryText) FmProvider(io.github.muntashirakon.AppManager.fm.FmProvider) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) ClipData(android.content.ClipData) SpannableStringBuilder(android.text.SpannableStringBuilder) Toast(android.widget.Toast) ArrayUtils(io.github.muntashirakon.AppManager.utils.ArrayUtils) Menu(android.view.Menu) IntentCompat(io.github.muntashirakon.AppManager.intercept.IntentCompat) ViewModelProvider(androidx.lifecycle.ViewModelProvider) SpannableString(android.text.SpannableString) AppPref(io.github.muntashirakon.AppManager.utils.AppPref) ScrollableDialogBuilder(io.github.muntashirakon.dialog.ScrollableDialogBuilder) File(java.io.File) UIUtils.getSmallerText(io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText) DigestUtils(io.github.muntashirakon.AppManager.utils.DigestUtils) DialogTitleBuilder(io.github.muntashirakon.dialog.DialogTitleBuilder) LeadingMarginSpan(android.text.style.LeadingMarginSpan) ListSpan(io.github.muntashirakon.text.style.ListSpan) Collections(java.util.Collections) ColorRes(androidx.annotation.ColorRes) FileNotFoundException(java.io.FileNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) VtFileReportScanItem(io.github.muntashirakon.AppManager.scanner.vt.VtFileReportScanItem) Uri(android.net.Uri) TextView(android.widget.TextView) ViewModelProvider(androidx.lifecycle.ViewModelProvider) DialogTitleBuilder(io.github.muntashirakon.dialog.DialogTitleBuilder) ClipboardManager(android.content.ClipboardManager) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) MaterialCardView(com.google.android.material.card.MaterialCardView) Spanned(android.text.Spanned) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) X509Certificate(java.security.cert.X509Certificate) FileNotFoundException(java.io.FileNotFoundException) CertificateEncodingException(java.security.cert.CertificateEncodingException) ColorInt(androidx.annotation.ColorInt) File(java.io.File) ClipData(android.content.ClipData) SpannableStringBuilder(android.text.SpannableStringBuilder) Spannable(android.text.Spannable)

Aggregations

ClipData (android.content.ClipData)1 ClipboardManager (android.content.ClipboardManager)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 LeadingMarginSpan (android.text.style.LeadingMarginSpan)1 Pair (android.util.Pair)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 ColorInt (androidx.annotation.ColorInt)1