From fb23ff98d90936bbae0d8f8ee3cd4465d9659680 Mon Sep 17 00:00:00 2001 From: Heemin Seog Date: Wed, 5 Feb 2020 17:35:19 -0800 Subject: [PATCH] Add null check for handler on Clock view On reboot, we've encountered an issue where the handler returns null, causing a null pointer exception in the broadcast receiver. In this CL, we add a null check and an error message to prevent a sysui crash. Bug: 148869042 Test: build, reboot a few times Change-Id: Iac7f3a538be9a53d4a76926523aa2a3f4f22723d Merged-In: Ie8a2627004876cf35291d52bd3686a5014498f52 --- .../systemui/statusbar/policy/Clock.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java index c2c3f81527e8d..371de7439f8f1 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java @@ -34,6 +34,7 @@ import android.text.format.DateFormat; import android.text.style.CharacterStyle; import android.text.style.RelativeSizeSpan; import android.util.AttributeSet; +import android.util.Log; import android.view.Display; import android.view.View; import android.widget.TextView; @@ -67,6 +68,7 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C DarkReceiver, ConfigurationListener { public static final String CLOCK_SECONDS = "clock_seconds"; + private static final String TAG = "StatusBarClock"; private static final String CLOCK_SUPER_PARCELABLE = "clock_super_parcelable"; private static final String CURRENT_USER_ID = "current_user_id"; private static final String VISIBLE_BY_POLICY = "visible_by_policy"; @@ -228,9 +230,18 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); + Handler handler = getHandler(); + if (handler == null) { + Log.e(TAG, + "Received intent, but handler is null - still attached to window? Window " + + "token: " + + getWindowToken()); + return; + } + if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { String tz = intent.getStringExtra("time-zone"); - getHandler().post(() -> { + handler.post(() -> { mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); if (mClockFormat != null) { mClockFormat.setTimeZone(mCalendar.getTimeZone()); @@ -238,14 +249,14 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C }); } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { final Locale newLocale = getResources().getConfiguration().locale; - getHandler().post(() -> { + handler.post(() -> { if (!newLocale.equals(mLocale)) { mLocale = newLocale; mClockFormatString = ""; // force refresh } }); } - getHandler().post(() -> updateClock()); + handler.post(() -> updateClock()); } };