From 61209bc9245764f86b7d9fa9f1105239b994fe0b Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Wed, 22 Jun 2016 16:12:27 -0700 Subject: [PATCH] Do not launch non exposed add printer activity Change-Id: I0b9b11ea0ce0140c77dff2539b1a0a9dcf1558b0 Fixes: 29580579 --- .../printspooler/ui/AddPrinterActivity.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java index 42ef10e01158f..2f58de531dda7 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java @@ -26,6 +26,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.Loader; +import android.content.pm.ResolveInfo; import android.database.DataSetObserver; import android.net.Uri; import android.os.Bundle; @@ -450,20 +451,42 @@ public class AddPrinterActivity extends ListActivity implements AdapterView.OnIt private class EnabledServicesAdapter extends PrintServiceInfoAdapter { @Override public void performAction(@IntRange(from = 0) int position) { - PrintServiceInfo service = (PrintServiceInfo) getItem(position); + Intent intent = getAddPrinterIntent((PrintServiceInfo) getItem(position)); + if (intent != null) { + try { + startActivity(intent); + } catch (ActivityNotFoundException|SecurityException e) { + Log.e(LOG_TAG, "Cannot start add printers activity", e); + } + } + } + + /** + * Get the intent used to launch the add printers activity. + * + * @param service The service the printer should be added for + * + * @return The intent to launch the activity or null if the activity could not be launched. + */ + private Intent getAddPrinterIntent(@NonNull PrintServiceInfo service) { String addPrinterActivityName = service.getAddPrintersActivityName(); if (!TextUtils.isEmpty(addPrinterActivityName)) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(new ComponentName(service.getComponentName().getPackageName(), - addPrinterActivityName)); + addPrinterActivityName)); - try { - startActivity(intent); - } catch (ActivityNotFoundException e) { - Log.e(LOG_TAG, "Cannot start add printers activity", e); + List resolvedActivities = getPackageManager().queryIntentActivities( + intent, 0); + if (!resolvedActivities.isEmpty()) { + // The activity is a component name, therefore it is one or none. + if (resolvedActivities.get(0).activityInfo.exported) { + return intent; + } } } + + return null; } @Override @@ -494,7 +517,7 @@ public class AddPrinterActivity extends ListActivity implements AdapterView.OnIt title.setText(service.getResolveInfo().loadLabel(getPackageManager())); icon.setImageDrawable(service.getResolveInfo().loadIcon(getPackageManager())); - if (TextUtils.isEmpty(service.getAddPrintersActivityName())) { + if (getAddPrinterIntent(service) == null) { subtitle.setText(getString(R.string.cannot_add_printer)); } else { subtitle.setText(getString(R.string.select_to_add_printers));