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.answer.impl;
18 
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.res.TypedArray;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.support.annotation.Nullable;
26 import android.support.design.widget.BottomSheetDialogFragment;
27 import android.view.ContextThemeWrapper;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.view.ViewGroup.LayoutParams;
33 import android.view.WindowManager;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
36 import com.android.dialer.common.DpUtil;
37 import com.android.dialer.common.FragmentUtils;
38 import com.android.dialer.common.LogUtil;
39 import com.android.incallui.incalluilock.InCallUiLock;
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 /** Shows options for rejecting call with SMS */
44 public class SmsBottomSheetFragment extends BottomSheetDialogFragment {
45 
46   private static final String ARG_OPTIONS = "options";
47 
48   private InCallUiLock inCallUiLock;
49 
newInstance(@ullable ArrayList<CharSequence> options)50   public static SmsBottomSheetFragment newInstance(@Nullable ArrayList<CharSequence> options) {
51     SmsBottomSheetFragment fragment = new SmsBottomSheetFragment();
52     Bundle args = new Bundle();
53     args.putCharSequenceArrayList(ARG_OPTIONS, options);
54     fragment.setArguments(args);
55     return fragment;
56   }
57 
58   @Nullable
59   @Override
onCreateView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle)60   public View onCreateView(
61       LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
62     LinearLayout layout = new LinearLayout(getContext());
63     layout.setOrientation(LinearLayout.VERTICAL);
64     List<CharSequence> items = getArguments().getCharSequenceArrayList(ARG_OPTIONS);
65     if (items != null) {
66       for (CharSequence item : items) {
67         layout.addView(newTextViewItem(item));
68       }
69     }
70     layout.addView(newTextViewItem(null));
71     layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
72     return layout;
73   }
74 
75   @Override
onAttach(Context context)76   public void onAttach(Context context) {
77     super.onAttach(context);
78     FragmentUtils.checkParent(this, SmsSheetHolder.class);
79   }
80 
81   @Override
onCreateDialog(final Bundle savedInstanceState)82   public Dialog onCreateDialog(final Bundle savedInstanceState) {
83     LogUtil.i("SmsBottomSheetFragment.onCreateDialog", null);
84     Dialog dialog = super.onCreateDialog(savedInstanceState);
85     dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
86 
87     inCallUiLock =
88         FragmentUtils.getParentUnsafe(SmsBottomSheetFragment.this, SmsSheetHolder.class)
89             .acquireInCallUiLock("SmsBottomSheetFragment");
90     return dialog;
91   }
92 
newTextViewItem(@ullable final CharSequence text)93   private TextView newTextViewItem(@Nullable final CharSequence text) {
94     int[] attrs = new int[] {android.R.attr.selectableItemBackground};
95     Context context = new ContextThemeWrapper(getContext(), getTheme());
96     TypedArray typedArray = context.obtainStyledAttributes(attrs);
97     Drawable background = typedArray.getDrawable(0);
98     // noinspection ResourceType
99     typedArray.recycle();
100 
101     TextView textView = new TextView(context);
102     textView.setText(text == null ? getString(R.string.call_incoming_message_custom) : text);
103     int padding = (int) DpUtil.dpToPx(context, 16);
104     textView.setPadding(padding, padding, padding, padding);
105     textView.setBackground(background);
106     textView.setTextColor(context.getColor(R.color.blue_grey_100));
107     textView.setTextAppearance(R.style.TextAppearance_AppCompat_Widget_PopupMenu_Large);
108 
109     LayoutParams params =
110         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
111     textView.setLayoutParams(params);
112 
113     textView.setOnClickListener(
114         new OnClickListener() {
115           @Override
116           public void onClick(View v) {
117             FragmentUtils.getParentUnsafe(SmsBottomSheetFragment.this, SmsSheetHolder.class)
118                 .smsSelected(text);
119             dismiss();
120           }
121         });
122     return textView;
123   }
124 
125   @Override
getTheme()126   public int getTheme() {
127     return R.style.Theme_Design_Light_BottomSheetDialog;
128   }
129 
130   @Override
onDismiss(DialogInterface dialogInterface)131   public void onDismiss(DialogInterface dialogInterface) {
132     super.onDismiss(dialogInterface);
133     FragmentUtils.getParentUnsafe(this, SmsSheetHolder.class).smsDismissed();
134     inCallUiLock.release();
135   }
136 
137   /** Callback interface for {@link SmsBottomSheetFragment} */
138   public interface SmsSheetHolder {
139 
acquireInCallUiLock(String tag)140     InCallUiLock acquireInCallUiLock(String tag);
141 
smsSelected(@ullable CharSequence text)142     void smsSelected(@Nullable CharSequence text);
143 
smsDismissed()144     void smsDismissed();
145   }
146 }
147