The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 19 | import android.content.ActivityNotFoundException; |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.net.Uri; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 24 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.widget.Toast; |
| 26 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 27 | import androidx.annotation.VisibleForTesting; |
| 28 | import androidx.core.content.FileProvider; |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 29 | import androidx.fragment.app.FragmentActivity; |
tmfang | 99cc23d | 2018-06-26 19:01:57 +0800 | [diff] [blame] | 30 | import androidx.loader.app.LoaderManager; |
| 31 | import androidx.loader.content.Loader; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 32 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 33 | import com.android.settingslib.license.LicenseHtmlLoaderCompat; |
| 34 | |
| 35 | import java.io.File; |
| 36 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 37 | /** |
| 38 | * The "dialog" that shows from "License" in the Settings app. |
| 39 | */ |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 40 | public class SettingsLicenseActivity extends FragmentActivity implements |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 41 | LoaderManager.LoaderCallbacks<File> { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 42 | private static final String TAG = "SettingsLicenseActivity"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 43 | |
Inseob Kim | 4d56f11 | 2018-12-19 12:05:22 +0900 | [diff] [blame] | 44 | private static final String LICENSE_PATH = "/system/etc/NOTICE.html.gz"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 45 | |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 46 | private static final int LOADER_ID_LICENSE_HTML_LOADER = 0; |
| 47 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | @Override |
| 49 | protected void onCreate(Bundle savedInstanceState) { |
| 50 | super.onCreate(savedInstanceState); |
| 51 | |
Inseob Kim | 4d56f11 | 2018-12-19 12:05:22 +0900 | [diff] [blame] | 52 | File file = new File(LICENSE_PATH); |
| 53 | if (isFileValid(file)) { |
| 54 | showHtmlFromUri(Uri.fromFile(file)); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 55 | } else { |
| 56 | showHtmlFromDefaultXmlFiles(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Loader<File> onCreateLoader(int id, Bundle args) { |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 62 | return new LicenseHtmlLoaderCompat(this); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 63 | } |
| 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() { |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 75 | getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @VisibleForTesting |
| 79 | Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) { |
Andras Kloczl | 106431e | 2020-05-18 14:53:07 +0100 | [diff] [blame] | 80 | return FileProvider.getUriForFile(this, Utils.FILE_PROVIDER_AUTHORITY, |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 81 | 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 Seok | 47f6e54 | 2017-11-30 18:10:34 +0900 | [diff] [blame] | 93 | private void showHtmlFromUri(Uri uri) { |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 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); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 98 | intent.setDataAndType(uri, "text/html"); |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 99 | intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title)); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 100 | if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
| 101 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 102 | } |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 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(); |
Amith Yamasani | c101d2d | 2011-11-11 13:25:07 -0800 | [diff] [blame] | 112 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private void showErrorAndFinish() { |
| 116 | Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) |
| 117 | .show(); |
| 118 | finish(); |
| 119 | } |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 120 | |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 121 | @VisibleForTesting |
| 122 | boolean isFileValid(final File file) { |
| 123 | return file.exists() && file.length() != 0; |
| 124 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 125 | } |