1 /* 2 * Copyright (C) 2018 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.settingslib.license; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import com.android.settingslib.R; 23 import com.android.settingslib.utils.AsyncLoaderCompat; 24 25 import java.io.File; 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * LicenseHtmlLoader is a loader which loads a license html file from default license xml files. 31 */ 32 public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> { 33 private static final String TAG = "LicenseHtmlLoaderCompat"; 34 35 static final String[] DEFAULT_LICENSE_XML_PATHS = { 36 "/system/etc/NOTICE.xml.gz", 37 "/vendor/etc/NOTICE.xml.gz", 38 "/odm/etc/NOTICE.xml.gz", 39 "/oem/etc/NOTICE.xml.gz", 40 "/product/etc/NOTICE.xml.gz", 41 "/system_ext/etc/NOTICE.xml.gz", 42 "/vendor_dlkm/etc/NOTICE.xml.gz", 43 "/odm_dlkm/etc/NOTICE.xml.gz", 44 }; 45 static final String NOTICE_HTML_FILE_NAME = "NOTICE.html"; 46 47 private final Context mContext; 48 LicenseHtmlLoaderCompat(Context context)49 public LicenseHtmlLoaderCompat(Context context) { 50 super(context); 51 mContext = context; 52 } 53 54 @Override loadInBackground()55 public File loadInBackground() { 56 return generateHtmlFromDefaultXmlFiles(); 57 } 58 59 @Override onDiscardResult(File f)60 protected void onDiscardResult(File f) { 61 } 62 generateHtmlFromDefaultXmlFiles()63 private File generateHtmlFromDefaultXmlFiles() { 64 final List<File> xmlFiles = getVaildXmlFiles(); 65 if (xmlFiles.isEmpty()) { 66 Log.e(TAG, "No notice file exists."); 67 return null; 68 } 69 70 File cachedHtmlFile = getCachedHtmlFile(mContext); 71 if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile) 72 || generateHtmlFile(mContext, xmlFiles, cachedHtmlFile)) { 73 return cachedHtmlFile; 74 } 75 76 return null; 77 } 78 getVaildXmlFiles()79 private List<File> getVaildXmlFiles() { 80 final List<File> xmlFiles = new ArrayList(); 81 for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) { 82 File file = new File(xmlPath); 83 if (file.exists() && file.length() != 0) { 84 xmlFiles.add(file); 85 } 86 } 87 return xmlFiles; 88 } 89 getCachedHtmlFile(Context context)90 private File getCachedHtmlFile(Context context) { 91 return new File(context.getCacheDir(), NOTICE_HTML_FILE_NAME); 92 } 93 isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile)94 private boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) { 95 boolean outdated = true; 96 if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) { 97 outdated = false; 98 for (File file : xmlFiles) { 99 if (cachedHtmlFile.lastModified() < file.lastModified()) { 100 outdated = true; 101 break; 102 } 103 } 104 } 105 return outdated; 106 } 107 generateHtmlFile(Context context, List<File> xmlFiles, File htmlFile)108 private boolean generateHtmlFile(Context context, List<File> xmlFiles, File htmlFile) { 109 return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile, 110 context.getString(R.string.notice_header)); 111 } 112 } 113