استخدام واجهة برمجة التطبيقات Custom Tab (واجهة برمجة التطبيقات المنخفضة المستوى)

الطريقة المقترَحة لدمج تطبيقك مع علامات التبويب المخصّصة هي استخدام مكتبة AndroidX Browser، ولكن يمكنك أيضًا بدء علامة تبويب مخصّصة بدون مكتبة الدعم. يقدّم هذا الدليل نظرة عامة حول كيفية تحقيق ذلك.

يتوفّر التنفيذ الكامل لمكتبة الدعم على [GitHub][1] ويمكن استخدامه كنقطة بداية. ويحتوي أيضًا على [ملفات AIDL][2] المطلوبة للاتصال بالخدمة، لأنّ الملفات المضمّنة في مستودع Chromium لا يمكن استخدامها مباشرةً مع "استوديو Android".

الأساسيات اللازمة لتشغيل علامات التبويب المخصّصة باستخدام Low Level API

// 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)

إضافة عمليات تخصيص واجهة المستخدم

يتم تضمين عمليات تخصيص واجهة المستخدم من خلال إضافة "إضافات" إلى الهدف ACTION_VIEW. يمكن العثور على قائمة كاملة بمفاتيح الإضافات المستخدَمة لتخصيص واجهة المستخدم في [مستندات 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