Skip to content

Commit f932ea4

Browse files
committed
API 29 fixes
1 parent dd0cd67 commit f932ea4

File tree

10 files changed

+137
-52
lines changed

10 files changed

+137
-52
lines changed

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77

88
defaultConfig {
99
applicationId "io.snabble.testapp"
10-
minSdkVersion 16
10+
minSdkVersion project.minSdkVersion
1111
targetSdkVersion project.targetSdkVersion
1212
versionCode 2
1313
versionName "1.0"

sample/src/main/java/io/snabble/testapp/BaseActivity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public void run() {
7777
});
7878
}
7979

80-
8180
@Override
8281
public boolean dispatchKeyEvent(KeyEvent event) {
8382
String code = ZebraSupport.dispatchKeyEvent(this, event);
@@ -94,10 +93,10 @@ public boolean dispatchKeyEvent(KeyEvent event) {
9493
}
9594

9695
@Override
97-
protected void onStart() {
96+
protected void onResume() {
9897
SnabbleUI.registerUiCallbacks(this);
9998

100-
super.onStart();
99+
super.onResume();
101100
}
102101

103102
@Override

sample/src/main/java/io/snabble/testapp/CreditCardInputActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import io.snabble.sdk.ui.SnabbleUICallback;
66
import io.snabble.sdk.ui.integration.CreditCardInputFragment;
7-
import io.snabble.sdk.ui.integration.SEPACardInputFragment;
87

98
public class CreditCardInputActivity extends BaseActivity implements SnabbleUICallback {
109
@Override

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutEncodedCodesView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.snabble.sdk.ui.SnabbleUICallback;
3232
import io.snabble.sdk.ui.scanner.BarcodeView;
3333
import io.snabble.sdk.ui.telemetry.Telemetry;
34+
import io.snabble.sdk.ui.utils.I18nUtils;
3435
import io.snabble.sdk.ui.utils.OneShotClickListener;
3536
import io.snabble.sdk.utils.Logger;
3637

@@ -308,9 +309,9 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
308309

309310
if (position == 0) {
310311
if (codes.size() > 1) {
311-
holder.title.setText(getResources().getString(R.string.Snabble_QRCode_showTheseCodes, getItemCount()));
312+
holder.title.setText(getResources().getString(I18nUtils.getIdentifier(getResources(), R.string.Snabble_QRCode_showTheseCodes), getItemCount()));
312313
} else {
313-
holder.title.setText(R.string.Snabble_QRCode_showThisCode);
314+
holder.title.setText(I18nUtils.getIdentifier(getResources(), R.string.Snabble_QRCode_showThisCode));
314315
}
315316
} else {
316317
if (explanationText2.getVisibility() == View.GONE) {

ui/src/main/java/io/snabble/sdk/ui/checkout/PaymentMethodView.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,32 @@ public void onKeyguardResult(int resultCode) {
168168
entries.add(e);
169169
}
170170
} else {
171-
Entry e = new Entry();
171+
final Entry e = new Entry();
172172
e.text = descriptions.get(paymentMethod);
173173
e.paymentMethod = paymentMethod;
174174

175-
if (paymentMethod == PaymentMethod.DE_DIRECT_DEBIT) {
176-
e.onClickListener = new OnClickListener() {
177-
@Override
178-
public void onClick(View v) {
179-
showSEPACardInput();
180-
}
181-
};
182-
} else if (paymentMethod == PaymentMethod.VISA) {
183-
e.onClickListener = new OnClickListener() {
184-
@Override
185-
public void onClick(View v) {
186-
showCreditCardInput();
187-
}
188-
};
189-
} else if (paymentMethod == PaymentMethod.MASTERCARD) {
190-
e.onClickListener = new OnClickListener() {
191-
@Override
192-
public void onClick(View v) {
193-
showCreditCardInput();
194-
}
195-
};
175+
switch (paymentMethod) {
176+
case DE_DIRECT_DEBIT:
177+
e.onClickListener = new OnClickListener() {
178+
@Override
179+
public void onClick(View v) {
180+
showSEPACardInput();
181+
}
182+
};
183+
break;
184+
case MASTERCARD:
185+
case VISA:
186+
e.onClickListener = new OnClickListener() {
187+
@Override
188+
public void onClick(View v) {
189+
showCreditCardInput();
190+
}
191+
};
192+
break;
193+
default:
194+
checkout.pay(e.paymentMethod, e.paymentCredentials);
195+
Telemetry.event(Telemetry.Event.SelectedPaymentMethod, e.paymentMethod);
196+
break;
196197
}
197198

198199
entries.add(e);

ui/src/main/java/io/snabble/sdk/ui/payment/CreditCardInputView.java

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
import android.os.Handler;
1010
import android.os.Looper;
1111
import android.util.AttributeSet;
12+
import android.util.Base64;
1213
import android.view.View;
1314
import android.webkit.CookieManager;
1415
import android.webkit.JavascriptInterface;
1516
import android.webkit.WebChromeClient;
1617
import android.webkit.WebView;
1718
import android.webkit.WebViewClient;
1819
import android.widget.FrameLayout;
20+
import android.widget.ProgressBar;
1921
import android.widget.Toast;
2022

2123
import androidx.annotation.Keep;
2224
import androidx.core.view.ViewCompat;
2325

26+
2427
import org.apache.commons.io.IOUtils;
2528

2629
import java.io.IOException;
@@ -48,6 +51,9 @@ public class CreditCardInputView extends FrameLayout {
4851
private OkHttpClient okHttpClient;
4952
private Resources resources;
5053
private HashResponse lastHashResponse;
54+
private Handler handler;
55+
private ProgressBar progressBar;
56+
private boolean isAttachedToWindow;
5157

5258
public CreditCardInputView(Context context) {
5359
super(context);
@@ -74,10 +80,41 @@ private void inflateView() {
7480

7581
okHttpClient = Snabble.getInstance().getProjects().get(0).getOkHttpClient();
7682
resources = getContext().getResources();
83+
handler = new Handler(Looper.getMainLooper());
84+
85+
progressBar = findViewById(R.id.progress);
7786

7887
webView = findViewById(R.id.web_view);
79-
webView.setWebViewClient(new WebViewClient());
80-
webView.setWebChromeClient(new WebChromeClient());
88+
webView.setWebViewClient(new WebViewClient() {
89+
@Override
90+
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
91+
handler.post(new Runnable() {
92+
@Override
93+
public void run() {
94+
finishWithError();
95+
}
96+
});
97+
}
98+
});
99+
100+
webView.setWebChromeClient(new WebChromeClient() {
101+
@Override
102+
public void onProgressChanged(WebView view, final int newProgress) {
103+
handler.post(new Runnable() {
104+
@Override
105+
public void run() {
106+
if (newProgress == 100) {
107+
progressBar.setVisibility(View.GONE);
108+
} else {
109+
progressBar.setVisibility(View.VISIBLE);
110+
}
111+
112+
progressBar.setProgress(newProgress);
113+
}
114+
});
115+
}
116+
});
117+
81118
webView.getSettings().setJavaScriptEnabled(true);
82119
webView.getSettings().setDomStorageEnabled(true);
83120
webView.addJavascriptInterface(new JsInterface(), "snabble");
@@ -111,7 +148,7 @@ public void success(final HashResponse hashResponse) {
111148
handler.post(new Runnable() {
112149
@Override
113150
public void run() {
114-
loadCreditCardForm(hashResponse);
151+
loadForm(hashResponse);
115152
}
116153
});
117154
}
@@ -122,14 +159,18 @@ public void error(Throwable t) {
122159
handler.post(new Runnable() {
123160
@Override
124161
public void run() {
125-
showConnectionError();
162+
finishWithError();
126163
}
127164
});
128165
}
129166
});
130167
}
131168

132-
private void loadCreditCardForm(HashResponse hashResponse) {
169+
private void loadForm(HashResponse hashResponse) {
170+
if (!isAttachedToWindow) {
171+
return;
172+
}
173+
133174
try {
134175
String data = IOUtils.toString(resources.openRawResource(R.raw.snabble_creditcardform), Charset.forName("UTF-8"));
135176
data = data.replace("{{url}}", hashResponse.url);
@@ -141,22 +182,12 @@ private void loadCreditCardForm(HashResponse hashResponse) {
141182

142183
// hides credit card selection, V = VISA, but in reality we can enter any credit card that is supported
143184
data = data.replace("{{paymentMethod}}", "V");
144-
145-
webView.loadData(data, null, null);
185+
webView.loadData(Base64.encodeToString(data.getBytes(), Base64.DEFAULT), null, "base64");
146186
} catch (IOException e) {
147187
Logger.e(e.getMessage());
148188
}
149189
}
150190

151-
private void showConnectionError() {
152-
UIUtils.snackbar(CreditCardInputView.this,
153-
R.string.Snabble_networkError,
154-
UIUtils.SNACKBAR_LENGTH_VERY_LONG)
155-
.show();
156-
157-
finish();
158-
}
159-
160191
private void authenticateAndSave(final CreditCardInfo creditCardInfo) {
161192
cancelPreAuth(creditCardInfo);
162193

@@ -263,6 +294,8 @@ public void onAttachedToWindow() {
263294

264295
Application application = (Application) getContext().getApplicationContext();
265296
application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
297+
298+
isAttachedToWindow = true;
266299
}
267300

268301
@Override
@@ -271,6 +304,8 @@ public void onDetachedFromWindow() {
271304

272305
Application application = (Application) getContext().getApplicationContext();
273306
application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
307+
308+
isAttachedToWindow = false;
274309
}
275310

276311
private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks =

ui/src/main/java/io/snabble/sdk/ui/scanner/SelfScanningView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.snabble.sdk.ui.SnabbleUICallback;
4646
import io.snabble.sdk.ui.telemetry.Telemetry;
4747
import io.snabble.sdk.ui.utils.DelayedProgressDialog;
48+
import io.snabble.sdk.ui.utils.I18nUtils;
4849
import io.snabble.sdk.ui.utils.OneShotClickListener;
4950
import io.snabble.sdk.ui.utils.UIUtils;
5051
import io.snabble.sdk.utils.SimpleActivityLifecycleCallbacks;
@@ -245,7 +246,7 @@ public void onDismiss() {
245246
.setOnProductNotFoundListener(new ProductResolver.OnProductNotFoundListener() {
246247
@Override
247248
public void onProductNotFound() {
248-
showWarning(getResources().getString(R.string.Snabble_Scanner_unknownBarcode));
249+
showWarning(getResources().getString(I18nUtils.getIdentifier(getResources(), R.string.Snabble_Scanner_unknownBarcode)));
249250
}
250251
})
251252
.setOnNetworkErrorListener(new ProductResolver.OnNetworkErrorListener() {
@@ -257,7 +258,7 @@ public void onNetworkError() {
257258
.setOnShelfCodeScannedListener(new ProductResolver.OnShelfCodeScannedListener() {
258259
@Override
259260
public void onShelfCodeScanned() {
260-
showWarning(getResources().getString(R.string.Snabble_Scanner_scannedShelfCode));
261+
showWarning(getResources().getString(I18nUtils.getIdentifier(getResources(), R.string.Snabble_Scanner_scannedShelfCode)));
261262
}
262263
})
263264
.setOnSaleStopListener(new ProductResolver.OnSaleStopListener() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.snabble.sdk.ui.utils;
2+
3+
import android.content.res.Resources;
4+
5+
import io.snabble.sdk.Project;
6+
import io.snabble.sdk.ui.SnabbleUI;
7+
8+
public class I18nUtils {
9+
public static int getIdentifierForProject(Resources res, Project project, int id) {
10+
try {
11+
if (project != null) {
12+
String entryName = res.getResourceEntryName(id);
13+
String typeName = res.getResourceTypeName(id);
14+
String packageName = res.getResourcePackageName(id);
15+
16+
entryName = project.getId().replace("-", ".") + "." + entryName;
17+
int newId = res.getIdentifier(entryName, typeName, packageName);
18+
if (newId != 0) {
19+
return newId;
20+
}
21+
}
22+
} catch (RuntimeException e) {
23+
return id;
24+
}
25+
26+
return id;
27+
}
28+
29+
public static int getIdentifier(Resources res, int id) {
30+
return getIdentifierForProject(res, SnabbleUI.getProject(), id);
31+
}
32+
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:id="@+id/web_view"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
android:layout_width="match_parent"
54
android:layout_height="match_parent">
6-
</WebView>
5+
<WebView
6+
android:id="@+id/web_view"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent" />
9+
10+
<ProgressBar
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:id="@+id/progress"
14+
style="?android:attr/progressBarStyleHorizontal"
15+
android:visibility="gone"
16+
android:layout_marginTop="-7dp"/>
17+
</FrameLayout>

ui/src/main/res/raw/snabble_creditcardform.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
66

77
<script>
8-
window.addEventListener("DOMContentLoaded", init, false);
8+
document.onreadystatechange = function onDocumentStateChange() {
9+
if (document.readyState !== 'complete') return;
10+
11+
init();
12+
};
13+
914
window.addEventListener("message", receiveMessage, false);
1015

1116
function init() {
1217
var form1 = document.getElementById("form1");
18+
console.log("#");
1319
form1.submit();
1420
}
1521

@@ -61,7 +67,7 @@
6167
</script>
6268
</head>
6369
<body>
64-
<iframe name="embed1" id="embed1" src="#" width="100%" height="900px" style="border: none;">
70+
<iframe name="embed1" id="embed1" src="#" style="width:100%; height:900px; border: none;">
6571
</iframe>
6672

6773
<form method="post" action="{{url}}" target="embed1" id="form1">

0 commit comments

Comments
 (0)