1 /*
2  * Copyright (C) 2017 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.systemui.statusbar.notification.row;
18 
19 import android.content.Context;
20 import android.util.Log;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
25 
26 import com.android.systemui.R;
27 import com.android.systemui.statusbar.InflationTask;
28 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
29 
30 import javax.inject.Inject;
31 
32 /**
33  * An inflater task that asynchronously inflates a ExpandableNotificationRow
34  */
35 public class RowInflaterTask implements InflationTask, AsyncLayoutInflater.OnInflateFinishedListener {
36 
37     private static final String TAG = "RowInflaterTask";
38     private static final boolean TRACE_ORIGIN = true;
39 
40     private RowInflationFinishedListener mListener;
41     private NotificationEntry mEntry;
42     private boolean mCancelled;
43     private Throwable mInflateOrigin;
44 
45     @Inject
RowInflaterTask()46     public RowInflaterTask() {
47     }
48 
49     /**
50      * Inflates a new notificationView. This should not be called twice on this object
51      */
inflate(Context context, ViewGroup parent, NotificationEntry entry, RowInflationFinishedListener listener)52     public void inflate(Context context, ViewGroup parent, NotificationEntry entry,
53             RowInflationFinishedListener listener) {
54         if (TRACE_ORIGIN) {
55             mInflateOrigin = new Throwable("inflate requested here");
56         }
57         mListener = listener;
58         AsyncLayoutInflater inflater = new AsyncLayoutInflater(context);
59         mEntry = entry;
60         entry.setInflationTask(this);
61         inflater.inflate(R.layout.status_bar_notification_row, parent, this);
62     }
63 
64     @Override
abort()65     public void abort() {
66         mCancelled = true;
67     }
68 
69     @Override
onInflateFinished(View view, int resid, ViewGroup parent)70     public void onInflateFinished(View view, int resid, ViewGroup parent) {
71         if (!mCancelled) {
72             try {
73                 mEntry.onInflationTaskFinished();
74                 mListener.onInflationFinished((ExpandableNotificationRow) view);
75             } catch (Throwable t) {
76                 if (mInflateOrigin != null) {
77                     Log.e(TAG, "Error in inflation finished listener: " + t, mInflateOrigin);
78                     t.addSuppressed(mInflateOrigin);
79                 }
80                 throw t;
81             }
82         }
83     }
84 
85     public interface RowInflationFinishedListener {
onInflationFinished(ExpandableNotificationRow row)86         void onInflationFinished(ExpandableNotificationRow row);
87     }
88 }
89