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