1 /* 2 * Copyright (C) 2021 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 com.android.car.qc.testutils; 18 19 import android.R; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.drawable.BitmapDrawable; 23 import android.graphics.drawable.Drawable; 24 import android.graphics.drawable.Icon; 25 import android.net.Uri; 26 import android.os.Bundle; 27 28 import androidx.annotation.NonNull; 29 30 import com.android.car.qc.QCItem; 31 import com.android.car.qc.QCTile; 32 import com.android.car.qc.provider.BaseQCProvider; 33 34 import java.io.ByteArrayOutputStream; 35 import java.util.HashSet; 36 import java.util.List; 37 import java.util.Set; 38 39 public abstract class TestQCProvider extends BaseQCProvider { 40 41 public static final String METHOD_IS_SUBSCRIBED = "METHOD_IS_SUBSCRIBED"; 42 public static final String IS_SUBSCRIBED_KEY = "IS_SUBSCRIBED"; 43 public static final String METHOD_IS_DESTROYED = "METHOD_IS_DESTROYED"; 44 public static final String IS_DESTROYED_KEY = "IS_DESTROYED"; 45 46 public static final String KEY_DEFAULT = "DEFAULT"; 47 public static final String KEY_SLOW = "SLOW"; 48 49 private final Set<Uri> mSubscribedUris = new HashSet<>(); 50 private final Set<Uri> mDestroyedUris = new HashSet<>(); 51 52 @Override call(String method, String arg, Bundle extras)53 public Bundle call(String method, String arg, Bundle extras) { 54 if (METHOD_IS_SUBSCRIBED.equals(method)) { 55 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_URI)); 56 Bundle bundle = new Bundle(); 57 bundle.putBoolean(IS_SUBSCRIBED_KEY, mSubscribedUris.contains(uri)); 58 return bundle; 59 } 60 if (METHOD_IS_DESTROYED.equals(method)) { 61 Uri uri = getUriWithoutUserId(extras.getParcelable(EXTRA_URI)); 62 Bundle bundle = new Bundle(); 63 bundle.putBoolean(IS_DESTROYED_KEY, mDestroyedUris.contains(uri)); 64 return bundle; 65 } 66 return super.call(method, arg, extras); 67 } 68 69 @Override onBind(@onNull Uri uri)70 protected QCItem onBind(@NonNull Uri uri) { 71 List<String> pathSegments = uri.getPathSegments(); 72 String key = pathSegments.get(0); 73 74 if (KEY_DEFAULT.equals(key)) { 75 return new QCTile.Builder() 76 .setIcon(Icon.createWithResource(getContext(), R.drawable.btn_star)) 77 .build(); 78 } else if (KEY_SLOW.equals(key)) { 79 // perform a slow operation that should trigger the strict thread policy 80 Drawable d = getContext().getDrawable(R.drawable.btn_star); 81 Bitmap bitmap = drawableToBitmap(d); 82 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 83 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 84 byte[] b = baos.toByteArray(); 85 Icon icon = Icon.createWithData(b, 0, b.length); 86 return new QCTile.Builder() 87 .setIcon(icon) 88 .build(); 89 } 90 return null; 91 } 92 93 @Override onSubscribed(@onNull Uri uri)94 protected void onSubscribed(@NonNull Uri uri) { 95 mSubscribedUris.add(uri); 96 } 97 98 @Override onUnsubscribed(@onNull Uri uri)99 protected void onUnsubscribed(@NonNull Uri uri) { 100 mSubscribedUris.remove(uri); 101 } 102 103 @Override onDestroy(@onNull Uri uri)104 protected void onDestroy(@NonNull Uri uri) { 105 mDestroyedUris.add(uri); 106 } 107 drawableToBitmap(Drawable drawable)108 private static Bitmap drawableToBitmap(Drawable drawable) { 109 110 if (drawable instanceof BitmapDrawable) { 111 return ((BitmapDrawable) drawable).getBitmap(); 112 } 113 114 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 115 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 116 Canvas canvas = new Canvas(bitmap); 117 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 118 drawable.draw(canvas); 119 120 return bitmap; 121 } 122 } 123