com.swisscom.safeconnect.fragment.InfoFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.swisscom.safeconnect.fragment.InfoFragment.java

Source

/*
 Swisscom Safe Connect
 Copyright (C) 2014 Swisscom
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.fragment;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.swisscom.safeconnect.R;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class InfoFragment extends Fragment {
    public static final String STATE = "InfoFragment.state";

    private TextView tvInfoTitle;
    private TextView tvInfoText;

    public enum State {
        NO_SUBSCIPTION, DISCONNECTED, NO_NETWORK
    }

    private State state;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dashboard_info, container, false);

        tvInfoTitle = (TextView) v.findViewById(R.id.tv_info_title);
        tvInfoText = (TextView) v.findViewById(R.id.tv_info_text);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setState(state);
    }

    public void setState(State state) {
        this.state = state;

        if (!isVisible())
            return;

        switch (state) {
        case DISCONNECTED:
            tvInfoTitle.setText(R.string.lab_insecure_connection);
            tvInfoText.setText(R.string.lab_connect_info);
            break;
        case NO_SUBSCIPTION:
            tvInfoTitle.setText(R.string.lab_no_abo);
            tvInfoText.setText(R.string.lab_no_abo_info);
            break;
        case NO_NETWORK:
            tvInfoTitle.setText(R.string.lab_no_connection);
            tvInfoText.setText(R.string.lab_no_connection_info);
            break;
        default:
            break;
        }
    }

    /*  private void updateConnectionInfo(){
    String name = getConnectedSSID();
    String connectedTo = String.format(getResources().getString(R.string.connected_to_wifi), name);
        
    if (name == null) {
        name = getCarrierName();
        connectedTo = String.format(getResources().getString(R.string.connected_to_mobile), name);
    }
        
    tvConnectionInfo.setText(connectedTo);
      }*/

    private String getConnectedSSID() {
        String ssid = null;
        ConnectivityManager connManager = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected()) {
            final WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
                ssid = connectionInfo.getSSID();
            }
        }
        return ssid;
    }

    private String getCarrierName() {
        TelephonyManager manager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String carrierName = manager.getNetworkOperatorName();

        return carrierName;
    }
}