1 /*
2  * Copyright (C) 2014 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 
18 package android.support.v4.widget;
19 
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.graphics.drawable.Drawable;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.WindowInsets;
27 
28 /**
29  * Provides functionality for DrawerLayout unique to API 21
30  */
31 class DrawerLayoutCompatApi21 {
32 
33     private static final int[] THEME_ATTRS = {
34             android.R.attr.colorPrimaryDark
35     };
36 
configureApplyInsets(View drawerLayout)37     public static void configureApplyInsets(View drawerLayout) {
38         if (drawerLayout instanceof DrawerLayoutImpl) {
39             drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
40             drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
41                     | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
42         }
43     }
44 
dispatchChildInsets(View child, Object insets, int gravity)45     public static void dispatchChildInsets(View child, Object insets, int gravity) {
46         WindowInsets wi = (WindowInsets) insets;
47         if (gravity == Gravity.LEFT) {
48             wi = wi.replaceSystemWindowInsets(wi.getSystemWindowInsetLeft(),
49                     wi.getSystemWindowInsetTop(), 0, wi.getSystemWindowInsetBottom());
50         } else if (gravity == Gravity.RIGHT) {
51             wi = wi.replaceSystemWindowInsets(0, wi.getSystemWindowInsetTop(),
52                     wi.getSystemWindowInsetRight(), wi.getSystemWindowInsetBottom());
53         }
54         child.dispatchApplyWindowInsets(wi);
55     }
56 
applyMarginInsets(ViewGroup.MarginLayoutParams lp, Object insets, int gravity)57     public static void applyMarginInsets(ViewGroup.MarginLayoutParams lp, Object insets,
58             int gravity) {
59         WindowInsets wi = (WindowInsets) insets;
60         if (gravity == Gravity.LEFT) {
61             wi = wi.replaceSystemWindowInsets(wi.getSystemWindowInsetLeft(),
62                     wi.getSystemWindowInsetTop(), 0, wi.getSystemWindowInsetBottom());
63         } else if (gravity == Gravity.RIGHT) {
64             wi = wi.replaceSystemWindowInsets(0, wi.getSystemWindowInsetTop(),
65                     wi.getSystemWindowInsetRight(), wi.getSystemWindowInsetBottom());
66         }
67         lp.leftMargin = wi.getSystemWindowInsetLeft();
68         lp.topMargin = wi.getSystemWindowInsetTop();
69         lp.rightMargin = wi.getSystemWindowInsetRight();
70         lp.bottomMargin = wi.getSystemWindowInsetBottom();
71     }
72 
getTopInset(Object insets)73     public static int getTopInset(Object insets) {
74         return insets != null ? ((WindowInsets) insets).getSystemWindowInsetTop() : 0;
75     }
76 
getDefaultStatusBarBackground(Context context)77     public static Drawable getDefaultStatusBarBackground(Context context) {
78         final TypedArray a = context.obtainStyledAttributes(THEME_ATTRS);
79         try {
80             return a.getDrawable(0);
81         } finally {
82             a.recycle();
83         }
84     }
85 
86     static class InsetsListener implements View.OnApplyWindowInsetsListener {
87         @Override
onApplyWindowInsets(View v, WindowInsets insets)88         public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
89             final DrawerLayoutImpl drawerLayout = (DrawerLayoutImpl) v;
90             drawerLayout.setChildInsets(insets, insets.getSystemWindowInsetTop() > 0);
91             return insets.consumeSystemWindowInsets();
92         }
93     }
94 }
95