[3.4.0] add null if in util

This commit is contained in:
zhongchao
2023-07-26 14:24:05 +08:00
parent 328245f3af
commit 5bd7c8a7ab
3 changed files with 25 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ package com.mogo.eagle.core.network;
import android.content.Context; import android.content.Context;
import android.view.KeyEvent;
import com.mogo.eagle.core.network.utils.Util; import com.mogo.eagle.core.network.utils.Util;
@@ -96,7 +97,7 @@ public class RequestOptions {
} }
public Object getCaller() { public Object getCaller() {
return caller; return caller == null ? null : caller;
} }
public Context getContext() { public Context getContext() {

View File

@@ -28,7 +28,7 @@ public abstract class SubscribeImpl<T extends BaseData> implements Observer<T> {
} }
private void onFinish() { private void onFinish() {
if (!Util.checkAlive(mRequestOptions.getCaller())) { if (Util.checkAlive(mRequestOptions.getCaller())) {
} }
} }

View File

@@ -15,30 +15,37 @@ import com.mogo.eagle.core.network.CallerNotAliveException;
public class Util { public class Util {
public static boolean checkAlive( Object caller ) { public static boolean checkAlive(Object caller ) {
if ( caller instanceof Activity) { if ( caller == null){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 ? !( ( Activity ) caller ).isDestroyed() : !( ( Activity ) caller ).isFinishing(); return false;
} else if ( caller instanceof Fragment) { }
return ( ( Fragment ) caller ).isAdded(); if ( caller instanceof Activity) {
} else if ( caller instanceof androidx.fragment.app.Fragment ) { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 ? ((Activity) caller).isDestroyed() : ((Activity) caller).isFinishing();
return ( ( androidx.fragment.app.Fragment ) caller ).isAdded(); } else if ( caller instanceof Fragment) {
} else if ( caller instanceof View) { return !((Fragment) caller).isAdded();
return true; } else if ( caller instanceof androidx.fragment.app.Fragment ) {
} else if ( caller instanceof Dialog) { return !((androidx.fragment.app.Fragment) caller).isAdded();
return ( ( Dialog ) caller ).getWindow() != null; } else if ( caller instanceof View) {
} else if ( caller instanceof PopupWindow) { return false;
return ( ( PopupWindow ) caller ).getContentView() != null; } else if ( caller instanceof Dialog) {
return ((Dialog) caller).getWindow() == null;
} else if ( caller instanceof PopupWindow) {
return ((PopupWindow) caller).getContentView() == null;
} else {
return false;
} }
return caller != null;
} }
public static void assertCallerAlive( Object caller ) throws CallerNotAliveException { public static void assertCallerAlive( Object caller ) throws CallerNotAliveException {
if ( !checkAlive( caller ) ) { if (checkAlive(caller)) {
throw new CallerNotAliveException( "Caller is not alive any more" ); throw new CallerNotAliveException( "Caller is not alive any more" );
} }
} }
public static Context getContext(Object object ) { public static Context getContext(Object object ) {
if( object == null){
return null;
}
if ( object instanceof Activity ) { if ( object instanceof Activity ) {
return ( Activity ) object; return ( Activity ) object;
} else if ( object instanceof Fragment ) { } else if ( object instanceof Fragment ) {