From 38f5e293c6f515c0b487c1acf035cfaee281b983 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Wed, 11 May 2022 19:58:26 +0200 Subject: [PATCH] Add GestureNavContract from Launcher3 --- .../android/launcher3/GestureNavContract.kt | 79 +++++++++++++++++++ .../launcher2/licenses/OpenSourceLicenses.kt | 8 ++ 2 files changed, 87 insertions(+) create mode 100644 base/src/main/java/com/android/launcher3/GestureNavContract.kt diff --git a/base/src/main/java/com/android/launcher3/GestureNavContract.kt b/base/src/main/java/com/android/launcher3/GestureNavContract.kt new file mode 100644 index 00000000..5a0317fb --- /dev/null +++ b/base/src/main/java/com/android/launcher3/GestureNavContract.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3 + +import android.annotation.TargetApi +import android.content.ComponentName +import android.content.Intent +import android.graphics.RectF +import android.os.* +import android.util.Log +import android.view.SurfaceControl +import de.mm20.launcher2.ktx.isAtLeastApiLevel + +/** + * Class to encapsulate the handshake protocol between Launcher and gestureNav. + */ +class GestureNavContract( + val componentName: ComponentName, + val user: UserHandle, + private val mCallback: Message +) { + /** + * Sends the position information to the receiver + */ + @TargetApi(Build.VERSION_CODES.R) + fun sendEndPosition(position: RectF?, surfaceControl: SurfaceControl?) { + val result = Bundle() + result.putParcelable(EXTRA_ICON_POSITION, position) + result.putParcelable(EXTRA_ICON_SURFACE, surfaceControl) + val callback = Message.obtain() + callback.copyFrom(mCallback) + callback.data = result + try { + callback.replyTo.send(callback) + } catch (e: RemoteException) { + Log.e(TAG, "Error sending icon position", e) + } + } + + companion object { + private const val TAG = "GestureNavContract" + const val EXTRA_GESTURE_CONTRACT = "gesture_nav_contract_v1" + const val EXTRA_ICON_POSITION = "gesture_nav_contract_icon_position" + const val EXTRA_ICON_SURFACE = "gesture_nav_contract_surface_control" + const val EXTRA_REMOTE_CALLBACK = "android.intent.extra.REMOTE_CALLBACK" + + /** + * Clears and returns the GestureNavContract if it was present in the intent. + */ + fun fromIntent(intent: Intent): GestureNavContract? { + if (!isAtLeastApiLevel(Build.VERSION_CODES.R)) { + return null + } + val extras = intent.getBundleExtra(EXTRA_GESTURE_CONTRACT) + ?: return null + intent.removeExtra(EXTRA_GESTURE_CONTRACT) + val componentName = extras.getParcelable(Intent.EXTRA_COMPONENT_NAME) + val userHandle = extras.getParcelable(Intent.EXTRA_USER) + val callback = extras.getParcelable(EXTRA_REMOTE_CALLBACK) + return if (componentName != null && userHandle != null && callback != null && callback.replyTo != null + ) { + GestureNavContract(componentName, userHandle, callback) + } else null + } + } +} diff --git a/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt b/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt index 61887281..4417ad4d 100644 --- a/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt +++ b/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt @@ -209,4 +209,12 @@ val OpenSourceLicenses = arrayOf( licenseText = R.raw.license_apache_2, url = "https://github.com/material-foundation/material-color-utilities" ), + OpenSourceLibrary( + name = "Launcher3", + copyrightNote = "Copyright (C) 2020 The Android Open Source Project", + description = "The AOSP launcher", + licenseName = R.string.apache_license_name, + licenseText = R.raw.license_apache_2, + url = "https://source.android.com/" + ), ) \ No newline at end of file