这是indexloc提供的服务,不要输入任何密码
Skip to content

[FragmentStrictMode] Detect calls to Fragment#setTargetFragment #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 @@ -463,6 +463,7 @@ package androidx.fragment.app.strictmode {
method public static androidx.fragment.app.strictmode.FragmentStrictMode.Policy getDefaultPolicy();
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onRetainInstanceUsage(androidx.fragment.app.Fragment);
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onSetUserVisibleHint(androidx.fragment.app.Fragment);
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onTargetFragmentUsage(androidx.fragment.app.Fragment);
method public static void setDefaultPolicy(androidx.fragment.app.strictmode.FragmentStrictMode.Policy);
}

Expand All @@ -479,6 +480,7 @@ package androidx.fragment.app.strictmode {
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy build();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectRetainInstanceUsage();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectSetUserVisibleHint();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectTargetFragmentUsage();
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();
Expand All @@ -492,6 +494,10 @@ package androidx.fragment.app.strictmode {
ctor public SetUserVisibleHintViolation();
}

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

@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 @@ -489,6 +489,7 @@ package androidx.fragment.app.strictmode {
method public static androidx.fragment.app.strictmode.FragmentStrictMode.Policy getDefaultPolicy();
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onRetainInstanceUsage(androidx.fragment.app.Fragment);
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onSetUserVisibleHint(androidx.fragment.app.Fragment);
method @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY) public static void onTargetFragmentUsage(androidx.fragment.app.Fragment);
method public static void setDefaultPolicy(androidx.fragment.app.strictmode.FragmentStrictMode.Policy);
}

Expand All @@ -505,6 +506,7 @@ package androidx.fragment.app.strictmode {
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy build();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectRetainInstanceUsage();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectSetUserVisibleHint();
method public androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder detectTargetFragmentUsage();
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();
Expand All @@ -518,6 +520,10 @@ package androidx.fragment.app.strictmode {
ctor public SetUserVisibleHintViolation();
}

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

@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 @@ -160,4 +160,28 @@ public class FragmentStrictModeTest {
StrictFragment().userVisibleHint = true
assertThat(violation).isInstanceOf(SetUserVisibleHintViolation::class.java)
}

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

@Suppress("DEPRECATION")
StrictFragment().setTargetFragment(StrictFragment(), 1)
assertThat(violation).isInstanceOf(TargetFragmentUsageViolation::class.java)

violation = null
@Suppress("DEPRECATION")
StrictFragment().targetFragment
assertThat(violation).isInstanceOf(TargetFragmentUsageViolation::class.java)

violation = null
@Suppress("DEPRECATION")
StrictFragment().targetRequestCode
assertThat(violation).isInstanceOf(TargetFragmentUsageViolation::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ public void setInitialSavedState(@Nullable SavedState state) {
@SuppressWarnings("ReferenceEquality, deprecation")
@Deprecated
public void setTargetFragment(@Nullable Fragment fragment, int requestCode) {
FragmentStrictMode.onTargetFragmentUsage(this);
// Don't allow a caller to set a target fragment in another FragmentManager,
// but there's a snag: people do set target fragments before fragments get added.
// We'll have the FragmentManager check that for validity when we move
Expand Down Expand Up @@ -853,6 +854,7 @@ public void setTargetFragment(@Nullable Fragment fragment, int requestCode) {
@Nullable
@Deprecated
final public Fragment getTargetFragment() {
FragmentStrictMode.onTargetFragmentUsage(this);
if (mTarget != null) {
// Ensure that any Fragment set with setTargetFragment is immediately
// available here
Expand All @@ -875,6 +877,7 @@ final public Fragment getTargetFragment() {
*/
@Deprecated
final public int getTargetRequestCode() {
FragmentStrictMode.onTargetFragmentUsage(this);
return mTargetRequestCode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private enum Flag {
PENALTY_DEATH,

DETECT_RETAIN_INSTANCE_USAGE,
DETECT_SET_USER_VISIBLE_HINT
DETECT_SET_USER_VISIBLE_HINT,
DETECT_TARGET_FRAGMENT_USAGE,
}

private FragmentStrictMode() {}
Expand Down Expand Up @@ -162,6 +163,17 @@ public Builder detectSetUserVisibleHint() {
return this;
}

/**
* Detects calls to #{@link Fragment#setTargetFragment},
* #{@link Fragment#getTargetFragment()} and #{@link Fragment#getTargetRequestCode()}.
*/
@NonNull
@SuppressLint("BuilderSetStyle")
public Builder detectTargetFragmentUsage() {
flags.add(Flag.DETECT_TARGET_FRAGMENT_USAGE);
return this;
}

/**
* Construct the Policy instance.
*
Expand Down Expand Up @@ -223,6 +235,14 @@ public static void onSetUserVisibleHint(@NonNull Fragment fragment) {
}
}

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

@VisibleForTesting
static void onPolicyViolation(@NonNull Fragment fragment, @NonNull Violation violation) {
Policy policy = getNearestPolicy(fragment);
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#detectTargetFragmentUsage()}. */
@RestrictTo(RestrictTo.Scope.LIBRARY) // TODO: Make API public as soon as we have a few checks
public final class TargetFragmentUsageViolation extends Violation {
}