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 package com.google.snippet.wifi.direct; 17 18 import android.net.wifi.p2p.WifiP2pDevice; 19 import android.net.wifi.p2p.WifiP2pDeviceList; 20 import android.net.wifi.p2p.WifiP2pGroup; 21 import android.net.wifi.p2p.WifiP2pGroupList; 22 import android.net.wifi.p2p.WifiP2pInfo; 23 import android.os.Bundle; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.Iterator; 28 29 /** 30 * The class for converting Wi-Fi p2p classes to bundles. 31 * 32 * This is for passing them to Mobly snippet events. 33 */ 34 public class BundleUtils { 35 36 /** 37 * Convert a WifiP2pDevice obeject to a bundle. 38 * @param device The object to be converted. 39 * @return The bundle. 40 */ fromWifiP2pDevice(WifiP2pDevice device)41 public static Bundle fromWifiP2pDevice(WifiP2pDevice device) { 42 Bundle bundle = new Bundle(); 43 bundle.putString("deviceAddress", device.deviceAddress); 44 bundle.putString("deviceName", device.deviceName); 45 bundle.putString("primaryDeviceType", device.primaryDeviceType); 46 bundle.putString("secondaryDeviceType", device.secondaryDeviceType); 47 bundle.putInt("status", device.status); 48 bundle.putBoolean("isGroupOwner", device.isGroupOwner()); 49 return bundle; 50 } 51 52 /** 53 * Convert a WifiP2pDeviceList obeject to a bundle array. 54 * @param deviceList The object to be converted. 55 * @return The bundle array. 56 */ fromWifiP2pDeviceList(WifiP2pDeviceList deviceList)57 public static ArrayList<Bundle> fromWifiP2pDeviceList(WifiP2pDeviceList deviceList) { 58 Collection<WifiP2pDevice> devices = deviceList.getDeviceList(); 59 ArrayList<Bundle> bundles = new ArrayList<Bundle>(); 60 Iterator<WifiP2pDevice> i = devices.iterator(); 61 while (i.hasNext()) { 62 bundles.add(BundleUtils.fromWifiP2pDevice(i.next())); 63 } 64 return bundles; 65 } 66 67 /** 68 * Convert a WifiP2pInfo object to a bundle. 69 * @param info The object to be converted. 70 * @return The bundle. 71 */ fromWifiP2pInfo(WifiP2pInfo info)72 public static Bundle fromWifiP2pInfo(WifiP2pInfo info) { 73 if (info == null) { 74 return null; 75 } 76 Bundle bundle = new Bundle(); 77 String ownerAddress = null; 78 if (info.groupOwnerAddress != null) { 79 ownerAddress = info.groupOwnerAddress.getHostAddress(); 80 } 81 bundle.putBoolean("groupFormed", info.groupFormed); 82 bundle.putString("groupOwnerAddress", ownerAddress); 83 bundle.putBoolean("isGroupOwner", info.isGroupOwner); 84 return bundle; 85 } 86 87 /** 88 * Convert a WifiP2pGroup object to a bundle. 89 * @param group The object to be converted. 90 * @return The bundle. 91 */ fromWifiP2pGroup(WifiP2pGroup group)92 public static Bundle fromWifiP2pGroup(WifiP2pGroup group) { 93 if (group == null) { 94 return null; 95 } 96 Bundle bundle = new Bundle(); 97 bundle.putInt("frequency", group.getFrequency()); 98 bundle.putString("interface", group.getInterface()); 99 bundle.putInt("networkId", group.getNetworkId()); 100 bundle.putString("networkName", group.getNetworkName()); 101 bundle.putBundle("owner", fromWifiP2pDevice(group.getOwner())); 102 bundle.putString("passphrase", group.getPassphrase()); 103 bundle.putBoolean("isGroupOwner", group.isGroupOwner()); 104 return bundle; 105 } 106 107 /** 108 * Convert a WifiP2pGroupList object to a bundle array. 109 * @param groupList The object to be converted 110 * @return The bundle array. 111 */ fromWifiP2pGroupList(WifiP2pGroupList groupList)112 public static ArrayList<Bundle> fromWifiP2pGroupList(WifiP2pGroupList groupList) { 113 Collection<WifiP2pGroup> groups = groupList.getGroupList(); 114 ArrayList<Bundle> bundles = new ArrayList<Bundle>(); 115 Iterator<WifiP2pGroup> i = groups.iterator(); 116 while (i.hasNext()) { 117 bundles.add(BundleUtils.fromWifiP2pGroup(i.next())); 118 } 119 return bundles; 120 } 121 } 122