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