Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b0f1352b authored by Yifan Hong's avatar Yifan Hong
Browse files

Fix BatteryManager.getIntProperty returns 0 as invalid value.

Long.MIN_VALUE cast to Integer.MIN_VALUE always returns 0.

Change-Id: Ief7a1fd12a356c83a4ef3f28139c330034507dfa
Fixes: 37090343
Test: BatteryManagerTest
parent b75425a0
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -304,14 +304,14 @@ final class SystemServiceRegistry {
            }});

        registerService(Context.BATTERY_SERVICE, BatteryManager.class,
                new StaticServiceFetcher<BatteryManager>() {
                new CachedServiceFetcher<BatteryManager>() {
            @Override
            public BatteryManager createService() throws ServiceNotFoundException {
            public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                IBatteryStats stats = IBatteryStats.Stub.asInterface(
                        ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
                IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
                        .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
                return new BatteryManager(stats, registrar);
                return new BatteryManager(ctx, stats, registrar);
            }});

        registerService(Context.NFC_SERVICE, NfcManager.class,
+27 −15
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.os;
import android.annotation.SystemService;
import android.content.Context;
import android.hardware.health.V1_0.Constants;

import com.android.internal.app.IBatteryStats;

/**
@@ -216,6 +217,7 @@ public class BatteryManager {
     */
    public static final int BATTERY_PROPERTY_STATUS = 6;

    private final Context mContext;
    private final IBatteryStats mBatteryStats;
    private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;

@@ -223,6 +225,7 @@ public class BatteryManager {
     * @removed Was previously made visible by accident.
     */
    public BatteryManager() {
        mContext = null;
        mBatteryStats = IBatteryStats.Stub.asInterface(
                ServiceManager.getService(BatteryStats.SERVICE_NAME));
        mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(
@@ -230,8 +233,10 @@ public class BatteryManager {
    }

    /** {@hide} */
    public BatteryManager(IBatteryStats batteryStats,
    public BatteryManager(Context context,
            IBatteryStats batteryStats,
            IBatteryPropertiesRegistrar batteryPropertiesRegistrar) {
        mContext = context;
        mBatteryStats = batteryStats;
        mBatteryPropertiesRegistrar = batteryPropertiesRegistrar;
    }
@@ -278,16 +283,23 @@ public class BatteryManager {
    }

    /**
     * Return the value of a battery property of integer type.  If the
     * platform does not provide the property queried, this value will
     * be Integer.MIN_VALUE.
     * Return the value of a battery property of integer type.
     *
     * @param id identifier of the requested property
     *
     * @return the property value, or Integer.MIN_VALUE if not supported.
     * @return the property value. If the property is not supported or there is any other error,
     *    return (a) 0 if {@code targetSdkVersion < VERSION_CODES.P} or (b) Integer.MIN_VALUE
     *    if {@code targetSdkVersion >= VERSION_CODES.P}.
     */
    public int getIntProperty(int id) {
        return (int)queryProperty(id);
        long value = queryProperty(id);
        if (value == Long.MIN_VALUE && mContext != null
                && mContext.getApplicationInfo().targetSdkVersion
                    >= android.os.Build.VERSION_CODES.P) {
            return Integer.MIN_VALUE;
        }

        return (int) value;
    }

    /**