OSDN Git Service

61325400846a9dcc8b5a60689d08f117f9e52eb2
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / RoundedCorners.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14
15 package com.android.systemui;
16
17 import static com.android.systemui.tuner.TunablePadding.FLAG_START;
18 import static com.android.systemui.tuner.TunablePadding.FLAG_END;
19
20 import android.app.Fragment;
21 import android.graphics.PixelFormat;
22 import android.support.annotation.VisibleForTesting;
23 import android.util.DisplayMetrics;
24 import android.view.Gravity;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.ViewGroup.LayoutParams;
29 import android.view.WindowManager;
30
31 import com.android.systemui.R.id;
32 import com.android.systemui.fragments.FragmentHostManager;
33 import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
34 import com.android.systemui.plugins.qs.QS;
35 import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment;
36 import com.android.systemui.statusbar.phone.NavigationBarFragment;
37 import com.android.systemui.statusbar.phone.StatusBar;
38 import com.android.systemui.tuner.TunablePadding;
39 import com.android.systemui.tuner.TunerService;
40 import com.android.systemui.tuner.TunerService.Tunable;
41
42 public class RoundedCorners extends SystemUI implements Tunable {
43     public static final String SIZE = "sysui_rounded_size";
44     public static final String PADDING = "sysui_rounded_content_padding";
45
46     private int mRoundedDefault;
47     private View mOverlay;
48     private View mBottomOverlay;
49     private float mDensity;
50     private TunablePadding mQsPadding;
51     private TunablePadding mStatusBarPadding;
52     private TunablePadding mNavBarPadding;
53
54     @Override
55     public void start() {
56         mRoundedDefault = mContext.getResources().getDimensionPixelSize(
57                 R.dimen.rounded_corner_radius);
58         if (mRoundedDefault != 0) {
59             setupRounding();
60         }
61         int padding = mContext.getResources().getDimensionPixelSize(
62                 R.dimen.rounded_corner_content_padding);
63         if (padding != 0) {
64             setupPadding(padding);
65         }
66     }
67
68     private void setupRounding() {
69         mOverlay = LayoutInflater.from(mContext)
70                 .inflate(R.layout.rounded_corners, null);
71         mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
72         mOverlay.findViewById(R.id.right).setRotation(90);
73
74         mContext.getSystemService(WindowManager.class)
75                 .addView(mOverlay, getWindowLayoutParams());
76         mBottomOverlay = LayoutInflater.from(mContext)
77                 .inflate(R.layout.rounded_corners, null);
78         mBottomOverlay.findViewById(R.id.right).setRotation(180);
79         mBottomOverlay.findViewById(R.id.left).setRotation(270);
80         WindowManager.LayoutParams layoutParams = getWindowLayoutParams();
81         layoutParams.gravity = Gravity.BOTTOM;
82         mContext.getSystemService(WindowManager.class)
83                 .addView(mBottomOverlay, layoutParams);
84
85         DisplayMetrics metrics = new DisplayMetrics();
86         mContext.getSystemService(WindowManager.class)
87                 .getDefaultDisplay().getMetrics(metrics);
88         mDensity = metrics.density;
89
90         Dependency.get(TunerService.class).addTunable(this, SIZE);
91     }
92
93     private void setupPadding(int padding) {
94         // Add some padding to all the content near the edge of the screen.
95         StatusBar sb = getComponent(StatusBar.class);
96         View statusBar = sb.getStatusBarWindow();
97
98         TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING,
99                 padding, FLAG_END);
100
101         View navigationBarWindow = sb.getNavigationBarWindow();
102         // Not all devices have on screen navigation bars.
103         if (navigationBarWindow != null) {
104             FragmentHostManager.get(navigationBarWindow).addTagListener(
105                 NavigationBarFragment.TAG,
106                 new TunablePaddingTagListener(padding, 0));
107         }
108
109         FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
110         fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
111                 new TunablePaddingTagListener(padding, R.id.status_bar));
112         fragmentHostManager.addTagListener(QS.TAG,
113                 new TunablePaddingTagListener(padding, R.id.header));
114     }
115
116     private WindowManager.LayoutParams getWindowLayoutParams() {
117         final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
118                 ViewGroup.LayoutParams.MATCH_PARENT,
119                 LayoutParams.WRAP_CONTENT,
120                 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
121                 0
122                     | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
123                     | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
124                     | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
125                     | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
126                     | WindowManager.LayoutParams.FLAG_SLIPPERY
127                     | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
128                     | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
129                 ,
130                 PixelFormat.TRANSLUCENT);
131         lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
132                 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MAGNIFICATION_REGION_EFFECT;
133         lp.setTitle("RoundedOverlay");
134         lp.gravity = Gravity.TOP;
135         return lp;
136     }
137
138
139     @Override
140     public void onTuningChanged(String key, String newValue) {
141         if (mOverlay == null) return;
142         if (SIZE.equals(key)) {
143             int size = mRoundedDefault;
144             try {
145                 size = (int) (Integer.parseInt(newValue) * mDensity);
146             } catch (Exception e) {
147             }
148             setSize(mOverlay.findViewById(R.id.left), size);
149             setSize(mOverlay.findViewById(R.id.right), size);
150             setSize(mBottomOverlay.findViewById(R.id.left), size);
151             setSize(mBottomOverlay.findViewById(R.id.right), size);
152         }
153     }
154
155     private void setSize(View view, int pixelSize) {
156         LayoutParams params = view.getLayoutParams();
157         params.width = pixelSize;
158         params.height = pixelSize;
159         view.setLayoutParams(params);
160     }
161
162     @VisibleForTesting
163     static class TunablePaddingTagListener implements FragmentListener {
164
165         private final int mPadding;
166         private final int mId;
167         private TunablePadding mTunablePadding;
168
169         public TunablePaddingTagListener(int padding, int id) {
170             mPadding = padding;
171             mId = id;
172         }
173
174         @Override
175         public void onFragmentViewCreated(String tag, Fragment fragment) {
176             if (mTunablePadding != null) {
177                 mTunablePadding.destroy();
178             }
179             View view = fragment.getView();
180             if (mId != 0) {
181                 view = view.findViewById(mId);
182             }
183             mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
184                     FLAG_START | FLAG_END);
185         }
186     }
187 }