-1

I create an android app (Point of sale Terminal), to be installed outdoors that is in petrol station. I have a problem: The application becomes invisible during the day (when the sunlight appears).

Based on the information from here, for android applications remain visible when the sun light appears is to set the brightness to the maximum value.

How to create "Automatic Brightness" to adjust lcd lighting, depending on sunlight conditions?

Outside the room, when conditions are:

  1. Morning / Overcast: Screen at medium brightness.
  2. Daytime / Sunny: Screen at high brightness.
  3. Night: Screen at low brightness.

Thanks.

Ihdina
  • 950
  • 6
  • 21
  • Pull the forecast info from an API, extract the data that you need, and use a scheduler to adjust the brightness accordingly. – mbmc Jul 06 '17 at 00:20
  • mbmc, Can you give me point out about API for brightness controlling in android? – Ihdina Jul 06 '17 at 00:22
  • As it sounds like you are doing a Custom ROM/device you could just look in the framework code for where the brightness settings are located and access via reflection. – Morrison Chang Jul 06 '17 at 00:36
  • @Morrison, Thank you for that information. – Ihdina Jul 06 '17 at 01:49

1 Answers1

0

To change brightness automatically based on predetermined time, I create thread worker which automatically run adjustBrightness function every 1 hour interval.

// AutoBrightness.java

import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

import static android.provider.Settings.System.SCREEN_BRIGHTNESS;

public class AutoBrightness {
    private final static String TAG = AutoBrightness.class.getName();
    private static boolean _isBrightnessThreadEnable;
    private static BrightnessThread _brightnessThread;
    private static Context _context;
    private static AutoBrightness _instance;

    private AutoBrightness(Context context) {
        _context = context;
    }

    public static AutoBrightness getInstance(Context context) {
        if (_instance == null) {
            _instance = new AutoBrightness(context);
        }
        return _instance;
    }

    public void start() {
        if (!_isBrightnessThreadEnable) {
            _brightnessThread = new BrightnessThread(_context);
            _brightnessThread.start();
            _isBrightnessThreadEnable = true;
        }
    }

    private class BrightnessThread extends Thread {
        Context _context;

        BrightnessThread(Context context) {
            _context = context;
            Settings.System.putInt(_context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            setPriority(Thread.NORM_PRIORITY);
        }

        private long getCurrentTime() {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SHORT_TIME_FORMAT);
            String currentTime = simpleDateFormat.format(Calendar.getInstance().getTime());
            try {
                return simpleDateFormat.parse(currentTime).getTime();
            } catch (ParseException ex) {
                Log.e(TAG, Log.getStackTraceString(ex));
            }
            return 0;
        }

        private void adjustBrightness(float backlightValue){
            WindowManager.LayoutParams layoutParams = ((Activity) _context).getWindow().getAttributes();
            layoutParams.screenBrightness = backlightValue;
            ((Activity) _context).getWindow().setAttributes(layoutParams);

            int systemBacklightValue = (int) (backlightValue * 255);
            android.provider.Settings.System.putInt(_context.getContentResolver(), SCREEN_BRIGHTNESS, systemBacklightValue);
        }

        @Override
        public void run() {
            long morningTime = -3600000L, dayTime = 10800000L, nightTime = 39600000L;
            float backlightValue = 0.5f;

            boolean isMorningAdjusted = false;
            boolean isDayAdjusted = false;
            boolean isNightAdjusted = false;

            while (_isBrightnessThreadEnable) {

                long currentTime = getCurrentTime();
                if (currentTime >= morningTime && currentTime < dayTime) {

                    // 06:00 - 09:59 (morning)
                    if (!isMorningAdjusted) {
                        backlightValue = 0.50f;
                        adjustBrightness(backlightValue);
                        isMorningAdjusted = true;

                        isDayAdjusted = false;
                        isNightAdjusted = false;
                    }

                } else if (currentTime >= dayTime && currentTime < nightTime) {

                    // 10:00 - 17:59 (day)
                    if (!isDayAdjusted) {
                        backlightValue = 1.00f;
                        adjustBrightness(backlightValue);
                        isDayAdjusted = true;

                        isMorningAdjusted = false;
                        isNightAdjusted = false;
                    }
                } else {

                    // 18:00 - 05:59 (night)
                    if (!isNightAdjusted) {
                        backlightValue = 0.35f;
                        adjustBrightness(backlightValue);
                        isNightAdjusted = true;

                        isMorningAdjusted = false;
                        isDayAdjusted = false;
                    }
                }

                try {
                    // delay for 1 hours
                    Thread.sleep(1000 * 60 * 60);
                } catch (InterruptedException ex) {
                    Log.e(TAG, Log.getStackTraceString(ex));
                }
            }
        }
    }
}

How to use?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    _autoBrightness = AutoBrightness.getInstance(this);
    _autoBrightness.start();
}
Ihdina
  • 950
  • 6
  • 21