The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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; |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 22 | import android.os.SystemProperties; |
| 23 | import android.text.TextUtils; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.webkit.WebView; |
| 26 | import android.webkit.WebViewClient; |
| 27 | import android.widget.Toast; |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 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; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | |
| 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; |
| 41 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 42 | /** |
| 43 | * The "dialog" that shows from "License" in the Settings app. |
| 44 | */ |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 45 | public class SettingsLicenseActivity extends Activity { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 46 | |
| 47 | private static final String TAG = "SettingsLicenseActivity"; |
Joe Onorato | cedfdd9 | 2011-04-07 18:41:13 -0700 | [diff] [blame] | 48 | private static final boolean LOGV = false || false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 49 | |
| 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"; |
| 52 | |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 53 | private Handler mHandler; |
| 54 | private WebView mWebView; |
| 55 | private ProgressDialog mSpinnerDlg; |
| 56 | private AlertDialog mTextDlg; |
| 57 | |
| 58 | private class LicenseFileLoader implements Runnable { |
| 59 | |
| 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; |
| 65 | |
| 66 | private String mFileName; |
| 67 | private Handler mHandler; |
| 68 | |
| 69 | public LicenseFileLoader(String fileName, Handler handler) { |
| 70 | mFileName = fileName; |
| 71 | mHandler = handler; |
| 72 | } |
| 73 | |
| 74 | public void run() { |
| 75 | |
| 76 | int status = STATUS_OK; |
| 77 | |
| 78 | InputStreamReader inputReader = null; |
| 79 | StringBuilder data = new StringBuilder(2048); |
| 80 | try { |
| 81 | char[] tmp = new char[2048]; |
| 82 | int numRead; |
| 83 | if (mFileName.endsWith(".gz")) { |
| 84 | inputReader = new InputStreamReader( |
| 85 | new GZIPInputStream(new FileInputStream(mFileName))); |
| 86 | } else { |
| 87 | inputReader = new FileReader(mFileName); |
| 88 | } |
| 89 | |
| 90 | while ((numRead = inputReader.read(tmp)) >= 0) { |
| 91 | data.append(tmp, 0, numRead); |
| 92 | } |
| 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; |
| 99 | } finally { |
| 100 | try { |
| 101 | if (inputReader != null) { |
| 102 | inputReader.close(); |
| 103 | } |
| 104 | } catch (IOException e) { |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if ((status == STATUS_OK) && TextUtils.isEmpty(data)) { |
| 109 | Log.e(INNER_TAG, "License HTML is empty (from " + mFileName + ")"); |
| 110 | status = STATUS_EMPTY_FILE; |
| 111 | } |
| 112 | |
| 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(); |
| 117 | } |
| 118 | mHandler.sendMessage(msg); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public SettingsLicenseActivity() { |
| 123 | super(); |
| 124 | mHandler = null; |
| 125 | mWebView = null; |
| 126 | mSpinnerDlg = null; |
| 127 | mTextDlg = null; |
| 128 | } |
| 129 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 130 | @Override |
| 131 | protected void onCreate(Bundle savedInstanceState) { |
| 132 | super.onCreate(savedInstanceState); |
| 133 | |
| 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(); |
| 138 | return; |
| 139 | } |
| 140 | |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 141 | // The activity does not have any view itself, |
| 142 | // so set it invisible to avoid displaying the title text in the background. |
| 143 | setVisible(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 144 | |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 145 | mWebView = new WebView(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 146 | |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 147 | mHandler = new Handler() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 148 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 149 | @Override |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 150 | public void handleMessage(Message msg) { |
| 151 | super.handleMessage(msg); |
| 152 | |
| 153 | if (msg.what == LicenseFileLoader.STATUS_OK) { |
| 154 | String text = (String) msg.obj; |
| 155 | showPageOfText(text); |
| 156 | } else { |
| 157 | showErrorAndFinish(); |
| 158 | } |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | CharSequence title = getText(R.string.settings_license_activity_title); |
| 163 | CharSequence msg = getText(R.string.settings_license_activity_loading); |
| 164 | |
| 165 | ProgressDialog pd = ProgressDialog.show(this, title, msg, true, false); |
| 166 | pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); |
| 167 | mSpinnerDlg = pd; |
| 168 | |
| 169 | // Start separate thread to do the actual loading. |
| 170 | Thread thread = new Thread(new LicenseFileLoader(fileName, mHandler)); |
| 171 | thread.start(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | protected void onDestroy() { |
Amith Yamasani | c101d2d | 2011-11-11 13:25:07 -0800 | [diff] [blame^] | 176 | if (mTextDlg != null && mTextDlg.isShowing()) { |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 177 | mTextDlg.dismiss(); |
| 178 | } |
Amith Yamasani | c101d2d | 2011-11-11 13:25:07 -0800 | [diff] [blame^] | 179 | if (mSpinnerDlg != null && mSpinnerDlg.isShowing()) { |
| 180 | mSpinnerDlg.dismiss(); |
| 181 | } |
| 182 | super.onDestroy(); |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private void showPageOfText(String text) { |
| 186 | // Create an AlertDialog to display the WebView in. |
| 187 | AlertDialog.Builder builder = new AlertDialog.Builder(SettingsLicenseActivity.this); |
| 188 | builder.setCancelable(true) |
| 189 | .setView(mWebView) |
| 190 | .setTitle(R.string.settings_license_activity_title); |
| 191 | |
| 192 | mTextDlg = builder.create(); |
| 193 | mTextDlg.setOnDismissListener(new OnDismissListener() { |
| 194 | |
| 195 | public void onDismiss(DialogInterface dlgi) { |
| 196 | SettingsLicenseActivity.this.finish(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 197 | } |
| 198 | }); |
| 199 | |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 200 | // Begin the loading. This will be done in a separate thread in WebView. |
| 201 | mWebView.loadDataWithBaseURL(null, text, "text/html", "utf-8", null); |
| 202 | mWebView.setWebViewClient(new WebViewClient() { |
| 203 | @Override |
| 204 | public void onPageFinished(WebView view, String url) { |
| 205 | mSpinnerDlg.dismiss(); |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 206 | mTextDlg.show(); |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 207 | } |
| 208 | }); |
| 209 | |
| 210 | mWebView = null; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private void showErrorAndFinish() { |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 214 | mSpinnerDlg.dismiss(); |
| 215 | mSpinnerDlg = null; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 216 | Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) |
| 217 | .show(); |
| 218 | finish(); |
| 219 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 220 | } |