Skip to content

Commit

Permalink
修复了执行完毕后SELinux状态不复原的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
flben233 committed Dec 16, 2022
1 parent af5b332 commit 7458f04
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
9 changes: 2 additions & 7 deletions app/src/main/java/org/shirakawatyu/dc/DisplayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
import java.lang.reflect.Method;

public class DisplayUtil {
public static boolean setAntiFlickMode(boolean status) {
boolean flag = false;
public static boolean setAntiFlickMode(boolean status, boolean selinux) {
try {
if (SELinuxUtil.checkStatus()) {
SELinuxUtil.setStatus(false);
flag = true;
}
Class<?> hardware = Class.forName("miui.hardware.display.DisplayFeatureManager");
Method getInstance = hardware.getDeclaredMethod("getInstance");
getInstance.setAccessible(true);
Expand All @@ -23,7 +18,7 @@ public static boolean setAntiFlickMode(boolean status) {
setScreenEffect.invoke(o, 20, 0);
Runtime.getRuntime().exec("su -c settings put system dc_back_light 0");
}
if (flag) {
if (selinux) {
SELinuxUtil.setStatus(true);
}
return true;
Expand Down
28 changes: 18 additions & 10 deletions app/src/main/java/org/shirakawatyu/dc/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.shirakawatyu.dc;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.shirakawatyu.dc.entity.Status;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand All @@ -14,24 +17,17 @@ public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Status status = new Status();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!checkRoot()) {
Toast.makeText(getApplicationContext(),"Tips:您的设备没有ROOT,本程序不会有任何作用",Toast.LENGTH_SHORT).show();
System.exit(0);
}
Button bt = findViewById(R.id.button);
bt.setOnClickListener(view -> {
boolean b = DisplayUtil.setAntiFlickMode(true);
if (!b) Toast.makeText(getApplicationContext(), "开启失败", Toast.LENGTH_SHORT).show();
else Toast.makeText(getApplicationContext(), "开启成功", Toast.LENGTH_SHORT).show();
});
bt.setOnClickListener(view -> simpleSetAntiFlickerMode(getApplicationContext(), status, true));
Button bt2 = findViewById(R.id.button2);
bt2.setOnClickListener(view -> {
boolean b = DisplayUtil.setAntiFlickMode(false);
if (!b) Toast.makeText(getApplicationContext(), "关闭失败", Toast.LENGTH_SHORT).show();
else Toast.makeText(getApplicationContext(), "关闭成功", Toast.LENGTH_SHORT).show();
});
bt2.setOnClickListener(view -> simpleSetAntiFlickerMode(getApplicationContext(), status, false));
}

private static boolean checkRoot() {
Expand All @@ -48,4 +44,16 @@ private static boolean checkRoot() {
}
return true;
}

private static void simpleSetAntiFlickerMode(Context context, Status status, boolean mode) {
if (SELinuxUtil.checkStatus()) {
SELinuxUtil.setStatus(false);
status.setLastSELinuxStatus(true);
simpleSetAntiFlickerMode(context, status, mode);
} else {
boolean b = DisplayUtil.setAntiFlickMode(mode, status.getLastSELinuxStatus());
if (!b) Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show();
else Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/org/shirakawatyu/dc/SELinuxUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static boolean checkStatus() {
public static boolean setStatus(boolean status) {
try {
if (status) {
Runtime.getRuntime().exec("su -c setenforce 0");
} else {
Runtime.getRuntime().exec("su -c setenforce 1");
} else {
Runtime.getRuntime().exec("su -c setenforce 0");
}
return true;
}catch (IOException e) {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/org/shirakawatyu/dc/entity/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.shirakawatyu.dc.entity;

public class Status {
private boolean lastSELinuxStatus;

public Status() {
this.lastSELinuxStatus = false;
}

public boolean getLastSELinuxStatus() {
return lastSELinuxStatus;
}

public void setLastSELinuxStatus(boolean lastSELinuxStatus) {
this.lastSELinuxStatus = lastSELinuxStatus;
}
}

0 comments on commit 7458f04

Please sign in to comment.