OSDN Git Service

Couple more fixes
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / ui / ViewParams.java
1 /* Copyright 2019 Braden Farmer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * 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
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package com.farmerbb.taskbar.ui;
17
18 import android.graphics.PixelFormat;
19 import android.view.WindowManager;
20
21 import com.farmerbb.taskbar.util.U;
22
23 import java.lang.reflect.Field;
24
25 public class ViewParams {
26     public int width;
27     public int height;
28     public int gravity;
29     public int flags;
30     public int bottomMargin;
31
32     public ViewParams(int width, int height, int gravity, int flags, int bottomMargin) {
33         this.width = width;
34         this.height = height;
35         this.gravity = gravity;
36         this.flags = flags;
37         this.bottomMargin = bottomMargin;
38     }
39
40     public WindowManager.LayoutParams toWindowManagerParams() {
41         final WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(
42                 width,
43                 height,
44                 U.getOverlayType(),
45                 flags,
46                 PixelFormat.TRANSLUCENT
47         );
48
49         if(gravity > -1)
50             wmParams.gravity = gravity;
51
52         if(bottomMargin > -1)
53             wmParams.y = bottomMargin;
54
55         U.allowReflection();
56         try {
57             Class<?> layoutParamsClass = Class.forName("android.view.WindowManager$LayoutParams");
58
59             Field privateFlags = layoutParamsClass.getField("privateFlags");
60             Field noAnim = layoutParamsClass.getField("PRIVATE_FLAG_NO_MOVE_ANIMATION");
61
62             int privateFlagsValue = privateFlags.getInt(wmParams);
63             int noAnimFlag = noAnim.getInt(wmParams);
64             privateFlagsValue |= noAnimFlag;
65
66             privateFlags.setInt(wmParams, privateFlagsValue);
67         } catch (Exception ignored) {}
68
69         return wmParams;
70     }
71
72     public ViewParams updateWidth(int width) {
73         return new ViewParams(width, height, gravity, flags, bottomMargin);
74     }
75
76     public ViewParams updateHeight(int height) {
77         return new ViewParams(width, height, gravity, flags, bottomMargin);
78     }
79
80     public ViewParams updateBottomMargin(int bottomMargin) {
81         return new ViewParams(width, height, gravity, flags, bottomMargin);
82     }
83 }