325 lines
13 KiB
Java
325 lines
13 KiB
Java
package com.bumptech.glide.manager;
|
|
|
|
import android.R;
|
|
import android.annotation.TargetApi;
|
|
import android.app.Activity;
|
|
import android.app.Application;
|
|
import android.app.FragmentManager;
|
|
import android.content.ComponentCallbacks;
|
|
import android.content.Context;
|
|
import android.content.ContextWrapper;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import androidx.collection.ArrayMap;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.fragment.app.FragmentActivity;
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
import com.bumptech.glide.Glide;
|
|
import com.bumptech.glide.RequestManager;
|
|
import com.bumptech.glide.util.Preconditions;
|
|
import com.bumptech.glide.util.Util;
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class RequestManagerRetriever implements Handler.Callback {
|
|
private static final RequestManagerFactory i = new RequestManagerFactory() { // from class: com.bumptech.glide.manager.RequestManagerRetriever.1
|
|
@Override // com.bumptech.glide.manager.RequestManagerRetriever.RequestManagerFactory
|
|
public RequestManager a(Glide glide, Lifecycle lifecycle, RequestManagerTreeNode requestManagerTreeNode, Context context) {
|
|
return new RequestManager(glide, lifecycle, requestManagerTreeNode, context);
|
|
}
|
|
};
|
|
private volatile RequestManager a;
|
|
private final Handler d;
|
|
private final RequestManagerFactory e;
|
|
final Map<FragmentManager, RequestManagerFragment> b = new HashMap();
|
|
final Map<androidx.fragment.app.FragmentManager, SupportRequestManagerFragment> c = new HashMap();
|
|
private final ArrayMap<View, Fragment> f = new ArrayMap<>();
|
|
private final ArrayMap<View, android.app.Fragment> g = new ArrayMap<>();
|
|
private final Bundle h = new Bundle();
|
|
|
|
public interface RequestManagerFactory {
|
|
RequestManager a(Glide glide, Lifecycle lifecycle, RequestManagerTreeNode requestManagerTreeNode, Context context);
|
|
}
|
|
|
|
public RequestManagerRetriever(RequestManagerFactory requestManagerFactory) {
|
|
this.e = requestManagerFactory == null ? i : requestManagerFactory;
|
|
this.d = new Handler(Looper.getMainLooper(), this);
|
|
}
|
|
|
|
@Deprecated
|
|
private void b(FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> arrayMap) {
|
|
int i2 = 0;
|
|
while (true) {
|
|
int i3 = i2 + 1;
|
|
this.h.putInt("key", i2);
|
|
android.app.Fragment fragment = null;
|
|
try {
|
|
fragment = fragmentManager.getFragment(this.h, "key");
|
|
} catch (Exception unused) {
|
|
}
|
|
if (fragment == null) {
|
|
return;
|
|
}
|
|
if (fragment.getView() != null) {
|
|
arrayMap.put(fragment.getView(), fragment);
|
|
if (Build.VERSION.SDK_INT >= 17) {
|
|
a(fragment.getChildFragmentManager(), arrayMap);
|
|
}
|
|
}
|
|
i2 = i3;
|
|
}
|
|
}
|
|
|
|
private RequestManager c(Context context) {
|
|
if (this.a == null) {
|
|
synchronized (this) {
|
|
if (this.a == null) {
|
|
this.a = this.e.a(Glide.b(context.getApplicationContext()), new ApplicationLifecycle(), new EmptyRequestManagerTreeNode(), context.getApplicationContext());
|
|
}
|
|
}
|
|
}
|
|
return this.a;
|
|
}
|
|
|
|
private static boolean d(Activity activity) {
|
|
return !activity.isFinishing();
|
|
}
|
|
|
|
public RequestManager a(Context context) {
|
|
if (context == null) {
|
|
throw new IllegalArgumentException("You cannot start a load on a null Context");
|
|
}
|
|
if (Util.d() && !(context instanceof Application)) {
|
|
if (context instanceof FragmentActivity) {
|
|
return a((FragmentActivity) context);
|
|
}
|
|
if (context instanceof Activity) {
|
|
return a((Activity) context);
|
|
}
|
|
if (context instanceof ContextWrapper) {
|
|
return a(((ContextWrapper) context).getBaseContext());
|
|
}
|
|
}
|
|
return c(context);
|
|
}
|
|
|
|
@Override // android.os.Handler.Callback
|
|
public boolean handleMessage(Message message) {
|
|
ComponentCallbacks remove;
|
|
int i2 = message.what;
|
|
Object obj = null;
|
|
boolean z = true;
|
|
if (i2 == 1) {
|
|
obj = (FragmentManager) message.obj;
|
|
remove = this.b.remove(obj);
|
|
} else if (i2 != 2) {
|
|
z = false;
|
|
remove = null;
|
|
} else {
|
|
obj = (androidx.fragment.app.FragmentManager) message.obj;
|
|
remove = this.c.remove(obj);
|
|
}
|
|
if (z && remove == null && Log.isLoggable("RMRetriever", 5)) {
|
|
Log.w("RMRetriever", "Failed to remove expected request manager fragment, manager: " + obj);
|
|
}
|
|
return z;
|
|
}
|
|
|
|
private Activity b(Context context) {
|
|
if (context instanceof Activity) {
|
|
return (Activity) context;
|
|
}
|
|
if (context instanceof ContextWrapper) {
|
|
return b(((ContextWrapper) context).getBaseContext());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@TargetApi(17)
|
|
private static void c(Activity activity) {
|
|
if (Build.VERSION.SDK_INT >= 17 && activity.isDestroyed()) {
|
|
throw new IllegalArgumentException("You cannot start a load for a destroyed activity");
|
|
}
|
|
}
|
|
|
|
public RequestManager a(FragmentActivity fragmentActivity) {
|
|
if (Util.c()) {
|
|
return a(fragmentActivity.getApplicationContext());
|
|
}
|
|
c((Activity) fragmentActivity);
|
|
return a(fragmentActivity, fragmentActivity.getSupportFragmentManager(), (Fragment) null, d(fragmentActivity));
|
|
}
|
|
|
|
@Deprecated
|
|
RequestManagerFragment b(Activity activity) {
|
|
return a(activity.getFragmentManager(), (android.app.Fragment) null, d(activity));
|
|
}
|
|
|
|
SupportRequestManagerFragment b(FragmentActivity fragmentActivity) {
|
|
return a(fragmentActivity.getSupportFragmentManager(), (Fragment) null, d(fragmentActivity));
|
|
}
|
|
|
|
public RequestManager a(Fragment fragment) {
|
|
Preconditions.a(fragment.getActivity(), "You cannot start a load on a fragment before it is attached or after it is destroyed");
|
|
if (Util.c()) {
|
|
return a(fragment.getActivity().getApplicationContext());
|
|
}
|
|
return a(fragment.getActivity(), fragment.getChildFragmentManager(), fragment, fragment.isVisible());
|
|
}
|
|
|
|
public RequestManager a(Activity activity) {
|
|
if (Util.c()) {
|
|
return a(activity.getApplicationContext());
|
|
}
|
|
c(activity);
|
|
return a(activity, activity.getFragmentManager(), (android.app.Fragment) null, d(activity));
|
|
}
|
|
|
|
public RequestManager a(View view) {
|
|
if (Util.c()) {
|
|
return a(view.getContext().getApplicationContext());
|
|
}
|
|
Preconditions.a(view);
|
|
Preconditions.a(view.getContext(), "Unable to obtain a request manager for a view without a Context");
|
|
Activity b = b(view.getContext());
|
|
if (b == null) {
|
|
return a(view.getContext().getApplicationContext());
|
|
}
|
|
if (b instanceof FragmentActivity) {
|
|
Fragment a = a(view, (FragmentActivity) b);
|
|
return a != null ? a(a) : a(b);
|
|
}
|
|
android.app.Fragment a2 = a(view, b);
|
|
if (a2 == null) {
|
|
return a(b);
|
|
}
|
|
return a(a2);
|
|
}
|
|
|
|
private static void a(Collection<Fragment> collection, Map<View, Fragment> map) {
|
|
if (collection == null) {
|
|
return;
|
|
}
|
|
for (Fragment fragment : collection) {
|
|
if (fragment != null && fragment.getView() != null) {
|
|
map.put(fragment.getView(), fragment);
|
|
a(fragment.getChildFragmentManager().b(), map);
|
|
}
|
|
}
|
|
}
|
|
|
|
private Fragment a(View view, FragmentActivity fragmentActivity) {
|
|
this.f.clear();
|
|
a(fragmentActivity.getSupportFragmentManager().b(), this.f);
|
|
View findViewById = fragmentActivity.findViewById(R.id.content);
|
|
Fragment fragment = null;
|
|
while (!view.equals(findViewById) && (fragment = this.f.get(view)) == null && (view.getParent() instanceof View)) {
|
|
view = (View) view.getParent();
|
|
}
|
|
this.f.clear();
|
|
return fragment;
|
|
}
|
|
|
|
@Deprecated
|
|
private android.app.Fragment a(View view, Activity activity) {
|
|
this.g.clear();
|
|
a(activity.getFragmentManager(), this.g);
|
|
View findViewById = activity.findViewById(R.id.content);
|
|
android.app.Fragment fragment = null;
|
|
while (!view.equals(findViewById) && (fragment = this.g.get(view)) == null && (view.getParent() instanceof View)) {
|
|
view = (View) view.getParent();
|
|
}
|
|
this.g.clear();
|
|
return fragment;
|
|
}
|
|
|
|
@TargetApi(26)
|
|
@Deprecated
|
|
private void a(FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> arrayMap) {
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
for (android.app.Fragment fragment : fragmentManager.getFragments()) {
|
|
if (fragment.getView() != null) {
|
|
arrayMap.put(fragment.getView(), fragment);
|
|
a(fragment.getChildFragmentManager(), arrayMap);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
b(fragmentManager, arrayMap);
|
|
}
|
|
|
|
@TargetApi(17)
|
|
@Deprecated
|
|
public RequestManager a(android.app.Fragment fragment) {
|
|
if (fragment.getActivity() != null) {
|
|
if (!Util.c() && Build.VERSION.SDK_INT >= 17) {
|
|
return a(fragment.getActivity(), fragment.getChildFragmentManager(), fragment, fragment.isVisible());
|
|
}
|
|
return a(fragment.getActivity().getApplicationContext());
|
|
}
|
|
throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached");
|
|
}
|
|
|
|
private RequestManagerFragment a(FragmentManager fragmentManager, android.app.Fragment fragment, boolean z) {
|
|
RequestManagerFragment requestManagerFragment = (RequestManagerFragment) fragmentManager.findFragmentByTag("com.bumptech.glide.manager");
|
|
if (requestManagerFragment == null && (requestManagerFragment = this.b.get(fragmentManager)) == null) {
|
|
requestManagerFragment = new RequestManagerFragment();
|
|
requestManagerFragment.a(fragment);
|
|
if (z) {
|
|
requestManagerFragment.a().b();
|
|
}
|
|
this.b.put(fragmentManager, requestManagerFragment);
|
|
fragmentManager.beginTransaction().add(requestManagerFragment, "com.bumptech.glide.manager").commitAllowingStateLoss();
|
|
this.d.obtainMessage(1, fragmentManager).sendToTarget();
|
|
}
|
|
return requestManagerFragment;
|
|
}
|
|
|
|
@Deprecated
|
|
private RequestManager a(Context context, FragmentManager fragmentManager, android.app.Fragment fragment, boolean z) {
|
|
RequestManagerFragment a = a(fragmentManager, fragment, z);
|
|
RequestManager b = a.b();
|
|
if (b != null) {
|
|
return b;
|
|
}
|
|
RequestManager a2 = this.e.a(Glide.b(context), a.a(), a.c(), context);
|
|
a.a(a2);
|
|
return a2;
|
|
}
|
|
|
|
private SupportRequestManagerFragment a(androidx.fragment.app.FragmentManager fragmentManager, Fragment fragment, boolean z) {
|
|
SupportRequestManagerFragment supportRequestManagerFragment = (SupportRequestManagerFragment) fragmentManager.a("com.bumptech.glide.manager");
|
|
if (supportRequestManagerFragment == null && (supportRequestManagerFragment = this.c.get(fragmentManager)) == null) {
|
|
supportRequestManagerFragment = new SupportRequestManagerFragment();
|
|
supportRequestManagerFragment.a(fragment);
|
|
if (z) {
|
|
supportRequestManagerFragment.b().b();
|
|
}
|
|
this.c.put(fragmentManager, supportRequestManagerFragment);
|
|
FragmentTransaction a = fragmentManager.a();
|
|
a.a(supportRequestManagerFragment, "com.bumptech.glide.manager");
|
|
a.b();
|
|
this.d.obtainMessage(2, fragmentManager).sendToTarget();
|
|
}
|
|
return supportRequestManagerFragment;
|
|
}
|
|
|
|
private RequestManager a(Context context, androidx.fragment.app.FragmentManager fragmentManager, Fragment fragment, boolean z) {
|
|
SupportRequestManagerFragment a = a(fragmentManager, fragment, z);
|
|
RequestManager o = a.o();
|
|
if (o != null) {
|
|
return o;
|
|
}
|
|
RequestManager a2 = this.e.a(Glide.b(context), a.b(), a.q(), context);
|
|
a.a(a2);
|
|
return a2;
|
|
}
|
|
}
|