1 /* 2 * Copyright (C) 2014 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.tv.settings.about; 18 19 import android.app.Activity; 20 import android.app.LoaderManager; 21 import android.content.ActivityNotFoundException; 22 import android.content.ContentResolver; 23 import android.content.Intent; 24 import android.content.Loader; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.os.SystemProperties; 28 import android.support.annotation.VisibleForTesting; 29 import android.support.v4.content.FileProvider; 30 import android.text.TextUtils; 31 import android.util.Log; 32 import android.widget.Toast; 33 34 import com.android.settingslib.license.LicenseHtmlLoader; 35 import com.android.tv.settings.R; 36 37 import java.io.File; 38 39 /** 40 * Displays open source NOTICE files. 41 */ 42 public class LicenseActivity extends Activity implements 43 LoaderManager.LoaderCallbacks<File> { 44 private static final String TAG = "LicenseActivity"; 45 private static final String FILE_PROVIDER_AUTHORITY = "com.android.settings.files"; 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 50 private static final int LOADER_ID_LICENSE_HTML_LOADER = 0; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 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) { 67 return new LicenseHtmlLoader(this); 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() { 80 getLoaderManager().initLoader(LOADER_ID_LICENSE_HTML_LOADER, Bundle.EMPTY, this); 81 } 82 83 @VisibleForTesting 84 Uri getUriFromGeneratedHtmlFile(File generatedHtmlFile) { 85 return FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, 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) { 98 if (TextUtils.isEmpty(path)) { 99 Log.e(TAG, "The system property for the license file is empty"); 100 showErrorAndFinish(); 101 return; 102 } 103 104 final File file = new File(path); 105 if (!isFileValid(file)) { 106 Log.e(TAG, "License file " + path + " does not exist"); 107 showErrorAndFinish(); 108 return; 109 } 110 showHtmlFromUri(Uri.fromFile(file)); 111 } 112 113 private void showHtmlFromUri(Uri uri) { 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); 118 intent.setDataAndType(uri, "text/html"); 119 intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.about_legal_license)); 120 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { 121 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 122 } 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(); 132 } 133 } 134 135 private void showErrorAndFinish() { 136 Toast.makeText(this, R.string.about_license_activity_unavailable, Toast.LENGTH_LONG) 137 .show(); 138 finish(); 139 } 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 } 149 } 150