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.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.service.quickaccesswallet.WalletServiceEvent; 32 import android.util.Log; 33 34 import androidx.annotation.NonNull; 35 import androidx.annotation.Nullable; 36 37 import java.lang.ref.WeakReference; 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.concurrent.CountDownLatch; 42 import java.util.concurrent.TimeUnit; 43 44 public class TestQuickAccessWalletService extends QuickAccessWalletService { 45 46 private static final String TAG = "TestQAWalletSvc"; 47 private static GetWalletCardsResponse sWalletCardsResponse; 48 private static GetWalletCardsError sWalletCardsError; 49 private static List<SelectWalletCardRequest> sSelectWalletCardRequests = new ArrayList<>(); 50 private static boolean sWalletDismissed; 51 private static WeakReference<TestQuickAccessWalletService> sServiceRef = 52 new WeakReference<>(null); 53 private static CountDownLatch sRequestCountDownLatch = new CountDownLatch(0); 54 private static CountDownLatch sBindCounter = new CountDownLatch(0); 55 private static CountDownLatch sUnbindCounter = new CountDownLatch(0); 56 resetStaticFields()57 public static void resetStaticFields() { 58 sWalletCardsResponse = null; 59 sWalletCardsError = null; 60 sSelectWalletCardRequests = new ArrayList<>(); 61 sWalletDismissed = false; 62 sRequestCountDownLatch = new CountDownLatch(0); 63 sBindCounter = new CountDownLatch(0); 64 sUnbindCounter = new CountDownLatch(0); 65 sServiceRef.clear(); 66 } 67 68 @Override onCreate()69 public void onCreate() { 70 super.onCreate(); 71 sServiceRef = new WeakReference<>(this); 72 } 73 74 @Nullable 75 @Override onBind(@onNull Intent intent)76 public IBinder onBind(@NonNull Intent intent) { 77 sBindCounter.countDown(); 78 sServiceRef = new WeakReference<>(this); 79 return super.onBind(intent); 80 } 81 82 @Override onUnbind(Intent intent)83 public boolean onUnbind(Intent intent) { 84 sUnbindCounter.countDown(); 85 return super.onUnbind(intent); 86 } 87 88 @Override onWalletCardsRequested( GetWalletCardsRequest request, GetWalletCardsCallback callback)89 public void onWalletCardsRequested( 90 GetWalletCardsRequest request, 91 GetWalletCardsCallback callback) { 92 Log.i(TAG, "onWalletCardsRequested"); 93 GetWalletCardsError error = sWalletCardsError; 94 if (error != null) { 95 callback.onFailure(error); 96 return; 97 } 98 GetWalletCardsResponse response = sWalletCardsResponse; 99 if (response == null) { 100 Bitmap bitmap = Bitmap.createBitmap( 101 request.getCardWidthPx(), request.getCardHeightPx(), Bitmap.Config.ARGB_8888); 102 Icon cardImage = Icon.createWithBitmap(bitmap.copy(Bitmap.Config.HARDWARE, false)); 103 Intent intent = new Intent(this, QuickAccessWalletActivity.class); 104 PendingIntent pendingIntent = 105 PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); 106 WalletCard walletCard = new WalletCard.Builder("card1", cardImage, "Card 1", 107 pendingIntent).build(); 108 List<WalletCard> walletCards = Collections.singletonList(walletCard); 109 response = new GetWalletCardsResponse(walletCards, 0); 110 } 111 callback.onSuccess(response); 112 } 113 114 @Override onWalletCardSelected(SelectWalletCardRequest request)115 public void onWalletCardSelected(SelectWalletCardRequest request) { 116 Log.i(TAG, "onWalletCardSelected"); 117 sSelectWalletCardRequests.add(request); 118 sRequestCountDownLatch.countDown(); 119 } 120 121 @Override onWalletDismissed()122 public void onWalletDismissed() { 123 Log.i(TAG, "onWalletDismissed"); 124 sWalletDismissed = true; 125 sRequestCountDownLatch.countDown(); 126 } 127 sendEvent(WalletServiceEvent event)128 public static void sendEvent(WalletServiceEvent event) { 129 TestQuickAccessWalletService service = sServiceRef.get(); 130 if (service != null) { 131 service.sendWalletServiceEvent(event); 132 } 133 } 134 getSelectRequests()135 public static List<SelectWalletCardRequest> getSelectRequests() { 136 return new ArrayList<>(sSelectWalletCardRequests); 137 } 138 setWalletCardsResponse(GetWalletCardsResponse response)139 public static void setWalletCardsResponse(GetWalletCardsResponse response) { 140 sWalletCardsResponse = response; 141 } 142 setWalletCardsError(GetWalletCardsError error)143 public static void setWalletCardsError(GetWalletCardsError error) { 144 sWalletCardsError = error; 145 } 146 isWalletDismissed()147 public static boolean isWalletDismissed() { 148 return sWalletDismissed; 149 } 150 setExpectedRequestCount(int countdown)151 public static void setExpectedRequestCount(int countdown) { 152 sRequestCountDownLatch = new CountDownLatch(countdown); 153 } 154 awaitRequests(long timeout, TimeUnit timeUnit)155 public static void awaitRequests(long timeout, TimeUnit timeUnit) throws InterruptedException { 156 sRequestCountDownLatch.await(timeout, timeUnit); 157 } 158 setExpectedBindCount(int count)159 public static void setExpectedBindCount(int count) { 160 sBindCounter = new CountDownLatch(count); 161 } 162 awaitBinding(long timeout, TimeUnit unit)163 public static void awaitBinding(long timeout, TimeUnit unit) throws InterruptedException { 164 sBindCounter.await(timeout, unit); 165 } 166 setExpectedUnbindCount(int count)167 public static void setExpectedUnbindCount(int count) { 168 sUnbindCounter = new CountDownLatch(count); 169 } 170 awaitUnbinding(long timeout, TimeUnit unit)171 public static void awaitUnbinding(long timeout, TimeUnit unit) throws InterruptedException { 172 sUnbindCounter.await(timeout, unit); 173 } 174 } 175