1 /* 2 * Copyright (C) 2022 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.internal.pm.pkg; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.pm.ApplicationInfo; 22 23 import com.android.server.pm.pkg.AndroidPackageSplit; 24 25 import java.util.Collections; 26 import java.util.List; 27 import java.util.Objects; 28 29 /** @hide */ 30 public class AndroidPackageSplitImpl implements AndroidPackageSplit { 31 32 @Nullable 33 private final String mName; 34 @NonNull 35 private final String mPath; 36 private final int mRevisionCode; 37 private final int mFlags; 38 @Nullable 39 private final String mClassLoaderName; 40 41 @NonNull 42 private List<AndroidPackageSplit> mDependencies = Collections.emptyList(); 43 AndroidPackageSplitImpl(@ullable String name, @NonNull String path, int revisionCode, int flags, @Nullable String classLoaderName)44 public AndroidPackageSplitImpl(@Nullable String name, @NonNull String path, int revisionCode, 45 int flags, @Nullable String classLoaderName) { 46 mName = name; 47 mPath = path; 48 mRevisionCode = revisionCode; 49 mFlags = flags; 50 mClassLoaderName = classLoaderName; 51 } 52 fillDependencies(@onNull List<AndroidPackageSplit> splits)53 public void fillDependencies(@NonNull List<AndroidPackageSplit> splits) { 54 if (!mDependencies.isEmpty()) { 55 throw new IllegalStateException("Cannot fill split dependencies more than once"); 56 } 57 mDependencies = splits; 58 } 59 60 @Nullable 61 @Override getName()62 public String getName() { 63 return mName; 64 } 65 66 @NonNull 67 @Override getPath()68 public String getPath() { 69 return mPath; 70 } 71 72 @Override getRevisionCode()73 public int getRevisionCode() { 74 return mRevisionCode; 75 } 76 77 @Override isHasCode()78 public boolean isHasCode() { 79 return (mFlags & ApplicationInfo.FLAG_HAS_CODE) != 0; 80 } 81 82 @Nullable 83 @Override getClassLoaderName()84 public String getClassLoaderName() { 85 return mClassLoaderName; 86 } 87 88 @NonNull 89 @Override getDependencies()90 public List<AndroidPackageSplit> getDependencies() { 91 return mDependencies; 92 } 93 94 @Override equals(Object o)95 public boolean equals(Object o) { 96 if (this == o) return true; 97 if (!(o instanceof AndroidPackageSplitImpl)) return false; 98 AndroidPackageSplitImpl that = (AndroidPackageSplitImpl) o; 99 var fieldsEqual = mRevisionCode == that.mRevisionCode && mFlags == that.mFlags 100 && Objects.equals(mName, that.mName) && Objects.equals(mPath, that.mPath) 101 && Objects.equals(mClassLoaderName, that.mClassLoaderName); 102 103 if (!fieldsEqual) return false; 104 if (mDependencies.size() != that.mDependencies.size()) return false; 105 106 // Should be impossible, but to avoid circular dependencies, 107 // only search 1 level deep using split name 108 for (int index = 0; index < mDependencies.size(); index++) { 109 if (!Objects.equals(mDependencies.get(index).getName(), 110 that.mDependencies.get(index).getName())) { 111 return false; 112 } 113 } 114 115 return true; 116 } 117 118 @Override hashCode()119 public int hashCode() { 120 // Should be impossible, but to avoid circular dependencies, 121 // only search 1 level deep using split name 122 var dependenciesHash = Objects.hash(mName, mPath, mRevisionCode, mFlags, mClassLoaderName); 123 for (int index = 0; index < mDependencies.size(); index++) { 124 var name = mDependencies.get(index).getName(); 125 dependenciesHash = 31 * dependenciesHash + (name == null ? 0 : name.hashCode()); 126 } 127 return dependenciesHash; 128 } 129 } 130