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;
18 
19 import com.google.auto.value.AutoValue;
20 import com.google.common.collect.ImmutableList;
21 
22 import org.checkerframework.checker.nullness.qual.Nullable;
23 
24 /** Info passed to PluginManagers to load & run plugins. */
25 @AutoValue
26 public abstract class PluginInfo {
27     /**
28      * Information about the plugin archive : filename, and optionally packageName, if not the
29      * current package.
30      */
31     @AutoValue
32     public abstract static class ArchiveInfo {
33         /** The filename of the plugin APK. */
filename()34         public abstract @Nullable String filename();
35 
36         /** The packageName that contains the plugin APK. */
packageName()37         public abstract @Nullable String packageName();
38 
39         /** Instantiate a default builder of {@link ArchiveInfo}. */
builder()40         public static Builder builder() {
41             return new AutoValue_PluginInfo_ArchiveInfo.Builder();
42         }
43 
44         /** Creates a Builder from this {@link ArchiveInfo} */
toBuilder()45         public abstract Builder toBuilder();
46 
47         /** Builder to {@link ArchiveInfo}. */
48         @AutoValue.Builder
49         public abstract static class Builder {
50             /** Sets filename. */
setFilename(String filename)51             public abstract Builder setFilename(String filename);
52 
53             /** Sets packageName. */
setPackageName(String packageName)54             public abstract Builder setPackageName(String packageName);
55 
56             /** Builds an {@link ArchiveInfo}. */
build()57             public abstract ArchiveInfo build();
58         }
59     }
60 
61     /** Distinguish the task. */
taskName()62     public abstract String taskName();
63 
64     /** Information about the plugin APKs and the package containing them. */
archives()65     public abstract ImmutableList<ArchiveInfo> archives();
66 
67     /** Name of the entry point class implementing {@code Plugin}. */
entryPointClassName()68     public abstract @Nullable String entryPointClassName();
69 
70     /** Create {@link PluginInfo} for a JVM plugin with list of {@link ArchiveInfo}. */
createJvmInfo( String taskName, ImmutableList<ArchiveInfo> archives, String entryPointClassName)71     public static PluginInfo createJvmInfo(
72             String taskName, ImmutableList<ArchiveInfo> archives, String entryPointClassName) {
73         Builder builder = builder();
74         builder.setTaskName(taskName);
75         builder.setArchives(archives);
76         builder.setEntryPointClassName(entryPointClassName);
77         return builder.build();
78     }
79 
80     /** Create {@link PluginInfo} for a JVM plugin with one APK from the current package. */
createJvmInfo( String taskName, String filename, String entryPointClassName)81     public static PluginInfo createJvmInfo(
82             String taskName, String filename, String entryPointClassName) {
83         return createJvmInfo(
84                 taskName,
85                 ImmutableList.of(ArchiveInfo.builder().setFilename(filename).build()),
86                 entryPointClassName);
87     }
88 
89     /** Instantiate a default builder of {@link PluginInfo}. */
builder()90     public static Builder builder() {
91         return new AutoValue_PluginInfo.Builder();
92     }
93 
94     /** Builder to {@link PluginInfo}. */
95     @AutoValue.Builder
96     public abstract static class Builder {
97         /** Sets taskName. */
setTaskName(String taskName)98         public abstract Builder setTaskName(String taskName);
99 
100         /** Sets archives. */
setArchives(ImmutableList<ArchiveInfo> archives)101         public abstract Builder setArchives(ImmutableList<ArchiveInfo> archives);
102 
103         /** Sets entryPointClassName. */
setEntryPointClassName(@ullable String entryPointClassName)104         public abstract Builder setEntryPointClassName(@Nullable String entryPointClassName);
105 
106         /** Builds a {@link PluginInfo}. */
build()107         public abstract PluginInfo build();
108     }
109 }
110