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