استفاده از میانای برنامه‌سازی کاربردی سطح پایین «برگه سفارشی»

روش توصیه‌شده برای ادغام برنامه با «برگه‌های سفارشی» استفاده از «کتابخانه مرورگر AndroidX» است، اما می‌توانید «برگه سفارشی» را بدون کتابخانه پشتیبانی نیز راه‌اندازی کنید. این راهنما نمای کلی از نحوه دستیابی به این هدف را ارائه می‌دهد.

پیاده‌سازی کامل «کتابخانه پشتیبانی» در [GitHub][1] دردسترس است و می‌توان از آن به‌عنوان نقطه شروع استفاده کرد. همچنین حاوی [فایل‌های AIDL][2] موردنیاز برای اتصال به سرویس است، زیرا فایل‌های موجود در مخزن Chromium مستقیماً با Android Studio قابل‌استفاده نیستند.

مبانی راه‌اندازی «برگه‌های سفارشی» بااستفاده از میانای برنامه‌سازی کاربردی سطح پایین

// Using a VIEW intent for compatibility with any other browsers on device.
// Caller should not be setting FLAG_ACTIVITY_NEW_TASK or
// FLAG_ACTIVITY_NEW_DOCUMENT.
val url = "https://paul.kinlan.me/"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
// Must have. Extra used to match the session. Its value is an IBinder passed
// whilst creating a news session. See newSession() below. Even if the service is not
// used and there is no valid session id to be provided, this extra has to be present
// with a null value to launch a custom tab.

val extraCustomTabsSession = "android.support.customtabs.extra.SESSION"
val extras = Bundle()
extras.putBinder(
    extraCustomTabsSession,
    sessionICustomTabsCallback.asBinder() /* Set to null for no session */
)
intent.putExtras(extras)

درحال افزودن سفارشی‌سازی‌های میانای کاربر

سفارشی‌سازی‌های میانای کاربر با افزودن Extras به ACTION_VIEW Intent اضافه می‌شوند. فهرست کامل کلیدهای اضافی استفاده‌شده برای سفارشی‌سازی میانای کاربر را می‌توانید در [اسناد CustomTabsIntent][3] پیدا کنید. نمونه‌ای از نحوه افزودن رنگ نوارابزار سفارشی در زیر آمده است:

// Extra that changes the background color for the address bar. colorInt is an int
// that specifies a Color.

val extraCustomTabsToolbarColor = "android.support.customtabs.extra.TOOLBAR_COLOR"
intent.putExtra(extraCustomTabsToolbarColor, colorInt)

درحال اتصال به سرویس «برگه‌های سفارشی»

از سرویس «برگه‌های سفارشی» می‌توان به همان روشی که از دیگر «سرویس‌های Android» استفاده می‌شود استفاده کرد. این میانای ایجادشده با AIDL به‌طور خودکار کلاس سرویس پیشکار را برای شما ایجاد می‌کند.

از روش‌های موجود در سرویس پیشکار برای آماده‌سازی، ایجاد جلسه، و پیش‌واکشی استفاده کنید:

// 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
val customTabPackageName = "com.chrome.dev" // Change when in stable

// Action to add to the service intent. This action can be used as a way
// generically pick apps that handle custom tabs for both activity and service
// side implementations.
val actionCustomTabsConnection =
    "android.support.customtabs.action.CustomTabsService"
val serviceIntent = Intent(actionCustomTabsConnection)

serviceIntent.setPackage(customTabPackageName)
// serviceConnection is your ServiceConnection implementation. Its
// onServiceConnected() callback receives the IBinder for the Custom Tabs service.
context.bindService(
    serviceIntent,
    serviceConnection,
    Context.BIND_AUTO_CREATE or Context.BIND_WAIVE_PRIORITY
)

[1]: https://github.com/GoogleChrome/custom-tabs-client/tree/master/customtabs [2]: https://developer.android.com/guide/components/aidl.html [3]: https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsIntent