Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit ceb394d

Browse files
authored
app/PopupMenu: add blur effect support for OUI 4 devices (#96)
* app/PopupMenu: add blur effect support for OUI 4 devices Signed-off-by: BlackMesa123 <giangrecosalvo9@gmail.com> * app/PopupMenu: enable blur effect by default only with OUI4 theme set Signed-off-by: BlackMesa123 <giangrecosalvo9@gmail.com>
1 parent 4316db1 commit ceb394d

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

yanndroid/oneui/src/main/java/androidx/reflect/SeslBaseReflector.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.util.Log;
44

5+
import java.lang.reflect.Constructor;
56
import java.lang.reflect.Field;
67
import java.lang.reflect.InvocationTargetException;
78
import java.lang.reflect.Method;
@@ -203,4 +204,13 @@ public static void set(Object obj, Field field, Object obj2) {
203204
Log.e(TAG, field.getName() + " IllegalArgumentException", e2);
204205
}
205206
}
207+
208+
public static Constructor<?> getConstructor(String str, Class<?>... clsArr) {
209+
try {
210+
return Class.forName(str).getDeclaredConstructor(clsArr);
211+
} catch (ClassNotFoundException | NoSuchMethodException e) {
212+
e.printStackTrace();
213+
return null;
214+
}
215+
}
206216
}

yanndroid/oneui/src/main/java/androidx/reflect/provider/SeslSystemReflector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ public static String getField_SEM_PEN_HOVERING() {
2727
}
2828
return obj instanceof String ? (String) obj : "pen_hovering";
2929
}
30+
31+
public static String getField_SEM_ACCESSIBILITY_REDUCE_TRANSPARENCY() {
32+
Method declaredMethod;
33+
Object obj = null;
34+
if (Build.VERSION.SDK_INT >= 31 && (declaredMethod = SeslBaseReflector.getDeclaredMethod(mClass, "hidden_SEM_ACCESSIBILITY_REDUCE_TRANSPARENCY", new Class[0])) != null) {
35+
obj = SeslBaseReflector.invoke(null, declaredMethod, new Object[0]);
36+
}
37+
return obj instanceof String ? (String) obj : "not_supported";
38+
}
3039
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package androidx.reflect.view;
2+
3+
import android.os.Build;
4+
import android.util.Log;
5+
import android.view.View;
6+
7+
import androidx.reflect.SeslBaseReflector;
8+
9+
import java.lang.reflect.Constructor;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
13+
public class SeslSemBlurInfoReflector {
14+
private static final String TAG = "SeslSemBlurInfoReflector";
15+
private static final String mBuilderClass = "android.view.SemBlurInfo$Builder";
16+
17+
private SeslSemBlurInfoReflector() {
18+
}
19+
20+
public static Object semCreateBlurBuilder(int i) {
21+
Constructor<?> constructor = SeslBaseReflector.getConstructor(mBuilderClass, Integer.TYPE);
22+
if (Build.VERSION.SDK_INT >= 31 && constructor != null) {
23+
try {
24+
return constructor.newInstance(Integer.valueOf(i));
25+
} catch (IllegalAccessException e) {
26+
Log.e(TAG, "semCreateBlurBuilder IllegalAccessException", e);
27+
} catch (InstantiationException e2) {
28+
Log.e(TAG, "semCreateBlurBuilder InstantiationException", e2);
29+
} catch (InvocationTargetException e3) {
30+
Log.e(TAG, "semCreateBlurBuilder InvocationTargetException", e3);
31+
}
32+
}
33+
return null;
34+
}
35+
36+
public static Object semSetBuilderBlurRadius(Object obj, int i) {
37+
Method declaredMethod = Build.VERSION.SDK_INT >= 31 ? SeslBaseReflector.getDeclaredMethod(mBuilderClass, "hidden_setRadius", Integer.TYPE) : null;
38+
if (declaredMethod != null) {
39+
declaredMethod.setAccessible(true);
40+
SeslBaseReflector.invoke(obj, declaredMethod, Integer.valueOf(i));
41+
}
42+
return obj;
43+
}
44+
45+
public static Object semSetBuilderBlurBackgroundColor(Object obj, int i) {
46+
Method declaredMethod = Build.VERSION.SDK_INT >= 31 ? SeslBaseReflector.getDeclaredMethod(mBuilderClass, "hidden_setBackgroundColor", Integer.TYPE) : null;
47+
if (declaredMethod != null) {
48+
declaredMethod.setAccessible(true);
49+
SeslBaseReflector.invoke(obj, declaredMethod, Integer.valueOf(i));
50+
}
51+
return obj;
52+
}
53+
54+
public static Object semSetBuilderBlurBackgroundCornerRadius(Object obj, float f) {
55+
Method declaredMethod = Build.VERSION.SDK_INT >= 31 ? SeslBaseReflector.getDeclaredMethod(mBuilderClass, "hidden_setBackgroundCornerRadius", Float.TYPE) : null;
56+
if (declaredMethod != null) {
57+
declaredMethod.setAccessible(true);
58+
SeslBaseReflector.invoke(obj, declaredMethod, Float.valueOf(f));
59+
}
60+
return obj;
61+
}
62+
63+
public static void semBuildSetBlurInfo(Object obj, View view) {
64+
Method declaredMethod = Build.VERSION.SDK_INT >= 31 ? SeslBaseReflector.getDeclaredMethod(mBuilderClass, "hidden_build", new Class[0]) : null;
65+
if (declaredMethod != null) {
66+
declaredMethod.setAccessible(true);
67+
SeslViewReflector.semSetBlurInfo(view, SeslBaseReflector.invoke(obj, declaredMethod, new Object[0]));
68+
}
69+
}
70+
}

