1 /*
2  * Copyright (C) 2022 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.ondevicepersonalization.libraries.plugin.internal;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import com.google.auto.value.AutoValue;
23 import com.google.common.base.Joiner;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.Lists;
26 
27 import org.checkerframework.checker.nullness.qual.Nullable;
28 
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Objects;
33 
34 /** Information used to perform preload & run tasks for Plugins. */
35 @AutoValue
36 public abstract class PluginInfoInternal implements Parcelable {
37     /** Human readable name of the task that is running. */
taskName()38     public abstract String taskName();
39 
40     /** Plugin code. */
pluginCodeList()41     public abstract ImmutableList<PluginCode> pluginCodeList();
42 
43     /** Fully qualified name of the entry point class implementing Plugin. */
entryPointClassName()44     public abstract String entryPointClassName();
45 
46     /** Computes the checksums of the plugin files. */
computeChecksum()47     public @Nullable String computeChecksum() {
48         List<String> checksums =
49                 new ArrayList<>(Lists.transform(pluginCodeList(), PluginCode::checksum));
50         if (checksums.contains("")) {
51             // If we're missing the checksum for any individual file, we cannot compute the combined
52             // checksum.
53             return null;
54         }
55         Collections.sort(checksums);
56         Joiner joiner = Joiner.on(":");
57         return joiner.join(checksums);
58     }
59 
60     @Override
describeContents()61     public final int describeContents() {
62         return Parcelable.CONTENTS_FILE_DESCRIPTOR;
63     }
64 
65     @Override
writeToParcel(Parcel dest, int flags)66     public final void writeToParcel(Parcel dest, int flags) {
67         dest.writeString(taskName());
68         dest.writeParcelableList(pluginCodeList(), flags);
69         dest.writeString(entryPointClassName());
70     }
71 
72     public static final Creator<PluginInfoInternal> CREATOR =
73             new Creator<PluginInfoInternal>() {
74                 @Override
75                 public PluginInfoInternal createFromParcel(Parcel source) {
76                     Builder builder = builder();
77                     String taskName = Objects.requireNonNull(source.readString());
78                     builder.setTaskName(taskName);
79 
80                     List<PluginCode> pluginCodeList = new ArrayList<>();
81                     builder.setPluginCodeList(
82                             ImmutableList.copyOf(
83                                     source.readParcelableList(
84                                             pluginCodeList, PluginCode.class.getClassLoader())));
85                     String entryPointClassName = Objects.requireNonNull(source.readString());
86                     builder.setEntryPointClassName(entryPointClassName);
87                     return builder.build();
88                 }
89 
90                 @Override
91                 public PluginInfoInternal[] newArray(int size) {
92                     return new PluginInfoInternal[size];
93                 }
94             };
95 
96     /** Instantiate a default builder of {@link PluginInfoInternal}. */
builder()97     public static Builder builder() {
98         return new AutoValue_PluginInfoInternal.Builder();
99     }
100 
101     /** Creates a builder from this {@link PluginInfoInternal}. */
toBuilder()102     public abstract Builder toBuilder();
103 
104     /** Builder to {@link PluginInfoInternal}. */
105     @AutoValue.Builder
106     public abstract static class Builder {
107         /** Sets taskName. */
setTaskName(String taskName)108         public abstract Builder setTaskName(String taskName);
109 
110         /** Sets pluginCodeList. */
setPluginCodeList(ImmutableList<PluginCode> fds)111         public abstract Builder setPluginCodeList(ImmutableList<PluginCode> fds);
112 
113         /** Sets entryPointClassName. */
setEntryPointClassName(String entryPointClassName)114         public abstract Builder setEntryPointClassName(String entryPointClassName);
115 
116         /** Builds a {@link PluginInfoInternal} */
build()117         public abstract PluginInfoInternal build();
118     }
119 }
120