FileDocCategorySizeDatePackage
GoogleWebContentHelper.javaAPI DocAndroid 1.5 API9318Wed May 06 22:41:56 BST 2009com.google.android.util

GoogleWebContentHelper

public class GoogleWebContentHelper extends Object
Helper to display Google web content, and fallback on a static message if the web content is unreachable. For example, this can be used to display "Legal terms".

The typical usage pattern is to have two Gservices settings defined:

  • A secure URL that will be displayed on the device. This should be HTTPS so hotspots won't intercept it giving us a false positive that the page loaded successfully.
  • A pretty human-readable URL that will be displayed to the user in case we cannot reach the above URL.

The typical call sequence is {@link #setUrlsFromGservices(String, String)}, {@link #setUnsuccessfulMessage(String)}, and {@link #loadUrl()}. At some point, you'll want to display the layout via {@link #getLayout()}.

Fields Summary
private android.content.Context
mContext
private String
mSecureUrl
private String
mPrettyUrl
private String
mUnsuccessfulMessage
private android.view.ViewGroup
mLayout
private android.webkit.WebView
mWebView
private android.view.View
mProgressBar
private android.widget.TextView
mTextView
private boolean
mReceivedResponse
Constructors Summary
public GoogleWebContentHelper(android.content.Context context)

        mContext = context;
    
Methods Summary
private synchronized voidensureViews()

        if (mLayout == null) {
            initializeViews();
        }
    
private static java.lang.StringfillUrl(java.lang.String url, android.content.Context context)
Fills the URL with the locale.

param
url The URL in Formatter style for the extra info to be filled in.
return
The filled URL.

        
        if (TextUtils.isEmpty(url)) {
            return "";
        }

        /* We add another layer of indirection here to allow mcc's to fill
         * in Locales for TOS.  TODO - REMOVE when needed locales supported
         * natively (when not shipping devices to country X without support
         * for their locale).
         */
        String localeReplacement = context.
                getString(com.android.internal.R.string.locale_replacement);
        if (localeReplacement != null && localeReplacement.length() != 0) {
            url = String.format(url, localeReplacement);
        }

        Locale locale = Locale.getDefault();
        String tmp = locale.getLanguage() + "_" + locale.getCountry().toLowerCase();
        return String.format(url, tmp);
    
public android.view.ViewGroupgetLayout()
Returns the layout containing the web view, progress bar, and text view. This class takes care of setting each one's visibility based on current state.

return
The layout you should display.

        ensureViews();
        return mLayout;
    
public booleanhandleKey(android.view.KeyEvent event)
Helper to handle the back key. Returns true if the back key was handled, otherwise returns false.

param
event the key event sent to {@link Activity#dispatchKeyEvent()}

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK 
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
        }
        return false;
    
private synchronized voidhandleWebViewCompletion(boolean success)

        
        if (mReceivedResponse) {
            return;
        } else {
            mReceivedResponse = true;
        }
        
        // In both cases, remove the progress bar
        ((ViewGroup) mProgressBar.getParent()).removeView(mProgressBar);

        // Remove the view that isn't relevant
        View goneView = success ? mTextView : mWebView;
        ((ViewGroup) goneView.getParent()).removeView(goneView);

        // Show the next view, which depends on success
        View visibleView = success ? mWebView : mTextView;
        visibleView.setVisibility(View.VISIBLE);
    
private voidinitializeViews()


        LayoutInflater inflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        mLayout = (ViewGroup) inflater.inflate(
                com.android.internal.R.layout.google_web_content_helper_layout, null);

        mWebView = (WebView) mLayout.findViewById(com.android.internal.R.id.web);
        mWebView.setWebViewClient(new MyWebViewClient());
        WebSettings settings = mWebView.getSettings();
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        
        mProgressBar = mLayout.findViewById(com.android.internal.R.id.progressContainer);
        TextView message = (TextView) mProgressBar.findViewById(com.android.internal.R.id.message);
        message.setText(com.android.internal.R.string.googlewebcontenthelper_loading);
        
        mTextView = (TextView) mLayout.findViewById(com.android.internal.R.id.text);
        mTextView.setText(mUnsuccessfulMessage);
    
public com.google.android.util.GoogleWebContentHelperloadUrl()
Begins loading the secure URL.

return
This {@link GoogleWebContentHelper} so methods can be chained.

        ensureViews();
        mWebView.loadUrl(mSecureUrl);
        return this;
    
public com.google.android.util.GoogleWebContentHelpersetUnsuccessfulMessage(java.lang.String message)
Sets the message that will be shown if we are unable to load the page.

This should be called after {@link #setUrlsFromGservices(String, String)} .

param
message The message to load. The first argument, according to {@link java.util.Formatter}, will be substituted with the pretty URL.
return
This {@link GoogleWebContentHelper} so methods can be chained.

        Locale locale = mContext.getResources().getConfiguration().locale;
        mUnsuccessfulMessage = String.format(locale, message, mPrettyUrl);
        return this;
    
public com.google.android.util.GoogleWebContentHelpersetUrls(java.lang.String secureUrl, java.lang.String prettyUrl)
Fetch directly from provided urls.

param
secureUrl The HTTPS URL.
param
prettyUrl The pretty URL.
return
This {@link GoogleWebContentHelper} so methods can be chained.

        mSecureUrl = fillUrl(secureUrl, mContext);
        mPrettyUrl = fillUrl(prettyUrl, mContext);
        return this;
    
public com.google.android.util.GoogleWebContentHelpersetUrlsFromGservices(java.lang.String secureSetting, java.lang.String prettySetting)
Fetches the URLs from Gservices.

param
secureSetting The setting key whose value contains the HTTPS URL.
param
prettySetting The setting key whose value contains the pretty URL.
return
This {@link GoogleWebContentHelper} so methods can be chained.

        ContentResolver contentResolver = mContext.getContentResolver();
        mSecureUrl = fillUrl(Settings.Gservices.getString(contentResolver, secureSetting),
                mContext);
        mPrettyUrl = fillUrl(Settings.Gservices.getString(contentResolver, prettySetting), 
                mContext);
        return this;