ANQPData.java revision 5bee0e4616e2f8025d60cbfe3eec3e274a68a452
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 @Override 71 public String toString() { 72 StringBuilder sb = new StringBuilder(); 73 sb.append(mNetwork.toKeyString()).append(", domid ").append(mNetwork.getAnqpDomainID()); 74 if (mANQPElements == null) { 75 sb.append(", unresolved, "); 76 } 77 else { 78 sb.append(", ").append(mANQPElements.size()).append(" elements, "); 79 } 80 long now = System.currentTimeMillis(); 81 sb.append(Utils.toHMS(now-mCtime)).append(" old, expires in "). 82 append(Utils.toHMS(mExpiry-now)).append(' '); 83 sb.append(expired(now) ? 'x' : '-'); 84 sb.append(mANQPElements == null ? 'u' : '-'); 85 return sb.toString(); 86 } 87} 88