1 /*
2  * Copyright (C) 2018 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 
17 package com.android.car.settings.system;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.ContentResolver;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.os.SystemProperties;
25 import android.text.TextUtils;
26 import android.widget.Toast;
27 
28 import androidx.core.content.FileProvider;
29 import androidx.fragment.app.FragmentActivity;
30 import androidx.loader.app.LoaderManager;
31 import androidx.loader.content.Loader;
32 
33 import com.android.car.settings.R;
34 import com.android.car.settings.common.Logger;
35 import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
36 
37 import java.io.File;
38 
39 /**
40  * The activity that displays third-party licenses.
41  */
42 public class ThirdPartyLicensesActivity extends FragmentActivity implements
43         LoaderManager.LoaderCallbacks<File> {
44     private static final Logger LOG = new Logger(ThirdPartyLicensesActivity.class);
45     private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
46     private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
47     private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
48     private static final String FILE_PROVIDER_AUTHORITY = "com.android.settings.files";
49     private static final String HTML_VIEWER_PACKAGE = "com.android.htmlviewer";
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
55 
56         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
onCreateLoader(int id, Bundle args)66     public Loader<File> onCreateLoader(int id, Bundle args) {
67         return new LicenseHtmlLoader(this);
68     }
69 
70     @Override
onLoadFinished(Loader<File> loader, File generatedHtmlFile)71     public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
72         showGeneratedHtmlFile(generatedHtmlFile);
73     }
74 
75     @Override
onLoaderReset(Loader<File> loader)76     public void onLoaderReset(Loader<File> loader) {
77     }
78 
showSelectedFile(final String path)79     private void showSelectedFile(final String path) {
80         if (TextUtils.isEmpty(path)) {
81             LOG.e("The system property for the license file is empty");
82             showErrorAndFinish();
83             return;
84         }
85 
86         final File file = new File(path);
87         if (!isFileValid(file)) {
88             LOG.e("License file " + path + " does not exist");
89             showErrorAndFinish();
90             return;
91         }
92         showHtmlFromUri(Uri.fromFile(file));
93     }
94 
showErrorAndFinish()95     private void showErrorAndFinish() {
96         Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
97                 .show();
98         finish();
99     }
100 
showHtmlFromUri(Uri uri)101     private void showHtmlFromUri(Uri uri) {
102         // Kick off external viewer due to WebView security restrictions; we
103         // carefully point it at HTMLViewer, since it offers to decompress
104         // before viewing.
105         final Intent intent = new Intent(Intent.ACTION_VIEW);
106         intent.setDataAndType(uri, "text/html");
107         intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
108         if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
109             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
110         }
111         intent.addCategory(Intent.CATEGORY_DEFAULT);
112         intent.setPackage(HTML_VIEWER_PACKAGE);
113 
114         try {
115             startActivity(intent);
116             finish();
117         } catch (ActivityNotFoundException e) {
118             LOG.e("Failed to find viewer", e);
119             showErrorAndFinish();
120         }
121     }
122 
showHtmlFromDefaultXmlFiles()123     private void showHtmlFromDefaultXmlFiles() {
124         LoaderManager.getInstance(this).initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY,
125                 this);
126     }
127 
showGeneratedHtmlFile(File generatedHtmlFile)128     private void showGeneratedHtmlFile(File generatedHtmlFile) {
129         if (generatedHtmlFile != null) {
130             LOG.i("File size: " + generatedHtmlFile.length());
131             showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile));
132         } else {
133             LOG.e("Failed to generate.");
134             showErrorAndFinish();
135         }
136     }
137 
getUriFromGeneratedHtmlFile(File generatedHtmlFile)138     private Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
139         return FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, generatedHtmlFile);
140     }
141 
isFilePathValid(final String path)142     private boolean isFilePathValid(final String path) {
143         return !TextUtils.isEmpty(path) && isFileValid(new File(path));
144     }
145 
isFileValid(final File file)146     private boolean isFileValid(final File file) {
147         return file.exists() && file.length() != 0;
148     }
149 }
150