• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.shared;
18 
19 import android.annotation.Nullable;
20 import android.net.DhcpResults;
21 import android.net.DhcpResultsParcelable;
22 import android.net.InetAddresses;
23 
24 import java.net.Inet4Address;
25 import java.net.InetAddress;
26 
27 /**
28  * Collection of utility methods to convert to and from stable AIDL parcelables for IpClient
29  * configuration classes.
30  * @hide
31  */
32 public final class IpConfigurationParcelableUtil {
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         return p;
46     }
47 
48     /**
49      * Convert a DhcpResultsParcelable to DhcpResults.
50      */
fromStableParcelable(@ullable DhcpResultsParcelable p)51     public static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) {
52         if (p == null) return null;
53         final DhcpResults results = new DhcpResults(p.baseConfiguration);
54         results.leaseDuration = p.leaseDuration;
55         results.mtu = p.mtu;
56         results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress);
57         results.vendorInfo = p.vendorInfo;
58         results.serverHostName = p.serverHostName;
59         return results;
60     }
61 
62     /**
63      * Convert InetAddress to String.
64      * TODO: have an InetAddressParcelable
65      */
parcelAddress(@ullable InetAddress addr)66     public static String parcelAddress(@Nullable InetAddress addr) {
67         if (addr == null) return null;
68         return addr.getHostAddress();
69     }
70 
71     /**
72      * Convert String to InetAddress.
73      * TODO: have an InetAddressParcelable
74      */
unparcelAddress(@ullable String addr)75     public static InetAddress unparcelAddress(@Nullable String addr) {
76         if (addr == null) return null;
77         return InetAddresses.parseNumericAddress(addr);
78     }
79 }
80