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; |
| 24 | import android.os.SystemProperties; |
| 25 | import android.text.TextUtils; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 27 | import android.widget.Toast; |
| 28 | |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 29 | import com.android.settings.users.RestrictedProfileSettings; |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame^] | 30 | import com.android.settingslib.license.LicenseHtmlLoaderCompat; |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 31 | |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 32 | import java.io.File; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 33 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 34 | import androidx.annotation.VisibleForTesting; |
| 35 | import androidx.core.content.FileProvider; |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame^] | 36 | import androidx.fragment.app.FragmentActivity; |
tmfang | 99cc23d | 2018-06-26 19:01:57 +0800 | [diff] [blame] | 37 | import androidx.loader.app.LoaderManager; |
| 38 | import androidx.loader.content.Loader; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 40 | /** |
| 41 | * The "dialog" that shows from "License" in the Settings app. |
| 42 | */ |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame^] | 43 | public class SettingsLicenseActivity extends FragmentActivity implements |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 44 | LoaderManager.LoaderCallbacks<File> { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 45 | private static final String TAG = "SettingsLicenseActivity"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 46 | |
| 47 | private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz"; |
| 48 | private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path"; |
| 49 | |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 50 | private static final int LOADER_ID_LICENSE_HTML_LOADER = 0; |
| 51 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 52 | @Override |
| 53 | protected void onCreate(Bundle savedInstanceState) { |
| 54 | super.onCreate(savedInstanceState); |
| 55 | |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 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 |
| 66 | public Loader<File> onCreateLoader(int id, Bundle args) { |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame^] | 67 | return new LicenseHtmlLoaderCompat(this); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onLoadFinished(Loader<File> loader, File generatedHtmlFile) { |
| 72 | showGeneratedHtmlFile(generatedHtmlFile); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onLoaderReset(Loader<File> loader) { |
| 77 | } |
| 78 | |
| 79 | private void showHtmlFromDefaultXmlFiles() { |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame^] | 80 | getSupportLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @VisibleForTesting |
| 84 | Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) { |
| 85 | return FileProvider.getUriForFile(this, RestrictedProfileSettings.FILE_PROVIDER_AUTHORITY, |
| 86 | generatedHtmlFile); |
| 87 | } |
| 88 | |
| 89 | private void showGeneratedHtmlFile(File generatedHtmlFile) { |
| 90 | if (generatedHtmlFile != null) { |
| 91 | showHtmlFromUri(getUriFromGeneratedHtmlFile(generatedHtmlFile)); |
| 92 | } else { |
| 93 | Log.e(TAG, "Failed to generate."); |
| 94 | showErrorAndFinish(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private void showSelectedFile(final String path) { |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 99 | if (TextUtils.isEmpty(path)) { |
| 100 | Log.e(TAG, "The system property for the license file is empty"); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 101 | showErrorAndFinish(); |
| 102 | return; |
| 103 | } |
| 104 | |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 105 | final File file = new File(path); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 106 | if (!isFileValid(file)) { |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 107 | Log.e(TAG, "License file " + path + " does not exist"); |
| 108 | showErrorAndFinish(); |
| 109 | return; |
Henrik Carlsson | 2cb19ab | 2010-09-03 11:08:19 +0200 | [diff] [blame] | 110 | } |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 111 | showHtmlFromUri(Uri.fromFile(file)); |
Jaekyun Seok | 47f6e54 | 2017-11-30 18:10:34 +0900 | [diff] [blame] | 112 | } |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 113 | |
Jaekyun Seok | 47f6e54 | 2017-11-30 18:10:34 +0900 | [diff] [blame] | 114 | private void showHtmlFromUri(Uri uri) { |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 115 | // Kick off external viewer due to WebView security restrictions; we |
| 116 | // carefully point it at HTMLViewer, since it offers to decompress |
| 117 | // before viewing. |
| 118 | final Intent intent = new Intent(Intent.ACTION_VIEW); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 119 | intent.setDataAndType(uri, "text/html"); |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 120 | intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title)); |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 121 | if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
| 122 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 123 | } |
Jeff Sharkey | e16e44f | 2014-11-13 18:02:31 -0800 | [diff] [blame] | 124 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 125 | intent.setPackage("com.android.htmlviewer"); |
| 126 | |
| 127 | try { |
| 128 | startActivity(intent); |
| 129 | finish(); |
| 130 | } catch (ActivityNotFoundException e) { |
| 131 | Log.e(TAG, "Failed to find viewer", e); |
| 132 | showErrorAndFinish(); |
Amith Yamasani | c101d2d | 2011-11-11 13:25:07 -0800 | [diff] [blame] | 133 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private void showErrorAndFinish() { |
| 137 | Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) |
| 138 | .show(); |
| 139 | finish(); |
| 140 | } |
Jaekyun Seok | 7481287 | 2017-04-18 15:22:01 +0900 | [diff] [blame] | 141 | |
| 142 | private boolean isFilePathValid(final String path) { |
| 143 | return !TextUtils.isEmpty(path) && isFileValid(new File(path)); |
| 144 | } |
| 145 | |
| 146 | @VisibleForTesting |
| 147 | boolean isFileValid(final File file) { |
| 148 | return file.exists() && file.length() != 0; |
| 149 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 150 | } |