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.incallui.hold;
18 
19 import android.os.Bundle;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.Nullable;
22 import android.support.v4.app.Fragment;
23 import android.telephony.PhoneNumberUtils;
24 import android.text.BidiFormatter;
25 import android.text.TextDirectionHeuristics;
26 import android.transition.TransitionManager;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.View.OnAttachStateChangeListener;
30 import android.view.ViewGroup;
31 import android.widget.ImageView;
32 import android.widget.TextView;
33 import com.android.dialer.common.Assert;
34 import com.android.incallui.incall.protocol.SecondaryInfo;
35 
36 /** Shows banner UI for background call */
37 public class OnHoldFragment extends Fragment {
38 
39   private static final String ARG_INFO = "info";
40   private boolean padTopInset = true;
41   private int topInset;
42 
newInstance(@onNull SecondaryInfo info)43   public static OnHoldFragment newInstance(@NonNull SecondaryInfo info) {
44     OnHoldFragment fragment = new OnHoldFragment();
45     Bundle args = new Bundle();
46     args.putParcelable(ARG_INFO, info);
47     fragment.setArguments(args);
48     return fragment;
49   }
50 
51   @Nullable
52   @Override
onCreateView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle)53   public View onCreateView(
54       LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
55     final View view = layoutInflater.inflate(R.layout.incall_on_hold_banner, viewGroup, false);
56 
57     SecondaryInfo secondaryInfo = getArguments().getParcelable(ARG_INFO);
58     secondaryInfo = Assert.isNotNull(secondaryInfo);
59 
60     ((TextView) view.findViewById(R.id.hold_contact_name))
61         .setText(
62             secondaryInfo.nameIsNumber()
63                 ? PhoneNumberUtils.createTtsSpannable(
64                     BidiFormatter.getInstance()
65                         .unicodeWrap(secondaryInfo.name(), TextDirectionHeuristics.LTR))
66                 : secondaryInfo.name());
67     ((ImageView) view.findViewById(R.id.hold_phone_icon))
68         .setImageResource(
69             secondaryInfo.isVideoCall()
70                 ? R.drawable.quantum_ic_videocam_white_18
71                 : R.drawable.quantum_ic_phone_paused_vd_theme_24);
72     view.addOnAttachStateChangeListener(
73         new OnAttachStateChangeListener() {
74           @Override
75           public void onViewAttachedToWindow(View v) {
76             topInset = v.getRootWindowInsets().getSystemWindowInsetTop();
77             applyInset();
78           }
79 
80           @Override
81           public void onViewDetachedFromWindow(View v) {}
82         });
83     return view;
84   }
85 
setPadTopInset(boolean padTopInset)86   public void setPadTopInset(boolean padTopInset) {
87     this.padTopInset = padTopInset;
88     applyInset();
89   }
90 
applyInset()91   private void applyInset() {
92     if (getView() == null) {
93       return;
94     }
95 
96     int newPadding = padTopInset ? topInset : 0;
97     if (newPadding != getView().getPaddingTop()) {
98       TransitionManager.beginDelayedTransition(((ViewGroup) getView().getParent()));
99       getView().setPadding(0, newPadding, 0, 0);
100     }
101   }
102 }
103