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

Commit 0f50a170 authored by YK Hung's avatar YK Hung Committed by Android (Google) Code Review
Browse files

Merge "Remove WirelessChargingNotification related methods." into main

parents c6375079 1ceb5bea
Loading
Loading
Loading
Loading
+0 −57
Original line number Diff line number Diff line
@@ -785,30 +785,6 @@ public class Utils {
        return false;
    }

    /** Whether to show the wireless charging notification. */
    public static boolean shouldShowWirelessChargingNotification(
            @NonNull Context context, @NonNull String tag) {
        try {
            return shouldShowWirelessChargingNotificationInternal(context, tag);
        } catch (Exception e) {
            Log.e(tag, "shouldShowWirelessChargingNotification()", e);
            return false;
        }
    }

    /** Stores the timestamp of the wireless charging notification. */
    public static void updateWirelessChargingNotificationTimestamp(
            @NonNull Context context, long timestamp, @NonNull String tag) {
        try {
            Secure.putLong(
                    context.getContentResolver(),
                    WIRELESS_CHARGING_NOTIFICATION_TIMESTAMP,
                    timestamp);
        } catch (Exception e) {
            Log.e(tag, "setWirelessChargingNotificationTimestamp()", e);
        }
    }

    /** Whether to show the wireless charging warning in Settings. */
    public static boolean shouldShowWirelessChargingWarningTip(
            @NonNull Context context, @NonNull String tag) {
@@ -833,37 +809,4 @@ public class Utils {
            Log.e(tag, "setWirelessChargingWarningEnabled()", e);
        }
    }

    private static boolean shouldShowWirelessChargingNotificationInternal(
            @NonNull Context context, @NonNull String tag) {
        final long lastNotificationTimeMillis =
                Secure.getLong(
                        context.getContentResolver(),
                        WIRELESS_CHARGING_NOTIFICATION_TIMESTAMP,
                        WIRELESS_CHARGING_DEFAULT_TIMESTAMP);
        if (isWirelessChargingNotificationDisabled(lastNotificationTimeMillis)) {
            return false;
        }
        if (isInitialWirelessChargingNotification(lastNotificationTimeMillis)) {
            updateWirelessChargingNotificationTimestamp(context, System.currentTimeMillis(), tag);
            updateWirelessChargingWarningEnabled(context, /* enabled= */ true, tag);
            return true;
        }
        final long durationMillis = System.currentTimeMillis() - lastNotificationTimeMillis;
        final boolean show = durationMillis > WIRELESS_CHARGING_NOTIFICATION_THRESHOLD_MILLIS;
        Log.d(tag, "shouldShowWirelessChargingNotification = " + show);
        if (show) {
            updateWirelessChargingNotificationTimestamp(context, System.currentTimeMillis(), tag);
            updateWirelessChargingWarningEnabled(context, /* enabled= */ true, tag);
        }
        return show;
    }

    private static boolean isWirelessChargingNotificationDisabled(long lastNotificationTimeMillis) {
        return lastNotificationTimeMillis == Long.MIN_VALUE;
    }

    private static boolean isInitialWirelessChargingNotification(long lastNotificationTimeMillis) {
        return lastNotificationTimeMillis == WIRELESS_CHARGING_DEFAULT_TIMESTAMP;
    }
}
+0 −81
Original line number Diff line number Diff line
@@ -16,9 +16,7 @@
package com.android.settingslib;

import static com.android.settingslib.Utils.STORAGE_MANAGER_ENABLED_PROPERTY;
import static com.android.settingslib.Utils.WIRELESS_CHARGING_DEFAULT_TIMESTAMP;
import static com.android.settingslib.Utils.shouldShowWirelessChargingWarningTip;
import static com.android.settingslib.Utils.updateWirelessChargingNotificationTimestamp;

import static com.google.common.truth.Truth.assertThat;

