• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings;
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.util.Log;
25 import android.widget.Toast;
26 
27 import androidx.annotation.VisibleForTesting;
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.settingslib.license.LicenseHtmlLoaderCompat;
34 
35 import java.io.File;
36 
37 /**
38  * The "dialog" that shows from "License" in the Settings app.
39  */
40 public class SettingsLicenseActivity extends FragmentActivity implements
41             LoaderManager.LoaderCallbacks<File> {
42     private static final String TAG = "SettingsLicenseActivity";
43 
44     private static final String LICENSE_PATH = "/system/etc/NOTICE.html.gz";
45 
46     private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51 
52         File file = new File(LICENSE_PATH);
53         if (isFileValid(file)) {
54             showHtmlFromUri(Uri.fromFile(file));
55         } else {
56             showHtmlFromDefaultXmlFiles();
57         }
58     }
59 
60     @Override
onCreateLoader(int id, Bundle args)61     public Loader<File> onCreateLoader(int id, Bundle args) {
62         return new LicenseHtmlLoaderCompat(this);
63     }
64 
65     @Override
onLoadFinished(Loader<File> loader, File generatedHtmlFile)66     public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
67         showGeneratedHtmlFile(generatedHtmlFile);
68     }
69 
70     @Override
onLoaderReset(Loader<File> loader)71     public void onLoaderReset(Loader<File> loader) {
72     }
73 
showHtmlFromDefaultXmlFiles()74     private void showHtmlFromDefaultXmlFiles() {
75         getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
76     }
77 
78     @VisibleForTesting
getUriFromGeneratedHtmlFile(File generatedHtmlFile)79     Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
80         return FileProvider.getUriForFile(this, Utils.FILE_PROVIDER_AUTHORITY,
81                 generatedHtmlFile);
82     }
83 
showGeneratedHtmlFile(File generatedHtmlFile)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 
showHtmlFromUri(Uri uri)93     private void showHtmlFromUri(Uri uri) {
94         // 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);
98         intent.setDataAndType(uri, "text/html");
99         intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
100         if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
101             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
102         }
103         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();
112         }
113     }
114 
showErrorAndFinish()115     private void showErrorAndFinish() {
116         Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
117                 .show();
118         finish();
119     }
120 
121     @VisibleForTesting
isFileValid(final File file)122     boolean isFileValid(final File file) {
123         return file.exists() && file.length() != 0;
124     }
125 }
126