Skip to content

Commit

Permalink
feat: Enable EdgeToEdge on the Egg pages
Browse files Browse the repository at this point in the history
Signed-off-by: Hu Shenghao <[email protected]>
  • Loading branch information
hushenghao committed Jan 10, 2025
1 parent 3493249 commit 3e0b5a9
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 82 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### v3.4.0

- EdgeToEdge mode is enabled on the Easter Egg pages
- Upgrade project dependencies

### v3.3.0 (2024-12-20)

- Add Rocket Launcher Easter Egg [#286](https://github.com/hushenghao/AndroidEasterEggs/issues/286)
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG_zh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 更新日志

### v3.4.0

- 彩蛋相关页面开启了 EdgeToEdge 模式
- 升级项目依赖项

### v3.3.0 (2024-12-20)

- 添加 Rocket Launcher 彩蛋 [#286](https://github.com/hushenghao/AndroidEasterEggs/issues/286)
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {

defaultConfig {
applicationId = "com.dede.android_eggs"
versionCode = 57
versionName = "3.3.0"
versionCode = 58
versionName = "3.4.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

resourceConfigurations += listOf(
Expand Down
35 changes: 0 additions & 35 deletions app/src/main/java/com/dede/android_eggs/util/EdgeUtils.kt

This file was deleted.

3 changes: 2 additions & 1 deletion basic/consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

# EdgeToEdgeCompat
-keep class androidx.activity.EdgeToEdgeImpl { *; }
-keep class * extends androidx.activity.EdgeToEdgeImpl { *; }
-keep class * extends androidx.activity.EdgeToEdgeImpl { *; }
-keep class com.google.android.material.internal.EdgeToEdgeUtils { *; }
57 changes: 47 additions & 10 deletions basic/src/main/java/androidx/activity/EdgeToEdgeCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.view.WindowInsetsCompat;

import com.dede.basic.utils.DynamicObjectUtils;
import com.dede.basic.utils.dynamic.DynamicResult;

import org.jetbrains.annotations.Nullable;

/**
* EdgeToEdge compat
*
* @see EdgeToEdge
*/
public class EdgeToEdgeCompat {

public static final int EDGE_INSETS_MASK = WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout();

private static final String TAG = "EdgeToEdgeCompat";

@Nullable
private final static EdgeToEdgeImpl impl = createEdgeToEdgeImpl();

/**
Expand All @@ -30,7 +33,6 @@ public class EdgeToEdgeCompat {
* @return EdgeToEdgeImpl instance
* @see EdgeToEdge#enable(ComponentActivity)
*/
@Nullable
private static EdgeToEdgeImpl createEdgeToEdgeImpl() {
String className = "androidx.activity.EdgeToEdgeBase";
final int[] apis = {
Expand All @@ -43,17 +45,57 @@ private static EdgeToEdgeImpl createEdgeToEdgeImpl() {
};
for (int api : apis) {
if (Build.VERSION.SDK_INT >= api) {
className = "androidx.activity.EdgeToEdgeApi" + api;
className = "androidx.activity.EdgeToEdgeApi1" + api;
break;
}
}
DynamicResult dynamicResult = DynamicObjectUtils.asDynamicObject(className)
.newInstance(new Class[0], new Object[0]);
EdgeToEdgeImpl impl = DynamicResult.getTypeValue(dynamicResult, EdgeToEdgeImpl.class);
if (impl == null) {
impl = new MaterialEdgeToEdgeUtilsImpl();
}
Log.i(TAG, "EdgeToEdgeImpl: " + impl);
return impl;
}

private static class MaterialEdgeToEdgeUtilsImpl implements EdgeToEdgeImpl {

private static int getScrimColor(SystemBarStyle systemBarStyle, boolean isDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// support dark mode
return systemBarStyle.getScrimWithEnforcedContrast$activity_release(isDark);
} else {
return systemBarStyle.getScrim$activity_release(isDark);
}
}

@Override
public void setUp(@NonNull SystemBarStyle statusBarStyle,
@NonNull SystemBarStyle navigationBarStyle,
@NonNull Window window,
@NonNull View view,
boolean statusBarIsDark,
boolean navigationBarIsDark) {
DynamicObjectUtils.asDynamicObject("com.google.android.material.internal.EdgeToEdgeUtils")
.invokeMethod("applyEdgeToEdge",
new Class[]{Window.class, boolean.class, Integer.class, Integer.class},
new Object[]{window, true, getScrimColor(statusBarStyle, statusBarIsDark), getScrimColor(navigationBarStyle, navigationBarIsDark)});
}

@Override
public void adjustLayoutInDisplayCutoutMode(@NonNull Window window) {
WindowManager.LayoutParams attributes = window.getAttributes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
}
}

private static boolean getDetectDarkMode(SystemBarStyle systemBarStyle, View view) {
// internal val detectDarkMode: (Resources) -> Boolean
// androidx.activity library
Expand All @@ -67,11 +109,6 @@ public static void enable(Activity activity, SystemBarStyle statusBarStyle, Syst
return;
}

if (impl == null) {
Log.w(TAG, "enableEdgeToEdge, impl == null");
return;
}

Window window = activity.getWindow();
View view = window.getDecorView();
boolean statusBarIsDark = getDetectDarkMode(statusBarStyle, view);
Expand Down
4 changes: 0 additions & 4 deletions basic/src/main/java/com/dede/basic/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.dede.basic

import android.animation.ValueAnimator
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
Expand Down Expand Up @@ -73,7 +72,4 @@ object Utils {
return null to -1L
}

val Activity.isPlatLogoActivity: Boolean
get() = javaClass.simpleName == "PlatLogoActivity"

}
6 changes: 6 additions & 0 deletions core/activity-actions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ dependencies {
implementation(project(":eggs:Tiramisu"))
implementation(project(":eggs:S"))
implementation(project(":eggs:R"))
implementation(project(":eggs:Oreo"))
implementation(project(":eggs:Nougat"))
implementation(project(":eggs:Marshmallow"))
implementation(project(":eggs:Lollipop"))
implementation(project(":eggs:KitKat"))
implementation(project(":eggs:JellyBean"))
implementation(project(":eggs:IceCreamSandwich"))

implementation(libs.androidx.startup)
implementation(platform(libs.androidx.compose.bom))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.content.Context
import android.os.Bundle
import androidx.startup.Initializer
import com.dede.android_eggs.activity_actions.noOpDelegate
import com.dede.android_eggs.util.actions.PlatLogoActivityAction
import com.dede.android_eggs.util.actions.EggActivityAction
import com.dede.android_eggs.util.actions.RequestNotificationPermissionAction
import com.dede.android_eggs.util.actions.WarningDialogAction

Expand All @@ -34,7 +34,7 @@ internal class ActivityActionDispatcher : Application.ActivityLifecycleCallbacks
}

private val actions: Array<ActivityAction> = arrayOf(
PlatLogoActivityAction(),
EggActivityAction(),
WarningDialogAction(),
RequestNotificationPermissionAction(),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.dede.android_eggs.util.actions

import android.app.Activity
import androidx.activity.EdgeToEdgeCompat
import com.dede.android_eggs.util.ActivityActionDispatcher
import com.dede.android_eggs.views.main.util.EasterEggShortcutsHelp

internal class EggActivityAction : ActivityActionDispatcher.ActivityAction {

private val edgeToEdgePagers: Set<Class<out Activity>> = setOf(
com.android_o.egg.octo.Ocquarium::class.java,
com.android_m.egg.MLandActivity::class.java,
com.android_l.egg.LLandActivity::class.java,
com.android_k.egg.DessertCase::class.java,
com.android_j.egg.BeanBag::class.java,
com.android_i.egg.Nyandroid::class.java,
)

private val Activity.isPlatLogoActivity: Boolean
get() = javaClass.simpleName == "PlatLogoActivity"

override fun onPreCreate(activity: Activity) {
if (activity.isPlatLogoActivity || edgeToEdgePagers.contains(activity.javaClass)) {
EdgeToEdgeCompat.enable(activity)
}
}

override fun onCreate(activity: Activity) {
if (activity.isPlatLogoActivity) {
EasterEggShortcutsHelp.autoReportShortcutUsed(activity, activity.intent)
}
}
}

This file was deleted.

3 changes: 2 additions & 1 deletion eggs/Lollipop/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
android:label="@string/l_lland"
android:launchMode="singleInstance"
android:screenOrientation="locked"
android:theme="@android:style/Theme.Material.NoActionBar" />
android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen" />
<!--android:theme="@android:style/Theme.Material.NoActionBar"-->
</application>

</manifest>
28 changes: 28 additions & 0 deletions eggs/Lollipop/src/main/java/com/android_l/egg/LLandActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdgeCompat;
import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


public class LLandActivity extends Activity {
@Override
Expand All @@ -31,5 +40,24 @@ public void onCreate(Bundle savedInstanceState) {
world.setScoreField((TextView) findViewById(R.id.score));
world.setSplash(findViewById(R.id.welcome));
Log.v(LLand.TAG, "focus: " + world.requestFocus());

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.score), new OnApplyWindowInsetsListener() {

private ViewGroup.MarginLayoutParams copyParams;

@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
Insets edge = insets.getInsets(EdgeToEdgeCompat.EDGE_INSETS_MASK);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
if (copyParams == null) {
copyParams = new ViewGroup.MarginLayoutParams(params);
}
params.topMargin = edge.top + copyParams.topMargin;
params.leftMargin = edge.left + copyParams.leftMargin;
v.setLayoutParams(params);
return insets;
}
});
}
}
3 changes: 2 additions & 1 deletion eggs/Marshmallow/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
android:label="@string/m_mland"
android:launchMode="singleInstance"
android:screenOrientation="locked"
android:theme="@android:style/Theme.Material.NoActionBar" />
android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen" />
<!--android:theme="@android:style/Theme.Material.NoActionBar"-->
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.EdgeToEdgeCompat;
import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
Expand Down Expand Up @@ -48,9 +49,9 @@ public void onCreate(Bundle savedInstanceState) {
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
Insets edge = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Insets edge = insets.getInsets(EdgeToEdgeCompat.EDGE_INSETS_MASK);
v.setPadding(0, edge.top, 0, 0);
return WindowInsetsCompat.CONSUMED;
return insets;
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion eggs/R/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
android:exported="true"
android:icon="@drawable/r_icon"
android:label="@string/r_egg_name"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar" />
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen" />
<!--android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"-->

<!-- Android N easter egg bits -->
<activity
Expand Down
3 changes: 2 additions & 1 deletion eggs/S/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
android:exported="true"
android:icon="@drawable/s_icon"
android:label="@string/s_egg_name"
android:theme="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar" />
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" />
<!--android:theme="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar"-->

<!-- Android N easter egg bits -->
<activity
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/58.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- EdgeToEdge mode is enabled on the Easter Egg pages
- Upgrade project dependencies
Loading

0 comments on commit 3e0b5a9

Please sign in to comment.