-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
455 additions
and
153 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
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
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/dede/android_eggs/ChromeTabsBrowser.kt
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,83 @@ | ||
package com.dede.android_eggs | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.graphics.Color | ||
import android.net.Uri | ||
import androidx.appcompat.app.AppCompatDelegate | ||
import androidx.browser.customtabs.* | ||
import com.google.android.material.color.MaterialColors | ||
|
||
/** | ||
* CustomTabs Help | ||
* | ||
* @author hsh | ||
* @since 2021/11/19 2:14 下午 | ||
*/ | ||
object ChromeTabsBrowser { | ||
|
||
// Package name for the Chrome channel the client wants to connect to. This depends on the channel name. | ||
// Stable = com.android.chrome | ||
// Beta = com.chrome.beta | ||
// Dev = com.chrome.dev | ||
private const val CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome" | ||
private const val CUSTOM_SESSION_ID = 10 | ||
|
||
private var mayLaunchUrl: Uri? = null | ||
private val customTabsCallback = CustomTabsCallback() | ||
private var customTabsSession: CustomTabsSession? = null | ||
|
||
private val customTabsServiceConnection = object : CustomTabsServiceConnection() { | ||
override fun onServiceDisconnected(name: ComponentName?) { | ||
customTabsSession = null | ||
} | ||
|
||
override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) { | ||
val result = client.warmup(0) | ||
if (result) { | ||
val session = client.newSession(customTabsCallback, CUSTOM_SESSION_ID) | ||
if (session != null) { | ||
customTabsSession = session | ||
if (mayLaunchUrl != null) { | ||
session.mayLaunchUrl(mayLaunchUrl, null, null) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 预热并预加载 | ||
*/ | ||
fun warmup(context: Context, mayLaunchUrl: Uri? = null) { | ||
if (customTabsSession != null) return | ||
this.mayLaunchUrl = mayLaunchUrl | ||
val appContext = context.applicationContext | ||
CustomTabsClient.bindCustomTabsService( | ||
appContext, | ||
CUSTOM_TAB_PACKAGE_NAME, | ||
customTabsServiceConnection | ||
) | ||
} | ||
|
||
fun launchUrl(context: Context, uri: Uri) { | ||
val colorScheme = | ||
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) | ||
CustomTabsIntent.COLOR_SCHEME_DARK else CustomTabsIntent.COLOR_SCHEME_LIGHT | ||
|
||
val color = MaterialColors.getColor(context, android.R.attr.colorPrimary, Color.WHITE) | ||
val params = CustomTabColorSchemeParams.Builder() | ||
.setToolbarColor(color) | ||
.build() | ||
|
||
val builder = CustomTabsIntent.Builder() | ||
.setColorScheme(colorScheme) | ||
.setDefaultColorSchemeParams(params) | ||
val session = customTabsSession | ||
if (session != null) { | ||
builder.setSession(session) | ||
} | ||
val customTabsIntent = builder.build() | ||
customTabsIntent.launchUrl(context, uri) | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/dede/android_eggs/OvalOutlineProvider.kt
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,20 @@ | ||
package com.dede.android_eggs | ||
|
||
import android.graphics.Outline | ||
import android.view.View | ||
import android.view.ViewOutlineProvider | ||
|
||
/** | ||
* View 圆角 | ||
* @author hsh | ||
* @since 2021/10/21 5:03 下午 | ||
*/ | ||
class OvalOutlineProvider : ViewOutlineProvider() { | ||
override fun getOutline(view: View, outline: Outline) { | ||
outline.setOval( | ||
view.paddingLeft, view.paddingTop, | ||
view.width - view.paddingRight, | ||
view.height - view.paddingBottom | ||
) | ||
} | ||
} |
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
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
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
Oops, something went wrong.