1 /*
2  * Copyright (C) 2016 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.dialer.app.calllog;
18 
19 import android.support.v7.widget.RecyclerView;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import com.android.dialer.app.R;
24 import com.android.dialer.app.alert.AlertManager;
25 import com.android.dialer.common.Assert;
26 
27 /** Manages "alerts" to be shown at the top of an call log to gain the user's attention. */
28 public class CallLogAlertManager implements AlertManager {
29 
30   private final CallLogAdapter adapter;
31   private final View view;
32   private final LayoutInflater inflater;
33   private final ViewGroup parent;
34   private final ViewGroup container;
35 
CallLogAlertManager(CallLogAdapter adapter, LayoutInflater inflater, ViewGroup parent)36   public CallLogAlertManager(CallLogAdapter adapter, LayoutInflater inflater, ViewGroup parent) {
37     this.adapter = adapter;
38     this.inflater = inflater;
39     this.parent = parent;
40     view = inflater.inflate(R.layout.call_log_alert_item, parent, false);
41     container = (ViewGroup) view.findViewById(R.id.container);
42   }
43 
44   @Override
inflate(int layoutId)45   public View inflate(int layoutId) {
46     return inflater.inflate(layoutId, container, false);
47   }
48 
createViewHolder(ViewGroup parent)49   public RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
50     Assert.checkArgument(
51         parent == this.parent,
52         "createViewHolder should be called with the same parent in constructor");
53     return new AlertViewHolder(view);
54   }
55 
isEmpty()56   public boolean isEmpty() {
57     return container.getChildCount() == 0;
58   }
59 
contains(View view)60   public boolean contains(View view) {
61     return container.indexOfChild(view) != -1;
62   }
63 
64   @Override
clear()65   public void clear() {
66     container.removeAllViews();
67     adapter.notifyItemRemoved(CallLogAdapter.ALERT_POSITION);
68   }
69 
70   @Override
add(View view)71   public void add(View view) {
72     if (contains(view)) {
73       return;
74     }
75     container.addView(view);
76     if (container.getChildCount() == 1) {
77       // Was empty before
78       adapter.notifyItemInserted(CallLogAdapter.ALERT_POSITION);
79     }
80   }
81 
82   /**
83    * Does nothing. The view this ViewHolder show is directly managed by {@link CallLogAlertManager}
84    */
85   private static class AlertViewHolder extends RecyclerView.ViewHolder {
AlertViewHolder(View view)86     private AlertViewHolder(View view) {
87       super(view);
88     }
89   }
90 }
91