* fix: don't crash at startup when the restriction bypass lib is missing Unseal.unseal() loads ChickenHook's libnrb.so. Repackaged APKs and multi-open containers can fail to load it, in which case the call throws UnsatisfiedLinkError (an Error) that catch (Exception) does not handle. This runs from Application.onCreate(), so the process crash-loops forever. Catch Exception | LinkageError. A failed unseal only disables the hidden API exemption for that launch; other Errors (OOM, etc.) still propagate. * fix: don't crash reading allowEnterPip when the restriction bypass is broken getAllowEnterPip() uses RestrictionBypass below SDK 35. If the native bypass library failed to load, the bypass class is permanently broken and any further access throws LinkageError (ExceptionInInitializerError on the first touch, NoClassDefFoundError afterwards), which catch (ReflectiveOperationException) does not handle. Return false (PIP not allowed) instead of crashing the transition. --------- Co-authored-by: caizhixing <17589399+caizhixing03@user.noreply.gitee.com>
141 lines
6.2 KiB
Java
141 lines
6.2 KiB
Java
/*
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* Modifications copyright 2025, Lawnchair
|
|
*/
|
|
|
|
package com.android.launcher3;
|
|
|
|
import static android.app.PendingIntent.FLAG_IMMUTABLE;
|
|
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
|
|
|
import static com.android.launcher3.Flags.enableStrictMode;
|
|
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationManager;
|
|
import android.app.PendingIntent;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.StrictMode;
|
|
import android.util.Log;
|
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
|
import com.android.launcher3.graphics.BitmapCreationCheck;
|
|
import com.android.launcher3.logging.FileLog;
|
|
import com.android.launcher3.util.LockedUserState;
|
|
import com.android.launcher3.util.ResourceBasedOverride;
|
|
|
|
import org.chickenhook.restrictionbypass.Unseal;
|
|
|
|
import app.lawnchair.preferences.PreferenceManager;
|
|
|
|
/**
|
|
* Utility class to handle one time initializations of the main process
|
|
*/
|
|
public class MainProcessInitializer implements ResourceBasedOverride {
|
|
|
|
private static final String TAG = "MainProcessInitializer";
|
|
|
|
private static final boolean DEBUG_STRICT_MODE = false;
|
|
|
|
public static void initialize(Context context) {
|
|
try {
|
|
Unseal.unseal();
|
|
Log.i(TAG, "Unseal success!");
|
|
} catch (Exception | LinkageError e) {
|
|
// A repackaged APK or a multi-open container can fail to load libnrb.so, in which
|
|
// case Unseal.unseal() throws UnsatisfiedLinkError (an Error). Startup must survive
|
|
// a broken bypass: only the hidden API exemption is lost for this launch.
|
|
Log.e(TAG, "Unseal fail!");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// LC-Note: SharedPreferences will initialise *after* Direct Boot is disengaged once the user has unlocked the Keyguard once.
|
|
// Attempting to call a prefs system while SharedPrefs are not initialised will result in a Java ISE crash
|
|
// because it's in (CE) Credential Encrypted storage. (In other words, just wait till the user unlocked their phone once)
|
|
//
|
|
// *Unless* your SharedPreferences instance is explicitly told to run in device-protected storage or (DE) Device Encrypted storage,
|
|
// but in this case it's not, but it's something to keep in mind.
|
|
LockedUserState.get(context).runOnUserUnlocked(() -> PreferenceManager.getInstance(context));
|
|
|
|
Overrides.getObject(
|
|
MainProcessInitializer.class, context, R.string.main_process_initializer_class)
|
|
.init(context);
|
|
}
|
|
|
|
protected void init(Context context) {
|
|
FileLog.setDir(context.getApplicationContext().getFilesDir());
|
|
|
|
if (BitmapCreationCheck.ENABLED) {
|
|
BitmapCreationCheck.startTracking(context);
|
|
}
|
|
|
|
if (DEBUG_STRICT_MODE || (BuildConfigs.IS_STUDIO_BUILD && enableStrictMode())) {
|
|
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
|
|
.detectDiskReads()
|
|
.detectDiskWrites()
|
|
.detectNetwork() // or .detectAll() for all detectable problems
|
|
.penaltyLog()
|
|
.build());
|
|
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
|
|
.detectLeakedSqlLiteObjects()
|
|
.detectLeakedClosableObjects()
|
|
.detectActivityLeaks()
|
|
.penaltyLog()
|
|
.penaltyDeath()
|
|
.build());
|
|
}
|
|
|
|
if (BuildConfigs.IS_DEBUG_DEVICE && FeatureFlags.NOTIFY_CRASHES.get()) {
|
|
final String notificationChannelId = "com.android.launcher3.Debug";
|
|
final String notificationChannelName = "Debug";
|
|
final String notificationTag = "Debug";
|
|
final int notificationId = 0;
|
|
|
|
NotificationManager notificationManager =
|
|
context.getSystemService(NotificationManager.class);
|
|
notificationManager.createNotificationChannel(new NotificationChannel(
|
|
notificationChannelId, notificationChannelName,
|
|
NotificationManager.IMPORTANCE_HIGH));
|
|
|
|
Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
|
|
String stackTrace = Log.getStackTraceString(throwable);
|
|
|
|
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
|
shareIntent.setType("text/plain");
|
|
shareIntent.putExtra(Intent.EXTRA_TEXT, stackTrace);
|
|
shareIntent = Intent.createChooser(shareIntent, null);
|
|
PendingIntent sharePendingIntent = PendingIntent.getActivity(
|
|
context, 0, shareIntent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
|
|
|
|
Notification notification = new Notification.Builder(context, notificationChannelId)
|
|
.setSmallIcon(android.R.drawable.ic_menu_close_clear_cancel)
|
|
.setContentTitle("Launcher crash detected!")
|
|
.setStyle(new Notification.BigTextStyle().bigText(stackTrace))
|
|
.addAction(android.R.drawable.ic_menu_share, "Share", sharePendingIntent)
|
|
.build();
|
|
notificationManager.notify(notificationTag, notificationId, notification);
|
|
|
|
Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler =
|
|
Thread.getDefaultUncaughtExceptionHandler();
|
|
if (defaultUncaughtExceptionHandler != null) {
|
|
defaultUncaughtExceptionHandler.uncaughtException(thread, throwable);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|