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.SystemProperties; 21 import android.text.TextUtils; 22 import android.util.Config; 23 import android.util.Log; 24 import android.webkit.WebView; 25 import android.webkit.WebViewClient; 26 import android.widget.Toast; 27 28 import java.io.FileInputStream; 29 import java.io.FileNotFoundException; 30 import java.io.FileReader; 31 import java.io.IOException; 32 import java.io.InputStreamReader; 33 import java.util.zip.GZIPInputStream; 34 35 import com.android.internal.app.AlertActivity; 36 import com.android.internal.app.AlertController; 37 38 /** 39 * The "dialog" that shows from "License" in the Settings app. 40 */ 41 public class SettingsLicenseActivity extends AlertActivity { 42 43 private static final String TAG = "SettingsLicenseActivity"; 44 private static final boolean LOGV = false || Config.LOGV; 45 46 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz"; 47 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path"; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 53 String fileName = SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH); 54 if (TextUtils.isEmpty(fileName)) { 55 Log.e(TAG, "The system property for the license file is empty."); 56 showErrorAndFinish(); 57 return; 58 } 59 60 InputStreamReader inputReader = null; 61 StringBuilder data = null; 62 try { 63 data = new StringBuilder(2048); 64 char tmp[] = new char[2048]; 65 int numRead; 66 if (fileName.endsWith(".gz")) { 67 inputReader = new InputStreamReader( 68 new GZIPInputStream(new FileInputStream(fileName))); 69 } else { 70 inputReader = new FileReader(fileName); 71 } 72 while ((numRead = inputReader.read(tmp)) >= 0) { 73 data.append(tmp, 0, numRead); 74 } 75 } catch (FileNotFoundException e) { 76 Log.e(TAG, "License HTML file not found at " + fileName, e); 77 showErrorAndFinish(); 78 return; 79 } catch (IOException e) { 80 Log.e(TAG, "Error reading license HTML file at " + fileName, e); 81 showErrorAndFinish(); 82 return; 83 } finally { 84 try { 85 if (inputReader != null) { 86 inputReader.close(); 87 } 88 } catch (IOException e) { 89 } 90 } 91 92 if (TextUtils.isEmpty(data)) { 93 Log.e(TAG, "License HTML is empty (from " + fileName + ")"); 94 showErrorAndFinish(); 95 return; 96 } 97 98 WebView webView = new WebView(this); 99 100 // Begin the loading. This will be done in a separate thread in WebView. 101 webView.loadDataWithBaseURL(null, data.toString(), "text/html", "utf-8", null); 102 webView.setWebViewClient(new WebViewClient() { 103 @Override 104 public void onPageFinished(WebView view, String url) { 105 // Change from 'Loading...' to the real title 106 mAlert.setTitle(getString(R.string.settings_license_activity_title)); 107 } 108 }); 109 110 final AlertController.AlertParams p = mAlertParams; 111 p.mTitle = getString(R.string.settings_license_activity_loading); 112 p.mView = webView; 113 p.mForceInverseBackground = true; 114 setupAlert(); 115 } 116 117 private void showErrorAndFinish() { 118 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) 119 .show(); 120 finish(); 121 } 122 123 } 124