• 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.settings.users.RestrictedProfileSettings;
34 import com.android.settingslib.license.LicenseHtmlLoaderCompat;
35 
36 import java.io.File;
37 
38 /**
39  * The "dialog" that shows from "License" in the Settings app.
40  */
41 public class SettingsLicenseActivity extends FragmentActivity implements
42             LoaderManager.LoaderCallbacks<File> {
43     private static final String TAG = "SettingsLicenseActivity";
44 
45     private static final String LICENSE_PATH = "/system/etc/NOTICE.html.gz";
46 
47     private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         File file = new File(LICENSE_PATH);
54         if (isFileValid(file)) {
55             showHtmlFromUri(Uri.fromFile(file));
56         } else {
57             showHtmlFromDefaultXmlFiles();
58         }
59     }
60 
61     @Override
onCreateLoader(int id, Bundle args)62     public Loader<File> onCreateLoader(int id, Bundle args) {
63         return new LicenseHtmlLoaderCompat(this);
64     }
65 
66     @Override
onLoadFinished(Loader<File> loader, File generatedHtmlFile)67     public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) {
68         showGeneratedHtmlFile(generatedHtmlFile);
69     }
70 
71     @Override
onLoaderReset(Loader<File> loader)72     public void onLoaderReset(Loader<File> loader) {
73     }
74 
showHtmlFromDefaultXmlFiles()75     private void showHtmlFromDefaultXmlFiles() {
76         getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this);
77     }
78 
79     @VisibleForTesting
getUriFromGeneratedHtmlFile(File generatedHtmlFile)80     Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) {
81         return FileProvider.getUriForFile(this, RestrictedProfileSettings.FILE_PROVIDER_AUTHORITY,
82                 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 
showHtmlFromUri(Uri uri)94     private void showHtmlFromUri(Uri uri) {
95         // Kick off external viewer due to WebView security restrictions; we
96         // carefully point it at HTMLViewer, since it offers to decompress
97         // before viewing.
98         final Intent intent = new Intent(Intent.ACTION_VIEW);
99         intent.setDataAndType(uri, "text/html");
100         intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
101         if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
102             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
103         }
104         intent.addCategory(Intent.CATEGORY_DEFAULT);
105         intent.setPackage("com.android.htmlviewer");
106 
107         try {
108             startActivity(intent);
109             finish();
110         } catch (ActivityNotFoundException e) {
111             Log.e(TAG, "Failed to find viewer", e);
112             showErrorAndFinish();
113         }
114     }
115 
showErrorAndFinish()116     private void showErrorAndFinish() {
117         Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
118                 .show();
119         finish();
120     }
121 
122     @VisibleForTesting
isFileValid(final File file)123     boolean isFileValid(final File file) {
124         return file.exists() && file.length() != 0;
125     }
126 }
127