SettingsSafetyLegalActivity.java revision 3c9f79ba35c29ee6988f5b0b8a7bb7e59614088a
1/*
2 * Copyright (C) 2009 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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.os.Build;
23import android.os.Bundle;
24import android.os.SystemProperties;
25import android.telephony.TelephonyManager;
26import android.text.TextUtils;
27import android.view.KeyEvent;
28import android.webkit.WebView;
29import android.webkit.WebViewClient;
30import com.android.internal.app.AlertActivity;
31import com.android.internal.app.AlertController;
32import android.content.DialogInterface;
33
34/**
35 * The "dialog" that shows from "Safety information" in the Settings app.
36 */
37public class SettingsSafetyLegalActivity extends AlertActivity
38        implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener {
39    private static final String PROPERTY_LSAFETYLEGAL_URL = "ro.url.safetylegal";
40
41    private WebView mWebView;
42
43    @Override
44    protected void onCreate(Bundle savedInstanceState) {
45        super.onCreate(savedInstanceState);
46
47        String userSafetylegalUrl = SystemProperties.get(PROPERTY_LSAFETYLEGAL_URL);
48
49        final Configuration configuration = getResources().getConfiguration();
50        final String language = configuration.locale.getLanguage();
51        final String country = configuration.locale.getCountry();
52
53        String loc = String.format("locale=%s-%s", language, country);
54
55        userSafetylegalUrl = String.format("%s&%s", userSafetylegalUrl, loc);
56
57        mWebView = new WebView(this);
58
59        // Begin accessing
60        mWebView.getSettings().setJavaScriptEnabled(true);
61        if (savedInstanceState == null) {
62            mWebView.loadUrl(userSafetylegalUrl);
63        } else {
64            mWebView.restoreState(savedInstanceState);
65        }
66        mWebView.setWebViewClient(new WebViewClient() {
67            @Override
68            public void onPageFinished(WebView view, String url) {
69                // Change from 'Loading...' to the real title
70                mAlert.setTitle(getString(R.string.settings_safetylegal_activity_title));
71            }
72
73            @Override
74            public void onReceivedError(WebView view, int errorCode,
75                    String description, String failingUrl) {
76                showErrorAndFinish(failingUrl);
77            }
78        });
79
80        final AlertController.AlertParams p = mAlertParams;
81        p.mTitle = getString(R.string.settings_safetylegal_activity_loading);
82        p.mView = mWebView;
83        p.mForceInverseBackground = true;
84        setupAlert();
85    }
86
87    private void showErrorAndFinish(String url) {
88        new AlertDialog.Builder(this)
89                .setMessage(getResources()
90                        .getString(R.string.settings_safetylegal_activity_unreachable, url))
91                .setTitle(R.string.settings_safetylegal_activity_title)
92                .setPositiveButton(android.R.string.ok, this)
93                .setOnCancelListener(this)
94                .setCancelable(true)
95                .show();
96    }
97
98    @Override
99    public boolean dispatchKeyEvent(KeyEvent event) {
100        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
101                && event.getAction() == KeyEvent.ACTION_DOWN) {
102            if (mWebView.canGoBack()) {
103                mWebView.goBack();
104                return true;
105            }
106        }
107        return super.dispatchKeyEvent(event);
108    }
109
110    public void onClick(DialogInterface dialog, int whichButton) {
111        finish();
112    }
113
114    public void onCancel(DialogInterface dialog) {
115        finish();
116    }
117
118    @Override
119    public void onSaveInstanceState(Bundle icicle) {
120        mWebView.saveState(icicle);
121        super.onSaveInstanceState(icicle);
122    }
123}
124