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 
17 package android.support.design.widget;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.Bundle;
22 import android.support.annotation.LayoutRes;
23 import android.support.annotation.NonNull;
24 import android.support.annotation.StyleRes;
25 import android.support.design.R;
26 import android.support.v7.app.AppCompatDialog;
27 import android.util.TypedValue;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.Window;
31 import android.widget.FrameLayout;
32 
33 /**
34  * Base class for {@link android.app.Dialog}s styled as a bottom sheet.
35  */
36 public class BottomSheetDialog extends AppCompatDialog {
37 
BottomSheetDialog(@onNull Context context)38     public BottomSheetDialog(@NonNull Context context) {
39         this(context, 0);
40     }
41 
BottomSheetDialog(@onNull Context context, @StyleRes int theme)42     public BottomSheetDialog(@NonNull Context context, @StyleRes int theme) {
43         super(context, getThemeResId(context, theme));
44         // We hide the title bar for any style configuration. Otherwise, there will be a gap
45         // above the bottom sheet when it is expanded.
46         supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
47     }
48 
BottomSheetDialog(@onNull Context context, boolean cancelable, OnCancelListener cancelListener)49     protected BottomSheetDialog(@NonNull Context context, boolean cancelable,
50             OnCancelListener cancelListener) {
51         super(context, cancelable, cancelListener);
52         supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
53     }
54 
55     @Override
setContentView(@ayoutRes int layoutResId)56     public void setContentView(@LayoutRes int layoutResId) {
57         super.setContentView(wrapInBottomSheet(layoutResId, null, null));
58     }
59 
60     @Override
onCreate(Bundle savedInstanceState)61     protected void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         getWindow().setLayout(
64                 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
65     }
66 
67     @Override
setContentView(View view)68     public void setContentView(View view) {
69         super.setContentView(wrapInBottomSheet(0, view, null));
70     }
71 
72     @Override
setContentView(View view, ViewGroup.LayoutParams params)73     public void setContentView(View view, ViewGroup.LayoutParams params) {
74         super.setContentView(wrapInBottomSheet(0, view, params));
75     }
76 
wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params)77     private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
78         final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
79                 R.layout.design_bottom_sheet_dialog, null);
80         if (layoutResId != 0 && view == null) {
81             view = getLayoutInflater().inflate(layoutResId, coordinator, false);
82         }
83         FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
84         BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
85         if (params == null) {
86             bottomSheet.addView(view);
87         } else {
88             bottomSheet.addView(view, params);
89         }
90         // We treat the CoordinatorLayout as outside the dialog though it is technically inside
91         if (shouldWindowCloseOnTouchOutside()) {
92             coordinator.findViewById(R.id.touch_outside).setOnClickListener(
93                     new View.OnClickListener() {
94                         @Override
95                         public void onClick(View view) {
96                             if (isShowing()) {
97                                 cancel();
98                             }
99                         }
100                     });
101         }
102         return coordinator;
103     }
104 
shouldWindowCloseOnTouchOutside()105     private boolean shouldWindowCloseOnTouchOutside() {
106         if (Build.VERSION.SDK_INT < 11) {
107             return true;
108         }
109         TypedValue value = new TypedValue();
110         //noinspection SimplifiableIfStatement
111         if (getContext().getTheme()
112                 .resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)) {
113             return value.data != 0;
114         }
115         return false;
116     }
117 
getThemeResId(Context context, int themeId)118     private static int getThemeResId(Context context, int themeId) {
119         if (themeId == 0) {
120             // If the provided theme is 0, then retrieve the dialogTheme from our theme
121             TypedValue outValue = new TypedValue();
122             if (context.getTheme().resolveAttribute(
123                     R.attr.bottomSheetDialogTheme, outValue, true)) {
124                 themeId = outValue.resourceId;
125             } else {
126                 // bottomSheetDialogTheme is not provided; we default to our light theme
127                 themeId = R.style.Theme_Design_Light_BottomSheetDialog;
128             }
129         }
130         return themeId;
131     }
132 
133     private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback
134             = new BottomSheetBehavior.BottomSheetCallback() {
135         @Override
136         public void onStateChanged(@NonNull View bottomSheet,
137                 @BottomSheetBehavior.State int newState) {
138             if (newState == BottomSheetBehavior.STATE_HIDDEN) {
139                 dismiss();
140             }
141         }
142 
143         @Override
144         public void onSlide(@NonNull View bottomSheet, float slideOffset) {
145         }
146     };
147 
148 }
149