Home | History | Annotate | Download | only in notification
      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;
     18 
     19 import android.content.Context;
     20 import android.support.v4.view.AsyncLayoutInflater;
     21 import android.util.Log;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 
     25 import com.android.systemui.R;
     26 import com.android.systemui.statusbar.InflationTask;
     27 import com.android.systemui.statusbar.ExpandableNotificationRow;
     28 import com.android.systemui.statusbar.NotificationData;
     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 NotificationData.Entry 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      */
     46     public void inflate(Context context, ViewGroup parent, NotificationData.Entry 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
     59     public void abort() {
     60         mCancelled = true;
     61     }
     62 
     63     @Override
     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 {
     80         void onInflationFinished(ExpandableNotificationRow row);
     81     }
     82 }
     83