2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.LoaderManager;
21 import android.content.ActivityNotFoundException;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.Loader;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.StrictMode;
29 import android.os.SystemProperties;
30 import android.support.annotation.VisibleForTesting;
31 import android.support.v4.content.FileProvider;
32 import android.text.TextUtils;
33 import android.util.Log;
34 import android.widget.Toast;
36 import com.android.settings.users.RestrictedProfileSettings;
39 import java.util.ArrayList;
40 import java.util.List;
43 * The "dialog" that shows from "License" in the Settings app.
45 public class SettingsLicenseActivity extends Activity implements
46 LoaderManager.LoaderCallbacks<File> {
47 private static final String TAG = "SettingsLicenseActivity";
49 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
50 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
52 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
58 final String licenseHtmlPath =
59 SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
60 if (isFilePathValid(licenseHtmlPath)) {
61 showSelectedFile(licenseHtmlPath);
63 showHtmlFromDefaultXmlFiles();
68 public Loader<File> onCreateLoader(int id, Bundle args) {
69 return new LicenseHtmlLoader(this);
73 public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
74 showGeneratedHtmlFile(generatedHtmlFile);
78 public void onLoaderReset(Loader<File> loader) {
81 private void showHtmlFromDefaultXmlFiles() {
82 getLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
86 Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
87 return FileProvider.getUriForFile(this, RestrictedProfileSettings.FILE_PROVIDER_AUTHORITY,
91 private void showGeneratedHtmlFile(File generatedHtmlFile) {
92 if (generatedHtmlFile != null) {
93 showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile));
95 Log.e(TAG, "Failed to generate.");
100 private void showSelectedFile(final String path) {
101 if (TextUtils.isEmpty(path)) {
102 Log.e(TAG, "The system property for the license file is empty");
103 showErrorAndFinish();
107 final File file = new File(path);
108 if (!isFileValid(file)) {
109 Log.e(TAG, "License file " + path + " does not exist");
110 showErrorAndFinish();
113 showHtmlFromUri(Uri.fromFile(file));
116 private void showHtmlFromUri(Uri uri) {
117 // Kick off external viewer due to WebView security restrictions; we
118 // carefully point it at HTMLViewer, since it offers to decompress
120 final Intent intent = new Intent(Intent.ACTION_VIEW);
121 intent.setDataAndType(uri, "text/html");
122 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
123 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
124 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
126 intent.addCategory(Intent.CATEGORY_DEFAULT);
127 intent.setPackage("com.android.htmlviewer");
130 startActivity(intent);
132 } catch (ActivityNotFoundException e) {
133 Log.e(TAG, "Failed to find viewer", e);
134 showErrorAndFinish();
138 private void showErrorAndFinish() {
139 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
144 private boolean isFilePathValid(final String path) {
145 return !TextUtils.isEmpty(path) && isFileValid(new File(path));
149 boolean isFileValid(final File file) {
150 return file.exists() && file.length() != 0;