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

Commit 25d8b05f authored by Jozef BABJAK's avatar Jozef BABJAK
Browse files

Ensuring thread-safe usage of DateFormat.

DateFormat is inherently unsafe for multithreaded use. This patch adds
proper synchronization. The 'sLock' is re-used. To avoid two consecutive
lockings, locked version of initFormatStrings() method is created and
used where appropriate, i.e. in original method AND in added synchronized
block.

Change-Id: Id3c34613623e743e703aadb2342aa788679dda8a
parent 7118bdec
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -642,6 +642,11 @@ public class DateUtils

    private static void initFormatStrings() {
        synchronized (sLock) {
            initFormatStringsLocked();
        }
    }

    private static void initFormatStringsLocked() {
        Resources r = Resources.getSystem();
        Configuration cfg = r.getConfiguration();
        if (sLastConfig == null || !sLastConfig.equals(cfg)) {
@@ -651,7 +656,6 @@ public class DateUtils
            sElapsedFormatHMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_h_mm_ss);
        }
    }
    }

    /**
     * Format a time so it appears like it would in the status bar clock.
@@ -659,9 +663,11 @@ public class DateUtils
     * @hide
     */
    public static final CharSequence timeString(long millis) {
        initFormatStrings();
        synchronized (sLock) {
            initFormatStringsLocked();
            return sStatusTimeFormat.format(millis);
        }
    }

    /**
     * Formats an elapsed time in the form "MM:SS" or "H:MM:SS"