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

Commit 57002f45 authored by Angela Wang's avatar Angela Wang
Browse files

Add flush() method to flush the data whenever needed

To sync the data accross processes, sometimes need to flush the data
manually. Add the method and put the I/O into background to prevent
trafiic in main thread.

Flag: EXEMPT bugfix
Bug: 357878944
Test: atest HearingDeviceLocalDataManagerTest
Change-Id: I0c096cfbdf240ed63af8abc80f4d6e102864ce8e
parent 9243216c
Loading
Loading
Loading
Loading
+12 −9
Original line number Diff line number Diff line
@@ -148,6 +148,14 @@ public class HearingDeviceLocalDataManager {
        }
    }

    /** Flushes the data into Settings . */
    public synchronized void flush() {
        if (!mIsStarted) {
            return;
        }
        putAmbientVolumeSettings();
    }

    /**
     * Puts the local data of the corresponding hearing device.
     *
@@ -274,9 +282,6 @@ public class HearingDeviceLocalDataManager {
            notifyIfDataChanged(mAddrToDataMap, updatedAddrToDataMap);
            mAddrToDataMap.clear();
            mAddrToDataMap.putAll(updatedAddrToDataMap);
            if (DEBUG) {
                Log.v(TAG, "getLocalDataFromSettings, " + mAddrToDataMap + ", manager: " + this);
            }
        }
    }

@@ -287,12 +292,10 @@ public class HearingDeviceLocalDataManager {
                builder.append(KEY_ADDR).append("=").append(entry.getKey());
                builder.append(entry.getValue().toSettingsFormat()).append(";");
            }
            if (DEBUG) {
                Log.v(TAG, "putAmbientVolumeSettings, " + builder + ", manager: " + this);
            }
            ThreadUtils.postOnBackgroundThread(() -> {
                Settings.Global.putStringForUser(mContext.getContentResolver(),
                    LOCAL_AMBIENT_VOLUME_SETTINGS, builder.toString(),
                    UserHandle.USER_SYSTEM);
                        LOCAL_AMBIENT_VOLUME_SETTINGS, builder.toString(), UserHandle.USER_SYSTEM);
            });
        }
    }

+14 −1
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import android.provider.Settings;

import androidx.test.core.app.ApplicationProvider;

import com.android.settingslib.utils.ThreadUtils;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -49,7 +51,10 @@ import java.util.Map;

/** Tests for {@link HearingDeviceLocalDataManager}. */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {HearingDeviceLocalDataManagerTest.ShadowGlobal.class})
@Config(shadows = {
        HearingDeviceLocalDataManagerTest.ShadowGlobal.class,
        HearingDeviceLocalDataManagerTest.ShadowThreadUtils.class,
})
public class HearingDeviceLocalDataManagerTest {

    private static final String TEST_ADDRESS = "XX:XX:XX:XX:11:22";
@@ -249,4 +254,12 @@ public class HearingDeviceLocalDataManagerTest {
            return sDataMap.computeIfAbsent(cr, k -> new HashMap<>());
        }
    }

    @Implements(value = ThreadUtils.class)
    public static class ShadowThreadUtils {
        @Implementation
        protected static void postOnBackgroundThread(Runnable runnable) {
            runnable.run();
        }
    }
}