Calling a method in another Activity in Android for setting screen brightness

442 views Asked by At

I have 2 Activities, MainActivity and VideoPlayerActivity. In the MainActivity I have a socket that I can connect to it from my PC using telnet. Then I can write line and it will execute the commands I send to it. for example if I write start it will start VideoPlayerActivity and plays a movie.

Now I want to control the screen brightness. In the MainActivity when the VideoPlayerActivity is not started yet, I can easily write a command in telnet like brightness=0.1 and that will set the brightness of the MainActivity to 0.1:

if(msg.startsWith("brightness="))
{
    String brightText = msg.substring(msg.indexOf('=') + 1, msg.length());
    BrightnessValue = Float.parseFloat(brightText);

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = BrightnessValue;
    getWindow().setAttributes(lp);

    if(_videoPlayerIntent != null && _videoPlayerActivity.isActive)
    {
            _videoPlayerActivity.setBrightnessLevel(BrightnessValue);
    }
}

Now the problem is, when VideoActivity starts, it ignores the preset brightness and will use the system defined brightness. So I put this method in VideoActivity :

public void setBrightnessLevel(float value)
{
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = value;
    getWindow().setAttributes(lp);
}

but as soon as I write command to change brightness the whole app stops. Because of this section in the first code I put above in the question:

if(_videoPlayerIntent != null && _videoPlayerActivity.isActive)
{
    Log.d("CALLING VIDEOACTIVITY", "SET BRIGHTNESS");
    _videoPlayerActivity.setBrightnessLevel(BrightnessValue);
}

Can you tell me how can I handle this situation? I need to be able to change brightness of screen when the VideoActivity is running, and my socket is in MainActivity...

This is the method in VideoActivity....I tried to make it static then the problem is I can not access getWindow() if the method is static:

public void setBrightnessLevel(float value)
{
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = value;
    getWindow().setAttributes(lp);
}
1

There are 1 answers

3
vilpe89 On

You need a handle to that second Activity to set it's window brightness so i suggest you to make a model that tells the activity creation and destruction to the first Activity.

Here is the model for listening the activitys state:

public class ActivityStateListenerModel {

    public interface OnActivityStateChangedListener {
        void activityStarted(Activity activity);
        void activityDestroyed();
    }

    private static ActivityStateListenerModel mInstance;
    private OnActivityStateChangedListener mListener;

    public static ActivityStateListenerModel getInstance() {
        if(mInstance == null) {
            mInstance = new ActivityStateListenerModel();
        }
        return mInstance;
    }

    public void setListener(OnActivityStateChangedListener listener) {
        mListener = listener;
    }

    public void activityStarted(Activity activity) {
        mListener.activityStarted(activity);
    }

    public void activityDestroyed() {
        mListener.activityDestroyed();
    }

}

And you need to implement OnActivityStateChangedListener interface in your MainActivity and set it to listen the changes:

public class MainActivity extends Activity implements 
        OnActivityStateChangedListener {

    // in onCreate(...)
    ActivityStateListenerModel.getInstance().setListener(this);

Then the callbacks, in those we set flag what we need to check to know is the activity still running:

private static boolean mOtherActivityStarted;
private static Activity mOtherActivity;

@Override
public void activityStarted(Activity activity) {
    Log.d(TAG, "Second activity created");
    mOtherActivityStarted = true;
    mOtherActivity = activity;
}

@Override
public void activityDestroyed() {
    Log.d(TAG, "Second activity destroyed");
    mOtherActivityStarted = false;
    mOtherActivity = null;
}

And when your socket reads data you just do this in your MainActivity:

if(mOtherActivity != null && mOtherActivityStarted) {
    SecondActivity.setBrightnessLevel(brightnessValue, mOtherActivity);
}

Then you have that other Activity (VideoActivity), so there you need to notify the model that activity is created or destroyed:

// in onCreate(...)
// This gives the handle to MainActivity
ActivityStateListenerModel.getInstance().activityStarted(this);

// in onDestroy()
ActivityStateListenerModel.getInstance().activityDestroyed();

And the method that changes the brightess from VideoActivity (in this case this method can be anywhere because we pass the activity in the parameters):

static public void setBrightnessLevel(float value, Activity activity)
{
    Window window = activity.getWindow();
    if(window != null) {
        Log.d(MainActivity.TAG, "Setting brightness to " + activity.getClass().getName());
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = value;
        window.setAttributes(lp);
    }
}

and also it's a good habit to name your variables starting with lower case letter...

(BrightnessValue => brightnessValue)