1 /* 2 * Copyright (C) 2024 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.wifi.usd; 18 19 import static android.Manifest.permission.MANAGE_WIFI_NETWORK_SELECTION; 20 21 import android.annotation.CallbackExecutor; 22 import android.annotation.FlaggedApi; 23 import android.annotation.NonNull; 24 import android.annotation.RequiresApi; 25 import android.annotation.RequiresPermission; 26 import android.annotation.SystemApi; 27 import android.net.wifi.flags.Flags; 28 import android.net.wifi.util.Environment; 29 import android.os.Build; 30 import android.util.Log; 31 32 import java.lang.ref.WeakReference; 33 import java.util.Objects; 34 import java.util.concurrent.Executor; 35 import java.util.function.Consumer; 36 37 /** 38 * A class to represent the USD Subscribe session. 39 * 40 * @hide 41 */ 42 @RequiresApi(Build.VERSION_CODES.BAKLAVA) 43 @SystemApi 44 @FlaggedApi(Flags.FLAG_USD) 45 public class SubscribeSession { 46 private static final String TAG = SubscribeSession.class.getName(); 47 private final WeakReference<UsdManager> mUsdManager; 48 private final int mSessionId; 49 50 /** @hide */ SubscribeSession(@onNull UsdManager usdManager, int sessionId)51 public SubscribeSession(@NonNull UsdManager usdManager, int sessionId) { 52 mUsdManager = new WeakReference<>(usdManager); 53 mSessionId = sessionId; 54 } 55 56 /** @hide */ getSessionId()57 public int getSessionId() { 58 return mSessionId; 59 } 60 61 /** 62 * Cancel the Subscribe Session 63 * 64 */ 65 @RequiresPermission(MANAGE_WIFI_NETWORK_SELECTION) cancel()66 public void cancel() { 67 if (!Environment.isSdkAtLeastB()) { 68 throw new UnsupportedOperationException(); 69 } 70 UsdManager usdManager = mUsdManager.get(); 71 if (usdManager == null) { 72 Log.w(TAG, "cancelSubscribe is called after the UsdManager has been garbage collected"); 73 return; 74 } 75 usdManager.cancelSubscribe(mSessionId); 76 } 77 78 /** 79 * Send a message to the peer. Message length is limited by 80 * {@link Characteristics#getMaxServiceSpecificInfoLength()}. 81 * 82 * @param peerId peer id obtained from {@link DiscoveryResult#getPeerId()} 83 * @param message byte array 84 * @param executor executor 85 * @param resultCallback result callback 86 */ 87 @RequiresPermission(MANAGE_WIFI_NETWORK_SELECTION) sendMessage(int peerId, @NonNull byte[] message, @NonNull @CallbackExecutor Executor executor, @NonNull Consumer<Boolean> resultCallback)88 public void sendMessage(int peerId, @NonNull byte[] message, 89 @NonNull @CallbackExecutor Executor executor, 90 @NonNull Consumer<Boolean> resultCallback) { 91 Objects.requireNonNull(executor, "executor must not be null"); 92 Objects.requireNonNull(resultCallback, "resultCallback must not be null"); 93 Objects.requireNonNull(message, "message must not be null"); 94 if (!Environment.isSdkAtLeastB()) { 95 throw new UnsupportedOperationException(); 96 } 97 UsdManager usdManager = mUsdManager.get(); 98 if (usdManager == null) { 99 Log.w(TAG, "sendMessage is called after the UsdManager has been garbage collected"); 100 executor.execute(() -> resultCallback.accept(false)); 101 return; 102 } 103 usdManager.sendMessage(mSessionId, peerId, message, executor, resultCallback); 104 } 105 } 106