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