Better Approach to Checking Intent Safety


#1

Regarding the end of Chapter 21, where the book addresses implicit intent validation by calling PackageManager’s queryIntentActivities() method, wouldn’t the better approach be to functionalize such a thing privately and then call the function in onCreateView()?.

Rather than setting up a separate structure to check for each individual intent, I just created the following private method in CrimeFragment.java:

private boolean isIntentSafe(Intent i) { PackageManager pm = getActivity().getPackageManager(); List<ResolveInfo> activities = pm.queryIntentActivities(i, 0); boolean safe = activities.size() > 0; return safe; }

And now I can run that anywhere I’d like when creating an intent in onCreateView, passing in the intent before starting an activity with it:

mSuspectButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); if (isIntentSafe(i)){ startActivityForResult(i, REQUEST_CONTACT); } } });

Anyway, it’s a small adjustment – I just thought I’d point it out as an alternate design approach.