Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

buildscript {
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:7.4.2'
}
}

apply plugin: 'com.android.library'

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

android {
compileSdkVersion 27
buildToolsVersion "28.0.3"
namespace "com.zeemyself.powermanager"
compileSdkVersion 34
buildToolsVersion "34.0.0"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
minSdkVersion 21
targetSdkVersion 34
versionCode 1
versionName "1.0"

buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch/java']
} else {
java.srcDirs += ['src/oldarch/java']
}
}
}

buildFeatures {
buildConfig true
}

lintOptions {
abortOnError false
}
}

repositories {
google()
mavenCentral()
}

dependencies {
implementation 'com.facebook.react:react-native:+'
}

if (isNewArchitectureEnabled()) {
// RNGP handles dependencies under New Architecture
} else {
implementation 'com.facebook.react:react-native:+'
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.zeemyself.powermanager;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.Map;
import java.util.HashMap;

public class RNPowermanagerModuleImpl {
public static final String NAME = "RNPowermanager";

private final ReactApplicationContext reactContext;

public RNPowermanagerModuleImpl(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

private static final Intent[] POWERMANAGER_INTENTS = {
new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(Uri.parse("mobilemanager://function/entry/AutoStart")),
new Intent().setComponent(new ComponentName("com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity"))
};

public void startPowerManager() {
for (Intent intent : POWERMANAGER_INTENTS) {
if (reactContext.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
reactContext.startActivity(intent);
break;
}
}
}

public Boolean isSupported() {
for (Intent intent : POWERMANAGER_INTENTS) {
if (reactContext.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
return true;
}
}
return false;
}

public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("isSupported", isSupported());
return constants;
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package com.zeemyself.powermanager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.facebook.react.ReactPackage;
import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
public class RNPowermanagerPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNPowermanagerModule(reactContext));
}
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import java.util.HashMap;
import java.util.Map;

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
public class RNPowermanagerPackage extends TurboReactPackage {

@Override
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
if (name.equals(RNPowermanagerModuleImpl.NAME)) {
return new RNPowermanagerModule(reactContext);
}
return null;
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return () -> {
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
moduleInfos.put(
RNPowermanagerModuleImpl.NAME,
new ReactModuleInfo(
RNPowermanagerModuleImpl.NAME,
RNPowermanagerModuleImpl.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
isTurboModule // isTurboModule
));
Comment on lines +28 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Pass hasConstants for RN 0.71 and 0.72

The package still advertises support for react-native >=0.71.0, but in RN 0.71/0.72 ReactModuleInfo requires the hasConstants argument before isCxxModule; this six-argument constructor is only compatible with newer RN versions. Apps using the declared minimum will fail Android compilation when this package is included, so either add the missing hasConstants value here or raise the peer dependency to the version where this constructor exists.

Useful? React with 👍 / 👎.

return moduleInfos;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zeemyself.powermanager;

import com.facebook.react.bridge.ReactApplicationContext;
import java.util.Map;

public class RNPowermanagerModule extends NativeRNPowermanagerSpec {

private final RNPowermanagerModuleImpl delegate;

public RNPowermanagerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.delegate = new RNPowermanagerModuleImpl(reactContext);
}

@Override
public String getName() {
return RNPowermanagerModuleImpl.NAME;
}

@Override
public void startPowerManager() {
delegate.startPowerManager();
}

@Override
protected Map<String, Object> getTypedExportedConstants() {
return delegate.getConstants();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.zeemyself.powermanager;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import androidx.annotation.Nullable;
import java.util.Map;

public class RNPowermanagerModule extends ReactContextBaseJavaModule {

private final RNPowermanagerModuleImpl delegate;

public RNPowermanagerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.delegate = new RNPowermanagerModuleImpl(reactContext);
}

@Override
public String getName() {
return RNPowermanagerModuleImpl.NAME;
}

@ReactMethod
public void startPowerManager() {
delegate.startPowerManager();
}

@Nullable
@Override
public Map<String, Object> getConstants() {
return delegate.getConstants();
}
}
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@

import { NativeModules, Platform } from 'react-native';
import { Platform } from 'react-native';
import RNPowermanager from './src/NativeRNPowermanager';

const { RNPowermanager } = NativeModules;
// Cache constants for isSupported if RNPowermanager exists
const constants = RNPowermanager
? (typeof RNPowermanager.getConstants === 'function'
? RNPowermanager.getConstants()
: RNPowermanager)
: null;

export default PowerManager = {
startPowerManager: () => {
if(Platform.OS === 'android') {
return RNPowermanager.startPowerManager()
if (Platform.OS === 'android' && RNPowermanager) {
return RNPowermanager.startPowerManager();
}
},
isSupported: () => {
if(Platform.OS === 'android') {
return RNPowermanager.isSupported
if (Platform.OS === 'android' && constants) {
return constants.isSupported;
}
return false;
}
}
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
"author": "zeemyself",
"license": "ISC",
"peerDependencies": {
"react-native": ">=0.60.0"
"react-native": ">=0.77.0"
},
"devDependencies": {
"standard-version": "^9.5.0"
},
"codegenConfig": {
"name": "RNPowermanagerSpec",
"type": "modules",
"jsSrcsDir": "src",
"android": {
"javaPackageName": "com.zeemyself.powermanager"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/zeemyself/react-native-powermanager.git"
Expand Down
11 changes: 11 additions & 0 deletions src/NativeRNPowermanager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
startPowerManager(): void;
getConstants(): {
isSupported: boolean;
};
}

export default TurboModuleRegistry.get<Spec>('RNPowermanager');