OSDN Git Service

Update nav bar layout for universal grid
[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         FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
102         fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
103                 new TunablePaddingTagListener(padding, R.id.status_bar));
104         fragmentHostManager.addTagListener(QS.TAG,
105                 new TunablePaddingTagListener(padding, R.id.header));
106     }
107
108     private WindowManager.LayoutParams getWindowLayoutParams() {
109         final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
110                 ViewGroup.LayoutParams.MATCH_PARENT,
111                 LayoutParams.WRAP_CONTENT,
112                 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
113                 0
114                     | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
115                     | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
116                     | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
117                     | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
118                     | WindowManager.LayoutParams.FLAG_SLIPPERY
119                     | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
120                     | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
121                 ,
122                 PixelFormat.TRANSLUCENT);
123         lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
124                 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MAGNIFICATION_REGION_EFFECT;
125         lp.setTitle("RoundedOverlay");
126         lp.gravity = Gravity.TOP;
127         return lp;
128     }
129
130
131     @Override
132     public void onTuningChanged(String key, String newValue) {
133         if (mOverlay == null) return;
134         if (SIZE.equals(key)) {
135             int size = mRoundedDefault;
136             try {
137                 size = (int) (Integer.parseInt(newValue) * mDensity);
138             } catch (Exception e) {
139             }
140             setSize(mOverlay.findViewById(R.id.left), size);
141             setSize(mOverlay.findViewById(R.id.right), size);
142             setSize(mBottomOverlay.findViewById(R.id.left), size);
143             setSize(mBottomOverlay.findViewById(R.id.right), size);
144         }
145     }
146
147     private void setSize(View view, int pixelSize) {
148         LayoutParams params = view.getLayoutParams();
149         params.width = pixelSize;
150         params.height = pixelSize;
151         view.setLayoutParams(params);
152     }
153
154     @VisibleForTesting
155     static class TunablePaddingTagListener implements FragmentListener {
156
157         private final int mPadding;
158         private final int mId;
159         private TunablePadding mTunablePadding;
160
161         public TunablePaddingTagListener(int padding, int id) {
162             mPadding = padding;
163             mId = id;
164         }
165
166         @Override
167         public void onFragmentViewCreated(String tag, Fragment fragment) {
168             if (mTunablePadding != null) {
169                 mTunablePadding.destroy();
170             }
171             View view = fragment.getView();
172             if (mId != 0) {
173                 view = view.findViewById(mId);
174             }
175             mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
176                     FLAG_START | FLAG_END);
177         }
178     }
179 }