Search in sources :

Example 1 with DeviceBatteryInfo

use of org.kde.kdeconnect.Plugins.BatteryPlugin.DeviceBatteryInfo in project kdeconnect-android by KDE.

the class DeviceFragment method displayBatteryInfoIfPossible.

/**
 * This method tries to display battery info for the remote device. Includes
 * <ul>
 *     <li>Current charge as a percentage</li>
 *     <li>Whether the remote device is low on power</li>
 *     <li>Whether the remote device is currently charging</li>
 * </ul>
 * <p>
 *     This will show a simple message on the view instead if we don't have
 *     accurate info right now.
 * </p>
 */
private void displayBatteryInfoIfPossible() {
    boolean canDisplayBatteryInfo = false;
    BatteryPlugin batteryPlugin = (BatteryPlugin) device.getLoadedPlugins().get(Plugin.getPluginKey(BatteryPlugin.class));
    if (batteryPlugin != null) {
        DeviceBatteryInfo info = batteryPlugin.getRemoteBatteryInfo();
        if (info != null) {
            canDisplayBatteryInfo = true;
            Context ctx = deviceBinding.viewBatteryStatus.getContext();
            boolean isCharging = info.isCharging();
            @StringRes int resId;
            if (isCharging) {
                resId = R.string.battery_status_charging_format;
            } else if (BatteryPlugin.isLowBattery(info)) {
                resId = R.string.battery_status_low_format;
            } else {
                resId = R.string.battery_status_format;
            }
            deviceBinding.viewBatteryStatus.setChecked(isCharging);
            deviceBinding.viewBatteryStatus.setText(ctx.getString(resId, info.getCurrentCharge()));
        }
    }
    if (!canDisplayBatteryInfo) {
        deviceBinding.viewBatteryStatus.setText(R.string.battery_status_unknown);
    }
}
Also used : Context(android.content.Context) BatteryPlugin(org.kde.kdeconnect.Plugins.BatteryPlugin.BatteryPlugin) DeviceBatteryInfo(org.kde.kdeconnect.Plugins.BatteryPlugin.DeviceBatteryInfo) StringRes(androidx.annotation.StringRes)

Aggregations

Context (android.content.Context)1 StringRes (androidx.annotation.StringRes)1 BatteryPlugin (org.kde.kdeconnect.Plugins.BatteryPlugin.BatteryPlugin)1 DeviceBatteryInfo (org.kde.kdeconnect.Plugins.BatteryPlugin.DeviceBatteryInfo)1