1daf88feb0aab5806723b850d22a372cafe758239Ray Chen/* 2daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Copyright (C) 2012 The Android Open Source Project 3daf88feb0aab5806723b850d22a372cafe758239Ray Chen * 4daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Licensed under the Apache License, Version 2.0 (the "License"); 5daf88feb0aab5806723b850d22a372cafe758239Ray Chen * you may not use this file except in compliance with the License. 6daf88feb0aab5806723b850d22a372cafe758239Ray Chen * You may obtain a copy of the License at 7daf88feb0aab5806723b850d22a372cafe758239Ray Chen * 8daf88feb0aab5806723b850d22a372cafe758239Ray Chen * http://www.apache.org/licenses/LICENSE-2.0 9daf88feb0aab5806723b850d22a372cafe758239Ray Chen * 10daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Unless required by applicable law or agreed to in writing, software 11daf88feb0aab5806723b850d22a372cafe758239Ray Chen * distributed under the License is distributed on an "AS IS" BASIS, 12daf88feb0aab5806723b850d22a372cafe758239Ray Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13daf88feb0aab5806723b850d22a372cafe758239Ray Chen * See the License for the specific language governing permissions and 14daf88feb0aab5806723b850d22a372cafe758239Ray Chen * limitations under the License. 15daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 16daf88feb0aab5806723b850d22a372cafe758239Ray Chen 17daf88feb0aab5806723b850d22a372cafe758239Ray Chenpackage com.android.gallery3d.util; 18daf88feb0aab5806723b850d22a372cafe758239Ray Chen 19daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.content.Context; 20daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.content.Intent; 21daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.content.pm.PackageInfo; 22daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.content.pm.PackageManager.NameNotFoundException; 23daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.net.Uri; 24daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.text.TextUtils; 25daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport android.util.Log; 26ba04cb7a9c5e57a166f8057950711ec7b8da2ba4Owen Lin 27daf88feb0aab5806723b850d22a372cafe758239Ray Chenimport java.util.Locale; 28daf88feb0aab5806723b850d22a372cafe758239Ray Chen 29daf88feb0aab5806723b850d22a372cafe758239Ray Chen/** 30daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Functions to easily prepare contextual help menu option items with an intent that opens up the 31daf88feb0aab5806723b850d22a372cafe758239Ray Chen * browser to a particular URL, while taking into account the preferred language and app version. 32daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 33daf88feb0aab5806723b850d22a372cafe758239Ray Chenpublic class HelpUtils { 34daf88feb0aab5806723b850d22a372cafe758239Ray Chen private final static String TAG = HelpUtils.class.getName(); 35daf88feb0aab5806723b850d22a372cafe758239Ray Chen 36daf88feb0aab5806723b850d22a372cafe758239Ray Chen /** 37daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Help URL query parameter key for the preferred language. 38daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 39daf88feb0aab5806723b850d22a372cafe758239Ray Chen private final static String PARAM_LANGUAGE_CODE = "hl"; 40daf88feb0aab5806723b850d22a372cafe758239Ray Chen 41daf88feb0aab5806723b850d22a372cafe758239Ray Chen /** 42daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Help URL query parameter key for the app version. 43daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 44daf88feb0aab5806723b850d22a372cafe758239Ray Chen private final static String PARAM_VERSION = "version"; 45daf88feb0aab5806723b850d22a372cafe758239Ray Chen 46daf88feb0aab5806723b850d22a372cafe758239Ray Chen /** 47daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Cached version code to prevent repeated calls to the package manager. 48daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 49daf88feb0aab5806723b850d22a372cafe758239Ray Chen private static String sCachedVersionCode = null; 50daf88feb0aab5806723b850d22a372cafe758239Ray Chen 51daf88feb0aab5806723b850d22a372cafe758239Ray Chen /** Static helper that is not instantiable*/ 522bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin private HelpUtils() {} 53daf88feb0aab5806723b850d22a372cafe758239Ray Chen 542bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin public static Intent getHelpIntent(Context context, int helpUrlResId) { 552bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin String helpUrlString = context.getString(helpUrlResId); 56daf88feb0aab5806723b850d22a372cafe758239Ray Chen 57daf88feb0aab5806723b850d22a372cafe758239Ray Chen if (TextUtils.isEmpty(helpUrlString)) { 582bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin return null; 59daf88feb0aab5806723b850d22a372cafe758239Ray Chen } else { 60daf88feb0aab5806723b850d22a372cafe758239Ray Chen // The help url string exists, so first add in some extra query parameters. 61daf88feb0aab5806723b850d22a372cafe758239Ray Chen final Uri fullUri = uriWithAddedParameters(context, Uri.parse(helpUrlString)); 62daf88feb0aab5806723b850d22a372cafe758239Ray Chen 63daf88feb0aab5806723b850d22a372cafe758239Ray Chen // Then, create an intent that will be fired when the user 64daf88feb0aab5806723b850d22a372cafe758239Ray Chen // selects this help menu item. 65daf88feb0aab5806723b850d22a372cafe758239Ray Chen Intent intent = new Intent(Intent.ACTION_VIEW, fullUri); 66daf88feb0aab5806723b850d22a372cafe758239Ray Chen intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 67daf88feb0aab5806723b850d22a372cafe758239Ray Chen | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 682bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin return intent; 69ba04cb7a9c5e57a166f8057950711ec7b8da2ba4Owen Lin } 70ba04cb7a9c5e57a166f8057950711ec7b8da2ba4Owen Lin } 71ba04cb7a9c5e57a166f8057950711ec7b8da2ba4Owen Lin 72daf88feb0aab5806723b850d22a372cafe758239Ray Chen /** 73daf88feb0aab5806723b850d22a372cafe758239Ray Chen * Adds two query parameters into the Uri, namely the language code and the version code 74daf88feb0aab5806723b850d22a372cafe758239Ray Chen * of the app's package as gotten via the context. 75daf88feb0aab5806723b850d22a372cafe758239Ray Chen * @return the uri with added query parameters 76daf88feb0aab5806723b850d22a372cafe758239Ray Chen */ 77daf88feb0aab5806723b850d22a372cafe758239Ray Chen private static Uri uriWithAddedParameters(Context context, Uri baseUri) { 78daf88feb0aab5806723b850d22a372cafe758239Ray Chen Uri.Builder builder = baseUri.buildUpon(); 79daf88feb0aab5806723b850d22a372cafe758239Ray Chen 80daf88feb0aab5806723b850d22a372cafe758239Ray Chen // Add in the preferred language 81daf88feb0aab5806723b850d22a372cafe758239Ray Chen builder.appendQueryParameter(PARAM_LANGUAGE_CODE, Locale.getDefault().toString()); 82daf88feb0aab5806723b850d22a372cafe758239Ray Chen 83daf88feb0aab5806723b850d22a372cafe758239Ray Chen // Add in the package version code 84daf88feb0aab5806723b850d22a372cafe758239Ray Chen if (sCachedVersionCode == null) { 85daf88feb0aab5806723b850d22a372cafe758239Ray Chen // There is no cached version code, so try to get it from the package manager. 86daf88feb0aab5806723b850d22a372cafe758239Ray Chen try { 87daf88feb0aab5806723b850d22a372cafe758239Ray Chen // cache the version code 88daf88feb0aab5806723b850d22a372cafe758239Ray Chen PackageInfo info = context.getPackageManager().getPackageInfo( 89daf88feb0aab5806723b850d22a372cafe758239Ray Chen context.getPackageName(), 0); 90daf88feb0aab5806723b850d22a372cafe758239Ray Chen sCachedVersionCode = Integer.toString(info.versionCode); 91daf88feb0aab5806723b850d22a372cafe758239Ray Chen 92daf88feb0aab5806723b850d22a372cafe758239Ray Chen // append the version code to the uri 93daf88feb0aab5806723b850d22a372cafe758239Ray Chen builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode); 94daf88feb0aab5806723b850d22a372cafe758239Ray Chen } catch (NameNotFoundException e) { 95daf88feb0aab5806723b850d22a372cafe758239Ray Chen // Cannot find the package name, so don't add in the version parameter 96daf88feb0aab5806723b850d22a372cafe758239Ray Chen // This shouldn't happen. 97daf88feb0aab5806723b850d22a372cafe758239Ray Chen Log.wtf(TAG, "Invalid package name for context", e); 98daf88feb0aab5806723b850d22a372cafe758239Ray Chen } 99daf88feb0aab5806723b850d22a372cafe758239Ray Chen } else { 100daf88feb0aab5806723b850d22a372cafe758239Ray Chen builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode); 101daf88feb0aab5806723b850d22a372cafe758239Ray Chen } 102daf88feb0aab5806723b850d22a372cafe758239Ray Chen 103daf88feb0aab5806723b850d22a372cafe758239Ray Chen // Build the full uri and return it 104daf88feb0aab5806723b850d22a372cafe758239Ray Chen return builder.build(); 105daf88feb0aab5806723b850d22a372cafe758239Ray Chen } 106daf88feb0aab5806723b850d22a372cafe758239Ray Chen} 107