• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package com.android.internal.app;
17 
18 import android.app.prediction.AppPredictor;
19 import android.app.prediction.AppTarget;
20 
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.function.Consumer;
24 
25 /**
26  * Callback wrapper that works around potential memory leaks in app predictor.
27  *
28  * Nulls the callback itself when destroyed, so at worst you'll leak just this object.
29  */
30 public class ResolverAppPredictorCallback {
31     private volatile Consumer<List<AppTarget>> mCallback;
32 
ResolverAppPredictorCallback(Consumer<List<AppTarget>> callback)33     public ResolverAppPredictorCallback(Consumer<List<AppTarget>> callback) {
34         mCallback = callback;
35     }
36 
notifyCallback(List<AppTarget> list)37     private void notifyCallback(List<AppTarget> list) {
38         Consumer<List<AppTarget>> callback = mCallback;
39         if (callback != null) {
40             callback.accept(Objects.requireNonNullElseGet(list, List::of));
41         }
42     }
43 
asConsumer()44     public Consumer<List<AppTarget>> asConsumer() {
45         return this::notifyCallback;
46     }
47 
asCallback()48     public AppPredictor.Callback asCallback() {
49         return this::notifyCallback;
50     }
51 
destroy()52     public void destroy() {
53         mCallback = null;
54     }
55 }
56