1 /* 2 * Copyright (C) 2016 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 com.android.server.wifi.hotspot2; 18 19 import com.android.internal.annotations.VisibleForTesting; 20 import com.android.server.wifi.Clock; 21 import com.android.server.wifi.hotspot2.anqp.ANQPElement; 22 import com.android.server.wifi.hotspot2.anqp.Constants; 23 24 import java.util.Collections; 25 import java.util.HashMap; 26 import java.util.Map; 27 28 /** 29 * Class for maintaining ANQP elements and managing the lifetime of the elements. 30 */ 31 public class ANQPData { 32 /** 33 * Entry lifetime. 34 */ 35 @VisibleForTesting 36 public static final long DATA_LIFETIME_MILLISECONDS = 3600000L; 37 38 private final Clock mClock; 39 private final Map<Constants.ANQPElementType, ANQPElement> mANQPElements; 40 private final long mExpiryTime; 41 42 public ANQPData(Clock clock, Map<Constants.ANQPElementType, ANQPElement> anqpElements) { 43 mClock = clock; 44 mANQPElements = new HashMap<>(); 45 if (anqpElements != null) { 46 mANQPElements.putAll(anqpElements); 47 } 48 mExpiryTime = mClock.getElapsedSinceBootMillis() + DATA_LIFETIME_MILLISECONDS; 49 } 50 51 /** 52 * Return the ANQP elements. 53 * 54 * @return Map of ANQP elements 55 */ 56 public Map<Constants.ANQPElementType, ANQPElement> getElements() { 57 return Collections.unmodifiableMap(mANQPElements); 58 } 59 60 /** 61 * Check if this entry is expired at the specified time. 62 * 63 * @param at The time to check for 64 * @return true if it is expired at the given time 65 */ 66 public boolean expired(long at) { 67 return mExpiryTime <= at; 68 } 69 70 @Override 71 public String toString() { 72 StringBuilder sb = new StringBuilder(); 73 sb.append(mANQPElements.size()).append(" elements, "); 74 long now = mClock.getElapsedSinceBootMillis(); 75 sb.append(" expires in ").append(Utils.toHMS(mExpiryTime - now)).append(' '); 76 sb.append(expired(now) ? 'x' : '-'); 77 return sb.toString(); 78 } 79 } 80