1 /*
2  * Copyright (C) 2020 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 android.net.dhcp;
18 
19 import static android.net.shared.IpConfigurationParcelableUtil.parcelAddress;
20 import static android.net.shared.IpConfigurationParcelableUtil.unparcelAddress;
21 
22 import android.net.DhcpResults;
23 import android.net.DhcpResultsParcelable;
24 
25 import androidx.annotation.Nullable;
26 
27 import java.net.Inet4Address;
28 
29 /**
30  * A utility class to convert DhcpResults to DhcpResultsParcelable.
31  */
32 public final class DhcpResultsParcelableUtil {
33     /**
34      * Convert DhcpResults to a DhcpResultsParcelable.
35      */
toStableParcelable(@ullable DhcpResults results)36     public static DhcpResultsParcelable toStableParcelable(@Nullable DhcpResults results) {
37         if (results == null) return null;
38         final DhcpResultsParcelable p = new DhcpResultsParcelable();
39         p.baseConfiguration = results.toStaticIpConfiguration();
40         p.leaseDuration = results.leaseDuration;
41         p.mtu = results.mtu;
42         p.serverAddress = parcelAddress(results.serverAddress);
43         p.vendorInfo = results.vendorInfo;
44         p.serverHostName = results.serverHostName;
45         p.captivePortalApiUrl = results.captivePortalApiUrl;
46         return p;
47     }
48 
49     /**
50      * Convert a DhcpResultsParcelable to DhcpResults.
51      */
fromStableParcelable(@ullable DhcpResultsParcelable p)52     public static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) {
53         if (p == null) return null;
54         final DhcpResults results = new DhcpResults(p.baseConfiguration);
55         results.leaseDuration = p.leaseDuration;
56         results.mtu = p.mtu;
57         results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress);
58         results.vendorInfo = p.vendorInfo;
59         results.serverHostName = p.serverHostName;
60         results.captivePortalApiUrl = p.captivePortalApiUrl;
61         return results;
62     }
63 }
64