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