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.Config;
25 import android.util.Log;
26 import android.webkit.WebView;
27 import android.webkit.WebViewClient;
28 import android.widget.Toast;
29 import android.app.Activity;
30 import android.app.AlertDialog;
31 import android.app.ProgressDialog;
32 import android.content.DialogInterface;
33 import android.content.DialogInterface.OnDismissListener;
34 import android.content.res.Configuration;
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.FileReader;
39 import java.io.IOException;
40 import java.io.InputStreamReader;
41 import java.util.zip.GZIPInputStream;
44 * The "dialog" that shows from "License" in the Settings app.
46 public class SettingsLicenseActivity extends Activity {
48 private static final String TAG = "SettingsLicenseActivity";
49 private static final boolean LOGV = false || Config.LOGV;
51 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
52 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
54 private Handler mHandler;
55 private WebView mWebView;
56 private ProgressDialog mSpinnerDlg;
57 private AlertDialog mTextDlg;
59 private class LicenseFileLoader implements Runnable {
61 private static final String INNER_TAG = "SettingsLicenseActivity.LicenseFileLoader";
62 public static final int STATUS_OK = 0;
63 public static final int STATUS_NOT_FOUND = 1;
64 public static final int STATUS_READ_ERROR = 2;
65 public static final int STATUS_EMPTY_FILE = 3;
67 private String mFileName;
68 private Handler mHandler;
70 public LicenseFileLoader(String fileName, Handler handler) {
77 int status = STATUS_OK;
79 InputStreamReader inputReader = null;
80 StringBuilder data = new StringBuilder(2048);
82 char[] tmp = new char[2048];
84 if (mFileName.endsWith(".gz")) {
85 inputReader = new InputStreamReader(
86 new GZIPInputStream(new FileInputStream(mFileName)));
88 inputReader = new FileReader(mFileName);
91 while ((numRead = inputReader.read(tmp)) >= 0) {
92 data.append(tmp, 0, numRead);
94 } catch (FileNotFoundException e) {
95 Log.e(INNER_TAG, "License HTML file not found at " + mFileName, e);
96 status = STATUS_NOT_FOUND;
97 } catch (IOException e) {
98 Log.e(INNER_TAG, "Error reading license HTML file at " + mFileName, e);
99 status = STATUS_READ_ERROR;
102 if (inputReader != null) {
105 } catch (IOException e) {
109 if ((status == STATUS_OK) && TextUtils.isEmpty(data)) {
110 Log.e(INNER_TAG, "License HTML is empty (from " + mFileName + ")");
111 status = STATUS_EMPTY_FILE;
114 // Tell the UI thread that we are finished.
115 Message msg = mHandler.obtainMessage(status, null);
116 if (status == STATUS_OK) {
117 msg.obj = data.toString();
119 mHandler.sendMessage(msg);
123 public SettingsLicenseActivity() {
132 protected void onCreate(Bundle savedInstanceState) {
133 super.onCreate(savedInstanceState);
135 String fileName = SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
136 if (TextUtils.isEmpty(fileName)) {
137 Log.e(TAG, "The system property for the license file is empty.");
138 showErrorAndFinish();
142 // The activity does not have any view itself,
143 // so set it invisible to avoid displaying the title text in the background.
146 mWebView = new WebView(this);
148 mHandler = new Handler() {
151 public void handleMessage(Message msg) {
152 super.handleMessage(msg);
154 if (msg.what == LicenseFileLoader.STATUS_OK) {
155 String text = (String) msg.obj;
156 showPageOfText(text);
158 showErrorAndFinish();
163 CharSequence title = getText(R.string.settings_license_activity_title);
164 CharSequence msg = getText(R.string.settings_license_activity_loading);
166 ProgressDialog pd = ProgressDialog.show(this, title, msg, true, false);
167 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
170 // Start separate thread to do the actual loading.
171 Thread thread = new Thread(new LicenseFileLoader(fileName, mHandler));
176 protected void onDestroy() {
178 if (mTextDlg != null) {
183 private void showPageOfText(String text) {
184 // Create an AlertDialog to display the WebView in.
185 AlertDialog.Builder builder = new AlertDialog.Builder(SettingsLicenseActivity.this);
186 builder.setCancelable(true)
188 .setTitle(R.string.settings_license_activity_title);
190 mTextDlg = builder.create();
191 mTextDlg.setOnDismissListener(new OnDismissListener() {
193 public void onDismiss(DialogInterface dlgi) {
194 SettingsLicenseActivity.this.finish();
198 // Begin the loading. This will be done in a separate thread in WebView.
199 mWebView.loadDataWithBaseURL(null, text, "text/html", "utf-8", null);
200 mWebView.setWebViewClient(new WebViewClient() {
202 public void onPageFinished(WebView view, String url) {
203 mSpinnerDlg.dismiss();
213 private void showErrorAndFinish() {
214 mSpinnerDlg.dismiss();
216 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)