Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fragment/fragment/api/public_plus_experimental_current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ package androidx.fragment.app.strictmode {

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public final class FragmentStrictMode {
method public static androidx.fragment.app.strictmode.FragmentStrictMode.Policy getDefaultPolicy();
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onSetUserVisibleHint(androidx.fragment.app.Fragment);
method public static void setDefaultPolicy(androidx.fragment.app.strictmode.FragmentStrictMode.Policy);
}

Expand All @@ -475,11 +476,16 @@ package androidx.fragment.app.strictmode {
@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static final class FragmentStrictMode.Policy.Builder {
ctor public FragmentStrictMode.Policy.Builder();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy build();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectSetUserVisibleHint();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyDeath();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyListener(androidx.fragment.app.strictmode.FragmentStrictMode.OnViolationListener);
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyLog();
}

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public final class SetUserVisibleHintViolation extends androidx.fragment.app.strictmode.Violation {
ctor public SetUserVisibleHintViolation();
}

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public abstract class Violation extends java.lang.RuntimeException {
ctor public Violation();
}
Expand Down
6 changes: 6 additions & 0 deletions fragment/fragment/api/restricted_current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ package androidx.fragment.app.strictmode {

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public final class FragmentStrictMode {
method public static androidx.fragment.app.strictmode.FragmentStrictMode.Policy getDefaultPolicy();
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onSetUserVisibleHint(androidx.fragment.app.Fragment);
method public static void setDefaultPolicy(androidx.fragment.app.strictmode.FragmentStrictMode.Policy);
}

Expand All @@ -501,11 +502,16 @@ package androidx.fragment.app.strictmode {
@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static final class FragmentStrictMode.Policy.Builder {
ctor public FragmentStrictMode.Policy.Builder();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy build();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectSetUserVisibleHint();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyDeath();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyListener(androidx.fragment.app.strictmode.FragmentStrictMode.OnViolationListener);
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder penaltyLog();
}

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public final class SetUserVisibleHintViolation extends androidx.fragment.app.strictmode.Violation {
ctor public SetUserVisibleHintViolation();
}

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public abstract class Violation extends java.lang.RuntimeException {
ctor public Violation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,18 @@ public class FragmentStrictModeTest {
assertThat(thread).isEqualTo(Looper.getMainLooper().thread)
}
}

@Test
public fun detectSetUserVisibleHint() {
var violation: Violation? = null
val policy = FragmentStrictMode.Policy.Builder()
.detectSetUserVisibleHint()
.penaltyListener { violation = it }
.build()
FragmentStrictMode.setDefaultPolicy(policy)

@Suppress("DEPRECATION")
StrictFragment().userVisibleHint = true
assertThat(violation).isInstanceOf(SetUserVisibleHintViolation::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.app.SharedElementCallback;
import androidx.core.view.LayoutInflaterCompat;
import androidx.fragment.app.strictmode.FragmentStrictMode;
import androidx.lifecycle.HasDefaultViewModelProviderFactory;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
Expand Down Expand Up @@ -1319,6 +1320,7 @@ public void setMenuVisibility(boolean menuVisible) {
*/
@Deprecated
public void setUserVisibleHint(boolean isVisibleToUser) {
FragmentStrictMode.onSetUserVisibleHint(this);
if (!mUserVisibleHint && isVisibleToUser && mState < STARTED
&& mFragmentManager != null && isAdded() && mIsCreated) {
mFragmentManager.performPendingDeferredStart(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public final class FragmentStrictMode {

private enum Flag {
PENALTY_LOG,
PENALTY_DEATH
PENALTY_DEATH,

DETECT_SET_USER_VISIBLE_HINT
}

private FragmentStrictMode() {}
Expand Down Expand Up @@ -140,6 +142,14 @@ public Builder penaltyListener(@NonNull OnViolationListener listener) {
return this;
}

/** Detects calls to #{@link Fragment#setUserVisibleHint}. */
@NonNull
@SuppressLint("BuilderSetStyle")
public Builder detectSetUserVisibleHint() {
flags.add(Flag.DETECT_SET_USER_VISIBLE_HINT);
return this;
}

/**
* Construct the Policy instance.
*
Expand Down Expand Up @@ -185,9 +195,25 @@ private static Policy getNearestPolicy(@Nullable Fragment fragment) {
return defaultPolicy;
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
public static void onSetUserVisibleHint(@NonNull Fragment fragment) {
Policy policy = getNearestPolicy(fragment);
if (policy.flags.contains(Flag.DETECT_SET_USER_VISIBLE_HINT)) {
handlePolicyViolation(fragment, policy, new SetUserVisibleHintViolation());
}
}

@VisibleForTesting
static void onPolicyViolation(@NonNull Fragment fragment, @NonNull final Violation violation) {
final Policy policy = getNearestPolicy(fragment);
static void onPolicyViolation(@NonNull Fragment fragment, @NonNull Violation violation) {
Policy policy = getNearestPolicy(fragment);
handlePolicyViolation(fragment, policy, violation);
}

private static void handlePolicyViolation(
@NonNull Fragment fragment,
@NonNull final Policy policy,
@NonNull final Violation violation
) {
final String fragmentName = fragment.getClass().getName();

if (policy.flags.contains(Flag.PENALTY_LOG)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.fragment.app.strictmode;

import androidx.annotation.RestrictTo;

/** See #{@link FragmentStrictMode.Policy.Builder#detectSetUserVisibleHint()}. */
@RestrictTo(RestrictTo.Scope.LIBRARY) // TODO: Make API public as soon as we have a few checks
public final class SetUserVisibleHintViolation extends Violation {
}