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