blob: 99828ce9e4b0052d95a5b19757bd6278414df6ee [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2007 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.os.Bundle;
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +020020import android.os.Handler;
21import android.os.Message;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.os.SystemProperties;
23import android.text.TextUtils;
24import android.util.Config;
25import android.util.Log;
26import android.webkit.WebView;
27import android.webkit.WebViewClient;
28import android.widget.Toast;
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +020029import android.app.Activity;
30import android.app.AlertDialog;
31import android.app.ProgressDialog;
32import android.content.DialogInterface;
33import android.content.DialogInterface.OnDismissListener;
34import android.content.res.Configuration;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035
36import java.io.FileInputStream;
37import java.io.FileNotFoundException;
38import java.io.FileReader;
39import java.io.IOException;
40import java.io.InputStreamReader;
41import java.util.zip.GZIPInputStream;
42
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043/**
44 * The "dialog" that shows from "License" in the Settings app.
45 */
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +020046public class SettingsLicenseActivity extends Activity {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047
48 private static final String TAG = "SettingsLicenseActivity";
49 private static final boolean LOGV = false || Config.LOGV;
50
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";
53
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +020054 private Handler mHandler;
55 private WebView mWebView;
56 private ProgressDialog mSpinnerDlg;
57 private AlertDialog mTextDlg;
58
59 private class LicenseFileLoader implements Runnable {
60
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;
66
67 private String mFileName;
68 private Handler mHandler;
69
70 public LicenseFileLoader(String fileName, Handler handler) {
71 mFileName = fileName;
72 mHandler = handler;
73 }
74
75 public void run() {
76
77 int status = STATUS_OK;
78
79 InputStreamReader inputReader = null;
80 StringBuilder data = new StringBuilder(2048);
81 try {
82 char[] tmp = new char[2048];
83 int numRead;
84 if (mFileName.endsWith(".gz")) {
85 inputReader = new InputStreamReader(
86 new GZIPInputStream(new FileInputStream(mFileName)));
87 } else {
88 inputReader = new FileReader(mFileName);
89 }
90
91 while ((numRead = inputReader.read(tmp)) >= 0) {
92 data.append(tmp, 0, numRead);
93 }
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;
100 } finally {
101 try {
102 if (inputReader != null) {
103 inputReader.close();
104 }
105 } catch (IOException e) {
106 }
107 }
108
109 if ((status == STATUS_OK) && TextUtils.isEmpty(data)) {
110 Log.e(INNER_TAG, "License HTML is empty (from " + mFileName + ")");
111 status = STATUS_EMPTY_FILE;
112 }
113
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();
118 }
119 mHandler.sendMessage(msg);
120 }
121 }
122
123 public SettingsLicenseActivity() {
124 super();
125 mHandler = null;
126 mWebView = null;
127 mSpinnerDlg = null;
128 mTextDlg = null;
129 }
130
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800131 @Override
132 protected void onCreate(Bundle savedInstanceState) {
133 super.onCreate(savedInstanceState);
134
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();
139 return;
140 }
141
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200142 // The activity does not have any view itself,
143 // so set it invisible to avoid displaying the title text in the background.
144 setVisible(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200146 mWebView = new WebView(this);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800147
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200148 mHandler = new Handler() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 @Override
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200151 public void handleMessage(Message msg) {
152 super.handleMessage(msg);
153
154 if (msg.what == LicenseFileLoader.STATUS_OK) {
155 String text = (String) msg.obj;
156 showPageOfText(text);
157 } else {
158 showErrorAndFinish();
159 }
160 }
161 };
162
163 CharSequence title = getText(R.string.settings_license_activity_title);
164 CharSequence msg = getText(R.string.settings_license_activity_loading);
165
166 ProgressDialog pd = ProgressDialog.show(this, title, msg, true, false);
167 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
168 mSpinnerDlg = pd;
169
170 // Start separate thread to do the actual loading.
171 Thread thread = new Thread(new LicenseFileLoader(fileName, mHandler));
172 thread.start();
173 }
174
175 @Override
176 protected void onDestroy() {
177 super.onDestroy();
178 if (mTextDlg != null) {
179 mTextDlg.dismiss();
180 }
181 }
182
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)
187 .setView(mWebView)
188 .setTitle(R.string.settings_license_activity_title);
189
190 mTextDlg = builder.create();
191 mTextDlg.setOnDismissListener(new OnDismissListener() {
192
193 public void onDismiss(DialogInterface dlgi) {
194 SettingsLicenseActivity.this.finish();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195 }
196 });
197
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200198 // 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() {
201 @Override
202 public void onPageFinished(WebView view, String url) {
203 mSpinnerDlg.dismiss();
204 mSpinnerDlg = null;
205 mTextDlg.show();
206 mTextDlg = null;
207 }
208 });
209
210 mWebView = null;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800211 }
212
213 private void showErrorAndFinish() {
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200214 mSpinnerDlg.dismiss();
215 mSpinnerDlg = null;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800216 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
217 .show();
218 finish();
219 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800220}