package de.tudarmstadt.informatik.hostage.ui2.fragment; import android.app.AlertDialog; import android.app.Fragment; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import de.tudarmstadt.informatik.hostage.R; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; /** * @author Alexander Brakowski * @created 13.01.14 19:06 */ public class HomeFragment extends Fragment { private Switch mHomeSwitchConnection; private TextView mHomeTextName; private TextView mHomeTextSecurity; private TextView mHomeTextAttacks; private TextView mHomeTextProfile; private TextView mHomeTextProfileHeader; private View rootView; private BroadcastReceiver wifiReceiver; private CompoundButton.OnCheckedChangeListener switchChangeListener = null; private int defaultTextColor; private void assignViews() { mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection); mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name); mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security); mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks); mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile); mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header); } private void registerBroadcastReceiver(){ wifiReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); //if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) || action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { updateUI(); /*if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){ } else { // wifi connection was lost }*/ //} } }; IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); getActivity().registerReceiver(wifiReceiver, intentFilter); } public HomeFragment(){} public void setStateNotActive(){ mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey)); mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey)); mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey)); mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey)); mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey)); mHomeSwitchConnection.setChecked(false); } public void setStateActive(){ mHomeTextAttacks.setText("0 attacks recorded"); mHomeTextSecurity.setText("Secure"); mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green)); mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green)); mHomeTextAttacks.setVisibility(View.VISIBLE); mHomeTextSecurity.setVisibility(View.VISIBLE); mHomeTextName.setTextColor(defaultTextColor); mHomeTextProfile.setTextColor(defaultTextColor); mHomeTextProfileHeader.setTextColor(defaultTextColor); mHomeSwitchConnection.setChecked(true); } public void setStateNotConnected(){ mHomeTextSecurity.setVisibility(View.INVISIBLE); mHomeTextAttacks.setVisibility(View.INVISIBLE); mHomeTextProfile.setVisibility(View.INVISIBLE); mHomeTextProfileHeader.setVisibility(View.INVISIBLE); mHomeTextName.setText("Not connected"); } public void setStateConnected(){ mHomeTextProfile.setVisibility(View.VISIBLE); mHomeTextProfileHeader.setVisibility(View.VISIBLE); } public void updateUI(){ if(!HelperUtils.isWifiConnected(getActivity())){ mHomeSwitchConnection.setOnCheckedChangeListener(null); setStateNotConnected(); setStateNotActive(); mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener); } else { setStateConnected(); mHomeTextName.setText(HelperUtils.getSSID(getActivity())); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); rootView = inflater.inflate(R.layout.fragment_home, container, false); assignViews(); defaultTextColor = mHomeTextName.getCurrentTextColor(); setStateNotActive(); setStateNotConnected(); registerBroadcastReceiver(); updateUI(); mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection); if(switchChangeListener == null){ switchChangeListener = new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(!HelperUtils.isWifiConnected(getActivity())){ new AlertDialog.Builder(getActivity()) .setTitle("Information") .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.") .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setIcon(android.R.drawable.ic_dialog_info) .show(); setStateNotActive(); setStateNotConnected(); } else { if(isChecked){ setStateActive(); } else { setStateNotActive(); } } } }; } mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener); return rootView; } @Override public void onDestroy(){ super.onDestroy(); getActivity().unregisterReceiver(wifiReceiver); } }