Screen dimming – ShiVa Engine

Screen dimming

Nav Gupta: “This is the method that works as many others out there are just running around in circles and/or are just confusing and most importantly are not even related to a ShiVa Pipeline. I have personally tested the result on a Kindle Fire and Barnes and Noble Nook Color (TM).”

ShiVa Steps

1. In ShiVa, create a button in your HUD which we will use for Dimming and connect it to yor code.
2. Create a new handler called onDimTouch()
3. Connect your button to the Handler using the following code.

    -- Assuming your HUD is called unique_instance_main_hud and button is btn_dimtouch
    if ( sTag == "unique_instance_main_hud.btn_dimtouch" ) then
   -- Assuming your main ai is called MainAI
        user.postEvent ( this.getUser ( ), 0, "MainAI", "onDimTouch" )

Export your stk file and make an Android Project.

Eclipse Steps

We are assuming at this point eclipse is open and you are now going to start working in the c++ file.

C++ Code

1. At approximately line 12 after the first #include, add the following include:

#include "S3DXAIVariable.h"

2. At approximately line 630 after “// @@BEGIN_JNI_INSTALL_EVENT_HOOKS@@” add the following HOOK code below it:

S3DClient_InstallCurrentUserEventHook ( "MainAI", "onDimTouch", fun_CallCPPDimTouch, NULL );  

3. At approximately line 64 after “// @@BEGIN_JNI_CALLBACKS@@” add the following CALLBACK function code

void fun_CallCPPDimTouch ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData )
    if ( pJavaVM )
        JNIEnv *pEnv ;
        if    ( pJavaVM->GetEnv ( (void**) &pEnv, JNI_VERSION_1_4 ) >= 0 )
            //EDIT BELOW FOR YOUR PACKAGE NAME ex. com/company/app/App
            jclass pClass  = pEnv->FindClass ( "COM/GAMESCORPION/ICLOCKSTATIONPRO/ICLOCKSTATIONPRO" );
            if   ( pClass != 0 )
                jmethodID pMethod = pEnv->GetStaticMethodID ( pClass, "CallJAVADimTouch", "()I");
                if      ( pMethod )
                    int dummy = pEnv->CallStaticIntMethod ( pClass, pMethod ) ;

Java Code

1. Go down to approx. line 500 and before “public void onLowMemory ( )” and paste the following:

public static int CallJAVADimTouch( )
    int nBrightness = 255;
    try {
        nBrightness = android.provider.Settings.System.getInt(oThis.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS );
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
    android.provider.Settings.System.putInt(oThis.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);
    Intent intent = new Intent(oThis.getBaseContext(), Bright.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important
    //in the next line 'brightness' should be a float number between 0.0 and 1.0
    intent.putExtra("brightness value", 0.5f);
    return 0;

NOTE: This is specifically where you can set your brightness of the dimming of the screen between 0 to 255. YOU MAY ALSO use the OBTAINED nBrightness to know what the current brightness is set to.
2. Now we must create a new class called Bright. Move onto the next step after Saving and Closing the JAVA file. Class

1. In Eclipse Package Explorer, click on the arrow under the “src” folder.
2. Right click on the package name that appears (ex. com.companyname.appname) and select New->Class
3. Type in the name “Bright” and hit Finish.
4. Replace the entire code (CTRL+A and then Delete to delete all the code) with the code below:

package com.gamescorpion.iclockstationpro;
//CHANGE LINE ABOVE ^ to com.companyname.appname
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
public class Bright extends Activity {
    private static final int DELAYED_MESSAGE = 1;
    private Handler handler;
    protected void onCreate(Bundle savedInstanceState) {
        handler = new Handler() {
            public void handleMessage(Message msg) {
                if(msg.what == DELAYED_MESSAGE) {
        Intent brightnessIntent = this.getIntent();
        float brightness = brightnessIntent.getFloatExtra("brightness value", 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        Message message = handler.obtainMessage(DELAYED_MESSAGE);
        //this next line is very important, you need to finish your activity with slight delay

Save file and then close and move onto next step.


1. In Eclipse Package Explorer, Double Click on AndroidManifest.xml
2. At approx. line 17 after the end tag < /activity>, paste the following code:


NOTE: If you need full explanation on why we are using the above code, please refer to the helper website that gave me the solution:
3. At approx. line 27 after < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> PASTE THE FOLLOWING CODE:

4. Save your manifest file and then COMPILE AND TEST
CONGRATULATIONS, you have now achieved screen dimming to save on power for Android Devices. From here you can customize the Java code and your own code as you like (if you read over the hooks/callbacks tutorial) you can even make it customizable dimming from your app by passing an integer variable right into the Java function. Its really up to you how you use the functionality, but remember that your app will restart each time to have the changes take effect.

Need more answers?

  • slackBanner