blob: 5b23a68990af1cf860ea14e8094340eee74a9398 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080019import android.app.Activity;
Jaekyun Seok74812872017-04-18 15:22:01 +090020import android.app.LoaderManager;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080021import android.content.ActivityNotFoundException;
Jaekyun Seok74812872017-04-18 15:22:01 +090022import android.content.ContentResolver;
23import android.content.Context;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080024import android.content.Intent;
Jaekyun Seok74812872017-04-18 15:22:01 +090025import android.content.Loader;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080026import android.net.Uri;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.os.Bundle;
Jeff Sharkeya806f2b2016-01-28 19:03:18 -070028import android.os.StrictMode;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.os.SystemProperties;
Jaekyun Seok74812872017-04-18 15:22:01 +090030import android.support.annotation.VisibleForTesting;
31import android.support.v4.content.FileProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080032import android.text.TextUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034import android.widget.Toast;
35
Jaekyun Seok74812872017-04-18 15:22:01 +090036import com.android.settings.users.RestrictedProfileSettings;
37
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080038import java.io.File;
Jaekyun Seok74812872017-04-18 15:22:01 +090039import java.util.ArrayList;
40import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042/**
43 * The "dialog" that shows from "License" in the Settings app.
44 */
Jaekyun Seok74812872017-04-18 15:22:01 +090045public class SettingsLicenseActivity extends Activity implements
46 LoaderManager.LoaderCallbacks<File> {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047 private static final String TAG = "SettingsLicenseActivity";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048
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";
51
Jaekyun Seok74812872017-04-18 15:22:01 +090052 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
53
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 @Override
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57
Jaekyun Seok74812872017-04-18 15:22:01 +090058 final String licenseHtmlPath =
59 SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
60 if (isFilePathValid(licenseHtmlPath)) {
61 showSelectedFile(licenseHtmlPath);
62 } else {
63 showHtmlFromDefaultXmlFiles();
64 }
65 }
66
67 @Override
68 public Loader<File> onCreateLoader(int id, Bundle args) {
69 return new LicenseHtmlLoader(this);
70 }
71
72 @Override
73 public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
74 showGeneratedHtmlFile(generatedHtmlFile);
75 }
76
77 @Override
78 public void onLoaderReset(Loader<File> loader) {
79 }
80
81 private void showHtmlFromDefaultXmlFiles() {
82 getLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
83 }
84
85 @VisibleForTesting
86 Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
87 return FileProvider.getUriForFile(this, RestrictedProfileSettings.FILE_PROVIDER_AUTHORITY,
88 generatedHtmlFile);
89 }
90
91 private void showGeneratedHtmlFile(File generatedHtmlFile) {
92 if (generatedHtmlFile != null) {
93 showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile));
94 } else {
95 Log.e(TAG, "Failed to generate.");
96 showErrorAndFinish();
97 }
98 }
99
100 private void showSelectedFile(final String path) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800101 if (TextUtils.isEmpty(path)) {
102 Log.e(TAG, "The system property for the license file is empty");
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103 showErrorAndFinish();
104 return;
105 }
106
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800107 final File file = new File(path);
Jaekyun Seok74812872017-04-18 15:22:01 +0900108 if (!isFileValid(file)) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800109 Log.e(TAG, "License file " + path + " does not exist");
110 showErrorAndFinish();
111 return;
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200112 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900113 showHtmlFromUri(Uri.fromFile(file));
114 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800115
Jaekyun Seok74812872017-04-18 15:22:01 +0900116 private void showHtmlFromUri(Uri uri) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800117 // Kick off external viewer due to WebView security restrictions; we
118 // carefully point it at HTMLViewer, since it offers to decompress
119 // before viewing.
120 final Intent intent = new Intent(Intent.ACTION_VIEW);
Jaekyun Seok74812872017-04-18 15:22:01 +0900121 intent.setDataAndType(uri, "text/html");
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800122 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
Jaekyun Seok74812872017-04-18 15:22:01 +0900123 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
124 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
125 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800126 intent.addCategory(Intent.CATEGORY_DEFAULT);
127 intent.setPackage("com.android.htmlviewer");
128
129 try {
130 startActivity(intent);
131 finish();
132 } catch (ActivityNotFoundException e) {
133 Log.e(TAG, "Failed to find viewer", e);
134 showErrorAndFinish();
Amith Yamasanic101d2d2011-11-11 13:25:07 -0800135 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 }
137
138 private void showErrorAndFinish() {
139 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
140 .show();
141 finish();
142 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900143
144 private boolean isFilePathValid(final String path) {
145 return !TextUtils.isEmpty(path) && isFileValid(new File(path));
146 }
147
148 @VisibleForTesting
149 boolean isFileValid(final File file) {
150 return file.exists() && file.length() != 0;
151 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800152}