Skip to content

Commit

Permalink
Add nullability annotations to manager package (#2790)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Jan 11, 2018
1 parent 35f6a0a commit bfa237c
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.widget.ImageView;
Expand Down Expand Up @@ -36,12 +37,12 @@ public void setUp() {
Glide glide = Glide.get(context);
requestManager = new RequestManager(glide, new Lifecycle() {
@Override
public void addListener(LifecycleListener listener) {
public void addListener(@NonNull LifecycleListener listener) {
listener.onStart();
}

@Override
public void removeListener(LifecycleListener listener) {
public void removeListener(@NonNull LifecycleListener listener) {
// Do nothing.
}
}, treeNode, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ boolean untrack(@NonNull Target<?> target) {
}
}

void track(Target<?> target, Request request) {
void track(@NonNull Target<?> target, @NonNull Request request) {
targetTracker.track(target);
requestTracker.runRequest(request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;
import com.bumptech.glide.util.Util;
import java.util.Collections;
import java.util.Set;
Expand Down Expand Up @@ -27,7 +28,7 @@ class ActivityFragmentLifecycle implements Lifecycle {
* avoid adding listeners multiple times. </p>
*/
@Override
public void addListener(LifecycleListener listener) {
public void addListener(@NonNull LifecycleListener listener) {
lifecycleListeners.add(listener);

if (isDestroyed) {
Expand All @@ -40,7 +41,7 @@ public void addListener(LifecycleListener listener) {
}

@Override
public void removeListener(LifecycleListener listener) {
public void removeListener(@NonNull LifecycleListener listener) {
lifecycleListeners.remove(listener);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;

/**
* A {@link com.bumptech.glide.manager.Lifecycle} implementation for tracking and notifying
* listeners of {@link android.app.Application} lifecycle events.
Expand All @@ -9,12 +11,12 @@
*/
class ApplicationLifecycle implements Lifecycle {
@Override
public void addListener(LifecycleListener listener) {
public void addListener(@NonNull LifecycleListener listener) {
listener.onStart();
}

@Override
public void removeListener(LifecycleListener listener) {
public void removeListener(@NonNull LifecycleListener listener) {
// Do nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Synthetic;
Expand All @@ -24,7 +25,7 @@ final class DefaultConnectivityMonitor implements ConnectivityMonitor {

private final BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
public void onReceive(@NonNull Context context, Intent intent) {
boolean wasConnected = isConnected;
isConnected = isConnected(context);
if (wasConnected != isConnected) {
Expand All @@ -37,7 +38,7 @@ public void onReceive(Context context, Intent intent) {
}
};

DefaultConnectivityMonitor(Context context, ConnectivityListener listener) {
DefaultConnectivityMonitor(@NonNull Context context, @NonNull ConnectivityListener listener) {
this.context = context.getApplicationContext();
this.listener = listener;
}
Expand Down Expand Up @@ -75,7 +76,7 @@ private void unregister() {
@Synthetic
// Permissions are checked in the factory instead.
@SuppressLint("MissingPermission")
boolean isConnected(Context context) {
boolean isConnected(@NonNull Context context) {
ConnectivityManager connectivityManager =
Preconditions.checkNotNull(
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;
import com.bumptech.glide.RequestManager;
import java.util.Collections;
import java.util.Set;
Expand All @@ -8,6 +9,7 @@
* A {@link RequestManagerTreeNode} that returns no relatives.
*/
final class EmptyRequestManagerTreeNode implements RequestManagerTreeNode {
@NonNull
@Override
public Set<RequestManager> getDescendants() {
return Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;

/**
* An interface for listening to Activity/Fragment lifecycle events.
*/
public interface Lifecycle {
/**
* Adds the given listener to the set of listeners managed by this Lifecycle implementation.
*/
void addListener(LifecycleListener listener);
void addListener(@NonNull LifecycleListener listener);

/**
* Removes the given listener from the set of listeners managed by this Lifecycle implementation,
Expand All @@ -16,5 +18,5 @@ public interface Lifecycle {
* <p>This is an optimization only, there is no guarantee that every added listener will
* eventually be removed.
*/
void removeListener(LifecycleListener listener);
void removeListener(@NonNull LifecycleListener listener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import android.app.Activity;
import android.app.Fragment;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
Expand Down Expand Up @@ -38,9 +40,9 @@ public RequestManagerFragment() {
this(new ActivityFragmentLifecycle());
}

// For testing only.
@VisibleForTesting
@SuppressLint("ValidFragment")
RequestManagerFragment(ActivityFragmentLifecycle lifecycle) {
RequestManagerFragment(@NonNull ActivityFragmentLifecycle lifecycle) {
this.lifecycle = lifecycle;
}

Expand All @@ -53,6 +55,7 @@ public void setRequestManager(@Nullable RequestManager requestManager) {
this.requestManager = requestManager;
}

@NonNull
ActivityFragmentLifecycle getGlideLifecycle() {
return lifecycle;
}
Expand All @@ -68,6 +71,7 @@ public RequestManager getRequestManager() {
/**
* Returns the {@link RequestManagerTreeNode} for this fragment.
*/
@NonNull
public RequestManagerTreeNode getRequestManagerTreeNode() {
return requestManagerTreeNode;
}
Expand All @@ -85,8 +89,10 @@ private void removeChildRequestManagerFragment(RequestManagerFragment child) {
* our parent is the fragment that we are annotating).
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Synthetic Set<RequestManagerFragment> getDescendantRequestManagerFragments() {
if (this.equals(rootRequestManagerFragment)) {
@Synthetic
@NonNull
Set<RequestManagerFragment> getDescendantRequestManagerFragments() {
if (equals(rootRequestManagerFragment)) {
return Collections.unmodifiableSet(childRequestManagerFragments);
} else if (rootRequestManagerFragment == null
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
Expand Down Expand Up @@ -116,6 +122,7 @@ void setParentFragmentHint(@Nullable Fragment parentFragmentHint) {
}
}

@Nullable
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private Fragment getParentFragmentUsingHint() {
final Fragment fragment;
Expand All @@ -131,8 +138,8 @@ private Fragment getParentFragmentUsingHint() {
* Returns true if the fragment is a descendant of our parent.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean isDescendant(Fragment fragment) {
Fragment root = this.getParentFragment();
private boolean isDescendant(@NonNull Fragment fragment) {
Fragment root = getParentFragment();
Fragment parentFragment;
while ((parentFragment = fragment.getParentFragment()) != null) {
if (parentFragment.equals(root)) {
Expand All @@ -143,11 +150,11 @@ private boolean isDescendant(Fragment fragment) {
return false;
}

private void registerFragmentWithRoot(Activity activity) {
private void registerFragmentWithRoot(@NonNull Activity activity) {
unregisterFragmentWithRoot();
rootRequestManagerFragment = Glide.get(activity).getRequestManagerRetriever()
.getRequestManagerFragment(activity.getFragmentManager(), null);
if (!this.equals(rootRequestManagerFragment)) {
if (!equals(rootRequestManagerFragment)) {
rootRequestManagerFragment.addChildRequestManagerFragment(this);
}
}
Expand Down Expand Up @@ -208,6 +215,7 @@ private class FragmentRequestManagerTreeNode implements RequestManagerTreeNode {
@Synthetic
FragmentRequestManagerTreeNode() { }

@NonNull
@Override
public Set<RequestManager> getDescendants() {
Set<RequestManagerFragment> descendantFragments = getDescendantRequestManagerFragments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public RequestManagerRetriever(@Nullable RequestManagerFactory factory) {
handler = new Handler(Looper.getMainLooper(), this /* Callback */);
}

@NonNull
private RequestManager getApplicationManager(@NonNull Context context) {
// Either an application context or we're on a background thread.
if (applicationManager == null) {
Expand Down Expand Up @@ -361,6 +362,7 @@ private RequestManager fragmentGet(@NonNull Context context,
return requestManager;
}

@NonNull
SupportRequestManagerFragment getSupportRequestManagerFragment(
@NonNull final FragmentManager fm, @Nullable Fragment parentHint) {
SupportRequestManagerFragment current =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;
import com.bumptech.glide.RequestManager;
import java.util.Set;

Expand All @@ -13,5 +14,6 @@ public interface RequestManagerTreeNode {
* Returns all descendant {@link RequestManager}s relative to the context of the current
* {@link RequestManager}.
*/
@NonNull
Set<RequestManager> getDescendants();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.manager;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import com.bumptech.glide.request.Request;
Expand Down Expand Up @@ -35,7 +36,7 @@ public class RequestTracker {
/**
* Starts tracking the given request.
*/
public void runRequest(Request request) {
public void runRequest(@NonNull Request request) {
requests.add(request);
if (!isPaused) {
request.begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
Expand Down Expand Up @@ -37,9 +39,9 @@ public SupportRequestManagerFragment() {
this(new ActivityFragmentLifecycle());
}

// For testing only.
@VisibleForTesting
@SuppressLint("ValidFragment")
public SupportRequestManagerFragment(ActivityFragmentLifecycle lifecycle) {
public SupportRequestManagerFragment(@NonNull ActivityFragmentLifecycle lifecycle) {
this.lifecycle = lifecycle;
}

Expand All @@ -52,6 +54,7 @@ public void setRequestManager(@Nullable RequestManager requestManager) {
this.requestManager = requestManager;
}

@NonNull
ActivityFragmentLifecycle getGlideLifecycle() {
return lifecycle;
}
Expand All @@ -69,6 +72,7 @@ public RequestManager getRequestManager() {
* to the
* associated {@link RequestManager}.
*/
@NonNull
public RequestManagerTreeNode getRequestManagerTreeNode() {
return requestManagerTreeNode;
}
Expand All @@ -85,10 +89,12 @@ private void removeChildRequestManagerFragment(SupportRequestManagerFragment chi
* Returns the set of fragments that this RequestManagerFragment's parent is a parent to. (i.e.
* our parent is the fragment that we are annotating).
*/
@Synthetic Set<SupportRequestManagerFragment> getDescendantRequestManagerFragments() {
@Synthetic
@NonNull
Set<SupportRequestManagerFragment> getDescendantRequestManagerFragments() {
if (rootRequestManagerFragment == null) {
return Collections.emptySet();
} else if (this.equals(rootRequestManagerFragment)) {
} else if (equals(rootRequestManagerFragment)) {
return Collections.unmodifiableSet(childRequestManagerFragments);
} else {
Set<SupportRequestManagerFragment> descendants = new HashSet<>();
Expand All @@ -113,6 +119,7 @@ void setParentFragmentHint(@Nullable Fragment parentFragmentHint) {
}
}

@Nullable
private Fragment getParentFragmentUsingHint() {
Fragment fragment = getParentFragment();
return fragment != null ? fragment : parentFragmentHint;
Expand All @@ -121,8 +128,8 @@ private Fragment getParentFragmentUsingHint() {
/**
* Returns true if the fragment is a descendant of our parent.
*/
private boolean isDescendant(Fragment fragment) {
Fragment root = this.getParentFragmentUsingHint();
private boolean isDescendant(@NonNull Fragment fragment) {
Fragment root = getParentFragmentUsingHint();
Fragment parentFragment;
while ((parentFragment = fragment.getParentFragment()) != null) {
if (parentFragment.equals(root)) {
Expand All @@ -133,11 +140,11 @@ private boolean isDescendant(Fragment fragment) {
return false;
}

private void registerFragmentWithRoot(FragmentActivity activity) {
private void registerFragmentWithRoot(@NonNull FragmentActivity activity) {
unregisterFragmentWithRoot();
rootRequestManagerFragment = Glide.get(activity).getRequestManagerRetriever()
.getSupportRequestManagerFragment(activity.getSupportFragmentManager(), null);
if (!this.equals(rootRequestManagerFragment)) {
if (!equals(rootRequestManagerFragment)) {
rootRequestManagerFragment.addChildRequestManagerFragment(this);
}
}
Expand Down Expand Up @@ -198,6 +205,7 @@ private class SupportFragmentRequestManagerTreeNode implements RequestManagerTre
@Synthetic
SupportFragmentRequestManagerTreeNode() { }

@NonNull
@Override
public Set<RequestManager> getDescendants() {
Set<SupportRequestManagerFragment> descendantFragments =
Expand Down
Loading

0 comments on commit bfa237c

Please sign in to comment.