• 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.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             "/system_dlkm/etc/NOTICE.xml.gz",
45     };
46     static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";
47 
48     private final Context mContext;
49 
LicenseHtmlLoaderCompat(Context context)50     public LicenseHtmlLoaderCompat(Context context) {
51         super(context);
52         mContext = context;
53     }
54 
55     @Override
loadInBackground()56     public File loadInBackground() {
57         return generateHtmlFromDefaultXmlFiles();
58     }
59 
60     @Override
onDiscardResult(File f)61     protected void onDiscardResult(File f) {
62     }
63 
generateHtmlFromDefaultXmlFiles()64     private File generateHtmlFromDefaultXmlFiles() {
65         final List<File> xmlFiles = getVaildXmlFiles();
66         if (xmlFiles.isEmpty()) {
67             Log.e(TAG, "No notice file exists.");
68             return null;
69         }
70 
71         File cachedHtmlFile = getCachedHtmlFile(mContext);
72         if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
73                 || generateHtmlFile(mContext, xmlFiles, cachedHtmlFile)) {
74             return cachedHtmlFile;
75         }
76 
77         return null;
78     }
79 
getVaildXmlFiles()80     private List<File> getVaildXmlFiles() {
81         final List<File> xmlFiles = new ArrayList();
82         for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) {
83             File file = new File(xmlPath);
84             if (file.exists() && file.length() != 0) {
85                 xmlFiles.add(file);
86             }
87         }
88         return xmlFiles;
89     }
90 
getCachedHtmlFile(Context context)91     private File getCachedHtmlFile(Context context) {
92         return new File(context.getCacheDir(), NOTICE_HTML_FILE_NAME);
93     }
94 
isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile)95     private boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
96         boolean outdated = true;
97         if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
98             outdated = false;
99             for (File file : xmlFiles) {
100                 if (cachedHtmlFile.lastModified() < file.lastModified()) {
101                     outdated = true;
102                     break;
103                 }
104             }
105         }
106         return outdated;
107     }
108 
generateHtmlFile(Context context, List<File> xmlFiles, File htmlFile)109     private boolean generateHtmlFile(Context context, List<File> xmlFiles, File htmlFile) {
110         return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile,
111                 context.getString(R.string.notice_header));
112     }
113 }
114