Skip to content

Commit ef6c71e

Browse files
committed
Refactoring IntentLauncher
1 parent c5e8e39 commit ef6c71e

File tree

3 files changed

+104
-90
lines changed

3 files changed

+104
-90
lines changed

omegalauncherslib/src/main/java/com/omegar/libs/omegalaunchers/ActivityLauncher.kt

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.omegar.libs.omegalaunchers
22

33
import android.app.Activity
4-
import android.app.PendingIntent
54
import android.content.Context
65
import android.content.Intent
7-
import android.os.Build
86
import android.os.Bundle
97
import 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
148
import com.omegar.libs.omegalaunchers.tools.BundlePair
159
import com.omegar.libs.omegalaunchers.tools.bundleOf
1610
import 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)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.omegar.libs.omegalaunchers
2+
3+
import android.app.Activity
4+
import android.app.PendingIntent
5+
import android.content.Context
6+
import android.content.Intent
7+
import android.os.Build
8+
import android.os.Bundle
9+
import android.util.AndroidRuntimeException
10+
import androidx.core.app.TaskStackBuilder
11+
import androidx.core.content.ContextCompat
12+
import androidx.fragment.app.Fragment
13+
14+
/**
15+
* Created by Anton Knyazev on 2019-07-08.
16+
*/
17+
abstract class BaseIntentLauncher : Launcher {
18+
19+
protected abstract fun getIntent(context: Context): Intent
20+
21+
override fun launch(context: Context) {
22+
launch(context, null)
23+
}
24+
25+
fun launch(context: Context, option: Bundle? = null) {
26+
val intent = getIntent(context)
27+
try {
28+
context.compatStartActivity(intent, option)
29+
} catch (exc: AndroidRuntimeException) {
30+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
31+
context.compatStartActivity(intent, option)
32+
}
33+
}
34+
35+
fun launch(context: Context, vararg parentLaunchers: BaseIntentLauncher) {
36+
launch(context, null, *parentLaunchers)
37+
}
38+
39+
fun launch(context: Context, option: Bundle? = null, vararg parentLaunchers: BaseIntentLauncher) {
40+
val list =
41+
listOf(*parentLaunchers.map { it.getIntent(context) }.toTypedArray(), getIntent(context))
42+
43+
ContextCompat.startActivities(context, list.toTypedArray(), option)
44+
}
45+
46+
fun getPendingIntent(
47+
context: Context, requestCode: Int = 0,
48+
flags: Int = PendingIntent.FLAG_UPDATE_CURRENT
49+
): PendingIntent {
50+
return PendingIntent.getActivity(context, requestCode, getIntent(context), flags)
51+
}
52+
53+
fun getPendingIntent(
54+
context: Context,
55+
requestCode: Int = 0,
56+
flags: Int = PendingIntent.FLAG_UPDATE_CURRENT,
57+
vararg parentLaunchers: BaseIntentLauncher
58+
59+
): PendingIntent {
60+
val list =
61+
listOf(*parentLaunchers.map { it.getIntent(context) }.toTypedArray(), getIntent(context))
62+
63+
val intents = list.toTypedArray()
64+
65+
return PendingIntent.getActivities(context, requestCode, intents, flags)
66+
}
67+
68+
fun getPendingIntentWithParentStack(
69+
context: Context,
70+
requestCode: Int = 0,
71+
flags: Int = PendingIntent.FLAG_UPDATE_CURRENT
72+
): PendingIntent {
73+
return TaskStackBuilder.create(context)
74+
.addNextIntentWithParentStack(getIntent(context))
75+
.getPendingIntent(requestCode, flags)!!
76+
}
77+
78+
fun launchForResult(activity: Activity, requestCode: Int, option: Bundle? = null) {
79+
activity.compatStartActivityForResult(getIntent(activity), requestCode, option)
80+
}
81+
82+
fun launchForResult(fragment: Fragment, requestCode: Int, option: Bundle? = null) {
83+
fragment.startActivityForResult(getIntent(fragment.context!!), requestCode, option)
84+
}
85+
86+
protected fun Context.compatStartActivity(intent: Intent, option: Bundle? = null) {
87+
ContextCompat.startActivity(this, intent, option)
88+
}
89+
90+
protected fun Activity.compatStartActivityForResult(intent: Intent, requestCode: Int, option: Bundle? = null) {
91+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
92+
startActivityForResult(intent, requestCode, option)
93+
} else {
94+
startActivityForResult(intent, requestCode)
95+
}
96+
}
97+
98+
99+
}

omegalauncherslib/src/main/java/com/omegar/libs/omegalaunchers/IntentLauncher.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import android.content.Context
44
import android.content.Intent
55

66
/**
7-
* Created by Anton Knyazev on 2019-07-08.
7+
* Created by Anton Knyazev on 2019-08-09.
88
*/
9-
class IntentLauncher(private val intent: Intent) : Launcher {
9+
class IntentLauncher(private val intent: Intent) : BaseIntentLauncher() {
1010

11-
override fun launch(context: Context) {
12-
context.startActivity(intent)
13-
}
11+
override fun getIntent(context: Context) = intent
1412

1513
}

0 commit comments

Comments
 (0)