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.server.companion; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.UserIdInt; 23 import android.companion.AssociationInfo; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 import java.util.Collection; 28 import java.util.List; 29 30 /** 31 * Interface for a store of {@link AssociationInfo}-s. 32 */ 33 public interface AssociationStore { 34 35 @IntDef(prefix = { "CHANGE_TYPE_" }, value = { 36 CHANGE_TYPE_ADDED, 37 CHANGE_TYPE_REMOVED, 38 CHANGE_TYPE_UPDATED_ADDRESS_CHANGED, 39 CHANGE_TYPE_UPDATED_ADDRESS_UNCHANGED, 40 }) 41 @Retention(RetentionPolicy.SOURCE) 42 @interface ChangeType {} 43 44 int CHANGE_TYPE_ADDED = 0; 45 int CHANGE_TYPE_REMOVED = 1; 46 int CHANGE_TYPE_UPDATED_ADDRESS_CHANGED = 2; 47 int CHANGE_TYPE_UPDATED_ADDRESS_UNCHANGED = 3; 48 49 /** Listener for any changes to {@link AssociationInfo}-s. */ 50 interface OnChangeListener { onAssociationChanged( @hangeType int changeType, AssociationInfo association)51 default void onAssociationChanged( 52 @ChangeType int changeType, AssociationInfo association) { 53 switch (changeType) { 54 case CHANGE_TYPE_ADDED: 55 onAssociationAdded(association); 56 break; 57 58 case CHANGE_TYPE_REMOVED: 59 onAssociationRemoved(association); 60 break; 61 62 case CHANGE_TYPE_UPDATED_ADDRESS_CHANGED: 63 onAssociationUpdated(association, true); 64 break; 65 66 case CHANGE_TYPE_UPDATED_ADDRESS_UNCHANGED: 67 onAssociationUpdated(association, false); 68 break; 69 } 70 } 71 onAssociationAdded(AssociationInfo association)72 default void onAssociationAdded(AssociationInfo association) {} 73 onAssociationRemoved(AssociationInfo association)74 default void onAssociationRemoved(AssociationInfo association) {} 75 onAssociationUpdated(AssociationInfo association, boolean addressChanged)76 default void onAssociationUpdated(AssociationInfo association, boolean addressChanged) {} 77 } 78 79 /** 80 * @return all CDM associations. 81 */ 82 @NonNull getAssociations()83 Collection<AssociationInfo> getAssociations(); 84 85 /** 86 * @return a {@link List} of associations that belong to the user. 87 */ 88 @NonNull getAssociationsForUser(@serIdInt int userId)89 List<AssociationInfo> getAssociationsForUser(@UserIdInt int userId); 90 91 /** 92 * @return a {@link List} of association that belong to the package. 93 */ 94 @NonNull getAssociationsForPackage( @serIdInt int userId, @NonNull String packageName)95 List<AssociationInfo> getAssociationsForPackage( 96 @UserIdInt int userId, @NonNull String packageName); 97 98 /** 99 * @return an association with the given address that belong to the given package if such an 100 * association exists, otherwise {@code null}. 101 */ 102 @Nullable getAssociationsForPackageWithAddress( @serIdInt int userId, @NonNull String packageName, @NonNull String macAddress)103 AssociationInfo getAssociationsForPackageWithAddress( 104 @UserIdInt int userId, @NonNull String packageName, @NonNull String macAddress); 105 106 /** 107 * @return an association with the given id if such an association exists, otherwise 108 * {@code null}. 109 */ 110 @Nullable getAssociationById(int id)111 AssociationInfo getAssociationById(int id); 112 113 /** 114 * @return all associations with the given MAc address. 115 */ 116 @NonNull getAssociationsByAddress(@onNull String macAddress)117 List<AssociationInfo> getAssociationsByAddress(@NonNull String macAddress); 118 119 /** Register a {@link OnChangeListener} */ registerListener(@onNull OnChangeListener listener)120 void registerListener(@NonNull OnChangeListener listener); 121 122 /** Un-register a previously registered {@link OnChangeListener} */ unregisterListener(@onNull OnChangeListener listener)123 void unregisterListener(@NonNull OnChangeListener listener); 124 125 /** @hide */ changeTypeToString(@hangeType int changeType)126 static String changeTypeToString(@ChangeType int changeType) { 127 switch (changeType) { 128 case CHANGE_TYPE_ADDED: 129 return "ASSOCIATION_ADDED"; 130 131 case CHANGE_TYPE_REMOVED: 132 return "ASSOCIATION_REMOVED"; 133 134 case CHANGE_TYPE_UPDATED_ADDRESS_CHANGED: 135 return "ASSOCIATION_UPDATED"; 136 137 case CHANGE_TYPE_UPDATED_ADDRESS_UNCHANGED: 138 return "ASSOCIATION_UPDATED_ADDRESS_UNCHANGED"; 139 140 default: 141 return "Unknown (" + changeType + ")"; 142 } 143 } 144 } 145