• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.support.annotation.VisibleForTesting;
21 import android.util.Log;
22 
23 import com.android.settingslib.utils.AsyncLoader;
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 LicenseHtmlLoader extends AsyncLoader<File> {
33     private static final String TAG = "LicenseHtmlLoader";
34 
35     private 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     private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";
41 
42     private Context mContext;
43 
LicenseHtmlLoader(Context context)44     public LicenseHtmlLoader(Context context) {
45         super(context);
46         mContext = context;
47     }
48 
49     @Override
loadInBackground()50     public File loadInBackground() {
51         return generateHtmlFromDefaultXmlFiles();
52     }
53 
54     @Override
onDiscardResult(File f)55     protected void onDiscardResult(File f) {
56     }
57 
generateHtmlFromDefaultXmlFiles()58     private File generateHtmlFromDefaultXmlFiles() {
59         final List<File> xmlFiles = getVaildXmlFiles();
60         if (xmlFiles.isEmpty()) {
61             Log.e(TAG, "No notice file exists.");
62             return null;
63         }
64 
65         File cachedHtmlFile = getCachedHtmlFile();
66         if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
67                 || generateHtmlFile(xmlFiles, cachedHtmlFile)) {
68             return cachedHtmlFile;
69         }
70 
71         return null;
72     }
73 
74     @VisibleForTesting
getVaildXmlFiles()75     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 
86     @VisibleForTesting
getCachedHtmlFile()87     File getCachedHtmlFile() {
88         return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME);
89     }
90 
91     @VisibleForTesting
isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile)92     boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
93         boolean outdated = true;
94         if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
95             outdated = false;
96             for (File file : xmlFiles) {
97                 if (cachedHtmlFile.lastModified() < file.lastModified()) {
98                     outdated = true;
99                     break;
100                 }
101             }
102         }
103         return outdated;
104     }
105 
106     @VisibleForTesting
generateHtmlFile(List<File> xmlFiles, File htmlFile)107     boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
108         return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
109     }
110 }
111