blob: 57b1f5c6cbbbf7295cef8f0bec9d330888988f92 [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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.widget.Toast;
26
Fan Zhangc7162cd2018-06-18 15:21:41 -070027import androidx.annotation.VisibleForTesting;
28import androidx.core.content.FileProvider;
tmfang27c84de2018-06-28 11:39:05 +080029import androidx.fragment.app.FragmentActivity;
tmfang99cc23d2018-06-26 19:01:57 +080030import androidx.loader.app.LoaderManager;
31import androidx.loader.content.Loader;
Fan Zhangc7162cd2018-06-18 15:21:41 -070032
Fan Zhang23f8d592018-08-28 15:11:40 -070033import com.android.settingslib.license.LicenseHtmlLoaderCompat;
34
35import java.io.File;
36
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037/**
38 * The "dialog" that shows from "License" in the Settings app.
39 */
tmfang27c84de2018-06-28 11:39:05 +080040public class SettingsLicenseActivity extends FragmentActivity implements
Jaekyun Seok74812872017-04-18 15:22:01 +090041 LoaderManager.LoaderCallbacks<File> {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042 private static final String TAG = "SettingsLicenseActivity";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
Inseob Kim4d56f112018-12-19 12:05:22 +090044 private static final String LICENSE_PATH = "/system/etc/NOTICE.html.gz";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045
Jaekyun Seok74812872017-04-18 15:22:01 +090046 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
47
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048 @Override
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51
Inseob Kim4d56f112018-12-19 12:05:22 +090052 File file = new File(LICENSE_PATH);
53 if (isFileValid(file)) {
54 showHtmlFromUri(Uri.fromFile(file));
Jaekyun Seok74812872017-04-18 15:22:01 +090055 } else {
56 showHtmlFromDefaultXmlFiles();
57 }
58 }
59
60 @Override
61 public Loader<File> onCreateLoader(int id, Bundle args) {
tmfang27c84de2018-06-28 11:39:05 +080062 return new LicenseHtmlLoaderCompat(this);
Jaekyun Seok74812872017-04-18 15:22:01 +090063 }
64
65 @Override
66 public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
67 showGeneratedHtmlFile(generatedHtmlFile);
68 }
69
70 @Override
71 public void onLoaderReset(Loader<File> loader) {
72 }
73
74 private void showHtmlFromDefaultXmlFiles() {
tmfang27c84de2018-06-28 11:39:05 +080075 getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
Jaekyun Seok74812872017-04-18 15:22:01 +090076 }
77
78 @VisibleForTesting
79 Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
Andras Kloczl106431e2020-05-18 14:53:07 +010080 return FileProvider.getUriForFile(this, Utils.FILE_PROVIDER_AUTHORITY,
Jaekyun Seok74812872017-04-18 15:22:01 +090081 generatedHtmlFile);
82 }
83
84 private void showGeneratedHtmlFile(File generatedHtmlFile) {
85 if (generatedHtmlFile != null) {
86 showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile));
87 } else {
88 Log.e(TAG, "Failed to generate.");
89 showErrorAndFinish();
90 }
91 }
92
Jaekyun Seok47f6e542017-11-30 18:10:34 +090093 private void showHtmlFromUri(Uri uri) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080094 // Kick off external viewer due to WebView security restrictions; we
95 // carefully point it at HTMLViewer, since it offers to decompress
96 // before viewing.
97 final Intent intent = new Intent(Intent.ACTION_VIEW);
Jaekyun Seok74812872017-04-18 15:22:01 +090098 intent.setDataAndType(uri, "text/html");
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080099 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
Jaekyun Seok74812872017-04-18 15:22:01 +0900100 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
101 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
102 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800103 intent.addCategory(Intent.CATEGORY_DEFAULT);
104 intent.setPackage("com.android.htmlviewer");
105
106 try {
107 startActivity(intent);
108 finish();
109 } catch (ActivityNotFoundException e) {
110 Log.e(TAG, "Failed to find viewer", e);
111 showErrorAndFinish();
Amith Yamasanic101d2d2011-11-11 13:25:07 -0800112 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 }
114
115 private void showErrorAndFinish() {
116 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
117 .show();
118 finish();
119 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900120
Jaekyun Seok74812872017-04-18 15:22:01 +0900121 @VisibleForTesting
122 boolean isFileValid(final File file) {
123 return file.exists() && file.length() != 0;
124 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125}