Merge "Do not launch non exposed add printer activity" into nyc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
737be111cc
@@ -26,6 +26,7 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.Loader;
|
import android.content.Loader;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
import android.database.DataSetObserver;
|
import android.database.DataSetObserver;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -450,7 +451,24 @@ public class AddPrinterActivity extends ListActivity implements AdapterView.OnIt
|
|||||||
private class EnabledServicesAdapter extends PrintServiceInfoAdapter {
|
private class EnabledServicesAdapter extends PrintServiceInfoAdapter {
|
||||||
@Override
|
@Override
|
||||||
public void performAction(@IntRange(from = 0) int position) {
|
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();
|
String addPrinterActivityName = service.getAddPrintersActivityName();
|
||||||
|
|
||||||
if (!TextUtils.isEmpty(addPrinterActivityName)) {
|
if (!TextUtils.isEmpty(addPrinterActivityName)) {
|
||||||
@@ -458,14 +476,19 @@ public class AddPrinterActivity extends ListActivity implements AdapterView.OnIt
|
|||||||
intent.setComponent(new ComponentName(service.getComponentName().getPackageName(),
|
intent.setComponent(new ComponentName(service.getComponentName().getPackageName(),
|
||||||
addPrinterActivityName));
|
addPrinterActivityName));
|
||||||
|
|
||||||
try {
|
List<ResolveInfo> resolvedActivities = getPackageManager().queryIntentActivities(
|
||||||
startActivity(intent);
|
intent, 0);
|
||||||
} catch (ActivityNotFoundException e) {
|
if (!resolvedActivities.isEmpty()) {
|
||||||
Log.e(LOG_TAG, "Cannot start add printers activity", e);
|
// The activity is a component name, therefore it is one or none.
|
||||||
|
if (resolvedActivities.get(0).activityInfo.exported) {
|
||||||
|
return intent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View getView(int position, View convertView, ViewGroup parent) {
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
if (position == 0) {
|
if (position == 0) {
|
||||||
@@ -494,7 +517,7 @@ public class AddPrinterActivity extends ListActivity implements AdapterView.OnIt
|
|||||||
title.setText(service.getResolveInfo().loadLabel(getPackageManager()));
|
title.setText(service.getResolveInfo().loadLabel(getPackageManager()));
|
||||||
icon.setImageDrawable(service.getResolveInfo().loadIcon(getPackageManager()));
|
icon.setImageDrawable(service.getResolveInfo().loadIcon(getPackageManager()));
|
||||||
|
|
||||||
if (TextUtils.isEmpty(service.getAddPrintersActivityName())) {
|
if (getAddPrinterIntent(service) == null) {
|
||||||
subtitle.setText(getString(R.string.cannot_add_printer));
|
subtitle.setText(getString(R.string.cannot_add_printer));
|
||||||
} else {
|
} else {
|
||||||
subtitle.setText(getString(R.string.select_to_add_printers));
|
subtitle.setText(getString(R.string.select_to_add_printers));
|
||||||
|
|||||||
Reference in New Issue
Block a user