2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.os.SystemProperties;
23 import android.text.TextUtils;
24 import android.util.Log;
25 import android.webkit.WebView;
26 import android.webkit.WebViewClient;
27 import android.widget.Toast;
28 import android.app.Activity;
29 import android.app.AlertDialog;
30 import android.app.ProgressDialog;
31 import android.content.DialogInterface;
32 import android.content.DialogInterface.OnDismissListener;
33 import android.content.res.Configuration;
35 import java.io.FileInputStream;
36 import java.io.FileNotFoundException;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.util.zip.GZIPInputStream;
43 * The "dialog" that shows from "License" in the Settings app.
45 public class SettingsLicenseActivity extends Activity {
47 private static final String TAG = "SettingsLicenseActivity";
48 private static final boolean LOGV = false || false;
50 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
51 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
53 private Handler mHandler;
54 private WebView mWebView;
55 private ProgressDialog mSpinnerDlg;
56 private AlertDialog mTextDlg;
58 private class LicenseFileLoader implements Runnable {
60 private static final String INNER_TAG = "SettingsLicenseActivity.LicenseFileLoader";
61 public static final int STATUS_OK = 0;
62 public static final int STATUS_NOT_FOUND = 1;
63 public static final int STATUS_READ_ERROR = 2;
64 public static final int STATUS_EMPTY_FILE = 3;
66 private String mFileName;
67 private Handler mHandler;
69 public LicenseFileLoader(String fileName, Handler handler) {
76 int status = STATUS_OK;
78 InputStreamReader inputReader = null;
79 StringBuilder data = new StringBuilder(2048);
81 char[] tmp = new char[2048];
83 if (mFileName.endsWith(".gz")) {
84 inputReader = new InputStreamReader(
85 new GZIPInputStream(new FileInputStream(mFileName)));
87 inputReader = new FileReader(mFileName);
90 while ((numRead = inputReader.read(tmp)) >= 0) {
91 data.append(tmp, 0, numRead);
93 } catch (FileNotFoundException e) {
94 Log.e(INNER_TAG, "License HTML file not found at " + mFileName, e);
95 status = STATUS_NOT_FOUND;
96 } catch (IOException e) {
97 Log.e(INNER_TAG, "Error reading license HTML file at " + mFileName, e);
98 status = STATUS_READ_ERROR;
101 if (inputReader != null) {
104 } catch (IOException e) {
108 if ((status == STATUS_OK) && TextUtils.isEmpty(data)) {
109 Log.e(INNER_TAG, "License HTML is empty (from " + mFileName + ")");
110 status = STATUS_EMPTY_FILE;
113 // Tell the UI thread that we are finished.
114 Message msg = mHandler.obtainMessage(status, null);
115 if (status == STATUS_OK) {
116 msg.obj = data.toString();
118 mHandler.sendMessage(msg);
122 public SettingsLicenseActivity() {
131 protected void onCreate(Bundle savedInstanceState) {
132 super.onCreate(savedInstanceState);
134 String fileName = SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
135 if (TextUtils.isEmpty(fileName)) {
136 Log.e(TAG, "The system property for the license file is empty.");
137 showErrorAndFinish();
141 // The activity does not have any view itself,
142 // so set it invisible to avoid displaying the title text in the background.
145 mWebView = new WebView(this);
147 mHandler = new Handler() {
150 public void handleMessage(Message msg) {
151 super.handleMessage(msg);
153 if (msg.what == LicenseFileLoader.STATUS_OK) {
154 String text = (String) msg.obj;
155 showPageOfText(text);
157 showErrorAndFinish();
162 CharSequence title = getText(R.string.settings_license_activity_title);
163 CharSequence msg = getText(R.string.settings_license_activity_loading);
165 ProgressDialog pd = ProgressDialog.show(this, title, msg, true, false);
166 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
169 // Start separate thread to do the actual loading.
170 Thread thread = new Thread(new LicenseFileLoader(fileName, mHandler));
175 protected void onDestroy() {
177 if (mTextDlg != null) {
182 private void showPageOfText(String text) {
183 // Create an AlertDialog to display the WebView in.
184 AlertDialog.Builder builder = new AlertDialog.Builder(SettingsLicenseActivity.this);
185 builder.setCancelable(true)
187 .setTitle(R.string.settings_license_activity_title);
189 mTextDlg = builder.create();
190 mTextDlg.setOnDismissListener(new OnDismissListener() {
192 public void onDismiss(DialogInterface dlgi) {
193 SettingsLicenseActivity.this.finish();
197 // Begin the loading. This will be done in a separate thread in WebView.
198 mWebView.loadDataWithBaseURL(null, text, "text/html", "utf-8", null);
199 mWebView.setWebViewClient(new WebViewClient() {
201 public void onPageFinished(WebView view, String url) {
202 mSpinnerDlg.dismiss();
212 private void showErrorAndFinish() {
213 mSpinnerDlg.dismiss();
215 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)