Debian Codegraph @

libartservice/service/java/com/android/server/art/model/Config.java

raw
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.server.art.model;
18
19 import static com.android.server.art.ArtManagerLocal.BatchDexoptStartCallback;
20 import static com.android.server.art.ArtManagerLocal.DexoptDoneCallback;
21 import static com.android.server.art.ArtManagerLocal.ScheduleBackgroundDexoptJobCallback;
22
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25
26 import com.android.internal.annotations.GuardedBy;
27 import com.android.server.art.ArtManagerLocal;
28
29 import com.google.auto.value.AutoValue;
30
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.concurrent.Executor;
35
36 /**
37 * A class that stores the configurations set by the consumer of ART Service at runtime. This class
38 * is thread-safe.
39 *
40 * @hide
41 */
42 public class Config {
43 /** @see ArtManagerLocal#setBatchDexoptStartCallback(Executor, BatchDexoptStartCallback) */
44 @GuardedBy("this")
45 @Nullable
46 private Callback<BatchDexoptStartCallback, Void> mBatchDexoptStartCallback = null;
47
48 /**
49 * @see ArtManagerLocal#setScheduleBackgroundDexoptJobCallback(Executor,
50 * ScheduleBackgroundDexoptJobCallback)
51 */
52 @GuardedBy("this")
53 @Nullable
54 private Callback<ScheduleBackgroundDexoptJobCallback, Void>
55 mScheduleBackgroundDexoptJobCallback = null;
56
57 /**
58 * @see ArtManagerLocal#addDexoptDoneCallback(Executor, DexoptDoneCallback)
59 */
60 @GuardedBy("this")
61 @NonNull
62 private LinkedHashMap<DexoptDoneCallback, Callback<DexoptDoneCallback, Boolean>>
63 mDexoptDoneCallbacks = new LinkedHashMap<>();
64
65 public synchronized void setBatchDexoptStartCallback(
66 @NonNull Executor executor, @NonNull BatchDexoptStartCallback callback) {
67 mBatchDexoptStartCallback = Callback.create(callback, executor);
68 }
69
70 public synchronized void clearBatchDexoptStartCallback() {
71 mBatchDexoptStartCallback = null;
72 }
73
74 @Nullable
75 public synchronized Callback<BatchDexoptStartCallback, Void> getBatchDexoptStartCallback() {
76 return mBatchDexoptStartCallback;
77 }
78
79 public synchronized void setScheduleBackgroundDexoptJobCallback(
80 @NonNull Executor executor, @NonNull ScheduleBackgroundDexoptJobCallback callback) {
81 mScheduleBackgroundDexoptJobCallback = Callback.create(callback, executor);
82 }
83
84 public synchronized void clearScheduleBackgroundDexoptJobCallback() {
85 mScheduleBackgroundDexoptJobCallback = null;
86 }
87
88 @Nullable
89 public synchronized Callback<ScheduleBackgroundDexoptJobCallback, Void>
90 getScheduleBackgroundDexoptJobCallback() {
91 return mScheduleBackgroundDexoptJobCallback;
92 }
93
94 public synchronized void addDexoptDoneCallback(boolean onlyIncludeUpdates,
95 @NonNull Executor executor, @NonNull DexoptDoneCallback callback) {
96 if (mDexoptDoneCallbacks.putIfAbsent(
97 callback, Callback.create(callback, executor, onlyIncludeUpdates))
98 != null) {
99 throw new IllegalStateException("callback already added");
100 }
101 }
102
103 public synchronized void removeDexoptDoneCallback(@NonNull DexoptDoneCallback callback) {
104 mDexoptDoneCallbacks.remove(callback);
105 }
106
107 @NonNull
108 public synchronized List<Callback<DexoptDoneCallback, Boolean>> getDexoptDoneCallbacks() {
109 return new ArrayList<>(mDexoptDoneCallbacks.values());
110 }
111
112 @AutoValue
113 public static abstract class Callback<CallbackType, ExtraType> {
114 public abstract @NonNull CallbackType get();
115 public abstract @NonNull Executor executor();
116 public abstract @Nullable ExtraType extra();
117 static <CallbackType, ExtraType> @NonNull Callback<CallbackType, ExtraType> create(
118 @NonNull CallbackType callback, @NonNull Executor executor,
119 @Nullable ExtraType extra) {
120 return new AutoValue_Config_Callback<CallbackType, ExtraType>(
121 callback, executor, extra);
122 }
123 static <CallbackType> @NonNull Callback<CallbackType, Void> create(
124 @NonNull CallbackType callback, @NonNull Executor executor) {
125 return new AutoValue_Config_Callback<CallbackType, Void>(
126 callback, executor, null /* extra */);
127 }
128 }
129 }