From bb4a7a1e043ea2a09eb1589b7cf1c66e1f5022c4 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 31 Jan 2022 13:54:57 +0100 Subject: [PATCH] Migrate from deprecated MS Graph Android SDK to MS Graph Java SDK --- .../launcher2/licenses/OpenSourceLicenses.kt | 8 ++--- build.gradle.kts | 2 +- .../de/mm20/launcher2/msservices/DriveItem.kt | 2 +- .../msservices/MicrosoftGraphApiHelper.kt | 35 ++++++++++--------- settings.gradle.kts | 8 ++--- 5 files changed, 27 insertions(+), 28 deletions(-) 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 45ed8815..27cb1259 100644 --- a/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt +++ b/base/src/main/java/de/mm20/launcher2/licenses/OpenSourceLicenses.kt @@ -126,12 +126,12 @@ val OpenSourceLicenses = arrayOf( url = "https://github.com/googleapis/google-api-java-client" ), OpenSourceLibrary( - name = "Microsoft Graph Android SDK", - description = "Deprecated API client for Microsoft Graph APIs", + name = "Microsoft Graph SDK for Java", + description = "Get started with the Microsoft Graph SDK for Java by integrating the Microsoft Graph API into your Java application!", licenseName = R.string.mit_license_name, licenseText = R.raw.license_mit, - copyrightNote = "Copyright (c) 2015 Microsoft Corporation", - url = "https://github.com/microsoftgraph/msgraph-sdk-android" + copyrightNote = "Copyright (c) 2018 Microsoft Graph", + url = "https://github.com/microsoftgraph/msgraph-sdk-java" ), OpenSourceLibrary( name = "Microsoft Authentication Library (MSAL) for Android", diff --git a/build.gradle.kts b/build.gradle.kts index f9098efb..ee2a1325 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ allprojects { } } maven(url = "https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1") - jcenter() // For MS SDK + jcenter() // For draglinearlayout } } diff --git a/ms-services/src/main/java/de/mm20/launcher2/msservices/DriveItem.kt b/ms-services/src/main/java/de/mm20/launcher2/msservices/DriveItem.kt index cff2ae1b..bb828f27 100644 --- a/ms-services/src/main/java/de/mm20/launcher2/msservices/DriveItem.kt +++ b/ms-services/src/main/java/de/mm20/launcher2/msservices/DriveItem.kt @@ -1,6 +1,6 @@ package de.mm20.launcher2.msservices -import com.microsoft.graph.extensions.DriveItem as MSDriveItem +import com.microsoft.graph.models.DriveItem as MSDriveItem data class DriveItem( val id : String, diff --git a/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt b/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt index 95f34ec2..0723e590 100644 --- a/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt +++ b/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt @@ -4,11 +4,13 @@ import android.app.Activity import android.content.Context import android.util.Log import androidx.core.content.edit +import com.azure.core.credential.AccessToken +import com.azure.core.credential.TokenCredential +import com.microsoft.graph.authentication.TokenCredentialAuthProvider import com.microsoft.graph.core.ClientException -import com.microsoft.graph.core.DefaultClientConfig -import com.microsoft.graph.extensions.GraphServiceClient -import com.microsoft.graph.extensions.IGraphServiceClient import com.microsoft.graph.http.GraphServiceException +import com.microsoft.graph.models.DriveSearchParameterSet +import com.microsoft.graph.requests.GraphServiceClient import com.microsoft.identity.client.AuthenticationCallback import com.microsoft.identity.client.IAuthenticationResult import com.microsoft.identity.client.ISingleAccountPublicClientApplication @@ -18,6 +20,8 @@ import com.microsoft.identity.client.exception.MsalException import de.mm20.launcher2.crashreporter.CrashReporter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import okhttp3.Request +import reactor.core.publisher.Mono import java.net.URLEncoder import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine @@ -25,17 +29,14 @@ import kotlin.coroutines.suspendCoroutine class MicrosoftGraphApiHelper(val context: Context) { private var accessToken: String? = null - private val client: IGraphServiceClient - private var clientApplication: ISingleAccountPublicClientApplication? = null + private val client: GraphServiceClient = GraphServiceClient + .builder() + .authenticationProvider(TokenCredentialAuthProvider { + Mono.just(AccessToken(accessToken, null)) + }) + .buildClient() - init { - client = GraphServiceClient - .Builder() - .fromConfig(DefaultClientConfig.createWithAuthenticationProvider { - it.addHeader("Authorization", "Bearer $accessToken") - }) - .buildClient() - } + private var clientApplication: ISingleAccountPublicClientApplication? = null private suspend fun getClientApplication(): ISingleAccountPublicClientApplication? { val resId = getConfigResId() @@ -124,7 +125,7 @@ class MicrosoftGraphApiHelper(val context: Context) { if (!acquireAccessToken()) return null return withContext(Dispatchers.IO) { try { - val user = client.me.buildRequest().get() ?: return@withContext null + val user = client.me().buildRequest().get() ?: return@withContext null val name = user.displayName ?: user.mail ?: "Microsoft User" context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit { putString(PREF_ACCOUNT_NAME, name) @@ -151,8 +152,10 @@ class MicrosoftGraphApiHelper(val context: Context) { if (!acquireAccessToken()) return null return try { withContext(Dispatchers.IO) { - client.me.drive.getSearch( - URLEncoder.encode(query.replace("'", "''"), "utf8") + client.me().drive().search( + DriveSearchParameterSet.newBuilder() + .withQ(query) + .build() ) .buildRequest() .select("id,name,file,size,video,image,webUrl,shared,createdBy") diff --git a/settings.gradle.kts b/settings.gradle.kts index e7b8ffd6..0778cc54 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -362,16 +362,12 @@ dependencyResolutionManagement { .version("30.1.1-android") alias("microsoft.graph") - .to("com.microsoft.graph", "msgraph-sdk-android") - .version("1.7.0") + .to("com.microsoft.graph", "microsoft-graph") + .version("5.13.0") alias("microsoft.identity") .to("com.microsoft.identity.client", "msal") .version("2.2.0") - alias("android.volley") - .to("com.android.volley", "volley") - .version("1.1.1") - version("protobuf", "3.14.0") alias("protobuf.protoc") .to("com.google.protobuf", "protoc")