|
4 | 4 | import android.app.Activity; |
5 | 5 | import android.app.Application; |
6 | 6 | import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.pm.ResolveInfo; |
7 | 9 | import android.net.Uri; |
8 | 10 | import android.os.Build; |
9 | 11 | import android.util.AttributeSet; |
|
19 | 21 |
|
20 | 22 | import androidx.activity.ComponentActivity; |
21 | 23 | import androidx.activity.OnBackPressedCallback; |
| 24 | +import androidx.annotation.NonNull; |
| 25 | +import androidx.annotation.Nullable; |
22 | 26 |
|
| 27 | +import java.util.List; |
23 | 28 | import java.util.Map; |
24 | 29 | import java.util.UUID; |
25 | 30 |
|
| 31 | +import io.snabble.sdk.Environment; |
26 | 32 | import io.snabble.sdk.Snabble; |
27 | 33 | import io.snabble.sdk.payment.PaymentCredentials; |
28 | 34 | import io.snabble.sdk.payment.data.GiropayAuthorizationData; |
@@ -122,10 +128,18 @@ public void onReceivedError(WebView view, int errorCode, String description, Str |
122 | 128 |
|
123 | 129 | @Override |
124 | 130 | public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { |
125 | | - Uri uri = request.getUrl(); |
126 | | - if (uri != null) { |
127 | | - String url = uri.toString(); |
128 | | - Logger.d("shouldOverrideUrlLoading " + url); |
| 131 | + final Uri uri = request.getUrl(); |
| 132 | + final Environment environment = Snabble.getInstance().getEnvironment(); |
| 133 | + if (isGiropayAppLinkUrl(uri, environment)) { |
| 134 | + final Intent giropayAppLinkIntent = new Intent(Intent.ACTION_VIEW); |
| 135 | + giropayAppLinkIntent.setData(request.getUrl()); |
| 136 | + if (isGiropayAppAvailable(giropayAppLinkIntent, view.getContext(), environment)) { |
| 137 | + view.getContext().startActivity(giropayAppLinkIntent); |
| 138 | + return true; |
| 139 | + } |
| 140 | + } else if (uri != null) { |
| 141 | + final String url = uri.toString(); |
| 142 | + Logger.d("shouldOverrideUrlLoading: <" + url + ">"); |
129 | 143 |
|
130 | 144 | switch (url) { |
131 | 145 | case SUCCESS_URL: |
@@ -216,6 +230,46 @@ public void error(Throwable t) { |
216 | 230 | }); |
217 | 231 | } |
218 | 232 |
|
| 233 | + private boolean isGiropayAppLinkUrl(@Nullable final Uri uri, @Nullable Environment environment) { |
| 234 | + if (uri != null && uri.getHost() != null) { |
| 235 | + return uri.getHost().startsWith(getGiropayAppLinkUrlHost(environment)); |
| 236 | + } |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + @NonNull |
| 241 | + private String getGiropayAppLinkUrlHost(@Nullable final Environment environment) { |
| 242 | + if (environment == Environment.PRODUCTION) { |
| 243 | + return "app.paydirekt.de"; |
| 244 | + } else { |
| 245 | + return "app.sandbox.paydirekt.de"; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + private boolean isGiropayAppAvailable( |
| 250 | + @NonNull final Intent intent, |
| 251 | + @NonNull final Context context, |
| 252 | + @Nullable final Environment environment |
| 253 | + ) { |
| 254 | + final List<ResolveInfo> intentInfo = context.getPackageManager().queryIntentActivities(intent, 0); |
| 255 | + final String appPackageName = getGiropayAppPackage(environment); |
| 256 | + for (final ResolveInfo info : intentInfo) { |
| 257 | + if (info.activityInfo.packageName.contains(appPackageName)) { |
| 258 | + return true; |
| 259 | + } |
| 260 | + } |
| 261 | + return false; |
| 262 | + } |
| 263 | + |
| 264 | + @NonNull |
| 265 | + private String getGiropayAppPackage(final @Nullable Environment environment) { |
| 266 | + if (environment == Environment.PRODUCTION) { |
| 267 | + return "com.gimb.paydirekt.app"; |
| 268 | + } else { |
| 269 | + return "com.gimb.paydirekt.app.sandbox"; |
| 270 | + } |
| 271 | + } |
| 272 | + |
219 | 273 | private void authenticateAndSave() { |
220 | 274 | Keyguard.unlock(UIUtils.getHostFragmentActivity(getContext()), new Keyguard.Callback() { |
221 | 275 | @Override |
|
0 commit comments