From ada13040d020a289805ddc1f2f49079570cc0e0f Mon Sep 17 00:00:00 2001 From: Anthony Chen Date: Tue, 19 Jan 2016 16:57:20 -0800 Subject: [PATCH] Add null checks for various buttons on the Nav Bar. CarNavigationBarView extends NavigationBarView and provides a different layout for the navigation bar which does not include all these buttons. As a result, SystemUI will crash. Change-Id: I1be079814faa26925721ec86a6f108506f54b99e --- .../phone/NavigationBarGestureHelper.java | 15 ++-- .../statusbar/phone/NavigationBarView.java | 90 ++++++++++++++----- .../statusbar/phone/PhoneStatusBar.java | 28 ++++-- 3 files changed, 99 insertions(+), 34 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarGestureHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarGestureHelper.java index 2db08041be8e8..8bf45723beca2 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarGestureHelper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarGestureHelper.java @@ -162,11 +162,16 @@ public class NavigationBarGestureHelper extends GestureDetector.SimpleOnGestureL mDockWindowTouchSlopExceeded = false; mTouchDownX = (int) event.getX(); mTouchDownY = (int) event.getY(); - View recentsButton = mNavigationBarView.getRecentsButton(); - mDownOnRecents = mTouchDownX >= recentsButton.getLeft() - && mTouchDownX <= recentsButton.getRight() - && mTouchDownY >= recentsButton.getTop() - && mTouchDownY <= recentsButton.getBottom(); + + if (mNavigationBarView != null) { + View recentsButton = mNavigationBarView.getRecentsButton(); + if (recentsButton != null) { + mDownOnRecents = mTouchDownX >= recentsButton.getLeft() + && mTouchDownX <= recentsButton.getRight() + && mTouchDownY >= recentsButton.getTop() + && mTouchDownY <= recentsButton.getBottom(); + } + } } private boolean handleDragActionMoveEvent(MotionEvent event) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java index efa8f5b94721d..5309903266aab 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java @@ -21,6 +21,7 @@ import android.animation.LayoutTransition.TransitionListener; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.animation.ValueAnimator; +import android.annotation.Nullable; import android.app.ActivityManagerNative; import android.app.StatusBarManager; import android.content.Context; @@ -131,12 +132,15 @@ public class NavigationBarView extends LinearLayout { } public void onBackAltCleared() { + View backButton = getBackButton(); + View homeButton = getHomeButton(); + // When dismissing ime during unlock, force the back button to run the same appearance // animation as home (if we catch this condition early enough). - if (!mBackTransitioning && getBackButton().getVisibility() == VISIBLE - && mHomeAppearing && getHomeButton().getAlpha() == 0) { + if (backButton != null && !mBackTransitioning && backButton.getVisibility() == VISIBLE + && mHomeAppearing && homeButton != null && getHomeButton().getAlpha() == 0) { getBackButton().setAlpha(0); - ValueAnimator a = ObjectAnimator.ofFloat(getBackButton(), "alpha", 0, 1); + ValueAnimator a = ObjectAnimator.ofFloat(backButton, "alpha", 0, 1); a.setStartDelay(mStartDelay); a.setDuration(mDuration); a.setInterpolator(mInterpolator); @@ -222,7 +226,10 @@ public class NavigationBarView extends LinearLayout { } public void abortCurrentGesture() { - getHomeButton().abortCurrentGesture(); + View homeButton = getHomeButton(); + if (homeButton != null) { + getHomeButton().abortCurrentGesture(); + } } private H mHandler = new H(); @@ -235,26 +242,34 @@ public class NavigationBarView extends LinearLayout { return mRotatedViews; } + // The following Buttons can possibly return null if NavigationBarView is extended to provide + // a different layout and the buttons do not exist in that new layout. + @Nullable public KeyButtonView getRecentsButton() { return (KeyButtonView) getCurrentView().findViewById(R.id.recent_apps); } + @Nullable public View getMenuButton() { return getCurrentView().findViewById(R.id.menu); } + @Nullable public View getBackButton() { return getCurrentView().findViewById(R.id.back); } + @Nullable public KeyButtonView getHomeButton() { return (KeyButtonView) getCurrentView().findViewById(R.id.home); } + @Nullable public View getImeSwitchButton() { return getCurrentView().findViewById(R.id.ime_switcher); } + @Nullable public View getAppShelf() { return getCurrentView().findViewById(R.id.app_shelf); } @@ -329,19 +344,27 @@ public class NavigationBarView extends LinearLayout { ? getBackIconWithAlt(mCarMode, mVertical) : getBackIcon(mCarMode, mVertical); - ((ImageView) getBackButton()).setImageDrawable(backIcon); + View backButton = getBackButton(); + if (backButton != null && backButton instanceof ImageView) { + ((ImageView) backButton).setImageDrawable(backIcon); + } - ((ImageView) getRecentsButton()).setImageDrawable( - mVertical ? mRecentLandIcon : mRecentIcon); + ImageView recentsButton = getRecentsButton(); + if (recentsButton != null) { + recentsButton.setImageDrawable(mVertical ? mRecentLandIcon : mRecentIcon); + } - if (mCarMode) { - ((ImageView) getHomeButton()).setImageDrawable(mHomeCarModeIcon); - } else { - ((ImageView) getHomeButton()).setImageDrawable(mHomeDefaultIcon); + ImageView homeButton = getHomeButton(); + if (homeButton != null) { + homeButton.setImageDrawable(mCarMode ? mHomeCarModeIcon : mHomeDefaultIcon); } final boolean showImeButton = ((hints & StatusBarManager.NAVIGATION_HINT_IME_SHOWN) != 0); - getImeSwitchButton().setVisibility(showImeButton ? View.VISIBLE : View.INVISIBLE); + View imeSwitchButton = getImeSwitchButton(); + if (imeSwitchButton != null) { + imeSwitchButton.setVisibility(showImeButton ? View.VISIBLE : View.INVISIBLE); + } + // Update menu button in case the IME state has changed. setMenuVisibility(mShowMenu, true); @@ -382,9 +405,20 @@ public class NavigationBarView extends LinearLayout { disableRecent = false; } - getBackButton() .setVisibility(disableBack ? View.INVISIBLE : View.VISIBLE); - getHomeButton() .setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE); - getRecentsButton().setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE); + View backButton = getBackButton(); + if (backButton != null) { + backButton.setVisibility(disableBack ? View.INVISIBLE : View.VISIBLE); + } + + View homeButton = getHomeButton(); + if (homeButton != null) { + homeButton.setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE); + } + + View recentsButton = getRecentsButton(); + if (recentsButton != null) { + recentsButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE); + } // The app shelf, if it exists, follows the visibility of the home button. View appShelf = getAppShelf(); @@ -475,7 +509,11 @@ public class NavigationBarView extends LinearLayout { // Only show Menu if IME switcher not shown. final boolean shouldShow = mShowMenu && ((mNavigationIconHints & StatusBarManager.NAVIGATION_HINT_IME_SHOWN) == 0); - getMenuButton().setVisibility(shouldShow ? View.VISIBLE : View.INVISIBLE); + + View menuButton = getMenuButton(); + if (menuButton != null) { + menuButton.setVisibility(shouldShow ? View.VISIBLE : View.INVISIBLE); + } } @Override @@ -489,7 +527,10 @@ public class NavigationBarView extends LinearLayout { mCurrentView = mRotatedViews[Surface.ROTATION_0]; - getImeSwitchButton().setOnClickListener(mImeSwitcherClickListener); + View imeSwitchButton = getImeSwitchButton(); + if (imeSwitchButton != null) { + imeSwitchButton.setOnClickListener(mImeSwitcherClickListener); + } updateRTLOrder(); @@ -515,9 +556,13 @@ public class NavigationBarView extends LinearLayout { } private void updateRecentsIcon(boolean dockedStackExists) { - getRecentsButton().setImageResource(dockedStackExists - ? R.drawable.ic_sysbar_docked - : R.drawable.ic_sysbar_recent); + ImageView recentsButton = getRecentsButton(); + + if (recentsButton != null) { + recentsButton.setImageResource(dockedStackExists + ? R.drawable.ic_sysbar_docked + : R.drawable.ic_sysbar_recent); + } } public boolean isVertical() { @@ -533,7 +578,10 @@ public class NavigationBarView extends LinearLayout { mCurrentView.setVisibility(View.VISIBLE); updateLayoutTransitionsEnabled(); - getImeSwitchButton().setOnClickListener(mImeSwitcherClickListener); + View imeSwitchButton = getImeSwitchButton(); + if (imeSwitchButton != null) { + imeSwitchButton.setOnClickListener(mImeSwitcherClickListener); + } mDeadZone = (DeadZone) mCurrentView.findViewById(R.id.deadzone); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java index 8a5cf26937c9f..7a4adbcb5bf85 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java @@ -1184,14 +1184,26 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode, private void prepareNavigationBarView() { mNavigationBarView.reorient(); - mNavigationBarView.getRecentsButton().setOnClickListener(mRecentsClickListener); - mNavigationBarView.getRecentsButton().setOnTouchListener(mRecentsPreloadOnTouchListener); - mNavigationBarView.getRecentsButton().setLongClickable(true); - mNavigationBarView.getRecentsButton().setOnLongClickListener(mRecentsLongClickListener); - mNavigationBarView.getBackButton().setLongClickable(true); - mNavigationBarView.getBackButton().setOnLongClickListener(mLongPressBackListener); - mNavigationBarView.getHomeButton().setOnTouchListener(mHomeActionListener); - mNavigationBarView.getHomeButton().setOnLongClickListener(mLongPressHomeListener); + View recentsButton = mNavigationBarView.getRecentsButton(); + if (recentsButton != null) { + recentsButton.setOnClickListener(mRecentsClickListener); + recentsButton.setOnTouchListener(mRecentsPreloadOnTouchListener); + recentsButton.setLongClickable(true); + recentsButton.setOnLongClickListener(mRecentsLongClickListener); + } + + View backButton = mNavigationBarView.getBackButton(); + if (backButton != null) { + backButton.setLongClickable(true); + backButton.setOnLongClickListener(mLongPressBackListener); + } + + View homeButton = mNavigationBarView.getHomeButton(); + if (homeButton != null) { + homeButton.setOnTouchListener(mHomeActionListener); + homeButton.setOnLongClickListener(mLongPressHomeListener); + } + mAssistManager.onConfigurationChanged(); }