From c7f440dcd86ae88fc1b51e4394d66e4e80c9e53e Mon Sep 17 00:00:00 2001
From: Ji-Hwan Lee
- * First tuned {@link TvView} becomes main automatically, and keeps to be main until {@link - * #setMain} is called for other {@link TvView}. Note that main {@link TvView} won't be reset - * even when current main {@link TvView} is removed from view hierarchy. + * First tuned {@link TvView} becomes main automatically, and keeps to be main until either + * {@link #reset} is called for the main {@link TvView} or {@link #setMain} is called for other + * {@link TvView}. *
* @hide */ @SystemApi public void setMain() { synchronized (sMainTvViewLock) { - sMainTvView = this; + sMainTvView = new WeakReference(this); if (hasWindowFocus() && mSession != null) { mSession.setMain(); } @@ -288,8 +291,8 @@ public class TvView extends ViewGroup { throw new IllegalArgumentException("inputId cannot be null or an empty string"); } synchronized (sMainTvViewLock) { - if (sMainTvView == null) { - sMainTvView = this; + if (sMainTvView.get() == null) { + sMainTvView = new WeakReference(this); } } if (mSessionCallback != null && mSessionCallback.mInputId.equals(inputId)) { @@ -320,6 +323,11 @@ public class TvView extends ViewGroup { */ public void reset() { if (DEBUG) Log.d(TAG, "reset()"); + synchronized (sMainTvViewLock) { + if (this == sMainTvView.get()) { + sMainTvView = NULL_TV_VIEW; + } + } if (mSession != null) { release(); resetSurfaceView(); @@ -530,7 +538,7 @@ public class TvView extends ViewGroup { // Other app may have shown its own main TvView. // Set main again to regain main session. synchronized (sMainTvViewLock) { - if (hasFocus && this == sMainTvView && mSession != null) { + if (hasFocus && this == sMainTvView.get() && mSession != null) { mSession.setMain(); } } @@ -828,7 +836,7 @@ public class TvView extends ViewGroup { mSession = session; if (session != null) { synchronized (sMainTvViewLock) { - if (hasWindowFocus() && TvView.this == sMainTvView) { + if (hasWindowFocus() && TvView.this == sMainTvView.get()) { mSession.setMain(); } }