Skip to content

Commit 705914a

Browse files
committed
Add custom tabs util
1 parent 7adc70e commit 705914a

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

lib/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ android {
2222

2323
dependencies {
2424
implementation 'androidx.appcompat:appcompat:1.0.2'
25-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
25+
implementation 'androidx.browser:browser:1.0.0'
26+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
2627
}
2728

2829
publish {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.buffer.android.utils
2+
3+
import android.content.ActivityNotFoundException
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.graphics.BitmapFactory
7+
import android.net.Uri
8+
import android.util.Log
9+
import androidx.annotation.ColorRes
10+
import androidx.annotation.DrawableRes
11+
import androidx.browser.customtabs.CustomTabsIntent
12+
import androidx.core.content.ContextCompat
13+
14+
object CustomTabUtil {
15+
16+
fun open(
17+
context: Context,
18+
uri: Uri,
19+
@ColorRes color: Int,
20+
@DrawableRes backArrow: Int
21+
) {
22+
val intent = buildIntent(context, uri, color, backArrow)
23+
try {
24+
context.startActivity(intent)
25+
} catch (error: ActivityNotFoundException) {
26+
Log.e(
27+
CustomTabUtil::class.java.name,
28+
"There was a problem start the browser intent"
29+
)
30+
}
31+
32+
}
33+
34+
private fun buildIntent(
35+
context: Context,
36+
uri: Uri,
37+
@ColorRes color: Int,
38+
@DrawableRes backArrow: Int
39+
): Intent {
40+
val customTabsIntent = CustomTabsIntent.Builder()
41+
.setToolbarColor(ContextCompat.getColor(context, color))
42+
.setShowTitle(true)
43+
.setCloseButtonIcon(
44+
BitmapFactory.decodeResource(
45+
context.resources,
46+
backArrow
47+
)
48+
)
49+
.addDefaultShareMenuItem()
50+
.build()
51+
return customTabsIntent.intent.apply {
52+
data = uri
53+
}
54+
}
55+
}

lib/src/main/java/org/buffer/android/utils/KeyboardUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.view.View
55
import android.view.inputmethod.InputMethodManager
66

77
object KeyboardUtil {
8+
89
open fun closeSoftKeyboard(view: View?) {
910
if (view != null) {
1011
val inputManager = view.context.getSystemService(

0 commit comments

Comments
 (0)