blob: 2581ed913472173a0b850b2ae49be36802079ef1 [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.content.ActivityNotFoundException;
Jaekyun Seok74812872017-04-18 15:22:01 +090020import android.content.ContentResolver;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080021import android.content.Intent;
22import android.net.Uri;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.os.Bundle;
24import android.os.SystemProperties;
25import android.text.TextUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.widget.Toast;
28
Fan Zhangc7162cd2018-06-18 15:21:41 -070029import androidx.annotation.VisibleForTesting;
30import androidx.core.content.FileProvider;
tmfang27c84de2018-06-28 11:39:05 +080031import androidx.fragment.app.FragmentActivity;
tmfang99cc23d2018-06-26 19:01:57 +080032import androidx.loader.app.LoaderManager;
33import androidx.loader.content.Loader;
Fan Zhangc7162cd2018-06-18 15:21:41 -070034
Fan Zhang23f8d592018-08-28 15:11:40 -070035import com.android.settings.users.RestrictedProfileSettings;
36import com.android.settingslib.license.LicenseHtmlLoaderCompat;
37
38import java.io.File;
39
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040/**
41 * The "dialog" that shows from "License" in the Settings app.
42 */
tmfang27c84de2018-06-28 11:39:05 +080043public class SettingsLicenseActivity extends FragmentActivity implements
Jaekyun Seok74812872017-04-18 15:22:01 +090044 LoaderManager.LoaderCallbacks<File> {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045 private static final String TAG = "SettingsLicenseActivity";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080046
47 private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
48 private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
49
Jaekyun Seok74812872017-04-18 15:22:01 +090050 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
51
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052 @Override
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55
Jaekyun Seok74812872017-04-18 15:22:01 +090056 final String licenseHtmlPath =
57 SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
58 if (isFilePathValid(licenseHtmlPath)) {
59 showSelectedFile(licenseHtmlPath);
60 } else {
61 showHtmlFromDefaultXmlFiles();
62 }
63 }
64
65 @Override
66 public Loader<File> onCreateLoader(int id, Bundle args) {
tmfang27c84de2018-06-28 11:39:05 +080067 return new LicenseHtmlLoaderCompat(this);
Jaekyun Seok74812872017-04-18 15:22:01 +090068 }
69
70 @Override
71 public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
72 showGeneratedHtmlFile(generatedHtmlFile);
73 }
74
75 @Override
76 public void onLoaderReset(Loader<File> loader) {
77 }
78
79 private void showHtmlFromDefaultXmlFiles() {
tmfang27c84de2018-06-28 11:39:05 +080080 getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
Jaekyun Seok74812872017-04-18 15:22:01 +090081 }
82
83 @VisibleForTesting
84 Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
85 return FileProvider.getUriForFile(this, RestrictedProfileSettings.FILE_PROVIDER_AUTHORITY,
86 generatedHtmlFile);
87 }
88
89 private void showGeneratedHtmlFile(File generatedHtmlFile) {
90 if (generatedHtmlFile != null) {
91 showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile));
92 } else {
93 Log.e(TAG, "Failed to generate.");
94 showErrorAndFinish();
95 }
96 }
97
98 private void showSelectedFile(final String path) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080099 if (TextUtils.isEmpty(path)) {
100 Log.e(TAG, "The system property for the license file is empty");
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 showErrorAndFinish();
102 return;
103 }
104
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800105 final File file = new File(path);
Jaekyun Seok74812872017-04-18 15:22:01 +0900106 if (!isFileValid(file)) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800107 Log.e(TAG, "License file " + path + " does not exist");
108 showErrorAndFinish();
109 return;
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200110 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900111 showHtmlFromUri(Uri.fromFile(file));
Jaekyun Seok47f6e542017-11-30 18:10:34 +0900112 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800113
Jaekyun Seok47f6e542017-11-30 18:10:34 +0900114 private void showHtmlFromUri(Uri uri) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800115 // Kick off external viewer due to WebView security restrictions; we
116 // carefully point it at HTMLViewer, since it offers to decompress
117 // before viewing.
118 final Intent intent = new Intent(Intent.ACTION_VIEW);
Jaekyun Seok74812872017-04-18 15:22:01 +0900119 intent.setDataAndType(uri, "text/html");
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800120 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
Jaekyun Seok74812872017-04-18 15:22:01 +0900121 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
122 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
123 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800124 intent.addCategory(Intent.CATEGORY_DEFAULT);
125 intent.setPackage("com.android.htmlviewer");
126
127 try {
128 startActivity(intent);
129 finish();
130 } catch (ActivityNotFoundException e) {
131 Log.e(TAG, "Failed to find viewer", e);
132 showErrorAndFinish();
Amith Yamasanic101d2d2011-11-11 13:25:07 -0800133 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 }
135
136 private void showErrorAndFinish() {
137 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
138 .show();
139 finish();
140 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900141
142 private boolean isFilePathValid(final String path) {
143 return !TextUtils.isEmpty(path) && isFileValid(new File(path));
144 }
145
146 @VisibleForTesting
147 boolean isFileValid(final File file) {
148 return file.exists() && file.length() != 0;
149 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150}