Home | History | Annotate | Download | only in settings
      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.settings;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.PackageInfo;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.net.Uri;
     25 import android.text.TextUtils;
     26 import android.util.Log;
     27 import android.view.MenuItem;
     28 
     29 import java.util.Locale;
     30 
     31 /**
     32  * Functions to easily prepare contextual help menu option items with an intent that opens up the
     33  * browser to a particular URL, while taking into account the preferred language and app version.
     34  */
     35 public class HelpUtils {
     36     private final static String TAG = HelpUtils.class.getName();
     37 
     38     /**
     39      * Help URL query parameter key for the preferred language.
     40      */
     41     private final static String PARAM_LANGUAGE_CODE = "hl";
     42 
     43     /**
     44      * Help URL query parameter key for the app version.
     45      */
     46     private final static String PARAM_VERSION = "version";
     47 
     48     /**
     49      * Cached version code to prevent repeated calls to the package manager.
     50      */
     51     private static String sCachedVersionCode = null;
     52 
     53     /** Static helper that is not instantiable*/
     54     private HelpUtils() { }
     55 
     56     /**
     57      * Prepares the help menu item by doing the following.
     58      * - If the string corresponding to the helpUrlResourceId is empty or null, then the help menu
     59      *   item is made invisible.
     60      * - Otherwise, this makes the help menu item visible and sets the intent for the help menu
     61      *   item to view the URL.
     62      *
     63      * @return returns whether the help menu item has been made visible.
     64      */
     65     public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem,
     66             int helpUrlResourceId) {
     67         String helpUrlString = context.getResources().getString(helpUrlResourceId);
     68         return prepareHelpMenuItem(context, helpMenuItem, helpUrlString);
     69     }
     70 
     71     /**
     72      * Prepares the help menu item by doing the following.
     73      * - If the helpUrlString is empty or null, the help menu item is made invisible.
     74      * - Otherwise, this makes the help menu item visible and sets the intent for the help menu
     75      *   item to view the URL.
     76      *
     77      * @return returns whether the help menu item has been made visible.
     78      */
     79     public static boolean prepareHelpMenuItem(Context context, MenuItem helpMenuItem,
     80             String helpUrlString) {
     81         if (TextUtils.isEmpty(helpUrlString)) {
     82             // The help url string is empty or null, so set the help menu item to be invisible.
     83             helpMenuItem.setVisible(false);
     84 
     85             // return that the help menu item is not visible (i.e. false)
     86             return false;
     87         } else {
     88             // The help url string exists, so first add in some extra query parameters.
     89             final Uri fullUri = uriWithAddedParameters(context, Uri.parse(helpUrlString));
     90 
     91             // Then, create an intent that will be fired when the user
     92             // selects this help menu item.
     93             Intent intent = new Intent(Intent.ACTION_VIEW, fullUri);
     94             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     95                     | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
     96 
     97             // Set the intent to the help menu item, show the help menu item in the overflow
     98             // menu, and make it visible.
     99             ComponentName component = intent.resolveActivity(context.getPackageManager());
    100             if (component != null) {
    101                 helpMenuItem.setIntent(intent);
    102                 helpMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    103                 helpMenuItem.setVisible(true);
    104             } else {
    105                 helpMenuItem.setVisible(false);
    106                 return false;
    107             }
    108 
    109             // return that the help menu item is visible (i.e., true)
    110             return true;
    111         }
    112     }
    113 
    114     /**
    115      * Adds two query parameters into the Uri, namely the language code and the version code
    116      * of the app's package as gotten via the context.
    117      * @return the uri with added query parameters
    118      */
    119     public static Uri uriWithAddedParameters(Context context, Uri baseUri) {
    120         Uri.Builder builder = baseUri.buildUpon();
    121 
    122         // Add in the preferred language
    123         builder.appendQueryParameter(PARAM_LANGUAGE_CODE, Locale.getDefault().toString());
    124 
    125         // Add in the package version code
    126         if (sCachedVersionCode == null) {
    127             // There is no cached version code, so try to get it from the package manager.
    128             try {
    129                 // cache the version code
    130                 PackageInfo info = context.getPackageManager().getPackageInfo(
    131                         context.getPackageName(), 0);
    132                 sCachedVersionCode = Integer.toString(info.versionCode);
    133 
    134                 // append the version code to the uri
    135                 builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode);
    136             } catch (NameNotFoundException e) {
    137                 // Cannot find the package name, so don't add in the version parameter
    138                 // This shouldn't happen.
    139                 Log.wtf(TAG, "Invalid package name for context", e);
    140             }
    141         } else {
    142             builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode);
    143         }
    144 
    145         // Build the full uri and return it
    146         return builder.build();
    147     }
    148 }
    149