|
| 1 | +/* |
| 2 | +Copyright 2014 Software Freedom Conservancy |
| 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 org.openqa.selenium.mobile; |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Control a device's network connection |
| 22 | + * <p> |
| 23 | + * Example usage: |
| 24 | + * |
| 25 | + * <pre> |
| 26 | + * NetworkConnection mobileDriver = (NetworkConnection) driver; |
| 27 | + * if (mobileDriver.getNetworkConnection() != ConnectionType.AIRPLANE_MODE) { |
| 28 | + * // enabling Airplane mode |
| 29 | + * mobileDriver.setNetworkConnection(ConnectionType.AIRPLANE_MODE); |
| 30 | + * } |
| 31 | + * </pre> |
| 32 | + */ |
| 33 | +public interface NetworkConnection { |
| 34 | + |
| 35 | + /** |
| 36 | + * ConnectionType is a bitmask to represent a device's network connection |
| 37 | + * Data | WIFI | Airplane |
| 38 | + * 0 0 1 == 1 |
| 39 | + * 1 1 0 == 6 |
| 40 | + * 1 0 0 == 4 |
| 41 | + * 0 1 0 == 2 |
| 42 | + * 0 0 0 == 0 |
| 43 | + * |
| 44 | + * Giving "Data" the first bit positions in order to give room for the future of enabling |
| 45 | + * specific types of data (Edge / 2G, 3G, 4G, LTE, etc) if the device allows it. |
| 46 | + */ |
| 47 | + public class ConnectionType { |
| 48 | + public static final ConnectionType WIFI = new ConnectionType(2); |
| 49 | + public static final ConnectionType DATA = new ConnectionType(4); |
| 50 | + public static final ConnectionType AIRPLANE_MODE = new ConnectionType(1); |
| 51 | + public static final ConnectionType ALL = new ConnectionType(6); |
| 52 | + public static final ConnectionType NONE = new ConnectionType(0); |
| 53 | + |
| 54 | + /* |
| 55 | + Future for Network Data types. With a new constructor accepting this enum. |
| 56 | + public enum DataType { |
| 57 | + _2G, _3G, _4G, LTE |
| 58 | +
|
| 59 | + } |
| 60 | + */ |
| 61 | + |
| 62 | + private int mask = 0; |
| 63 | + |
| 64 | + public ConnectionType(Boolean wifi, Boolean data, Boolean airplaneMode) { |
| 65 | + if (wifi) { |
| 66 | + mask += WIFI.mask; |
| 67 | + } |
| 68 | + if (data) { |
| 69 | + mask += DATA.mask; |
| 70 | + } |
| 71 | + if (airplaneMode) { |
| 72 | + mask += AIRPLANE_MODE.mask; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public ConnectionType(int mask) { |
| 77 | + // must be a positive number |
| 78 | + this.mask = Math.max(mask, 0); |
| 79 | + } |
| 80 | + |
| 81 | + public Boolean isAirplaneMode() { |
| 82 | + return mask % 2 == 1; |
| 83 | + } |
| 84 | + |
| 85 | + public Boolean isWifiEnabled() { |
| 86 | + // shift right 1 bit, check last bit |
| 87 | + return (mask / 2) % 2 == 1; |
| 88 | + } |
| 89 | + |
| 90 | + public Boolean isDataEnabled() { |
| 91 | + // shift right 2 bits, check if any bits set |
| 92 | + return (mask / 4) > 0; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object type) { |
| 97 | + return type instanceof ConnectionType && this.mask == ((ConnectionType)type).mask; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + return Integer.toString(mask); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Query the driver for the Airplane Mode setting state |
| 109 | + * |
| 110 | + * @return ConnectionType indicating if the device is in Airplane Mode |
| 111 | + * @see org.openqa.selenium.mobile.NetworkConnection.ConnectionType |
| 112 | + */ |
| 113 | + public ConnectionType getNetworkConnection(); |
| 114 | + |
| 115 | + /** |
| 116 | + * Set the Connection type |
| 117 | + * Not all connection type combinations are valid for an individual type of device |
| 118 | + * and the remote endpoint will make a best effort to set the type as requested |
| 119 | + * |
| 120 | + * @param type ConnectionType of what the network connection should be |
| 121 | + * |
| 122 | + * @return @ConnectionType of what the device's network connection is |
| 123 | + * @see org.openqa.selenium.mobile.NetworkConnection.ConnectionType |
| 124 | + */ |
| 125 | + public ConnectionType setNetworkConnection(ConnectionType type); |
| 126 | + |
| 127 | +} |
0 commit comments