blob: 368ee1dd68cfca351b2325a1713b2baf99b7b8ef [file] [log] [blame]
Wink Savillea40fb232009-09-26 16:47:18 -07001/*
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;
Amith Yamasanibe023c82009-09-30 12:18:51 -070027import android.view.KeyEvent;
Wink Savillea40fb232009-09-26 16:47:18 -070028import android.webkit.WebView;
29import android.webkit.WebViewClient;
30import com.android.internal.app.AlertActivity;
31import com.android.internal.app.AlertController;
32import android.content.DialogInterface;
Wink Savillea40fb232009-09-26 16:47:18 -070033
34/**
35 * The "dialog" that shows from "Safety information" in the Settings app.
36 */
Amith Yamasanibe023c82009-09-30 12:18:51 -070037public class SettingsSafetyLegalActivity extends AlertActivity
38 implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener {
Wink Savillea40fb232009-09-26 16:47:18 -070039 private static final String PROPERTY_LSAFETYLEGAL_URL = "ro.url.safetylegal";
40
Amith Yamasanibe023c82009-09-30 12:18:51 -070041 private WebView mWebView;
42
Jiehua.Dai20108e22010-05-12 08:37:52 +020043 private AlertDialog mErrorDialog = null;
44
Wink Savillea40fb232009-09-26 16:47:18 -070045 @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
Amith Yamasanibe023c82009-09-30 12:18:51 -070059 mWebView = new WebView(this);
Wink Savillea40fb232009-09-26 16:47:18 -070060
61 // Begin accessing
Amith Yamasanibe023c82009-09-30 12:18:51 -070062 mWebView.getSettings().setJavaScriptEnabled(true);
Amith Yamasani3c9f79b2009-10-06 13:10:14 -070063 if (savedInstanceState == null) {
64 mWebView.loadUrl(userSafetylegalUrl);
65 } else {
66 mWebView.restoreState(savedInstanceState);
67 }
Amith Yamasanibe023c82009-09-30 12:18:51 -070068 mWebView.setWebViewClient(new WebViewClient() {
Wink Savillea40fb232009-09-26 16:47:18 -070069 @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 }
Amith Yamasanibe023c82009-09-30 12:18:51 -070074
75 @Override
76 public void onReceivedError(WebView view, int errorCode,
77 String description, String failingUrl) {
78 showErrorAndFinish(failingUrl);
79 }
Wink Savillea40fb232009-09-26 16:47:18 -070080 });
81
82 final AlertController.AlertParams p = mAlertParams;
83 p.mTitle = getString(R.string.settings_safetylegal_activity_loading);
Amith Yamasanibe023c82009-09-30 12:18:51 -070084 p.mView = mWebView;
Wink Savillea40fb232009-09-26 16:47:18 -070085 p.mForceInverseBackground = true;
86 setupAlert();
87 }
88
89 private void showErrorAndFinish(String url) {
Jiehua.Dai20108e22010-05-12 08:37:52 +020090 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 }
Wink Savillea40fb232009-09-26 16:47:18 -0700115 }
116
Amith Yamasanibe023c82009-09-30 12:18:51 -0700117 @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 }
Wink Savillea40fb232009-09-26 16:47:18 -0700125 }
Amith Yamasanibe023c82009-09-30 12:18:51 -0700126 return super.dispatchKeyEvent(event);
Wink Savillea40fb232009-09-26 16:47:18 -0700127 }
128
Amith Yamasanibe023c82009-09-30 12:18:51 -0700129 public void onClick(DialogInterface dialog, int whichButton) {
130 finish();
131 }
132
133 public void onCancel(DialogInterface dialog) {
134 finish();
135 }
Amith Yamasani3c9f79b2009-10-06 13:10:14 -0700136
137 @Override
138 public void onSaveInstanceState(Bundle icicle) {
139 mWebView.saveState(icicle);
140 super.onSaveInstanceState(icicle);
141 }
Wink Savillea40fb232009-09-26 16:47:18 -0700142}