yanndroid/oneui/src/main/java/androidx/reflect/view/SeslViewReflector.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.graphics.Rect;
44
import android.os.Build;
5+
import android.util.Log;
56
import android.view.PointerIcon;
67
import android.view.View;
78

@@ -11,6 +12,7 @@
1112
import java.lang.reflect.Method;
1213

1314
public class SeslViewReflector {
15+
private static final String TAG = "SeslViewReflector";
1416
private static final Class<?> mClass = View.class;
1517

1618
private SeslViewReflector() {
@@ -165,6 +167,19 @@ public static boolean isHoveringUIEnabled(View view) {
165167
return false;
166168
}
167169

170+
public static void semSetBlurInfo(View view, Object obj) {
171+
if (Build.VERSION.SDK_INT >= 31) {
172+
try {
173+
Method declaredMethod = SeslBaseReflector.getDeclaredMethod(mClass, "hidden_semSetBlurInfo", Class.forName("android.view.SemBlurInfo"));
174+
if (declaredMethod != null) {
175+
SeslBaseReflector.invoke(view, declaredMethod, obj);
176+
}
177+
} catch (ClassNotFoundException e) {
178+
Log.e(TAG, "semSetBlurInfo ClassNotFoundException", e);
179+
}
180+
}
181+
}
182+
168183
public static void semSetPointerIcon(View view, int i, PointerIcon pointerIcon) {
169184
Method method;
170185
if (Build.VERSION.SDK_INT >= 29) {

yanndroid/oneui/src/main/java/de/dlyt/yanndroid/oneui/menu/PopupMenu.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.Typeface;
5+
import android.provider.Settings;
56
import android.util.TypedValue;
67
import android.view.Gravity;
78
import android.view.KeyEvent;
@@ -15,6 +16,8 @@
1516

1617
import androidx.annotation.MenuRes;
1718
import androidx.core.content.res.ResourcesCompat;
19+
import androidx.reflect.provider.SeslSystemReflector;
20+
import androidx.reflect.view.SeslSemBlurInfoReflector;
1821

1922
import java.util.ArrayList;
2023

@@ -38,6 +41,7 @@ public class PopupMenu {
3841
private CharSequence title;
3942

4043
private int mAnimationStyleRes = 0;
44+
private boolean mBlurEffectEnabled;
4145
private boolean mGroupDividerEnabled;
4246

4347
private int lastGroupId = 0;
@@ -48,6 +52,7 @@ public PopupMenu(View anchor) {
4852
this.context = anchor.getContext();
4953
this.anchor = anchor;
5054
mIsOneUI4 = context.getTheme().obtainStyledAttributes(new int[]{R.attr.isOneUI4}).getBoolean(0, false);
55+
mBlurEffectEnabled = mIsOneUI4;
5156
}
5257

5358
public interface PopupMenuListener {
@@ -178,6 +183,29 @@ private TextView createTitleView() {
178183
return titleView;
179184
}
180185

186+
private boolean isReduceTransparencySettingsEnabled() {
187+
String reduceTransparencyFlag = SeslSystemReflector.getField_SEM_ACCESSIBILITY_REDUCE_TRANSPARENCY();
188+
return !reduceTransparencyFlag.equals("not_supported") && Settings.System.getInt(context.getContentResolver(), reduceTransparencyFlag, 0) == 1;
189+
}
190+
191+
private void setupBlurEffect() {
192+
if (mBlurEffectEnabled) {
193+
if (popupWindow.getContentView() != null && context != null) {
194+
Object blurBuilder = SeslSemBlurInfoReflector.semCreateBlurBuilder(0 /* SemBlurInfo.BLUR_MODE_WINDOW */);
195+
196+
if (blurBuilder != null && !isReduceTransparencySettingsEnabled()) {
197+
SeslSemBlurInfoReflector.semSetBuilderBlurRadius(blurBuilder, 120);
198+
SeslSemBlurInfoReflector.semSetBuilderBlurBackgroundColor(blurBuilder, context.getResources().getColor(R.color.sesl_popup_menu_blur_background));
199+
SeslSemBlurInfoReflector.semSetBuilderBlurBackgroundCornerRadius(blurBuilder, context.getResources().getDimension(R.dimen.sesl_menu_popup_corner_radius));
200+
SeslSemBlurInfoReflector.semBuildSetBlurInfo(blurBuilder, popupWindow.getContentView());
201+
if (listView != null) {
202+
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
203+
}
204+
}
205+
}
206+
}
207+
}
208+
181209
private boolean shouldShowGroupDivider(MenuItem menuItem) {
182210
if (mGroupDividerEnabled) {
183211
if (menuItem.getGroupId() == lastGroupId) {
@@ -228,6 +256,7 @@ public void show() {
228256
public void show(int xoff, int yoff) {
229257
this.xoff = xoff;
230258
this.yoff = yoff;
259+
setupBlurEffect();
231260
popupWindow.showAsDropDown(anchor, xoff, yoff);
232261
updatePopupSize();
233262
((View) ReflectUtils.genericGetField(popupWindow, "mBackgroundView")).setClipToOutline(true);
@@ -240,6 +269,10 @@ public void setAnimationStyle(int animationStyle) {
240269
}
241270
}
242271

272+
public void setBlurEffectEnabled(boolean enabled) {
273+
mBlurEffectEnabled = enabled;
274+
}
275+
243276
public void setGroupDividerEnabled(boolean enabled) {
244277
mGroupDividerEnabled = enabled;
245278

yanndroid/oneui/src/main/res/values-night/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<color name="sesl_menu_popup_background_stroke_color">#ff252525</color>
7777
<color name="sesl4_menu_popup_background_color">#ff3a3a3a</color>
7878
<color name="sesl4_menu_popup_background_stroke_color">#ff3a3a3a</color>
79+
<color name="sesl_popup_menu_blur_background">#d6252525</color>
7980
<color name="sesl_spinner_dropdown_item_text_color_normal">#fffafafa</color>
8081
<color name="sesl_spinner_dropdown_item_text_color_disabled">#66fafafa</color>
8182
<color name="sesl_spinner_icon_color_default">#ffd9d9d9</color>

yanndroid/oneui/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<color name="sesl_menu_popup_background_stroke_color">#fffcfcfc</color>
136136
<color name="sesl4_menu_popup_background_color">#fffcfcfc</color>
137137
<color name="sesl4_menu_popup_background_stroke_color">#fffcfcfc</color>
138+
<color name="sesl_popup_menu_blur_background">#e6fcfcfc</color>
138139
<color name="sesl_popup_menu_item_text_color_checked">?colorPrimaryDark</color>
139140
<color name="sesl_spinner_dropdown_item_checkmark">?colorPrimaryDark</color>
140141
<color name="sesl_spinner_dropdown_item_text_color_disabled">#66000000</color>

0 commit comments

Comments
 (0)