1 /* 2 * Copyright (C) 2023 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.quickaccesswallet; 18 19 import android.app.PendingIntent; 20 import android.content.Intent; 21 import android.graphics.Bitmap; 22 import android.graphics.drawable.Icon; 23 import android.os.IBinder; 24 import android.service.quickaccesswallet.GetWalletCardsCallback; 25 import android.service.quickaccesswallet.GetWalletCardsError; 26 import android.service.quickaccesswallet.GetWalletCardsRequest; 27 import android.service.quickaccesswallet.GetWalletCardsResponse; 28 import android.service.quickaccesswallet.QuickAccessWalletService; 29 import android.service.quickaccesswallet.SelectWalletCardRequest; 30 import android.service.quickaccesswallet.WalletCard; 31 import android.util.Log; 32 33 import androidx.annotation.NonNull; 34 import androidx.annotation.Nullable; 35 36 import java.util.ArrayList; 37 import java.util.Collections; 38 import java.util.List; 39 import java.util.concurrent.CountDownLatch; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Extends {@link QuickAccessWalletService} to allow for a different manifest configuration. 44 */ 45 public class TestBaseQuickAccessWalletService extends QuickAccessWalletService{ 46 private static final String TAG = "QAWalletServiceBase"; 47 48 private static GetWalletCardsError sWalletCardsError; 49 private static GetWalletCardsResponse sWalletCardsResponse; 50 private static boolean sWalletDismissed; 51 private static List<SelectWalletCardRequest> sSelectWalletCardRequests = new ArrayList<>(); 52 private static CountDownLatch sRequestCountDownLatch = new CountDownLatch(0); 53 private static CountDownLatch sBindCounter = new CountDownLatch(0); 54 private static CountDownLatch sUnbindCounter = new CountDownLatch(0); 55 resetStaticFields()56 public static void resetStaticFields() { 57 sWalletCardsError = null; 58 sWalletCardsResponse = null; 59 sWalletDismissed = false; 60 sSelectWalletCardRequests = new ArrayList<>(); 61 sRequestCountDownLatch = new CountDownLatch(0); 62 sBindCounter = new CountDownLatch(0); 63 sUnbindCounter = new CountDownLatch(0); 64 } 65 66 @Nullable 67 @Override onBind(@onNull Intent intent)68 public IBinder onBind(@NonNull Intent intent) { 69 sBindCounter.countDown(); 70 return super.onBind(intent); 71 } 72 73 @Override onUnbind(Intent intent)74 public boolean onUnbind(Intent intent) { 75 sUnbindCounter.countDown(); 76 return super.onUnbind(intent); 77 } 78 79 @Override onWalletCardsRequested( GetWalletCardsRequest request, GetWalletCardsCallback callback)80 public void onWalletCardsRequested( 81 GetWalletCardsRequest request, 82 GetWalletCardsCallback callback) { 83 Log.i(TAG, "onWalletCardsRequested"); 84 GetWalletCardsError error = sWalletCardsError; 85 if (error != null) { 86 callback.onFailure(error); 87 return; 88 } 89 GetWalletCardsResponse response = sWalletCardsResponse; 90 if (response == null) { 91 Bitmap bitmap = Bitmap.createBitmap( 92 request.getCardWidthPx(), request.getCardHeightPx(), Bitmap.Config.ARGB_8888); 93 Icon cardImage = Icon.createWithBitmap(bitmap.copy(Bitmap.Config.HARDWARE, false)); 94 Intent intent = new Intent(this, QuickAccessWalletActivity.class); 95 PendingIntent pendingIntent = 96 PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); 97 WalletCard walletCard = new WalletCard.Builder("card1", cardImage, "Card 1", 98 pendingIntent).build(); 99 List<WalletCard> walletCards = Collections.singletonList(walletCard); 100 response = new GetWalletCardsResponse(walletCards, 0); 101 } 102 callback.onSuccess(response); 103 } 104 105 @Override onWalletCardSelected(SelectWalletCardRequest request)106 public void onWalletCardSelected(SelectWalletCardRequest request) { 107 Log.i(TAG, "onWalletCardSelected"); 108 sSelectWalletCardRequests.add(request); 109 sRequestCountDownLatch.countDown(); 110 } 111 112 @Override onWalletDismissed()113 public void onWalletDismissed() { 114 Log.i(TAG, "onWalletDismissed"); 115 sWalletDismissed = true; 116 sRequestCountDownLatch.countDown(); 117 } 118 isWalletDismissed()119 public static boolean isWalletDismissed() { 120 return sWalletDismissed; 121 } 122 setWalletCardsResponse(GetWalletCardsResponse response)123 public static void setWalletCardsResponse(GetWalletCardsResponse response) { 124 sWalletCardsResponse = response; 125 } 126 setWalletCardsError(GetWalletCardsError error)127 public static void setWalletCardsError(GetWalletCardsError error) { 128 sWalletCardsError = error; 129 } 130 getSelectRequests()131 public static List<SelectWalletCardRequest> getSelectRequests() { 132 return new ArrayList<>(sSelectWalletCardRequests); 133 } 134 setExpectedRequestCount(int countdown)135 public static void setExpectedRequestCount(int countdown) { 136 sRequestCountDownLatch = new CountDownLatch(countdown); 137 } 138 awaitRequests(long timeout, TimeUnit timeUnit)139 public static void awaitRequests(long timeout, TimeUnit timeUnit) throws InterruptedException { 140 sRequestCountDownLatch.await(timeout, timeUnit); 141 } 142 setExpectedBindCount(int count)143 public static void setExpectedBindCount(int count) { 144 sBindCounter = new CountDownLatch(count); 145 } 146 awaitBinding(long timeout, TimeUnit unit)147 public static void awaitBinding(long timeout, TimeUnit unit) throws InterruptedException { 148 sBindCounter.await(timeout, unit); 149 } 150 setExpectedUnbindCount(int count)151 public static void setExpectedUnbindCount(int count) { 152 sUnbindCounter = new CountDownLatch(count); 153 } 154 awaitUnbinding(long timeout, TimeUnit unit)155 public static void awaitUnbinding(long timeout, TimeUnit unit) throws InterruptedException { 156 sUnbindCounter.await(timeout, unit); 157 } 158 }