ANQPData.java revision 9f7795c694d048fdfecc4117cf5ddbd3e6ae1406
1package com.android.server.wifi.hotspot2; 2 3import com.android.server.wifi.anqp.ANQPElement; 4import com.android.server.wifi.anqp.Constants; 5 6import java.util.Collections; 7import java.util.Map; 8 9public class ANQPData { 10 /** 11 * The regular cache time for entries with a non-zero domain id. 12 */ 13 private static final long ANQP_QUALIFIED_CACHE_TIMEOUT = 3600000L; 14 /** 15 * The cache time for entries with a zero domain id. The zero domain id indicates that ANQP 16 * data from the AP may change at any time, thus a relatively short cache time is given to 17 * such data, but still long enough to avoid excessive querying. 18 */ 19 private static final long ANQP_UNQUALIFIED_CACHE_TIMEOUT = 60000L; 20 /** 21 * This is the hold off time for pending queries, i.e. the time during which subsequent queries 22 * are squelched. 23 */ 24 private static final long ANQP_HOLDOFF_TIME = 10000L; 25 26 private final NetworkDetail mNetwork; 27 private final Map<Constants.ANQPElementType, ANQPElement> mANQPElements; 28 private final long mCtime; 29 private final long mExpiry; 30 31 public ANQPData(NetworkDetail network, 32 Map<Constants.ANQPElementType, ANQPElement> anqpElements) { 33 34 mNetwork = network; 35 mANQPElements = anqpElements != null ? Collections.unmodifiableMap(anqpElements) : null; 36 mCtime = System.currentTimeMillis(); 37 if (anqpElements == null) { 38 mExpiry = mCtime + ANQP_HOLDOFF_TIME; 39 } 40 else if (network.getAnqpDomainID() == 0) { 41 mExpiry = mCtime + ANQP_UNQUALIFIED_CACHE_TIMEOUT; 42 } 43 else { 44 mExpiry = mCtime + ANQP_QUALIFIED_CACHE_TIMEOUT; 45 } 46 } 47 48 public Map<Constants.ANQPElementType, ANQPElement> getANQPElements() { 49 return Collections.unmodifiableMap(mANQPElements); 50 } 51 52 public NetworkDetail getNetwork() { 53 return mNetwork; 54 } 55 56 public boolean expired() { 57 return expired(System.currentTimeMillis()); 58 } 59 60 public boolean expired(long at) { 61 return mExpiry <= at; 62 } 63 64 protected boolean isValid(NetworkDetail nwk) { 65 return mANQPElements != null && 66 nwk.getAnqpDomainID() == mNetwork.getAnqpDomainID() && 67 mExpiry > System.currentTimeMillis(); 68 } 69 70 public String toString(boolean brief) { 71 StringBuilder sb = new StringBuilder(); 72 sb.append(mNetwork.toKeyString()).append(", domid ").append(mNetwork.getAnqpDomainID()); 73 if (mANQPElements == null) { 74 sb.append(", unresolved, "); 75 } 76 else { 77 sb.append(", ").append(mANQPElements.size()).append(" elements, "); 78 } 79 long now = System.currentTimeMillis(); 80 sb.append(Utils.toHMS(now-mCtime)).append(" old, expires in "). 81 append(Utils.toHMS(mExpiry-now)).append(' '); 82 if (brief) { 83 sb.append(expired(now) ? 'x' : '-'); 84 sb.append(mANQPElements == null ? 'u' : '-'); 85 } 86 else if (mANQPElements != null) { 87 sb.append(" data=").append(mANQPElements); 88 } 89 return sb.toString(); 90 } 91 92 @Override 93 public String toString() { 94 return toString(true); 95 } 96} 97