|
| 1 | +package com.google.android.apps.forscience.whistlepunk.opensource.licenses; |
| 2 | + |
| 3 | +import android.content.res.Resources; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import androidx.annotation.NonNull; |
| 7 | + |
| 8 | +import com.google.android.apps.forscience.whistlepunk.opensource.R; |
| 9 | + |
| 10 | +import org.w3c.dom.Document; |
| 11 | +import org.w3c.dom.Node; |
| 12 | +import org.w3c.dom.NodeList; |
| 13 | +import org.xml.sax.SAXException; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Comparator; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import javax.xml.parsers.DocumentBuilder; |
| 22 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 23 | +import javax.xml.parsers.ParserConfigurationException; |
| 24 | + |
| 25 | +import io.reactivex.Scheduler; |
| 26 | +import io.reactivex.Single; |
| 27 | +import io.reactivex.disposables.CompositeDisposable; |
| 28 | +import io.reactivex.observers.DisposableSingleObserver; |
| 29 | + |
| 30 | +public class LoadLicenseTask { |
| 31 | + public static class Args { |
| 32 | + @NonNull |
| 33 | + final Resources resources; |
| 34 | + |
| 35 | + @NonNull |
| 36 | + final String tag_license; |
| 37 | + |
| 38 | + @NonNull |
| 39 | + final String attribute_key; |
| 40 | + |
| 41 | + @NonNull |
| 42 | + final String tag_title; |
| 43 | + |
| 44 | + @NonNull |
| 45 | + final String tag_resource; |
| 46 | + |
| 47 | + @NonNull |
| 48 | + final String tag_header; |
| 49 | + |
| 50 | + @NonNull |
| 51 | + final Comparator<LicenseActivity.License> comparator; |
| 52 | + |
| 53 | + public Args(@NonNull Resources resources, @NonNull String tag_license, @NonNull String attribute_key, @NonNull String tag_title, @NonNull String tag_resource, @NonNull String tag_header, @NonNull Comparator<LicenseActivity.License> comparator) { |
| 54 | + this.resources = resources; |
| 55 | + this.tag_license = tag_license; |
| 56 | + this.attribute_key = attribute_key; |
| 57 | + this.tag_title = tag_title; |
| 58 | + this.tag_resource = tag_resource; |
| 59 | + this.tag_header = tag_header; |
| 60 | + this.comparator = comparator; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static final String TAG = "TAG"; |
| 65 | + |
| 66 | + private final CompositeDisposable disposable = new CompositeDisposable(); |
| 67 | + |
| 68 | + private final Scheduler operationScheduler; |
| 69 | + private final Scheduler notificationScheduler; |
| 70 | + |
| 71 | + public LoadLicenseTask(@NonNull Scheduler operationScheduler, @NonNull Scheduler notificationScheduler) { |
| 72 | + this.operationScheduler = operationScheduler; |
| 73 | + this.notificationScheduler = notificationScheduler; |
| 74 | + } |
| 75 | + |
| 76 | + public void invoke(@NonNull Args args, @NonNull DisposableSingleObserver<List<LicenseActivity.License>> observer) { |
| 77 | + disposable.add(performOperation(args) |
| 78 | + .subscribeOn(operationScheduler) |
| 79 | + .observeOn(notificationScheduler) |
| 80 | + .subscribeWith(observer)); |
| 81 | + } |
| 82 | + |
| 83 | + @NonNull |
| 84 | + private Single<List<LicenseActivity.License>> performOperation(@NonNull Args args) { |
| 85 | + return getLicenses(args); |
| 86 | + } |
| 87 | + |
| 88 | + private Single<List<LicenseActivity.License>> getLicenses(@NonNull Args args) { |
| 89 | + return Single.create(e -> { |
| 90 | + DocumentBuilder builder = null; |
| 91 | + List<LicenseActivity.License> licenses = new ArrayList<>(); |
| 92 | + try { |
| 93 | + builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 94 | + Document licenseDoc = builder.parse(args.resources.openRawResource(R.raw.license_list)); |
| 95 | + NodeList licenseNodes = licenseDoc.getElementsByTagName(args.tag_license); |
| 96 | + final int size = licenseNodes.getLength(); |
| 97 | + for (int index = 0; index < size; index++) { |
| 98 | + Node licenseNode = licenseNodes.item(index); |
| 99 | + LicenseActivity.License license = new LicenseActivity.License(); |
| 100 | + license.key = licenseNode.getAttributes().getNamedItem(args.attribute_key).getNodeValue(); |
| 101 | + Node childNode = licenseNode.getFirstChild(); |
| 102 | + while (childNode != null) { |
| 103 | + String nodeName = childNode.getNodeName(); |
| 104 | + String nodeValue = |
| 105 | + childNode.getFirstChild() != null ? childNode.getFirstChild().getNodeValue() : null; |
| 106 | + if (args.tag_title.equals(nodeName)) { |
| 107 | + license.title = nodeValue; |
| 108 | + } else if (args.tag_resource.equals(nodeName)) { |
| 109 | + license.resource = nodeValue; |
| 110 | + } else if (args.tag_header.equals(nodeName)) { |
| 111 | + license.copyrightHeader = nodeValue; |
| 112 | + } |
| 113 | + childNode = childNode.getNextSibling(); |
| 114 | + } |
| 115 | + if (license.isValid()) { |
| 116 | + licenses.add(license); |
| 117 | + } else { |
| 118 | + Log.e(TAG, "Not adding invalid license: " + license); |
| 119 | + } |
| 120 | + } |
| 121 | + } catch (ParserConfigurationException | SAXException | IOException exception) { |
| 122 | + Log.e(TAG, "Could not parse license file.", exception); |
| 123 | + } |
| 124 | + Collections.sort(licenses, args.comparator); |
| 125 | + e.onSuccess(licenses); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public void discard() { |
| 130 | + disposable.clear(); |
| 131 | + } |
| 132 | +} |
0 commit comments