blob: 7ed357c42390e6149d3790ab2f1d4a759b39e021 [file] [log] [blame]
Jaekyun Seok8d923f02017-12-01 10:37:10 +09001/*
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
17package com.android.settingslib.license;
18
19import android.content.Context;
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070020import androidx.annotation.VisibleForTesting;
Jaekyun Seok8d923f02017-12-01 10:37:10 +090021import android.util.Log;
22
23import com.android.settingslib.utils.AsyncLoader;
24
25import java.io.File;
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * LicenseHtmlLoader is a loader which loads a license html file from default license xml files.
31 */
32public 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",
Dario Frenib2e8c9a2018-07-09 18:45:30 +010039 "/oem/etc/NOTICE.xml.gz",
40 "/product/etc/NOTICE.xml.gz"};
Jaekyun Seok8d923f02017-12-01 10:37:10 +090041 private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";
42
43 private Context mContext;
44
45 public LicenseHtmlLoader(Context context) {
46 super(context);
47 mContext = context;
48 }
49
50 @Override
51 public File loadInBackground() {
52 return generateHtmlFromDefaultXmlFiles();
53 }
54
55 @Override
56 protected void onDiscardResult(File f) {
57 }
58
59 private File generateHtmlFromDefaultXmlFiles() {
60 final List<File> xmlFiles = getVaildXmlFiles();
61 if (xmlFiles.isEmpty()) {
62 Log.e(TAG, "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
75 @VisibleForTesting
76 List<File> getVaildXmlFiles() {
77 final List<File> xmlFiles = new ArrayList();
78 for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) {
79 File file = new File(xmlPath);
80 if (file.exists() && file.length() != 0) {
81 xmlFiles.add(file);
82 }
83 }
84 return xmlFiles;
85 }
86
87 @VisibleForTesting
88 File getCachedHtmlFile() {
89 return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME);
90 }
91
92 @VisibleForTesting
93 boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
94 boolean outdated = true;
95 if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
96 outdated = false;
97 for (File file : xmlFiles) {
98 if (cachedHtmlFile.lastModified() < file.lastModified()) {
99 outdated = true;
100 break;
101 }
102 }
103 }
104 return outdated;
105 }
106
107 @VisibleForTesting
108 boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
109 return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
110 }
111}