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 17package com.android.settings; 18 19import android.os.Bundle; 20import android.os.Handler; 21import android.os.Message; 22import android.os.SystemProperties; 23import android.text.TextUtils; 24import android.util.Log; 25import android.webkit.WebView; 26import android.webkit.WebViewClient; 27import android.widget.Toast; 28import android.app.Activity; 29import android.app.AlertDialog; 30import android.app.ProgressDialog; 31import android.content.DialogInterface; 32import android.content.DialogInterface.OnDismissListener; 33import android.content.res.Configuration; 34 35import java.io.FileInputStream; 36import java.io.FileNotFoundException; 37import java.io.FileReader; 38import java.io.IOException; 39import java.io.InputStreamReader; 40import java.util.zip.GZIPInputStream; 41 42/** 43 * The "dialog" that shows from "License" in the Settings app. 44 */ 45public class SettingsLicenseActivity extends Activity { 46 47 private static final String TAG = "SettingsLicenseActivity"; 48 private static final boolean LOGV = false || false; 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 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 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 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); 144 145 mWebView = new WebView(this); 146 147 mHandler = new Handler() { 148 149 @Override 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() { 176 if (mTextDlg != null && mTextDlg.isShowing()) { 177 mTextDlg.dismiss(); 178 } 179 if (mSpinnerDlg != null && mSpinnerDlg.isShowing()) { 180 mSpinnerDlg.dismiss(); 181 } 182 super.onDestroy(); 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(); 197 } 198 }); 199 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(); 206 if (SettingsLicenseActivity.this.isResumed()) { 207 mTextDlg.show(); 208 } 209 } 210 }); 211 212 mWebView = null; 213 } 214 215 private void showErrorAndFinish() { 216 mSpinnerDlg.dismiss(); 217 mSpinnerDlg = null; 218 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) 219 .show(); 220 finish(); 221 } 222} 223