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.ui.conversation;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.view.animation.Animation;
24 import android.view.animation.TranslateAnimation;
25 import android.widget.ArrayAdapter;
26 import android.widget.FrameLayout;
27 import android.widget.ListView;
28 
29 import com.android.messaging.R;
30 import com.android.messaging.datamodel.data.SubscriptionListData;
31 import com.android.messaging.datamodel.data.SubscriptionListData.SubscriptionListEntry;
32 import com.android.messaging.util.UiUtils;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /**
38  * Displays a SIM selector above the compose message view and overlays the message list.
39  */
40 public class SimSelectorView extends FrameLayout implements SimSelectorItemView.HostInterface {
41     public interface SimSelectorViewListener {
onSimItemClicked(SubscriptionListEntry item)42         void onSimItemClicked(SubscriptionListEntry item);
onSimSelectorVisibilityChanged(boolean visible)43         void onSimSelectorVisibilityChanged(boolean visible);
44     }
45 
46     private ListView mSimListView;
47     private final SimSelectorAdapter mAdapter;
48     private boolean mShow;
49     private SimSelectorViewListener mListener;
50     private int mItemLayoutId;
51 
SimSelectorView(Context context, AttributeSet attrs)52     public SimSelectorView(Context context, AttributeSet attrs) {
53         super(context, attrs);
54         mAdapter = new SimSelectorAdapter(getContext());
55     }
56 
57     @Override
onFinishInflate()58     protected void onFinishInflate() {
59         super.onFinishInflate();
60         mSimListView = (ListView) findViewById(R.id.sim_list);
61         mSimListView.setAdapter(mAdapter);
62 
63         // Clicking anywhere outside the switcher list should dismiss.
64         setOnClickListener(new OnClickListener() {
65             @Override
66             public void onClick(View v) {
67                 showOrHide(false, true);
68             }
69         });
70     }
71 
bind(final SubscriptionListData data)72     public void bind(final SubscriptionListData data) {
73         mAdapter.bindData(data.getActiveSubscriptionEntriesExcludingDefault());
74     }
75 
setItemLayoutId(final int layoutId)76     public void setItemLayoutId(final int layoutId) {
77         mItemLayoutId = layoutId;
78     }
79 
setListener(final SimSelectorViewListener listener)80     public void setListener(final SimSelectorViewListener listener) {
81         mListener = listener;
82     }
83 
toggleVisibility()84     public void toggleVisibility() {
85         showOrHide(!mShow, true);
86     }
87 
showOrHide(final boolean show, final boolean animate)88     public void showOrHide(final boolean show, final boolean animate) {
89         final boolean oldShow = mShow;
90         mShow = show && mAdapter.getCount() > 1;
91         if (oldShow != mShow) {
92             if (mListener != null) {
93                 mListener.onSimSelectorVisibilityChanged(mShow);
94             }
95 
96             if (animate) {
97                 // Fade in the background pane.
98                 setVisibility(VISIBLE);
99                 setAlpha(mShow ? 0.0f : 1.0f);
100                 animate().alpha(mShow ? 1.0f : 0.0f)
101                     .setDuration(UiUtils.REVEAL_ANIMATION_DURATION)
102                     .withEndAction(new Runnable() {
103                         @Override
104                         public void run() {
105                             setAlpha(1.0f);
106                             setVisibility(mShow ? VISIBLE : GONE);
107                         }
108                     });
109             } else {
110                 setVisibility(mShow ? VISIBLE : GONE);
111             }
112 
113             // Slide in the SIM selector list via a translate animation.
114             mSimListView.setVisibility(mShow ? VISIBLE : GONE);
115             if (animate) {
116                 mSimListView.clearAnimation();
117                 final TranslateAnimation translateAnimation = new TranslateAnimation(
118                         Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
119                         Animation.RELATIVE_TO_SELF, mShow ? 1.0f : 0.0f,
120                         Animation.RELATIVE_TO_SELF, mShow ? 0.0f : 1.0f);
121                 translateAnimation.setInterpolator(UiUtils.EASE_OUT_INTERPOLATOR);
122                 translateAnimation.setDuration(UiUtils.REVEAL_ANIMATION_DURATION);
123                 mSimListView.startAnimation(translateAnimation);
124             }
125         }
126     }
127 
128     /**
129      * An adapter that takes a list of SubscriptionListEntry and displays them as a list of
130      * available SIMs in the SIM selector.
131      */
132     private class SimSelectorAdapter extends ArrayAdapter<SubscriptionListEntry> {
SimSelectorAdapter(final Context context)133         public SimSelectorAdapter(final Context context) {
134             super(context, R.layout.sim_selector_item_view, new ArrayList<SubscriptionListEntry>());
135         }
136 
bindData(final List<SubscriptionListEntry> newList)137         public void bindData(final List<SubscriptionListEntry> newList) {
138             clear();
139             addAll(newList);
140             notifyDataSetChanged();
141         }
142 
143         @Override
getView(final int position, final View convertView, final ViewGroup parent)144         public View getView(final int position, final View convertView, final ViewGroup parent) {
145             SimSelectorItemView itemView;
146             if (convertView != null && convertView instanceof SimSelectorItemView) {
147                 itemView = (SimSelectorItemView) convertView;
148             } else {
149                 final LayoutInflater inflater = (LayoutInflater) getContext()
150                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
151                 itemView = (SimSelectorItemView) inflater.inflate(mItemLayoutId,
152                         parent, false);
153                 itemView.setHostInterface(SimSelectorView.this);
154             }
155             itemView.bind(getItem(position));
156             return itemView;
157         }
158     }
159 
160     @Override
onSimItemClicked(SubscriptionListEntry item)161     public void onSimItemClicked(SubscriptionListEntry item) {
162         mListener.onSimItemClicked(item);
163         showOrHide(false, true);
164     }
165 
isOpen()166     public boolean isOpen() {
167         return mShow;
168     }
169 }
170