The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [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; |
| 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.FileNotFoundException; |
| 29 | import java.io.FileReader; |
| 30 | import java.io.IOException; |
| 31 | import java.io.UnsupportedEncodingException; |
| 32 | |
| 33 | import com.android.internal.app.AlertActivity; |
| 34 | import com.android.internal.app.AlertController; |
| 35 | |
| 36 | import org.apache.commons.codec.binary.Base64; |
| 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"; |
| 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 | FileReader fileReader = null; |
| 61 | StringBuilder data = null; |
| 62 | try { |
| 63 | data = new StringBuilder(2048); |
| 64 | char tmp[] = new char[2048]; |
| 65 | int numRead; |
| 66 | fileReader = new FileReader(fileName); |
| 67 | while ((numRead = fileReader.read(tmp)) >= 0) { |
| 68 | data.append(tmp, 0, numRead); |
| 69 | } |
| 70 | } catch (FileNotFoundException e) { |
| 71 | Log.e(TAG, "License HTML file not found at " + fileName, e); |
| 72 | showErrorAndFinish(); |
| 73 | return; |
| 74 | } catch (IOException e) { |
| 75 | Log.e(TAG, "Error reading license HTML file at " + fileName, e); |
| 76 | showErrorAndFinish(); |
| 77 | return; |
| 78 | } finally { |
| 79 | try { |
| 80 | if (fileReader != null) { |
| 81 | fileReader.close(); |
| 82 | } |
| 83 | } catch (IOException e) { |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (TextUtils.isEmpty(data)) { |
| 88 | Log.e(TAG, "License HTML is empty (from " + fileName + ")"); |
| 89 | showErrorAndFinish(); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | WebView webView = new WebView(this); |
| 94 | |
| 95 | if (LOGV) Log.v(TAG, "Started encode at " + System.currentTimeMillis()); |
| 96 | // Need to encode to base64 for WebView to load the contents properly |
| 97 | String dataStr; |
| 98 | try { |
| 99 | byte[] base64Bytes = Base64.encodeBase64(data.toString().getBytes("ISO8859_1")); |
| 100 | dataStr = new String(base64Bytes); |
| 101 | } catch (UnsupportedEncodingException e) { |
| 102 | Log.e(TAG, "Could not convert to base64", e); |
| 103 | showErrorAndFinish(); |
| 104 | return; |
| 105 | } |
| 106 | if (LOGV) Log.v(TAG, "Ended encode at " + System.currentTimeMillis()); |
| 107 | if (LOGV) { |
| 108 | Log.v(TAG, "Started test decode at " + System.currentTimeMillis()); |
| 109 | Base64.decodeBase64(dataStr.getBytes()); |
| 110 | Log.v(TAG, "Ended decode at " + System.currentTimeMillis()); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | // Begin the loading. This will be done in a separate thread in WebView. |
| 115 | webView.loadData(dataStr, "text/html", "base64"); |
| 116 | webView.setWebViewClient(new WebViewClient() { |
| 117 | @Override |
| 118 | public void onPageFinished(WebView view, String url) { |
| 119 | // Change from 'Loading...' to the real title |
| 120 | mAlert.setTitle(getString(R.string.settings_license_activity_title)); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | final AlertController.AlertParams p = mAlertParams; |
| 125 | p.mTitle = getString(R.string.settings_license_activity_loading); |
| 126 | p.mView = webView; |
| 127 | p.mForceInverseBackground = true; |
| 128 | setupAlert(); |
| 129 | } |
| 130 | |
| 131 | private void showErrorAndFinish() { |
| 132 | Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) |
| 133 | .show(); |
| 134 | finish(); |
| 135 | } |
| 136 | |
| 137 | } |