GithubHelp home page GithubHelp logo

eral2001 / displaytoggle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hunterxprogrammer/displaytoggle

0.0 0.0 0.0 11 KB

Turn ON/OFF the display of your Android phone, like scrcpy, using ADB Shell or Root.

displaytoggle's Introduction

Description

Turn ON/OFF the display of your Android phone, like scrcpy, using ADB Shell or Root.

Check Reddit Tasker for Tasker users.

Termux users can continue to the next section.

Building

You can follow these steps to build it in Termux.

pkg upgrade -y

 

pkg install -y wget openjdk-17 dx android-tools

 

cd; wget -O android.jar "https://github.com/Sable/android-platforms/blob/master/android-30/android.jar?raw=true"

 

nano DisplayToggle.java

Now copy paste this:-

import android.os.Build;
import android.os.IBinder;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class DisplayToggle {

    private static final Class<?> CLASS;
	static {
        	try {
            		CLASS = Class.forName("android.view.SurfaceControl");
		} catch (ClassNotFoundException e) {
        	throw new AssertionError(e);
     	 }
    }

    public static void main(String... args) throws Exception {
	System.out.print("Display mode: "+args[0]);
	Method method = CLASS.getMethod("setDisplayPowerMode", IBinder.class, int.class);
	try {
            method.invoke(null, getBuiltInDisplay(),Integer.parseInt(args[0]));
        } catch (InvocationTargetException | IllegalAccessException e) {
	    e.printStackTrace();
        }
    }

    private static Method getGetBuiltInDisplayMethod() throws NoSuchMethodException {
       Method getBuiltInDisplayMethod;
            // the method signature has changed in Android Q
            // <https://github.com/Genymobile/scrcpy/issues/586>
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                getBuiltInDisplayMethod = CLASS.getMethod("getBuiltInDisplay", int.class);
            } else {
                getBuiltInDisplayMethod = CLASS.getMethod("getInternalDisplayToken");
            }
        return getBuiltInDisplayMethod;
    }

    public static IBinder getBuiltInDisplay() {
        try {
            Method method = getGetBuiltInDisplayMethod();
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                // call getBuiltInDisplay(0)
                return (IBinder) method.invoke(null, 0);
            }
            // call getInternalDisplayToken()
            return (IBinder) method.invoke(null);
           } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Then press CTRL+X+Y+ENTER and save it.

And then create its Java CLASS file:-

javac -Xlint:none -source 1.7 -target 1.7 -cp android.jar DisplayToggle.java

Then DEX it using:-

dx --dex --output DisplayToggle.dex DisplayToggle.class

You have compiled the DisplayToggle.dex file (around 2.5kb).

Now copy it to internal storage to use it later.

termux-setup-storage

 

cp -f DisplayToggle.dex /storage/emulated/0

How To Use It

In an ADB Shell (from Termux or PC):-

To Turn Display OFF

adb shell CLASSPATH=/storage/emulated/0/DisplayToggle.dex app_process / DisplayToggle 0

To Turn Display ON

adb shell CLASSPATH=/storage/emulated/0/DisplayToggle.dex app_process / DisplayToggle 2

Note:-

In Termux if you are rooted, you can just replace every adb shell with su -c.

Credits

rom1v - Method to make java code executable

CheerfulPianissimo - Java code to make this possible

displaytoggle's People

Contributors

hunterxprogrammer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.