Skip to content

Commit 435b9e1

Browse files
authored
Convert shared utils exception classes and AppHelper to Kotlin (#1369)
* Convert shared utils exception classes and AppHelper * Fix nullability issues * Fix lint
1 parent e122c68 commit 435b9e1

22 files changed

+93
-166
lines changed

BraintreeCore/src/main/java/com/braintreepayments/api/core/BraintreeHttpResponseParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class BraintreeHttpResponseParser(
2424
baseParser.parse(responseCode, connection)
2525
} catch (e: AuthorizationException) {
2626
val errorMessage = ErrorWithResponse(AUTH_ERROR_CODE, e.message).message
27-
throw AuthorizationException(errorMessage)
27+
throw AuthorizationException(errorMessage ?: "AuthorizationException with null message")
2828
} catch (e: UnprocessableEntityException) {
2929
throw ErrorWithResponse(UNPROCESSABLE_ENTITY_ERROR_CODE, e.message)
3030
}

BraintreeCore/src/main/java/com/braintreepayments/api/core/DeviceInspector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DeviceInspector(
5151
* @param context A context to access the installed packages.
5252
* @return boolean depending on if the Venmo app is installed, and has a valid signature.
5353
*/
54-
fun isVenmoAppSwitchAvailable(context: Context?): Boolean {
54+
fun isVenmoAppSwitchAvailable(context: Context): Boolean {
5555
val isVenmoIntentAvailable = appHelper.isIntentAvailable(context, venmoIntent)
5656
val isVenmoSignatureValid = signatureVerifier.isSignatureValid(
5757
context, VENMO_APP_PACKAGE, VENMO_BASE_64_ENCODED_SIGNATURE
@@ -63,7 +63,7 @@ class DeviceInspector(
6363
return appHelper.isAppInstalled(context, PAYPAL_APP_PACKAGE)
6464
}
6565

66-
fun isVenmoInstalled(context: Context?): Boolean {
66+
fun isVenmoInstalled(context: Context): Boolean {
6767
return appHelper.isAppInstalled(context, VENMO_APP_PACKAGE)
6868
}
6969

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/AppHelper.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import android.content.pm.PackageManager
6+
import androidx.annotation.RestrictTo
7+
8+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
9+
class AppHelper {
10+
11+
companion object {
12+
const val NO_FLAGS = 0
13+
}
14+
15+
fun isIntentAvailable(context: Context, intent: Intent): Boolean {
16+
val activities = context.packageManager.queryIntentActivities(intent, NO_FLAGS)
17+
return activities.size == 1
18+
}
19+
20+
fun isAppInstalled(context: Context, packageName: String): Boolean {
21+
return try {
22+
context.packageManager.getApplicationInfo(packageName, NO_FLAGS)
23+
true
24+
} catch (_: PackageManager.NameNotFoundException) {
25+
false
26+
}
27+
}
28+
}

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/AuthenticationException.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
/**
4+
* Exception thrown when a 401 HTTP_UNAUTHORIZED response is encountered. Indicates authentication
5+
* has failed in some way.
6+
*/
7+
class AuthenticationException(message: String) : Exception(message)

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/AuthorizationException.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
/**
4+
* Exception thrown when a 403 HTTP_FORBIDDEN response is encountered. Indicates the current
5+
* authorization does not have permission to make the request.
6+
*/
7+
class AuthorizationException(message: String) : Exception(message)

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/HttpClientException.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
/**
4+
* Exception thrown when an HTTP request fails.
5+
*/
6+
class HttpClientException(message: String) : Exception(message)

0 commit comments

Comments
 (0)