Skip to content

Commit

Permalink
Merge pull request #8 from saibotd/master
Browse files Browse the repository at this point in the history
General update and possibility for additional packages
  • Loading branch information
temaivanoff authored Nov 15, 2021
2 parents 6b7ebd5 + f034635 commit 8c2f8a0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import RNLockTask from 'react-native-lock-task';

RNLockTask.startLockTask();
RNLockTask.startLockTaskWith(["com.google.android.youtube", "com.sega.sonicdash"]);
RNLockTask.stopLockTask();
RNLockTask.clearDeviceOwnerApp();
```
11 changes: 6 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:4.0.0'
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
Expand All @@ -28,9 +28,10 @@ android {

repositories {
mavenCentral()
google()
}

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

54 changes: 41 additions & 13 deletions android/src/main/java/com/rnlocktask/RNLockTaskModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
package com.rnlocktask;

import android.app.Activity;
import android.view.WindowManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableArray;

import java.util.ArrayList;

public class RNLockTaskModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext reactContext;

private static final String ACTIVITY_GONE = "ACTIVITY_GONE";
private static final String DEVICE_OWNER_CLEARED = "DEVICE_OWNER_CLEARED";
private static final String LOCKED_TASK = "LOCKED_TASK";
private static final String LOCKED_TASK_AS_OWNER = "LOCKED_TASK_AS_OWNER";
private static final String UNLOCKED_TASK = "UNLOCKED_TASK";

public RNLockTaskModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
Expand All @@ -28,45 +33,68 @@ public String getName() {
}

@ReactMethod
public void clearDeviceOwnerApp() {
public void clearDeviceOwnerApp(Promise promise) {
try {
Activity mActivity = reactContext.getCurrentActivity();
Activity mActivity = getCurrentActivity();
if (mActivity != null) {
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) mActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
myDevicePolicyManager.clearDeviceOwnerApp(mActivity.getPackageName());
promise.resolve(DEVICE_OWNER_CLEARED);
}
promise.reject(ACTIVITY_GONE, "Activity gone or mismatch");
} catch (Exception e) {
promise.reject(e);
}
}

@ReactMethod
public void startLockTask() {
public void startLockTaskWith(ReadableArray additionalPackages, Promise promise) {
try {
Activity mActivity = reactContext.getCurrentActivity();
Activity mActivity = getCurrentActivity();
if (mActivity != null) {
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) mActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mDPM = new ComponentName(mActivity, MyAdmin.class);

if (myDevicePolicyManager.isDeviceOwnerApp(mActivity.getPackageName())) {
String[] packages = {mActivity.getPackageName()};
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
ArrayList<String> packages = new ArrayList<>();
packages.add(mActivity.getPackageName());
if(additionalPackages != null){
for (int i = 0; i < additionalPackages.size(); i++) {
packages.add(additionalPackages.getString(i));
}
}
myDevicePolicyManager.setLockTaskPackages(mDPM, packages.toArray(new String[0]));
mActivity.startLockTask();
promise.resolve(LOCKED_TASK_AS_OWNER);
} else {
mActivity.startLockTask();
promise.resolve(LOCKED_TASK);
}
} else{
promise.reject(ACTIVITY_GONE, "Activity gone or mismatch");
}
} catch (Exception e) {
promise.reject(e);
}
}

@ReactMethod
public void stopLockTask() {
public void startLockTask(Promise promise) {
startLockTaskWith(null, promise);
}

@ReactMethod
public void stopLockTask(Promise promise) {
try {
Activity mActivity = reactContext.getCurrentActivity();
Activity mActivity = getCurrentActivity();
if (mActivity != null) {
mActivity.stopLockTask();
promise.resolve(UNLOCKED_TASK);
} else {
promise.reject(ACTIVITY_GONE, "Activity gone or mismatch");
}
} catch (Exception e) {
promise.reject(e);
}
}
}
10 changes: 5 additions & 5 deletions android/src/main/java/com/rnlocktask/RNLockTaskPackage.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

package com.rnlocktask;

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

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class RNLockTaskPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
Expand Down

0 comments on commit 8c2f8a0

Please sign in to comment.