@@ -62,7 +60,6 @@ import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowSettings;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@@ -546,77 +543,6 @@ public class UtilsTest {
        assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse();
    }

    @Test
    public void shouldShowWirelessChargingNotification_neverSendNotification_returnTrue() {
        updateWirelessChargingNotificationTimestamp(
                mContext, WIRELESS_CHARGING_DEFAULT_TIMESTAMP, TAG);

        assertThat(Utils.shouldShowWirelessChargingNotification(mContext, TAG)).isTrue();
    }

    @Test
    public void shouldShowNotification_neverSendNotification_updateTimestampAndEnabledState() {
        updateWirelessChargingNotificationTimestamp(
                mContext, WIRELESS_CHARGING_DEFAULT_TIMESTAMP, TAG);

        Utils.shouldShowWirelessChargingNotification(mContext, TAG);

        assertThat(getWirelessChargingNotificationTimestamp())
                .isNotEqualTo(WIRELESS_CHARGING_DEFAULT_TIMESTAMP);
        assertThat(shouldShowWirelessChargingWarningTip(mContext, TAG)).isTrue();
    }

    @Test
    public void shouldShowWirelessChargingNotification_notificationDisabled_returnFalse() {
        updateWirelessChargingNotificationTimestamp(mContext, CURRENT_TIMESTAMP, TAG);

        assertThat(Utils.shouldShowWirelessChargingNotification(mContext, TAG)).isFalse();
    }

    @Test
    public void shouldShowWirelessChargingNotification_withinTimeThreshold_returnFalse() {
        updateWirelessChargingNotificationTimestamp(mContext, CURRENT_TIMESTAMP, TAG);

        assertThat(Utils.shouldShowWirelessChargingNotification(mContext, TAG)).isFalse();
    }

    @Test
    public void shouldShowWirelessChargingNotification_exceedTimeThreshold_returnTrue() {
        final long monthAgo = Duration.ofDays(31).toMillis();
        final long timestamp = CURRENT_TIMESTAMP - monthAgo;
        updateWirelessChargingNotificationTimestamp(mContext, timestamp, TAG);

        assertThat(Utils.shouldShowWirelessChargingNotification(mContext, TAG)).isTrue();
    }

    @Test
    public void shouldShowNotification_exceedTimeThreshold_updateTimestampAndEnabledState() {
        final long monthAgo = Duration.ofDays(31).toMillis();
        final long timestamp = CURRENT_TIMESTAMP - monthAgo;
        updateWirelessChargingNotificationTimestamp(mContext, timestamp, TAG);

        Utils.shouldShowWirelessChargingNotification(mContext, TAG);

        assertThat(getWirelessChargingNotificationTimestamp()).isNotEqualTo(timestamp);
        assertThat(shouldShowWirelessChargingWarningTip(mContext, TAG)).isTrue();
    }

    @Test
    public void updateWirelessChargingNotificationTimestamp_dismissForever_setMinValue() {
        updateWirelessChargingNotificationTimestamp(mContext, Long.MIN_VALUE, TAG);

        assertThat(getWirelessChargingNotificationTimestamp()).isEqualTo(Long.MIN_VALUE);
    }

    @Test
    public void updateWirelessChargingNotificationTimestamp_notDismissForever_setTimestamp() {
        updateWirelessChargingNotificationTimestamp(mContext, CURRENT_TIMESTAMP, TAG);

        assertThat(getWirelessChargingNotificationTimestamp())
                .isNotEqualTo(WIRELESS_CHARGING_DEFAULT_TIMESTAMP);
        assertThat(getWirelessChargingNotificationTimestamp()).isNotEqualTo(Long.MIN_VALUE);
    }

    @Test
    public void shouldShowWirelessChargingWarningTip_enabled_returnTrue() {
        Utils.updateWirelessChargingWarningEnabled(mContext, true, TAG);
@@ -644,11 +570,4 @@ public class UtilsTest {
        when(mUsbPortStatus.isConnected()).thenReturn(true);
        when(mUsbPortStatus.getComplianceWarnings()).thenReturn(new int[] {complianceWarningType});
    }

    private long getWirelessChargingNotificationTimestamp() {
        return Settings.Secure.getLong(
                mContext.getContentResolver(),
                Utils.WIRELESS_CHARGING_NOTIFICATION_TIMESTAMP,
                WIRELESS_CHARGING_DEFAULT_TIMESTAMP);
    }
}