OSDN Git Service

am 533b7b53: am 90599cdf: Merge "Used space visible in SD Card bar graph" into honeyc...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / SettingsLicenseActivity.java
1 /*
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
17 package com.android.settings;
18
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;
35
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;
42
43 /**
44  * The "dialog" that shows from "License" in the Settings app.
45  */
46 public class SettingsLicenseActivity extends Activity {
47
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
54     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
131     @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
142         // 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);
145
146         mWebView = new WebView(this);
147
148         mHandler = new Handler() {
149
150             @Override
151             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();
195             }
196         });
197
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() {
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;
211     }
212
213     private void showErrorAndFinish() {
214         mSpinnerDlg.dismiss();
215         mSpinnerDlg = null;
216         Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
217                 .show();
218         finish();
219     }
220 }