Debian Codegraph @

core/java/android/service/quickaccesswallet/GetWalletCardsCallbackImpl.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Handles response from the {@link QuickAccessWalletService} for {@link GetWalletCardsRequest}
29 *
30 * @hide
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 * Notifies the Android System that an {@link QuickAccessWalletService#onWalletCardsRequested}
50 * was successfully handled by the service.
51 *
52 * @param response The response contains the list of {@link WalletCard walletCards} to be shown
53 * to the user as well as the index of the card that should initially be
54 * presented as the selected card.
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 * Notifies the Android System that an {@link QuickAccessWalletService#onWalletCardsRequested}
67 * could not be handled by the service.
68 *
69 * @param error The error message. <b>Note: </b> this message should <b>not</b> contain PII
70 * (Personally Identifiable Information, such as username or email address).
71 * @throws IllegalStateException if this method or {@link #onSuccess} was already called.
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 }