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