blob: ebb1ae13afde556a74b059ad10cf80f21c173fa7 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080019import android.app.Activity;
Jaekyun Seok74812872017-04-18 15:22:01 +090020import android.app.LoaderManager;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080021import android.content.ActivityNotFoundException;
Jaekyun Seok74812872017-04-18 15:22:01 +090022import android.content.ContentResolver;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080023import android.content.Intent;
Jaekyun Seok74812872017-04-18 15:22:01 +090024import android.content.Loader;
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080025import android.net.Uri;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.os.Bundle;
27import android.os.SystemProperties;
Jaekyun Seok74812872017-04-18 15:22:01 +090028import android.support.annotation.VisibleForTesting;
29import android.support.v4.content.FileProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030import android.text.TextUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080032import android.widget.Toast;
33
Jaekyun Seok74812872017-04-18 15:22:01 +090034import com.android.settings.users.RestrictedProfileSettings;
Jaekyun Seok47f6e542017-11-30 18:10:34 +090035import com.android.settingslib.license.LicenseHtmlLoader;
Jaekyun Seok74812872017-04-18 15:22:01 +090036
Jeff Sharkeye16e44f2014-11-13 18:02:31 -080037import java.io.File;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039/**
40 * The "dialog" that shows from "License" in the Settings app.
41 */
Jaekyun Seok74812872017-04-18 15:22:01 +090042public class SettingsLicenseActivity extends Activity implements
43 LoaderManager.LoaderCallbacks<File> {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080044 private static final String TAG = "SettingsLicenseActivity";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045
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 Seok74812872017-04-18 15:22:01 +090049 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0;
50
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54
Jaekyun Seok74812872017-04-18 15:22:01 +090055 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 Sharkeye16e44f2014-11-13 18:02:31 -080098 if (TextUtils.isEmpty(path)) {
99 Log.e(TAG, "The system property for the license file is empty");
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 showErrorAndFinish();
101 return;
102 }
103
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800104 final File file = new File(path);
Jaekyun Seok74812872017-04-18 15:22:01 +0900105 if (!isFileValid(file)) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800106 Log.e(TAG, "License file " + path + " does not exist");
107 showErrorAndFinish();
108 return;
Henrik Carlsson2cb19ab2010-09-03 11:08:19 +0200109 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900110 showHtmlFromUri(Uri.fromFile(file));
Jaekyun Seok47f6e542017-11-30 18:10:34 +0900111 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800112
Jaekyun Seok47f6e542017-11-30 18:10:34 +0900113 private void showHtmlFromUri(Uri uri) {
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800114 // 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 Seok74812872017-04-18 15:22:01 +0900118 intent.setDataAndType(uri, "text/html");
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800119 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_license_activity_title));
Jaekyun Seok74812872017-04-18 15:22:01 +0900120 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
121 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
122 }
Jeff Sharkeye16e44f2014-11-13 18:02:31 -0800123 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 Yamasanic101d2d2011-11-11 13:25:07 -0800132 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 }
134
135 private void showErrorAndFinish() {
136 Toast.makeText(this, R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
137 .show();
138 finish();
139 }
Jaekyun Seok74812872017-04-18 15:22:01 +0900140
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 Projectafc4ab22009-03-03 19:32:34 -0800149}