1 /*
2  * Copyright (C) 2015 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.messaging.datamodel.media;
17 
18 import com.android.messaging.datamodel.binding.BindableOnceData;
19 import com.android.messaging.datamodel.media.MediaResourceManager.MediaResourceLoadListener;
20 
21 /**
22  * The {@link MediaRequest} interface is threading-model-oblivious, allowing the implementations to
23  * be processed synchronously or asynchronously.
24  * This is a {@link MediaRequest} implementation that includes functionalities such as binding and
25  * event callbacks for multi-threaded media request processing.
26  */
27 public abstract class BindableMediaRequest<T extends RefCountedMediaResource>
28         extends BindableOnceData
29         implements MediaRequest<T>, MediaResourceLoadListener<T> {
30     private MediaResourceLoadListener<T> mListener;
31 
BindableMediaRequest(final MediaResourceLoadListener<T> listener)32     public BindableMediaRequest(final MediaResourceLoadListener<T> listener) {
33         mListener = listener;
34     }
35 
36     /**
37      * Delegates the media resource callback to the listener. Performs binding check to ensure
38      * the listener is still bound to this request.
39      */
40     @Override
onMediaResourceLoaded(final MediaRequest<T> request, final T resource, final boolean cached)41     public void onMediaResourceLoaded(final MediaRequest<T> request, final T resource,
42             final boolean cached) {
43         if (isBound() && mListener != null) {
44             mListener.onMediaResourceLoaded(request, resource, cached);
45         }
46     }
47 
48     /**
49      * Delegates the media resource callback to the listener. Performs binding check to ensure
50      * the listener is still bound to this request.
51      */
52     @Override
onMediaResourceLoadError(final MediaRequest<T> request, final Exception exception)53     public void onMediaResourceLoadError(final MediaRequest<T> request, final Exception exception) {
54         if (isBound() && mListener != null) {
55             mListener.onMediaResourceLoadError(request, exception);
56         }
57     }
58 
59     @Override
unregisterListeners()60     protected void unregisterListeners() {
61         mListener = null;
62     }
63 }
64