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.tv.license;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * Container class to store the name of a library and the filename of its associated license file.
24  */
25 public final class License implements Comparable<License>, Parcelable {
26     // Name of the third-party library.
27     private final String mLibraryName;
28     // Byte offset in the file to the start of the license text.
29     private final long mLicenseOffset;
30     // Byte length of the license text.
31     private final int mLicenseLength;
32     // Path to the archive that has bundled licenses.
33     // Empty string if the license is bundled in the apk itself.
34     private final String mPath;
35 
36     /**
37      * Create an object representing a stored license. The text for all licenses is stored in a
38      * single file, so the offset and length describe this license's position within the file.
39      *
40      * @param path a path to an .apk-compatible archive that contains the license. An empty string
41      *     in case the license is contained within the app itself.
42      */
create(String libraryName, long licenseOffset, int licenseLength, String path)43     static License create(String libraryName, long licenseOffset, int licenseLength, String path) {
44         return new License(libraryName, licenseOffset, licenseLength, path);
45     }
46 
47     public static final Parcelable.Creator<License> CREATOR =
48             new Parcelable.Creator<License>() {
49                 @Override
50                 public License createFromParcel(Parcel in) {
51                     return new License(in);
52                 }
53 
54                 @Override
55                 public License[] newArray(int size) {
56                     return new License[size];
57                 }
58             };
59 
60     @Override
describeContents()61     public int describeContents() {
62         return 0;
63     }
64 
65     @Override
writeToParcel(Parcel dest, int flags)66     public void writeToParcel(Parcel dest, int flags) {
67         dest.writeString(mLibraryName);
68         dest.writeLong(mLicenseOffset);
69         dest.writeInt(mLicenseLength);
70         dest.writeString(mPath);
71     }
72 
73     @Override
compareTo(License o)74     public int compareTo(License o) {
75         return mLibraryName.compareToIgnoreCase(o.getLibraryName());
76     }
77 
78     @Override
toString()79     public String toString() {
80         return getLibraryName();
81     }
82 
License(String libraryName, long licenseOffset, int licenseLength, String path)83     private License(String libraryName, long licenseOffset, int licenseLength, String path) {
84         this.mLibraryName = libraryName;
85         this.mLicenseOffset = licenseOffset;
86         this.mLicenseLength = licenseLength;
87         this.mPath = path;
88     }
89 
License(Parcel in)90     private License(Parcel in) {
91         mLibraryName = in.readString();
92         mLicenseOffset = in.readLong();
93         mLicenseLength = in.readInt();
94         mPath = in.readString();
95     }
96 
getLibraryName()97     String getLibraryName() {
98         return mLibraryName;
99     }
100 
getLicenseOffset()101     long getLicenseOffset() {
102         return mLicenseOffset;
103     }
104 
getLicenseLength()105     int getLicenseLength() {
106         return mLicenseLength;
107     }
108 
getPath()109     public String getPath() {
110         return mPath;
111     }
112 }
113