Java Code Examples for android.view.Display#getState()

The following examples show how to use android.view.Display#getState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OBSystemsManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public boolean isScreenOn(Context context)
{
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
    {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays())
        {
            if (display.getState() != Display.STATE_OFF)
            {
                screenOn = true;
            }
        }
        return screenOn;
    } else
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}
 
Example 2
Source File: HelperNotificationAndBadge.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Is the screen of the device on.
 *
 * @param context the context
 * @return true when (at least one) screen is on
 */
public boolean isScreenOn(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }
}
 
Example 3
Source File: AndroidMessenger.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean isScreenOn() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}
 
Example 4
Source File: FlutterIncallManagerPlugin.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
private void debugScreenPowerState() {
    String isDeviceIdleMode = "unknow"; // --- API 23
    String isIgnoringBatteryOptimizations = "unknow"; // --- API 23
    String isPowerSaveMode = "unknow"; // --- API 21
    String isInteractive = "unknow"; // --- API 20 ( before since API 7 is: isScreenOn())
    String screenState = "unknow"; // --- API 20

    if (Build.VERSION.SDK_INT >= 23) {
        isDeviceIdleMode = String.format("%s", mPowerManager.isDeviceIdleMode());
        isIgnoringBatteryOptimizations = String.format("%s", mPowerManager.isIgnoringBatteryOptimizations(mPackageName));
    }
    if (Build.VERSION.SDK_INT >= 21) {
        isPowerSaveMode = String.format("%s", mPowerManager.isPowerSaveMode());
    }
    if (Build.VERSION.SDK_INT >= 20) {
        isInteractive = String.format("%s", mPowerManager.isInteractive());
        Display display = mWindowManager.getDefaultDisplay();
        switch (display.getState()) {
            case Display.STATE_OFF:
                screenState = "STATE_OFF";
                break;
            case Display.STATE_ON:
                screenState = "STATE_ON";
                break;
            case Display.STATE_DOZE:
                screenState = "STATE_DOZE";
                break;
            case Display.STATE_DOZE_SUSPEND:
                screenState = "STATE_DOZE_SUSPEND";
                break;
            default:
                break;
        }
    } else {
        isInteractive = String.format("%s", mPowerManager.isScreenOn());
    }
    Log.d(TAG, String.format("debugScreenPowerState(): screenState='%s', isInteractive='%s', isPowerSaveMode='%s', isDeviceIdleMode='%s', isIgnoringBatteryOptimizations='%s'", screenState, isInteractive, isPowerSaveMode, isDeviceIdleMode, isIgnoringBatteryOptimizations));
}
 
Example 5
Source File: Utils.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isScreenOn(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                return true;
            }
        }
        return false;
    } else {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isScreenOn();
    }
}
 
Example 6
Source File: HelperNotification.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean isScreenOn(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        @SuppressLint("WrongConstant") DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }
}
 
Example 7
Source File: Utils.java    From aosp_screen_stabilization with Apache License 2.0 5 votes vote down vote up
public static boolean isScreenOn(Context context)
{
	DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
	for (Display display : dm.getDisplays())
	{
		if (display.getState() != Display.STATE_OFF)
			return true;
	}
	return false;
}
 
Example 8
Source File: Utils.java    From ForceDoze with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isScreenOn(Context context) {
    DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
    for (Display display : dm.getDisplays()) {
        if (display.getState() == Display.STATE_ON
                || display.getState() == Display.STATE_UNKNOWN) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: StarterService.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
boolean isScreenOn() {
    if (Utils.isAndroidNewerThanL()) {
        DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                return true;
            }
        }
        return false;
    } else {
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        return powerManager.isScreenOn();
    }
}
 
Example 10
Source File: ChargeChangeReceiver.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
private boolean isDisplayOn(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                return true;
            }
        }
        return false;
    } else {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isScreenOn();
    }
}
 
Example 11
Source File: InCallManagerModule.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
private void debugScreenPowerState() {
    String isDeviceIdleMode = "unknow"; // --- API 23
    String isIgnoringBatteryOptimizations = "unknow"; // --- API 23
    String isPowerSaveMode = "unknow"; // --- API 21
    String isInteractive = "unknow"; // --- API 20 ( before since API 7 is: isScreenOn())
    String screenState = "unknow"; // --- API 20

    if (android.os.Build.VERSION.SDK_INT >= 23) {
        isDeviceIdleMode = String.format("%s", mPowerManager.isDeviceIdleMode());
        isIgnoringBatteryOptimizations = String.format("%s", mPowerManager.isIgnoringBatteryOptimizations(mPackageName));
    }
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        isPowerSaveMode = String.format("%s", mPowerManager.isPowerSaveMode());
    }
    if (android.os.Build.VERSION.SDK_INT >= 20) {
        isInteractive = String.format("%s", mPowerManager.isInteractive());
        Display display = mWindowManager.getDefaultDisplay();
        switch (display.getState()) {
            case Display.STATE_OFF:
                screenState = "STATE_OFF";
                break;
            case Display.STATE_ON:
                screenState = "STATE_ON";
                break;
            case Display.STATE_DOZE:
                screenState = "STATE_DOZE";
                break;
            case Display.STATE_DOZE_SUSPEND:
                screenState = "STATE_DOZE_SUSPEND";
                break;
            default:
                break;
        }
    } else {
        isInteractive = String.format("%s", mPowerManager.isScreenOn());
    }
    Log.d(TAG, String.format("debugScreenPowerState(): screenState='%s', isInteractive='%s', isPowerSaveMode='%s', isDeviceIdleMode='%s', isIgnoringBatteryOptimizations='%s'", screenState, isInteractive, isPowerSaveMode, isDeviceIdleMode, isIgnoringBatteryOptimizations));
}
 
