Chalard Jean | 8c141bd | 2018-12-04 20:20:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.net; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Chalard Jean | f89d7be | 2018-12-07 23:09:02 +0900 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Chalard Jean | 8c141bd | 2018-12-04 20:20:56 +0900 | [diff] [blame] | 21 | import android.annotation.SystemService; |
| 22 | import android.content.Context; |
Chalard Jean | f89d7be | 2018-12-07 23:09:02 +0900 | [diff] [blame] | 23 | import android.net.ipmemorystore.Blob; |
| 24 | import android.net.ipmemorystore.IOnBlobRetrievedListener; |
| 25 | import android.net.ipmemorystore.IOnL2KeyResponseListener; |
| 26 | import android.net.ipmemorystore.IOnNetworkAttributesRetrieved; |
| 27 | import android.net.ipmemorystore.IOnSameNetworkResponseListener; |
| 28 | import android.net.ipmemorystore.IOnStatusListener; |
| 29 | import android.net.ipmemorystore.NetworkAttributes; |
Chalard Jean | 8c141bd | 2018-12-04 20:20:56 +0900 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | |
| 32 | import com.android.internal.util.Preconditions; |
| 33 | |
| 34 | /** |
| 35 | * The interface for system components to access the IP memory store. |
| 36 | * @see com.android.server.net.ipmemorystore.IpMemoryStoreService |
| 37 | * @hide |
| 38 | */ |
| 39 | @SystemService(Context.IP_MEMORY_STORE_SERVICE) |
| 40 | public class IpMemoryStore { |
| 41 | @NonNull final Context mContext; |
| 42 | @NonNull final IIpMemoryStore mService; |
| 43 | |
| 44 | public IpMemoryStore(@NonNull final Context context, @NonNull final IIpMemoryStore service) { |
| 45 | mContext = Preconditions.checkNotNull(context, "missing context"); |
| 46 | mService = Preconditions.checkNotNull(service, "missing IIpMemoryStore"); |
| 47 | } |
| 48 | |
| 49 | /** |
Chalard Jean | f89d7be | 2018-12-07 23:09:02 +0900 | [diff] [blame] | 50 | * Store network attributes for a given L2 key. |
| 51 | * If L2Key is null, choose automatically from the attributes ; passing null is equivalent to |
| 52 | * calling findL2Key with the attributes and storing in the returned value. |
| 53 | * |
| 54 | * @param l2Key The L2 key for the L2 network. Clients that don't know or care about the L2 |
| 55 | * key and only care about grouping can pass a unique ID here like the ones |
| 56 | * generated by {@code java.util.UUID.randomUUID()}, but keep in mind the low |
| 57 | * relevance of such a network will lead to it being evicted soon if it's not |
| 58 | * refreshed. Use findL2Key to try and find a similar L2Key to these attributes. |
| 59 | * @param attributes The attributes for this network. |
| 60 | * @param listener A listener that will be invoked to inform of the completion of this call, |
| 61 | * or null if the client is not interested in learning about success/failure. |
| 62 | * Through the listener, returns the L2 key. This is useful if the L2 key was not specified. |
| 63 | * If the call failed, the L2 key will be null. |
| 64 | */ |
| 65 | public void storeNetworkAttributes(@NonNull final String l2Key, |
| 66 | @NonNull final NetworkAttributes attributes, |
| 67 | @Nullable final IOnStatusListener listener) { |
Chalard Jean | 8c141bd | 2018-12-04 20:20:56 +0900 | [diff] [blame] | 68 | try { |
Chalard Jean | f89d7be | 2018-12-07 23:09:02 +0900 | [diff] [blame] | 69 | mService.storeNetworkAttributes(l2Key, attributes.toParcelable(), listener); |
| 70 | } catch (RemoteException e) { |
| 71 | throw e.rethrowFromSystemServer(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Store a binary blob associated with an L2 key and a name. |
| 77 | * |
| 78 | * @param l2Key The L2 key for this network. |
| 79 | * @param clientId The ID of the client. |
| 80 | * @param name The name of this data. |
| 81 | * @param data The data to store. |
| 82 | * @param listener A listener to inform of the completion of this call, or null if the client |
| 83 | * is not interested in learning about success/failure. |
| 84 | * Through the listener, returns a status to indicate success or failure. |
| 85 | */ |
| 86 | public void storeBlob(@NonNull final String l2Key, @NonNull final String clientId, |
| 87 | @NonNull final String name, @NonNull final Blob data, |
| 88 | @Nullable final IOnStatusListener listener) { |
| 89 | try { |
| 90 | mService.storeBlob(l2Key, clientId, name, data, listener); |
| 91 | } catch (RemoteException e) { |
| 92 | throw e.rethrowFromSystemServer(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the best L2 key associated with the attributes. |
| 98 | * |
| 99 | * This will find a record that would be in the same group as the passed attributes. This is |
| 100 | * useful to choose the key for storing a sample or private data when the L2 key is not known. |
| 101 | * If multiple records are group-close to these attributes, the closest match is returned. |
| 102 | * If multiple records have the same closeness, the one with the smaller (unicode codepoint |
| 103 | * order) L2 key is returned. |
| 104 | * If no record matches these attributes, null is returned. |
| 105 | * |
| 106 | * @param attributes The attributes of the network to find. |
| 107 | * @param listener The listener that will be invoked to return the answer. |
| 108 | * Through the listener, returns the L2 key if one matched, or null. |
| 109 | */ |
| 110 | public void findL2Key(@NonNull final NetworkAttributes attributes, |
| 111 | @NonNull final IOnL2KeyResponseListener listener) { |
| 112 | try { |
| 113 | mService.findL2Key(attributes.toParcelable(), listener); |
| 114 | } catch (RemoteException e) { |
| 115 | throw e.rethrowFromSystemServer(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns whether, to the best of the store's ability to tell, the two specified L2 keys point |
| 121 | * to the same L3 network. Group-closeness is used to determine this. |
| 122 | * |
| 123 | * @param l2Key1 The key for the first network. |
| 124 | * @param l2Key2 The key for the second network. |
| 125 | * @param listener The listener that will be invoked to return the answer. |
| 126 | * Through the listener, a SameL3NetworkResponse containing the answer and confidence. |
| 127 | */ |
| 128 | public void isSameNetwork(@NonNull final String l2Key1, @NonNull final String l2Key2, |
| 129 | @NonNull final IOnSameNetworkResponseListener listener) { |
| 130 | try { |
| 131 | mService.isSameNetwork(l2Key1, l2Key2, listener); |
| 132 | } catch (RemoteException e) { |
| 133 | throw e.rethrowFromSystemServer(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Retrieve the network attributes for a key. |
| 139 | * If no record is present for this key, this will return null attributes. |
| 140 | * |
| 141 | * @param l2Key The key of the network to query. |
| 142 | * @param listener The listener that will be invoked to return the answer. |
| 143 | * Through the listener, returns the network attributes and the L2 key associated with |
| 144 | * the query. |
| 145 | */ |
| 146 | public void retrieveNetworkAttributes(@NonNull final String l2Key, |
| 147 | @NonNull final IOnNetworkAttributesRetrieved listener) { |
| 148 | try { |
| 149 | mService.retrieveNetworkAttributes(l2Key, listener); |
| 150 | } catch (RemoteException e) { |
| 151 | throw e.rethrowFromSystemServer(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Retrieve previously stored private data. |
| 157 | * If no data was stored for this L2 key and name this will return null. |
| 158 | * |
| 159 | * @param l2Key The L2 key. |
| 160 | * @param clientId The id of the client that stored this data. |
| 161 | * @param name The name of the data. |
| 162 | * @param listener The listener that will be invoked to return the answer. |
| 163 | * Through the listener, returns the private data (or null), with the L2 key |
| 164 | * and the name of the data associated with the query. |
| 165 | */ |
| 166 | public void retrieveBlob(@NonNull final String l2Key, @NonNull final String clientId, |
| 167 | @NonNull final String name, @NonNull final IOnBlobRetrievedListener listener) { |
| 168 | try { |
| 169 | mService.retrieveBlob(l2Key, clientId, name, listener); |
Chalard Jean | 8c141bd | 2018-12-04 20:20:56 +0900 | [diff] [blame] | 170 | } catch (RemoteException e) { |
| 171 | throw e.rethrowFromSystemServer(); |
| 172 | } |
| 173 | } |
| 174 | } |