11package com.omegar.libs.omegalaunchers
22
33import android.app.Activity
4- import android.app.PendingIntent
54import android.content.Context
65import android.content.Intent
7- import android.os.Build
86import android.os.Bundle
97import android.os.Parcelable
10- import android.util.AndroidRuntimeException
11- import androidx.core.app.TaskStackBuilder
12- import androidx.core.content.ContextCompat
13- import androidx.fragment.app.Fragment
148import com.omegar.libs.omegalaunchers.tools.BundlePair
159import com.omegar.libs.omegalaunchers.tools.bundleOf
1610import com.omegar.libs.omegalaunchers.tools.equalsBundle
@@ -25,7 +19,7 @@ class ActivityLauncher(
2519 private val activityClass : Class <Activity >,
2620 private val bundle : Bundle ? = null ,
2721 private var flags : Int = 0
28- ) : Launcher , Parcelable {
22+ ) : BaseIntentLauncher() , Parcelable {
2923
3024 companion object {
3125
@@ -40,7 +34,7 @@ class ActivityLauncher(
4034 constructor (activityClass: Class <Activity >, vararg extraParams: BundlePair , flags: Int = 0 )
4135 : this (activityClass, bundleOf(* extraParams), flags)
4236
43- private fun createIntent (context : Context ): Intent {
37+ override fun getIntent (context : Context ): Intent {
4438 return Intent (context, activityClass).apply {
4539 if (bundle != null ) {
4640 putExtras(bundle)
@@ -57,83 +51,6 @@ class ActivityLauncher(
5751 flags = flags and (flag.inv ())
5852 }
5953
60- override fun launch (context : Context ) {
61- launch(context, null )
62- }
63-
64- fun launch (context : Context , option : Bundle ? = null) {
65- val intent = createIntent(context)
66- try {
67- context.compatStartActivity(intent, option)
68- } catch (exc: AndroidRuntimeException ) {
69- intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK );
70- context.compatStartActivity(intent, option)
71- }
72- }
73-
74- fun launch (context : Context , vararg parentLaunchers : ActivityLauncher ) {
75- launch(context, null , * parentLaunchers)
76- }
77-
78- fun launch (context : Context , option : Bundle ? = null, vararg parentLaunchers : ActivityLauncher ) {
79- val list =
80- listOf (* parentLaunchers.map { it.createIntent(context) }.toTypedArray(), createIntent(context))
81-
82- ContextCompat .startActivities(context, list.toTypedArray(), option)
83- }
84-
85- private fun Context.compatStartActivity (intent : Intent , option : Bundle ? = null) {
86- ContextCompat .startActivity(this , intent, option)
87- }
88-
89- private fun Activity.compatStartActivityForResult (intent : Intent , requestCode : Int , option : Bundle ? = null) {
90- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
91- startActivityForResult(intent, requestCode, option)
92- } else {
93- startActivityForResult(intent, requestCode)
94- }
95- }
96-
97- fun getPendingIntent (
98- context : Context , requestCode : Int = 0,
99- flags : Int = PendingIntent .FLAG_UPDATE_CURRENT
100- ): PendingIntent {
101- return PendingIntent .getActivity(context, requestCode, createIntent(context), flags)
102- }
103-
104- fun getPendingIntent (
105- context : Context ,
106- requestCode : Int = 0,
107- flags : Int = PendingIntent .FLAG_UPDATE_CURRENT ,
108- vararg parentLaunchers : ActivityLauncher
109-
110- ): PendingIntent {
111- val list =
112- listOf (* parentLaunchers.map { it.createIntent(context) }.toTypedArray(), createIntent(context))
113-
114- val intents = list.toTypedArray()
115-
116- return PendingIntent .getActivities(context, requestCode, intents, flags)
117- }
118-
119- fun getPendingIntentWithParentStack (
120- context : Context ,
121- requestCode : Int = 0,
122- flags : Int = PendingIntent .FLAG_UPDATE_CURRENT
123- ): PendingIntent {
124- return TaskStackBuilder .create(context)
125- .addNextIntentWithParentStack(createIntent(context))
126- .getPendingIntent(requestCode, flags)!!
127- }
128-
129- fun launchForResult (activity : Activity , requestCode : Int , option : Bundle ? = null) {
130- activity.compatStartActivityForResult(createIntent(activity), requestCode, option)
131- }
132-
133- fun launchForResult (fragment : Fragment , requestCode : Int , option : Bundle ? = null) {
134- fragment.startActivityForResult(createIntent(fragment.context!! ), requestCode, option)
135- }
136-
13754 fun isOurActivity (activity : Activity ): Boolean {
13855 return activityClass.isInstance(activity)
13956 && activity.intent.extras.equalsBundle(bundle)
0 commit comments