-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/hss/onlyglide' into hss/onlyglide
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
downloader/src/main/java/com/hss/downloader/download/LifecycleObjectUtil2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.hss.downloader.download; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.ContextWrapper; | ||
|
||
import androidx.fragment.app.Fragment; | ||
import androidx.lifecycle.LifecycleOwner; | ||
|
||
/** | ||
* @Despciption todo | ||
* @Author hss | ||
* @Date 11/08/2022 10:04 | ||
* @Version 1.0 | ||
*/ | ||
class LifecycleObjectUtil2 { | ||
|
||
public static LifecycleOwner getLifecycleOwnerFromObj(Object contextOrFragment){ | ||
if(contextOrFragment instanceof LifecycleOwner){ | ||
LifecycleOwner owner = (LifecycleOwner) contextOrFragment; | ||
return owner; | ||
} | ||
if(contextOrFragment instanceof Context){ | ||
Activity activity = getActivityFromContext((Context) contextOrFragment); | ||
if(activity instanceof LifecycleOwner){ | ||
return (LifecycleOwner)activity; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} | ||
public static Object getLifecycledObjectFromObj(Object contextOrFragment){ | ||
if(contextOrFragment instanceof LifecycleOwner){ | ||
LifecycleOwner owner = (LifecycleOwner) contextOrFragment; | ||
return owner; | ||
} | ||
if(contextOrFragment instanceof Context){ | ||
Activity activity = getActivityFromContext((Context) contextOrFragment); | ||
return activity; | ||
} | ||
if(contextOrFragment instanceof Fragment){ | ||
return contextOrFragment; | ||
} | ||
if(contextOrFragment instanceof android.app.Fragment){ | ||
return contextOrFragment; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
|
||
public static Activity getActivityFromContext(Context context) { | ||
if (context == null) { | ||
return null; | ||
} | ||
if (context instanceof Activity) { | ||
return (Activity) context; | ||
} | ||
while (context instanceof ContextWrapper) { | ||
if (context instanceof Activity) { | ||
return (Activity) context; | ||
} | ||
context = ((ContextWrapper) context).getBaseContext(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters