1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.contacts.util; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.net.Uri; 24 import android.text.TextUtils; 25 import android.util.Log; 26 import android.view.MenuItem; 27 28 import java.util.Locale; 29 30 /** 31 * Functions to easily prepare contextual help menu option items with an intent that opens up the 32 * browser to a particular URL, while taking into account the preferred language and app version. 33 */ 34 public class HelpUtils { 35 private final static String TAG = HelpUtils.class.getName(); 36 37 /** 38 * Help URL query parameter key for the preferred language. 39 */ 40 private final static String PARAM_LANGUAGE_CODE = "hl"; 41 42 /** 43 * Help URL query parameter key for the app version. 44 */ 45 private final static String PARAM_VERSION = "version"; 46 47 /** 48 * Cached version code to prevent repeated calls to the package manager. 49 */ 50 private static String sCachedVersionCode = null; 51 52 /** Static helper that is not instantiable*/ HelpUtils()53 private HelpUtils() { } 54 55 /** 56 * Prepares the help menu item by doing the following. 57 * - If the string corresponding to the helpUrlResourceId is empty or null, then the help menu 58 * item is made invisible. 59 * - Otherwise, this makes the help menu item visible and sets the intent for the help menu 60 * item to view the URL. 61 * 62 * @return returns whether the help menu item has been made visible. 63 */ prepareHelpMenuItem(Context context, MenuItem helpMenuItem, int helpUrlResourceId)64 public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem, 65 int helpUrlResourceId) { 66 String helpUrlString = context.getResources().getString(helpUrlResourceId); 67 return prepareHelpMenuItem(context, helpMenuItem, helpUrlString); 68 } 69 70 /** 71 * Prepares the help menu item by doing the following. 72 * - If the helpUrlString is empty or null, the help menu item is made invisible. 73 * - Otherwise, this makes the help menu item visible and sets the intent for the help menu 74 * item to view the URL. 75 * 76 * @return returns whether the help menu item has been made visible. 77 */ prepareHelpMenuItem(Context context, MenuItem helpMenuItem, String helpUrlString)78 public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem, 79 String helpUrlString) { 80 if (TextUtils.isEmpty(helpUrlString)) { 81 // The help url string is empty or null, so set the help menu item to be invisible. 82 helpMenuItem.setVisible(false); 83 84 // return that the help menu item is not visible (i.e. false) 85 return false; 86 } else { 87 // The help url string exists, so first add in some extra query parameters. 88 final Uri fullUri = uriWithAddedParameters(context, Uri.parse(helpUrlString)); 89 90 // Then, create an intent that will be fired when the user 91 // selects this help menu item. 92 Intent intent = new Intent(Intent.ACTION_VIEW, fullUri); 93 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 94 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 95 96 // Set the intent to the help menu item, show the help menu item in the overflow 97 // menu, and make it visible. 98 helpMenuItem.setIntent(intent); 99 helpMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 100 helpMenuItem.setVisible(true); 101 102 // return that the help menu item is visible (i.e., true) 103 return true; 104 } 105 } 106 107 /** 108 * Adds two query parameters into the Uri, namely the language code and the version code 109 * of the app's package as gotten via the context. 110 * @return the uri with added query parameters 111 */ uriWithAddedParameters(Context context, Uri baseUri)112 private static Uri uriWithAddedParameters(Context context, Uri baseUri) { 113 Uri.Builder builder = baseUri.buildUpon(); 114 115 // Add in the preferred language 116 builder.appendQueryParameter(PARAM_LANGUAGE_CODE, Locale.getDefault().toString()); 117 118 // Add in the package version code 119 if (sCachedVersionCode == null) { 120 // There is no cached version code, so try to get it from the package manager. 121 try { 122 // cache the version code 123 PackageInfo info = context.getPackageManager().getPackageInfo( 124 context.getPackageName(), 0); 125 sCachedVersionCode = Integer.toString(info.versionCode); 126 127 // append the version code to the uri 128 builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode); 129 } catch (NameNotFoundException e) { 130 // Cannot find the package name, so don't add in the version parameter 131 // This shouldn't happen. 132 Log.wtf(TAG, "Invalid package name for context", e); 133 } 134 } else { 135 builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode); 136 } 137 138 // Build the full uri and return it 139 return builder.build(); 140 } 141 } 142