Help fallbacks and intent work

- Handle a backup URI, so that if the specified URI is not available,
   another can be used.
 - Add some data to help intents when they are intent URIs
 - Fill in the context with a classname when it isn't present

Bug: 15475009
Change-Id: I7050fa61121901929e650b20bd7a0ae21e8ba207
This commit is contained in:
Jason Monk
2015-05-27 16:02:08 -04:00
parent ecf4339bc7
commit 15dcebe1e7
8 changed files with 53 additions and 32 deletions

View File

@@ -588,7 +588,7 @@ public class DataUsageSummary extends HighlightingFragment implements Indexable
final MenuItem help = menu.findItem(R.id.data_usage_menu_help); final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
String helpUrl; String helpUrl;
if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) { if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
HelpUtils.prepareHelpMenuItem(context, help, helpUrl); HelpUtils.prepareHelpMenuItem(context, help, helpUrl, getClass().getName());
} else { } else {
help.setVisible(false); help.setVisible(false);
} }

View File

@@ -21,9 +21,11 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources.Theme;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -49,6 +51,12 @@ public class HelpUtils {
*/ */
private final static String PARAM_VERSION = "version"; private final static String PARAM_VERSION = "version";
// Constants for help intents.
private static final String EXTRA_CONTEXT = "EXTRA_CONTEXT";
private static final String EXTRA_THEME = "EXTRA_THEME";
private static final String EXTRA_PRIMARY_COLOR = "EXTRA_PRIMARY_COLOR";
private static final String EXTRA_BACKUP_URI = "EXTRA_BACKUP_URI";
/** /**
* Cached version code to prevent repeated calls to the package manager. * Cached version code to prevent repeated calls to the package manager.
*/ */
@@ -57,29 +65,17 @@ public class HelpUtils {
/** Static helper that is not instantiable*/ /** Static helper that is not instantiable*/
private HelpUtils() { } private HelpUtils() { }
public static boolean prepareHelpMenuItem(Context context, Menu menu, String helpUri) { public static boolean prepareHelpMenuItem(Context context, Menu menu, String helpUri,
String backupContext) {
MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label); MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
return prepareHelpMenuItem(context, helpItem, helpUri); return prepareHelpMenuItem(context, helpItem, helpUri, backupContext);
} }
public static boolean prepareHelpMenuItem(Context context, Menu menu, int helpUriResource) { public static boolean prepareHelpMenuItem(Context context, Menu menu, int helpUriResource,
String backupContext) {
MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label); MenuItem helpItem = menu.add(0, MENU_HELP, 0, R.string.help_label);
return prepareHelpMenuItem(context, helpItem, context.getString(helpUriResource)); return prepareHelpMenuItem(context, helpItem, context.getString(helpUriResource),
} backupContext);
/**
* Prepares the help menu item by doing the following.
* - If the string corresponding to the helpUrlResourceId is empty or null, then the help menu
* item is made invisible.
* - Otherwise, this makes the help menu item visible and sets the intent for the help menu
* item to view the URL.
*
* @return returns whether the help menu item has been made visible.
*/
public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem,
int helpUrlResourceId) {
String helpUrlString = context.getResources().getString(helpUrlResourceId);
return prepareHelpMenuItem(context, helpMenuItem, helpUrlString);
} }
/** /**
@@ -91,7 +87,7 @@ public class HelpUtils {
* @return returns whether the help menu item has been made visible. * @return returns whether the help menu item has been made visible.
*/ */
public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem, public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem,
String helpUriString) { String helpUriString, String backupContext) {
if (TextUtils.isEmpty(helpUriString)) { if (TextUtils.isEmpty(helpUriString)) {
// The help url string is empty or null, so set the help menu item to be invisible. // The help url string is empty or null, so set the help menu item to be invisible.
helpMenuItem.setVisible(false); helpMenuItem.setVisible(false);
@@ -99,12 +95,11 @@ public class HelpUtils {
// return that the help menu item is not visible (i.e. false) // return that the help menu item is not visible (i.e. false)
return false; return false;
} else { } else {
Intent intent = getHelpIntent(context, helpUriString); Intent intent = getHelpIntent(context, helpUriString, backupContext);
// Set the intent to the help menu item, show the help menu item in the overflow // Set the intent to the help menu item, show the help menu item in the overflow
// menu, and make it visible. // menu, and make it visible.
ComponentName component = intent.resolveActivity(context.getPackageManager()); if (intent != null) {
if (component != null) {
helpMenuItem.setIntent(intent); helpMenuItem.setIntent(intent);
helpMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); helpMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
helpMenuItem.setVisible(true); helpMenuItem.setVisible(true);
@@ -118,11 +113,23 @@ public class HelpUtils {
} }
} }
public static Intent getHelpIntent(Context context, String helpUriString) { public static Intent getHelpIntent(Context context, String helpUriString,
String backupContext) {
// Try to handle as Intent Uri, otherwise just treat as Uri. // Try to handle as Intent Uri, otherwise just treat as Uri.
try { try {
return Intent.parseUri(helpUriString, Intent intent = Intent.parseUri(helpUriString,
Intent.URI_ANDROID_APP_SCHEME | Intent.URI_INTENT_SCHEME); Intent.URI_ANDROID_APP_SCHEME | Intent.URI_INTENT_SCHEME);
addIntentParameters(context, intent, backupContext);
ComponentName component = intent.resolveActivity(context.getPackageManager());
if (component != null) {
return intent;
} else if (intent.hasExtra(EXTRA_BACKUP_URI)) {
// This extra contains a backup URI for when the intent isn't available.
return getHelpIntent(context, intent.getStringExtra(EXTRA_BACKUP_URI),
backupContext);
} else {
return null;
}
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
} }
// The help url string exists, so first add in some extra query parameters. // The help url string exists, so first add in some extra query parameters.
@@ -136,6 +143,18 @@ public class HelpUtils {
return intent; return intent;
} }
private static void addIntentParameters(Context context, Intent intent, String backupContext) {
if (!intent.hasExtra(EXTRA_CONTEXT)) {
// Insert some context if none exists.
intent.putExtra(EXTRA_CONTEXT, backupContext);
}
intent.putExtra(EXTRA_THEME, 1 /* Light, dark action bar */);
Theme theme = context.getTheme();
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
intent.putExtra(EXTRA_PRIMARY_COLOR, context.getColor(typedValue.resourceId));
}
/** /**
* Adds two query parameters into the Uri, namely the language code and the version code * Adds two query parameters into the Uri, namely the language code and the version code
* of the app's package as gotten via the context. * of the app's package as gotten via the context.

View File

@@ -294,7 +294,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (mHelpUri != null && getActivity() != null) { if (mHelpUri != null && getActivity() != null) {
HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri); HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
} }
} }

View File

@@ -461,7 +461,7 @@ public class ManageApplications extends InstrumentedFragment
return; return;
} }
HelpUtils.prepareHelpMenuItem(getActivity(), menu, mListType == LIST_TYPE_MAIN HelpUtils.prepareHelpMenuItem(getActivity(), menu, mListType == LIST_TYPE_MAIN
? R.string.help_uri_apps : R.string.help_uri_notifications); ? R.string.help_uri_apps : R.string.help_uri_notifications, getClass().getName());
mOptionsMenu = menu; mOptionsMenu = menu;
inflater.inflate(R.menu.manage_apps, menu); inflater.inflate(R.menu.manage_apps, menu);
updateOptionsMenu(); updateOptionsMenu();

View File

@@ -87,7 +87,8 @@ public class DashboardSummary extends InstrumentedFragment {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard); HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard,
getClass().getName());
} }
@Override @Override

View File

@@ -542,7 +542,7 @@ public class FingerprintSettings extends SubSettings {
@Override @Override
public void onClick(View widget) { public void onClick(View widget) {
Context ctx = widget.getContext(); Context ctx = widget.getContext();
Intent intent = HelpUtils.getHelpIntent(ctx, getURL()); Intent intent = HelpUtils.getHelpIntent(ctx, getURL(), ctx.getClass().getName());
try { try {
ctx.startActivity(intent); ctx.startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {

View File

@@ -135,7 +135,7 @@ public class PowerUsageSummary extends PowerUsageBase {
String helpUrl; String helpUrl;
if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_battery))) { if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_battery))) {
final MenuItem help = menu.add(0, MENU_HELP, 0, R.string.help_label); final MenuItem help = menu.add(0, MENU_HELP, 0, R.string.help_label);
HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl); HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl, getClass().getName());
} }
} }

View File

@@ -61,7 +61,8 @@ public class AndroidBeam extends InstrumentedFragment
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_beam); HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_beam,
getClass().getName());
} }
@Override @Override