blob: 0b809e19a9d7edf27967575a210119b613a3a1b5 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
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.SystemProperties;
21import android.text.TextUtils;
22import android.util.Config;
23import android.util.Log;
24import android.webkit.WebView;
25import android.webkit.WebViewClient;
26import android.widget.Toast;
27
The Android Open Source Project1152aff2009-01-15 16:12:13 -080028import java.io.FileInputStream;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070029import java.io.FileNotFoundException;
30import java.io.FileReader;
31import java.io.IOException;
The Android Open Source Project1152aff2009-01-15 16:12:13 -080032import java.io.InputStreamReader;
33import java.util.zip.GZIPInputStream;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070034
35import com.android.internal.app.AlertActivity;
36import com.android.internal.app.AlertController;
37
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070038/**
39 * The "dialog" that shows from "License" in the Settings app.
40 */
41public class SettingsLicenseActivity extends AlertActivity {
42
43 private static final String TAG = "SettingsLicenseActivity";
44 private static final boolean LOGV = false || Config.LOGV;
The Android Open Source Project1152aff2009-01-15 16:12:13 -080045
46 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070047 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
The Android Open Source Project1152aff2009-01-15 16:12:13 -080048
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070049 @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
The Android Open Source Project1152aff2009-01-15 16:12:13 -080060 InputStreamReader inputReader = null;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070061 StringBuilder data = null;
62 try {
The Android Open Source Project1152aff2009-01-15 16:12:13 -080063 data = new StringBuilder(2048);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070064 char tmp[] = new char[2048];
65 int numRead;
The Android Open Source Project1152aff2009-01-15 16:12:13 -080066 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) {
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070073 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 {
The Android Open Source Project1152aff2009-01-15 16:12:13 -080085 if (inputReader != null) {
86 inputReader.close();
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070087 }
88 } catch (IOException e) {
89 }
90 }
The Android Open Source Project1152aff2009-01-15 16:12:13 -080091
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070092 if (TextUtils.isEmpty(data)) {
93 Log.e(TAG, "License HTML is empty (from " + fileName + ")");
94 showErrorAndFinish();
95 return;
96 }
The Android Open Source Project1152aff2009-01-15 16:12:13 -080097
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070098 WebView webView = new WebView(this);
99
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700100 // Begin the loading. This will be done in a separate thread in WebView.
The Android Open Source Projectabc48f82008-12-17 18:06:01 -0800101 webView.loadDataWithBaseURL(null, data.toString(), "text/html", "utf-8", null);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700102 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 });
The Android Open Source Project1152aff2009-01-15 16:12:13 -0800109
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700110 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 }
The Android Open Source Project1152aff2009-01-15 16:12:13 -0800122
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700123}