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    private AlertDialog mErrorDialog = null;
44
45    @Override
46    protected void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48
49        String userSafetylegalUrl = SystemProperties.get(PROPERTY_LSAFETYLEGAL_URL);
50
51        final Configuration configuration = getResources().getConfiguration();
52        final String language = configuration.locale.getLanguage();
53        final String country = configuration.locale.getCountry();
54
55        String loc = String.format("locale=%s-%s", language, country);
56
57        userSafetylegalUrl = String.format("%s&%s", userSafetylegalUrl, loc);
58
59        mWebView = new WebView(this);
60
61        // Begin accessing
62        mWebView.getSettings().setJavaScriptEnabled(true);
63        if (savedInstanceState == null) {
64            mWebView.loadUrl(userSafetylegalUrl);
65        } else {
66            mWebView.restoreState(savedInstanceState);
67        }
68        mWebView.setWebViewClient(new WebViewClient() {
69            @Override
70            public void onPageFinished(WebView view, String url) {
71                // Change from 'Loading...' to the real title
72                mAlert.setTitle(getString(R.string.settings_safetylegal_activity_title));
73            }
74
75            @Override
76            public void onReceivedError(WebView view, int errorCode,
77                    String description, String failingUrl) {
78                showErrorAndFinish(failingUrl);
79            }
80        });
81
82        final AlertController.AlertParams p = mAlertParams;
83        p.mTitle = getString(R.string.settings_safetylegal_activity_loading);
84        p.mView = mWebView;
85        p.mForceInverseBackground = true;
86        setupAlert();
87    }
88
89    private void showErrorAndFinish(String url) {
90        if (mErrorDialog == null) {
91            mErrorDialog = new AlertDialog.Builder(this)
92                    .setTitle(R.string.settings_safetylegal_activity_title)
93                    .setPositiveButton(android.R.string.ok, this)
94                    .setOnCancelListener(this)
95                    .setCancelable(true)
96                    .create();
97        } else {
98            if (mErrorDialog.isShowing()) {
99                mErrorDialog.dismiss();
100            }
101        }
102        mErrorDialog.setMessage(getResources()
103                .getString(R.string.settings_safetylegal_activity_unreachable, url));
104        mErrorDialog.show();
105    }
106
107    @Override
108    protected void onDestroy() {
109        super.onDestroy();
110
111        if (mErrorDialog != null) {
112            mErrorDialog.dismiss();
113            mErrorDialog = null;
114        }
115    }
116
117    @Override
118    public boolean dispatchKeyEvent(KeyEvent event) {
119        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
120                && event.getAction() == KeyEvent.ACTION_DOWN) {
121            if (mWebView.canGoBack()) {
122                mWebView.goBack();
123                return true;
124            }
125        }
126        return super.dispatchKeyEvent(event);
127    }
128
129    public void onClick(DialogInterface dialog, int whichButton) {
130        finish();
131    }
132
133    public void onCancel(DialogInterface dialog) {
134        finish();
135    }
136
137    @Override
138    public void onSaveInstanceState(Bundle icicle) {
139        mWebView.saveState(icicle);
140        super.onSaveInstanceState(icicle);
141    }
142}
143