Skip to content

Commit bdb0fd8

Browse files
committed
The year already contains 4 digits, remove the unnecessary formatting
1 parent 0111031 commit bdb0fd8

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

java/app/src/main/java/com/google/android/gms/samples/wallet/activity/CheckoutActivity.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.app.AlertDialog;
2121
import android.app.PendingIntent;
2222
import android.content.Intent;
23-
import android.content.IntentSender;
2423
import android.content.IntentSender.SendIntentException;
2524
import android.os.Bundle;
2625
import android.text.Html;
@@ -39,14 +38,11 @@
3938
import com.google.android.gms.samples.wallet.util.Json;
4039
import com.google.android.gms.samples.wallet.util.PaymentsUtil;
4140
import com.google.android.gms.tasks.OnCompleteListener;
42-
import com.google.android.gms.tasks.OnFailureListener;
43-
import com.google.android.gms.tasks.OnSuccessListener;
4441
import com.google.android.gms.tasks.Task;
4542
import com.google.android.gms.wallet.AutoResolveHelper;
4643
import com.google.android.gms.wallet.CreditCardExpirationDate;
4744
import com.google.android.gms.wallet.IsReadyToPayRequest;
4845
import com.google.android.gms.wallet.PaymentCardRecognitionIntentRequest;
49-
import com.google.android.gms.wallet.PaymentCardRecognitionIntentResponse;
5046
import com.google.android.gms.wallet.PaymentCardRecognitionResult;
5147
import com.google.android.gms.wallet.PaymentData;
5248
import com.google.android.gms.wallet.PaymentDataRequest;
@@ -170,21 +166,10 @@ private void initializeUi() {
170166

171167
// The Google Pay button is a layout file – take the root view
172168
googlePayButton = layoutBinding.googlePayButton.getRoot();
173-
googlePayButton.setOnClickListener(
174-
new View.OnClickListener() {
175-
@Override
176-
public void onClick(View view) {
177-
requestPayment(view);
178-
}
179-
});
169+
googlePayButton.setOnClickListener(this::requestPayment);
180170

181171
paymentCardOcrButton = layoutBinding.paymentCardOcrButton;
182-
paymentCardOcrButton.setOnClickListener(new View.OnClickListener() {
183-
@Override
184-
public void onClick(View view) {
185-
startPaymentCardOcr(view);
186-
}
187-
});
172+
paymentCardOcrButton.setOnClickListener(this::startPaymentCardOcr);
188173
}
189174

190175
private void displayGarment(JSONObject garment) throws JSONException {
@@ -220,16 +205,13 @@ private void possiblyShowGooglePayButton() {
220205
// The call to isReadyToPay is asynchronous and returns a Task. We need to provide an
221206
// OnCompleteListener to be triggered when the result of the call is known.
222207
IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(isReadyToPayJson.get().toString());
223-
Task<Boolean> task = paymentsClient.isReadyToPay(request);
224-
task.addOnCompleteListener(this,
225-
new OnCompleteListener<Boolean>() {
226-
@Override
227-
public void onComplete(@NonNull Task<Boolean> task) {
228-
if (task.isSuccessful()) {
229-
setGooglePayAvailable(task.getResult());
230-
} else {
231-
Log.w("isReadyToPay failed", task.getException());
232-
}
208+
Task<Boolean> requestTask = paymentsClient.isReadyToPay(request);
209+
requestTask.addOnCompleteListener(this,
210+
task -> {
211+
if (task.isSuccessful()) {
212+
setGooglePayAvailable(task.getResult());
213+
} else {
214+
Log.w("isReadyToPay failed", task.getException());
233215
}
234216
});
235217
}
@@ -307,7 +289,7 @@ private void handleCardRecognitionSuccess(PaymentCardRecognitionResult cardResul
307289
CreditCardExpirationDate cardExpirationDate = cardResult.getCreditCardExpirationDate();
308290
if(cardExpirationDate != null) {
309291
expirationDate = String.format(locale,
310-
"%02d/%02d", cardExpirationDate.getMonth(), cardExpirationDate.getYear());
292+
"%02d/%d", cardExpirationDate.getMonth(), cardExpirationDate.getYear());
311293
}
312294

313295
String cardResultString = String.format(locale,

kotlin/app/src/main/java/com/google/android/gms/samples/wallet/CheckoutActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import kotlinx.android.synthetic.main.activity_checkout.*
3434
import org.json.JSONArray
3535
import org.json.JSONException
3636
import org.json.JSONObject
37+
import kotlin.math.roundToInt
3738
import kotlin.math.roundToLong
3839

3940

@@ -288,7 +289,7 @@ class CheckoutActivity : Activity() {
288289
)
289290

290291
} catch (e: JSONException) {
291-
Log.e("handlePaymentSuccess", "Error: " + e.toString())
292+
Log.e("handlePaymentSuccess", "Error: $e")
292293
}
293294

294295
}
@@ -309,13 +310,13 @@ class CheckoutActivity : Activity() {
309310
/**
310311
* Parses the results from the payment card recognition API and displays a toast.
311312
*
312-
* @param paymentCardRecognitionResult Result object from the payment card recognition API.
313+
* @param cardRecognitionResult Result object from the payment card recognition API.
313314
*/
314315
private fun handlePaymentCardRecognitionSuccess(
315316
cardRecognitionResult: PaymentCardRecognitionResult
316317
) {
317318
val creditCardExpirationDate = cardRecognitionResult.creditCardExpirationDate
318-
val expirationDate = creditCardExpirationDate?.let { "%02d/%02d".format(it.month, it.year) }
319+
val expirationDate = creditCardExpirationDate?.let { "%02d/%d".format(it.month, it.year) }
319320
val cardResultText = "PAN: ${cardRecognitionResult.pan}\nExpiration date: $expirationDate"
320321
Toast.makeText(this, cardResultText, Toast.LENGTH_LONG).show()
321322
}
@@ -333,7 +334,7 @@ class CheckoutActivity : Activity() {
333334
garmentList = Json.readFromResources(this, R.raw.tshirts)
334335
}
335336

336-
val randomIndex: Int = Math.round(Math.random() * (garmentList.length() - 1)).toInt()
337+
val randomIndex: Int = (Math.random() * (garmentList.length() - 1)).roundToInt()
337338
return garmentList.getJSONObject(randomIndex)
338339
}
339340

0 commit comments

Comments
 (0)