2021-07-24 02:37:17 +00:00
package androidx.core.os ;
import android.os.Build ;
import android.os.Handler ;
import android.os.Looper ;
import android.os.Message ;
import android.util.Log ;
import androidx.annotation.NonNull ;
import androidx.annotation.Nullable ;
2021-09-15 06:33:54 +00:00
import androidx.annotation.RequiresApi ;
2021-07-24 02:37:17 +00:00
import java.lang.reflect.InvocationTargetException ;
2022-03-07 09:34:54 +00:00
/* loaded from: classes.dex */
2021-07-24 02:37:17 +00:00
public final class HandlerCompat {
private static final String TAG = " HandlerCompat " ;
2021-09-15 06:33:54 +00:00
@RequiresApi ( 28 )
2022-03-07 09:34:54 +00:00
/* loaded from: classes.dex */
2021-09-15 06:33:54 +00:00
public static class Api28Impl {
private Api28Impl ( ) {
}
public static Handler createAsync ( Looper looper ) {
return Handler . createAsync ( looper ) ;
}
public static Handler createAsync ( Looper looper , Handler . Callback callback ) {
return Handler . createAsync ( looper , callback ) ;
}
public static boolean postDelayed ( Handler handler , Runnable runnable , Object obj , long j ) {
return handler . postDelayed ( runnable , obj , j ) ;
}
}
@RequiresApi ( 29 )
2022-03-07 09:34:54 +00:00
/* loaded from: classes.dex */
2021-09-15 06:33:54 +00:00
public static class Api29Impl {
private Api29Impl ( ) {
}
public static boolean hasCallbacks ( Handler handler , Runnable runnable ) {
return handler . hasCallbacks ( runnable ) ;
}
}
2021-07-24 02:37:17 +00:00
private HandlerCompat ( ) {
}
@NonNull
public static Handler createAsync ( @NonNull Looper looper ) {
2022-03-07 09:34:54 +00:00
Throwable e ;
2021-07-24 02:37:17 +00:00
if ( Build . VERSION . SDK_INT > = 28 ) {
2021-09-15 06:33:54 +00:00
return Api28Impl . createAsync ( looper ) ;
2021-07-24 02:37:17 +00:00
}
try {
return ( Handler ) Handler . class . getDeclaredConstructor ( Looper . class , Handler . Callback . class , Boolean . TYPE ) . newInstance ( looper , null , Boolean . TRUE ) ;
2022-03-07 09:34:54 +00:00
} catch ( IllegalAccessException e2 ) {
e = e2 ;
2021-09-15 06:33:54 +00:00
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
2021-07-24 02:37:17 +00:00
return new Handler ( looper ) ;
2022-03-07 09:34:54 +00:00
} catch ( InstantiationException e3 ) {
e = e3 ;
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
return new Handler ( looper ) ;
} catch ( NoSuchMethodException e4 ) {
e = e4 ;
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
return new Handler ( looper ) ;
} catch ( InvocationTargetException e5 ) {
Throwable cause = e5 . getCause ( ) ;
2021-07-24 02:37:17 +00:00
if ( cause instanceof RuntimeException ) {
throw ( ( RuntimeException ) cause ) ;
} else if ( cause instanceof Error ) {
throw ( ( Error ) cause ) ;
} else {
throw new RuntimeException ( cause ) ;
}
}
}
@NonNull
public static Handler createAsync ( @NonNull Looper looper , @NonNull Handler . Callback callback ) {
2022-03-07 09:34:54 +00:00
Throwable e ;
2021-07-24 02:37:17 +00:00
if ( Build . VERSION . SDK_INT > = 28 ) {
2021-09-15 06:33:54 +00:00
return Api28Impl . createAsync ( looper , callback ) ;
2021-07-24 02:37:17 +00:00
}
try {
return ( Handler ) Handler . class . getDeclaredConstructor ( Looper . class , Handler . Callback . class , Boolean . TYPE ) . newInstance ( looper , callback , Boolean . TRUE ) ;
2022-03-07 09:34:54 +00:00
} catch ( IllegalAccessException e2 ) {
e = e2 ;
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
return new Handler ( looper , callback ) ;
} catch ( InstantiationException e3 ) {
e = e3 ;
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
return new Handler ( looper , callback ) ;
} catch ( NoSuchMethodException e4 ) {
e = e4 ;
2021-09-15 06:33:54 +00:00
Log . w ( TAG , " Unable to invoke Handler(Looper, Callback, boolean) constructor " , e ) ;
2021-07-24 02:37:17 +00:00
return new Handler ( looper , callback ) ;
2022-03-07 09:34:54 +00:00
} catch ( InvocationTargetException e5 ) {
Throwable cause = e5 . getCause ( ) ;
2021-09-15 06:33:54 +00:00
if ( cause instanceof RuntimeException ) {
throw ( ( RuntimeException ) cause ) ;
} else if ( cause instanceof Error ) {
throw ( ( Error ) cause ) ;
} else {
throw new RuntimeException ( cause ) ;
}
}
}
@RequiresApi ( 16 )
public static boolean hasCallbacks ( @NonNull Handler handler , @NonNull Runnable runnable ) {
2022-03-07 09:34:54 +00:00
Throwable e ;
2021-09-15 06:33:54 +00:00
if ( Build . VERSION . SDK_INT > = 29 ) {
return Api29Impl . hasCallbacks ( handler , runnable ) ;
}
try {
return ( ( Boolean ) Handler . class . getMethod ( " hasCallbacks " , Runnable . class ) . invoke ( handler , runnable ) ) . booleanValue ( ) ;
2022-03-07 09:34:54 +00:00
} catch ( IllegalAccessException e2 ) {
e = e2 ;
throw new UnsupportedOperationException ( " Failed to call Handler.hasCallbacks(), but there is no safe failure mode for this method. Raising exception. " , e ) ;
} catch ( NoSuchMethodException e3 ) {
e = e3 ;
throw new UnsupportedOperationException ( " Failed to call Handler.hasCallbacks(), but there is no safe failure mode for this method. Raising exception. " , e ) ;
} catch ( NullPointerException e4 ) {
e = e4 ;
throw new UnsupportedOperationException ( " Failed to call Handler.hasCallbacks(), but there is no safe failure mode for this method. Raising exception. " , e ) ;
} catch ( InvocationTargetException e5 ) {
Throwable cause = e5 . getCause ( ) ;
2021-07-24 02:37:17 +00:00
if ( cause instanceof RuntimeException ) {
throw ( ( RuntimeException ) cause ) ;
} else if ( cause instanceof Error ) {
throw ( ( Error ) cause ) ;
} else {
throw new RuntimeException ( cause ) ;
}
}
}
public static boolean postDelayed ( @NonNull Handler handler , @NonNull Runnable runnable , @Nullable Object obj , long j ) {
if ( Build . VERSION . SDK_INT > = 28 ) {
2021-09-15 06:33:54 +00:00
return Api28Impl . postDelayed ( handler , runnable , obj , j ) ;
2021-07-24 02:37:17 +00:00
}
Message obtain = Message . obtain ( handler , runnable ) ;
obtain . obj = obj ;
return handler . sendMessageDelayed ( obtain , j ) ;
}
}