PixelParts: Introduce and switch to ComponentUtils

Signed-off-by: AnierinB <anierin@evolution-x.org>
This commit is contained in:
2023-07-31 22:26:23 -07:00
parent 2cd36944c5
commit 81fab5b17c
2 changed files with 43 additions and 15 deletions

View File

@@ -7,14 +7,13 @@
package org.evolution.pixelparts;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import org.evolution.pixelparts.saturation.Saturation;
import org.evolution.pixelparts.services.PixelTorchTileService;
import org.evolution.pixelparts.utils.AutoHBMUtils;
import org.evolution.pixelparts.utils.ComponentUtils;
import org.evolution.pixelparts.utils.TorchUtils;
public class Startup extends BroadcastReceiver {
@@ -33,18 +32,11 @@ public class Startup extends BroadcastReceiver {
AutoHBMUtils.enableAutoHBM(context);
Saturation.restoreSaturationSetting(context);
if (!TorchUtils.hasTorch(context)) {
ComponentName componentName = new ComponentName(context, PixelTorchTileService.class);
PackageManager packageManager = context.getPackageManager();
int currentState = packageManager.getComponentEnabledSetting(componentName);
if (currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
);
}
}
// PixelTorchTileService
ComponentUtils.setComponentEnabled(
context,
PixelTorchTileService.class,
TorchUtils.hasTorch(context)
);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2023 The Evolution X Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.evolution.pixelparts.utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
public class ComponentUtils {
/**
* Enables or disables a specified Android component dynamically at runtime.
*
* @param context The context from which the component will be enabled or disabled.
* @param componentClass The class of the component to be enabled or disabled.
* @param enable If true, the component will be enabled; if false, it will be disabled.
*/
public static void setComponentEnabled(Context context, Class<?> componentClass, boolean enable) {
ComponentName componentName = new ComponentName(context, componentClass);
PackageManager packageManager = context.getPackageManager();
int currentState = packageManager.getComponentEnabledSetting(componentName);
int newState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
if (currentState != newState) {
packageManager.setComponentEnabledSetting(
componentName,
newState,
PackageManager.DONT_KILL_APP
);
}
}
}