| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.quickaccesswallet; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.graphics.Bitmap; |
| 21 |
import android.graphics.drawable.Icon; |
| 22 |
import android.os.Handler; |
| 23 |
import android.os.RemoteException; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.util.Log; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
final class GetWalletCardsCallbackImpl implements GetWalletCardsCallback { |
| 33 |
|
| 34 |
private static final String TAG = "QAWalletCallback"; |
| 35 |
|
| 36 |
private final IQuickAccessWalletServiceCallbacks mCallback; |
| 37 |
private final GetWalletCardsRequest mRequest; |
| 38 |
private final Handler mHandler; |
| 39 |
private boolean mCalled; |
| 40 |
|
| 41 |
GetWalletCardsCallbackImpl(GetWalletCardsRequest request, |
| 42 |
IQuickAccessWalletServiceCallbacks callback, Handler handler) { |
| 43 |
mRequest = request; |
| 44 |
mCallback = callback; |
| 45 |
mHandler = handler; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public void onSuccess(@NonNull GetWalletCardsResponse response) { |
| 57 |
if (isValidResponse(response)) { |
| 58 |
mHandler.post(() -> onSuccessInternal(response)); |
| 59 |
} else { |
| 60 |
Log.w(TAG, "Invalid GetWalletCards response"); |
| 61 |
mHandler.post(() -> onFailureInternal(new GetWalletCardsError(null, null))); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void onFailure(@NonNull GetWalletCardsError error) { |
| 74 |
mHandler.post(() -> onFailureInternal(error)); |
| 75 |
} |
| 76 |
|
| 77 |
private void onSuccessInternal(GetWalletCardsResponse response) { |
| 78 |
if (mCalled) { |
| 79 |
Log.w(TAG, "already called"); |
| 80 |
return; |
| 81 |
} |
| 82 |
mCalled = true; |
| 83 |
try { |
| 84 |
mCallback.onGetWalletCardsSuccess(response); |
| 85 |
} catch (RemoteException e) { |
| 86 |
Log.w(TAG, "Error returning wallet cards", e); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private void onFailureInternal(GetWalletCardsError error) { |
| 91 |
if (mCalled) { |
| 92 |
Log.w(TAG, "already called"); |
| 93 |
return; |
| 94 |
} |
| 95 |
mCalled = true; |
| 96 |
try { |
| 97 |
mCallback.onGetWalletCardsFailure(error); |
| 98 |
} catch (RemoteException e) { |
| 99 |
Log.e(TAG, "Error returning failure message", e); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private boolean isValidResponse(@NonNull GetWalletCardsResponse response) { |
| 104 |
if (response == null) { |
| 105 |
Log.w(TAG, "Invalid response: response is null"); |
| 106 |
return false; |
| 107 |
} |
| 108 |
if (response.getWalletCards() == null) { |
| 109 |
Log.w(TAG, "Invalid response: walletCards is null"); |
| 110 |
return false; |
| 111 |
} |
| 112 |
if (response.getSelectedIndex() < 0) { |
| 113 |
Log.w(TAG, "Invalid response: selectedIndex is negative"); |
| 114 |
return false; |
| 115 |
} |
| 116 |
if (!response.getWalletCards().isEmpty() |
| 117 |
&& response.getSelectedIndex() >= response.getWalletCards().size()) { |
| 118 |
Log.w(TAG, "Invalid response: selectedIndex out of bounds"); |
| 119 |
return false; |
| 120 |
} |
| 121 |
if (response.getWalletCards().size() > mRequest.getMaxCards()) { |
| 122 |
Log.w(TAG, "Invalid response: too many cards"); |
| 123 |
return false; |
| 124 |
} |
| 125 |
for (WalletCard walletCard : response.getWalletCards()) { |
| 126 |
if (walletCard == null) { |
| 127 |
Log.w(TAG, "Invalid response: card is null"); |
| 128 |
return false; |
| 129 |
} |
| 130 |
if (walletCard.getCardId() == null) { |
| 131 |
Log.w(TAG, "Invalid response: cardId is null"); |
| 132 |
return false; |
| 133 |
} |
| 134 |
Icon cardImage = walletCard.getCardImage(); |
| 135 |
if (cardImage == null) { |
| 136 |
Log.w(TAG, "Invalid response: cardImage is null"); |
| 137 |
return false; |
| 138 |
} |
| 139 |
if (cardImage.getType() == Icon.TYPE_BITMAP |
| 140 |
&& cardImage.getBitmap().getConfig() != Bitmap.Config.HARDWARE) { |
| 141 |
Log.w(TAG, "Invalid response: cardImage bitmaps must be hardware bitmaps"); |
| 142 |
return false; |
| 143 |
} |
| 144 |
if (TextUtils.isEmpty(walletCard.getContentDescription())) { |
| 145 |
Log.w(TAG, "Invalid response: contentDescription is null"); |
| 146 |
return false; |
| 147 |
} |
| 148 |
if (walletCard.getPendingIntent() == null) { |
| 149 |
Log.w(TAG, "Invalid response: pendingIntent is null"); |
| 150 |
return false; |
| 151 |
} |
| 152 |
} |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |