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