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.health.connect.aidl; 18 19 import android.annotation.NonNull; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.health.connect.HealthConnectManager; 23 import android.health.connect.datatypes.AppInfo; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Objects; 32 33 /** 34 * A parcel to carry response to {@link HealthConnectManager#getContributorApplicationsInfo} 35 * 36 * @hide 37 */ 38 public class ApplicationInfoResponseParcel implements Parcelable { 39 40 private final List<AppInfo> mAppInfoList; 41 private static final int COMPRESS_FACTOR = 100; 42 ApplicationInfoResponseParcel(@onNull List<AppInfo> appInfoList)43 public ApplicationInfoResponseParcel(@NonNull List<AppInfo> appInfoList) { 44 Objects.requireNonNull(appInfoList); 45 mAppInfoList = appInfoList; 46 } 47 48 public static final Creator<ApplicationInfoResponseParcel> CREATOR = 49 new Creator<ApplicationInfoResponseParcel>() { 50 @Override 51 public ApplicationInfoResponseParcel createFromParcel(Parcel in) { 52 return new ApplicationInfoResponseParcel(in); 53 } 54 55 @Override 56 public ApplicationInfoResponseParcel[] newArray(int size) { 57 return new ApplicationInfoResponseParcel[size]; 58 } 59 }; 60 ApplicationInfoResponseParcel(Parcel in)61 protected ApplicationInfoResponseParcel(Parcel in) { 62 int size = in.readInt(); 63 64 mAppInfoList = new ArrayList<>(size); 65 for (int i = 0; i < size; i++) { 66 String packageName = in.readString(); 67 String name = in.readString(); 68 byte[] icon = in.createByteArray(); 69 Bitmap bitmap = 70 icon != null ? BitmapFactory.decodeByteArray(icon, 0, icon.length) : null; 71 mAppInfoList.add(new AppInfo.Builder(packageName, name, bitmap).build()); 72 } 73 } 74 75 @NonNull getAppInfoList()76 public List<AppInfo> getAppInfoList() { 77 return mAppInfoList; 78 } 79 80 @Override describeContents()81 public int describeContents() { 82 return 0; 83 } 84 85 /** 86 * Flatten this object in to a Parcel. 87 * 88 * @param dest The Parcel in which the object should be written. 89 * @param flags Additional flags about how the object should be written. May be 0 or {@link 90 * #PARCELABLE_WRITE_RETURN_VALUE}. 91 */ 92 @Override writeToParcel(@onNull Parcel dest, int flags)93 public void writeToParcel(@NonNull Parcel dest, int flags) { 94 dest.writeInt(mAppInfoList.size()); 95 mAppInfoList.forEach( 96 (appInfo -> { 97 dest.writeString(appInfo.getPackageName()); 98 dest.writeString(appInfo.getName()); 99 Bitmap bitmap = appInfo.getIcon(); 100 byte[] bitmapData = null; 101 if (bitmap != null) { 102 try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { 103 bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESS_FACTOR, stream); 104 bitmapData = stream.toByteArray(); 105 } catch (IOException exception) { 106 throw new IllegalArgumentException(exception); 107 } 108 } 109 dest.writeByteArray(bitmapData); 110 })); 111 } 112 } 113