• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tv.settings.connectivity.setup;
18 
19 import android.net.IpConfiguration;
20 import android.net.LinkAddress;
21 import android.net.ProxyInfo;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.IntDef;
25 import androidx.lifecycle.ViewModel;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 import java.net.InetAddress;
30 import java.util.HashMap;
31 
32 /**
33  * Class that stores the page information for advanced flow.
34  */
35 public class AdvancedOptionsFlowInfo extends ViewModel {
36     public static final int ADVANCED_OPTIONS = 1;
37     public static final int PROXY_SETTINGS = 2;
38     public static final int PROXY_HOSTNAME = 3;
39     public static final int PROXY_PORT = 4;
40     public static final int PROXY_BYPASS = 5;
41     public static final int IP_SETTINGS = 6;
42     public static final int IP_ADDRESS = 7;
43     public static final int NETWORK_PREFIX_LENGTH = 8;
44     public static final int GATEWAY = 9;
45     public static final int DNS1 = 10;
46     public static final int DNS2 = 11;
47 
48     @IntDef({
49             ADVANCED_OPTIONS,
50             PROXY_SETTINGS,
51             PROXY_HOSTNAME,
52             PROXY_PORT,
53             PROXY_BYPASS,
54             IP_SETTINGS,
55             IP_ADDRESS,
56             NETWORK_PREFIX_LENGTH,
57             GATEWAY,
58             DNS1,
59             DNS2
60     })
61     @Retention(RetentionPolicy.SOURCE)
62     public @interface PAGE {
63     }
64 
65     private IpConfiguration mIpConfiguration;
66     private String mPrintableSsid;
67     private boolean mSettingsFlow;
68     private boolean mCanStart = false;
69 
70     private HashMap<Integer, CharSequence> mPageSummary = new HashMap<>();
71 
72     /**
73      * Store the page summary into a HashMap.
74      *
75      * @param page The page as the key.
76      * @param info The info as the value.
77      */
put(@AGE int page, CharSequence info)78     public void put(@PAGE int page, CharSequence info) {
79         mPageSummary.put(page, info);
80     }
81 
82     /**
83      * Check if the summary of the queried page matches with expected string.
84      *
85      * @param choice The expected string.
86      * @param page   The page queried.
87      * @return true if matched.
88      */
choiceChosen(CharSequence choice, @PAGE int page)89     public boolean choiceChosen(CharSequence choice, @PAGE int page) {
90         if (!mPageSummary.containsKey(page)) {
91             return false;
92         }
93         return TextUtils.equals(choice, mPageSummary.get(page));
94     }
95 
96     /**
97      * Check if the Hashmap contains the summary of a particular page.
98      *
99      * @param page the queried page
100      * @return true if contains.
101      */
containsPage(@AGE int page)102     public boolean containsPage(@PAGE int page) {
103         return mPageSummary.containsKey(page);
104     }
105 
106     /**
107      * Get summary of a page.
108      *
109      * @param page The queried page.
110      * @return The summary of the page.
111      */
get(@AGE int page)112     public String get(@PAGE int page) {
113         if (!mPageSummary.containsKey(page)) {
114             return "";
115         }
116         return mPageSummary.get(page).toString();
117     }
118 
119     /**
120      * Remove the summary of a page.
121      *
122      * @param page The page.
123      */
remove(@AGE int page)124     public void remove(@PAGE int page) {
125         mPageSummary.remove(page);
126     }
127 
getIpConfiguration()128     public IpConfiguration getIpConfiguration() {
129         return mIpConfiguration;
130     }
131 
setIpConfiguration(IpConfiguration ipConfiguration)132     public void setIpConfiguration(IpConfiguration ipConfiguration) {
133         this.mIpConfiguration = ipConfiguration;
134     }
135 
isSettingsFlow()136     public boolean isSettingsFlow() {
137         return mSettingsFlow;
138     }
139 
setSettingsFlow(boolean settingsFlow)140     public void setSettingsFlow(boolean settingsFlow) {
141         mSettingsFlow = settingsFlow;
142     }
143 
setCanStart(boolean canStart)144     public void setCanStart(boolean canStart) {
145         this.mCanStart = canStart;
146     }
147 
148     /**
149      * Determine whether can start advanced flow.
150      *
151      * @return true if can.
152      */
canStart()153     public boolean canStart() {
154         return mCanStart;
155     }
156 
getPrintableSsid()157     public String getPrintableSsid() {
158         return mPrintableSsid;
159     }
160 
setPrintableSsid(String ssid)161     public void setPrintableSsid(String ssid) {
162         this.mPrintableSsid = ssid;
163     }
164 
165     /**
166      * Get the initial DNS.
167      */
getInitialDns(int index)168     public InetAddress getInitialDns(int index) {
169         if (mIpConfiguration != null && mIpConfiguration.getStaticIpConfiguration() != null
170                 && mIpConfiguration.getStaticIpConfiguration().getDnsServers() != null) {
171             try {
172                 return mIpConfiguration.getStaticIpConfiguration().getDnsServers().get(index);
173             } catch (IndexOutOfBoundsException e) {
174                 return null;
175             }
176         }
177         return null;
178     }
179 
180     /**
181      * Get the initial gateway.
182      */
getInitialGateway()183     public InetAddress getInitialGateway() {
184         if (mIpConfiguration != null && mIpConfiguration.getStaticIpConfiguration() != null) {
185             return mIpConfiguration.getStaticIpConfiguration().getGateway();
186         } else {
187             return null;
188         }
189     }
190 
191     /**
192      * Get the initial static ip configuration address.
193      */
getInitialLinkAddress()194     public LinkAddress getInitialLinkAddress() {
195         if (mIpConfiguration != null && mIpConfiguration.getStaticIpConfiguration() != null) {
196             return mIpConfiguration.getStaticIpConfiguration().getIpAddress();
197         } else {
198             return null;
199         }
200     }
201 
202     /**
203      * Get the initial proxy info.
204      */
getInitialProxyInfo()205     public ProxyInfo getInitialProxyInfo() {
206         if (mIpConfiguration != null) {
207             return mIpConfiguration.getHttpProxy();
208         } else {
209             return null;
210         }
211     }
212 }
213