Example 12
Source File: Utils.java    From KernelAdiutor with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isScreenOn(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                return true;
            }
        }
        return false;
    } else {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isScreenOn();
    }
}
 
Example 13
Source File: NotificationService.java    From an2linuxclient with GNU General Public License v3.0 4 votes vote down vote up
private boolean filter(StatusBarNotification sbn) {
    String packageName = sbn.getPackageName();
    if (!globalEnabled() || !appEnabled(packageName)) {
        return false;
    }
    boolean usingCustomSettings = isUsingCustomSettings(packageName);
    SharedPreferences sp;
    if (usingCustomSettings) {
        sp = getSharedPreferences(getString(R.string.notification_settings_custom), MODE_PRIVATE);
    } else {
        sp = getSharedPreferences(getString(R.string.notification_settings_global), MODE_PRIVATE);
    }

    boolean isAn2linuxTestNotification = packageName.startsWith("kiwi.root.an2linuxclient");
    if (dontSendIfScreenIsOn(sp, packageName, usingCustomSettings) && !isAn2linuxTestNotification) {
        boolean screenIsOn = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
            for (Display display : dm.getDisplays()) {
                if (display.getState() == Display.STATE_ON) {
                    // private as in samsung always-on feature, not sure if this is how it works
                    // https://stackoverflow.com/questions/2474367/how-can-i-tell-if-the-screen-is-on-in-android#comment71534994_17348755
                    boolean displayIsPrivate = (display.getFlags() & Display.FLAG_PRIVATE) == Display.FLAG_PRIVATE;
                    if (!displayIsPrivate) {
                        screenIsOn = true;
                        break;
                    }
                }
            }
        } else {
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            if (powerManager.isScreenOn()){
                screenIsOn = true;
            }
        }

        if (screenIsOn) {
            return false;
        }
    }

    int flags = sbn.getNotification().flags;
    if (isOngoing(flags) && blockOngoing(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (isForeground(flags) && blockForeground(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH){
        if (isGroupSummary(flags) && blockGroupSummary(sp, packageName, usingCustomSettings)){
            return false;
        }
        if (isLocalOnly(flags) && blockLocalOnly(sp, packageName, usingCustomSettings)){
            return false;
        }
    }
    return priorityAllowed(sp, packageName, usingCustomSettings, sbn.getNotification().priority);
}
 
Example 14
Source File: NotificationService.java    From an2linuxclient with GNU General Public License v3.0 4 votes vote down vote up
private boolean filter(StatusBarNotification sbn) {
    String packageName = sbn.getPackageName();
    if (!globalEnabled() || !appEnabled(packageName)) {
        return false;
    }
    boolean usingCustomSettings = isUsingCustomSettings(packageName);
    SharedPreferences sp;
    if (usingCustomSettings) {
        sp = getSharedPreferences(getString(R.string.notification_settings_custom), MODE_PRIVATE);
    } else {
        sp = getSharedPreferences(getString(R.string.notification_settings_global), MODE_PRIVATE);
    }

    boolean isAn2linuxTestNotification = packageName.startsWith("kiwi.root.an2linuxclient");
    if (dontSendIfScreenIsOn(sp, packageName, usingCustomSettings) && !isAn2linuxTestNotification) {
        boolean screenIsOn = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
            for (Display display : dm.getDisplays()) {
                if (display.getState() == Display.STATE_ON) {
                    // private as in samsung always-on feature, not sure if this is how it works
                    // https://stackoverflow.com/questions/2474367/how-can-i-tell-if-the-screen-is-on-in-android#comment71534994_17348755
                    boolean displayIsPrivate = (display.getFlags() & Display.FLAG_PRIVATE) == Display.FLAG_PRIVATE;
                    if (!displayIsPrivate) {
                        screenIsOn = true;
                        break;
                    }
                }
            }
        } else {
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            if (powerManager.isScreenOn()){
                screenIsOn = true;
            }
        }

        if (screenIsOn) {
            return false;
        }
    }

    int flags = sbn.getNotification().flags;
    if (isOngoing(flags) && blockOngoing(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (isForeground(flags) && blockForeground(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH){
        if (isGroupSummary(flags) && blockGroupSummary(sp, packageName, usingCustomSettings)){
            return false;
        }
        if (isLocalOnly(flags) && blockLocalOnly(sp, packageName, usingCustomSettings)){
            return false;
        }
    }
    return priorityAllowed(sp, packageName, usingCustomSettings, sbn.getNotification().priority);
}
 
Example 15
Source File: PowerUtils.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public static boolean isScreenOn(@NonNull Context context) {
    display_api:
    if (Device.hasKitKatWatchApi()) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = dm.getDisplays(null);
        Display display = null;
        if (displays == null || displays.length == 0) {
            break display_api;
        } else if (displays.length > 1) {
            Timber.tag(TAG).i("The number of logical displays is " + displays.length);
        }

        for (Display d : displays) {
            final boolean virtual = Operator.bitAnd(d.getFlags(), Display.FLAG_PRESENTATION);
            if (d.isValid() && !virtual) {
                display = d;

                final int type;
                try {
                    Method method = Display.class.getDeclaredMethod("getType");
                    method.setAccessible(true);
                    type = (int) method.invoke(d);
                } catch (Exception e) {
                    continue;
                }

                if (type == 1 /* built-in display */) {
                    break;
                }
            }
        }

        if (display == null) {
            return false;
        }

        Timber.tag(TAG).i("Display state=" + display.getState());
        return display.getState() == Display.STATE_ON;
    }
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    return isInteractive(pm);
}