Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 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 package com.android.mail.utils;
     17 
     18 import android.content.Context;
     19 import android.content.pm.PackageInfo;
     20 import android.content.pm.PackageManager;
     21 import android.net.Uri;
     22 import android.text.TextUtils;
     23 
     24 import java.util.Locale;
     25 
     26 /**
     27  * Constructs the URL for the context sensitive help page for this mail application. The URL will be
     28  * specific to the user's language, application, application version and topic. For example:
     29  *
     30  * <p>http://support.example.com/email/?hl=en-us&p=androidhelp&version=1 for the top level english
     31  * help page for version 1 of the mail application</p>
     32  * <p>http://support.example.com/email/?hl=fr-fr&p=email_compose&version=2 for the compose email
     33  * french help page for version 2 of the mail application</p>
     34  */
     35 public final class HelpUrl {
     36     private static final String LOG_TAG = LogTag.getLogTag();
     37 
     38     private HelpUrl() {}
     39 
     40     /**
     41      * Constructs the URL for the context sensitive help page for this mail application.
     42      *
     43      * @param context a context from which to read resources
     44      * @param topic describes the help topic to display; this String cannot be empty.
     45      * @return Url for the Help page that is specific to a language, application, version and topic
     46      */
     47     public static Uri getHelpUrl(final Context context, Uri helpUri, String topic) {
     48         if (TextUtils.isEmpty(topic)) {
     49             throw new IllegalArgumentException("topic must be non-empty");
     50         }
     51 
     52         // %locale% is a special variable encoded in the Uri that should be replaced if it exists
     53         if (helpUri.toString().contains("%locale%")) {
     54             helpUri = Uri.parse(helpUri.toString().replace("%locale%", getLocale()));
     55         }
     56 
     57         final Uri.Builder builder = helpUri.buildUpon();
     58         builder.appendQueryParameter("p", topic);
     59         builder.appendQueryParameter("version", getVersion(context));
     60 
     61         return builder.build();
     62     }
     63 
     64     private static String getLocale() {
     65         final Locale locale = Locale.getDefault();
     66         return locale.getLanguage() + "-" + locale.getCountry().toLowerCase();
     67     }
     68 
     69     private static String getVersion(final Context context) {
     70         final String packageName = context.getApplicationInfo().packageName;
     71         try {
     72             final PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0);
     73             return String.valueOf(pi.versionCode);
     74         } catch (PackageManager.NameNotFoundException e) {
     75             LogUtils.e(LOG_TAG, "Error finding package name for application" + packageName);
     76             throw new IllegalStateException("unable to determine package name for application");
     77         }
     78     }
     79 }
     80