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.support.v4.util.Pair;
20 import android.text.TextUtils;
21 
22 import com.android.messaging.Factory;
23 import com.android.messaging.R;
24 import com.android.messaging.datamodel.data.SubscriptionListData;
25 import com.android.messaging.datamodel.data.SubscriptionListData.SubscriptionListEntry;
26 import com.android.messaging.ui.conversation.SimSelectorView.SimSelectorViewListener;
27 import com.android.messaging.util.AccessibilityUtil;
28 import com.android.messaging.util.Assert;
29 import com.android.messaging.util.OsUtil;
30 import com.android.messaging.util.ThreadUtil;
31 
32 /**
33  * Manages showing/hiding the SIM selector in conversation.
34  */
35 abstract class ConversationSimSelector extends ConversationInput {
36     private SimSelectorView mSimSelectorView;
37     private Pair<Boolean /* show */, Boolean /* animate */> mPendingShow;
38     private boolean mDataReady;
39     private String mSelectedSimText;
40 
ConversationSimSelector(ConversationInputBase baseHost)41     public ConversationSimSelector(ConversationInputBase baseHost) {
42         super(baseHost, false);
43     }
44 
onSubscriptionListDataLoaded(final SubscriptionListData subscriptionListData)45     public void onSubscriptionListDataLoaded(final SubscriptionListData subscriptionListData) {
46         ensureSimSelectorView();
47         mSimSelectorView.bind(subscriptionListData);
48         mDataReady = subscriptionListData != null && subscriptionListData.hasData();
49         if (mPendingShow != null && mDataReady) {
50             Assert.isTrue(OsUtil.isAtLeastL_MR1());
51             final boolean show = mPendingShow.first;
52             final boolean animate = mPendingShow.second;
53             ThreadUtil.getMainThreadHandler().post(new Runnable() {
54                 @Override
55                 public void run() {
56                     // This will No-Op if we are no longer attached to the host.
57                     mConversationInputBase.showHideInternal(ConversationSimSelector.this,
58                             show, animate);
59                 }
60             });
61             mPendingShow = null;
62         }
63     }
64 
announcedSelectedSim()65     private void announcedSelectedSim() {
66         final Context context = Factory.get().getApplicationContext();
67         if (AccessibilityUtil.isTouchExplorationEnabled(context) &&
68                 !TextUtils.isEmpty(mSelectedSimText)) {
69             AccessibilityUtil.announceForAccessibilityCompat(
70                     mSimSelectorView, null,
71                     context.getString(R.string.selected_sim_content_message, mSelectedSimText));
72         }
73     }
74 
setSelected(final SubscriptionListEntry subEntry)75     public void setSelected(final SubscriptionListEntry subEntry) {
76         mSelectedSimText = subEntry == null ? null : subEntry.displayName;
77     }
78 
79     @Override
show(boolean animate)80     public boolean show(boolean animate) {
81         announcedSelectedSim();
82         return showHide(true, animate);
83     }
84 
85     @Override
hide(boolean animate)86     public boolean hide(boolean animate) {
87         return showHide(false, animate);
88     }
89 
showHide(final boolean show, final boolean animate)90     private boolean showHide(final boolean show, final boolean animate) {
91         if (!OsUtil.isAtLeastL_MR1()) {
92             return false;
93         }
94 
95         if (mDataReady) {
96             mSimSelectorView.showOrHide(show, animate);
97             return mSimSelectorView.isOpen() == show;
98         } else {
99             mPendingShow = Pair.create(show, animate);
100             return false;
101         }
102     }
103 
ensureSimSelectorView()104     private void ensureSimSelectorView() {
105         if (mSimSelectorView == null) {
106             // Grab the SIM selector view from the host. This class assumes ownership of it.
107             mSimSelectorView = getSimSelectorView();
108             mSimSelectorView.setItemLayoutId(getSimSelectorItemLayoutId());
109             mSimSelectorView.setListener(new SimSelectorViewListener() {
110 
111                 @Override
112                 public void onSimSelectorVisibilityChanged(boolean visible) {
113                     onVisibilityChanged(visible);
114                 }
115 
116                 @Override
117                 public void onSimItemClicked(SubscriptionListEntry item) {
118                     selectSim(item);
119                 }
120             });
121         }
122     }
123 
getSimSelectorView()124     protected abstract SimSelectorView getSimSelectorView();
selectSim(final SubscriptionListEntry item)125     protected abstract void selectSim(final SubscriptionListEntry item);
getSimSelectorItemLayoutId()126     protected abstract int getSimSelectorItemLayoutId();
127 
128 }
129