123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- 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.content.SharedPreferences;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.support.v4.content.LocalBroadcastManager;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CompoundButton;
- import android.widget.ImageView;
- import android.widget.Switch;
- import android.widget.TextView;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
- import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
- /**
- * @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 ImageView mHomeProfileImage;
- private View rootView;
- private BroadcastReceiver mReceiver;
- private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
- private int defaultTextColor;
- private ProfileManager mProfileManager;
- private SharedPreferences mConnectionInfo;
- private UglyDbHelper dbh;
- private static int DANGER_LEVEL= 0;
- 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);
- mHomeProfileImage = (ImageView) rootView.findViewById(R.id.home_image_profile);
- }
- private void registerBroadcastReceiver(){
- mReceiver = 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
- }*/
- //}
- }
- };
- LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
- }
- public HomeFragment(){}
- public void setStateNotActive(boolean initial){
- 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));
- if(!initial){
- ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNotMonitoring);
- }
- mHomeSwitchConnection.setChecked(false);
- }
- public void setStateNotActive(){
- setStateNotActive(false);
- }
- public void setStateActive(){
- setStateActive(false);
- }
- public void setStateActive(boolean initial){
- mHomeTextAttacks.setVisibility(View.VISIBLE);
- mHomeTextSecurity.setVisibility(View.VISIBLE);
- mHomeTextName.setTextColor(defaultTextColor);
- mHomeTextProfile.setTextColor(defaultTextColor);
- mHomeTextProfileHeader.setTextColor(defaultTextColor);
- if(!initial){
- ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNoThreat);
- }
- 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(){
- Profile profile = mProfileManager.getCurrentActivatedProfile();
- if(profile != null){
- mHomeTextProfile.setText(profile.mLabel);
- mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
- }
- if(HelperUtils.isWifiConnected(getActivity())){
- setStateConnected();
- mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
- }
- int dangerLevel = 0;
- boolean hasActiveListeners = false;
- int totalAttacks = 0;
- int totalLogged = 0;
- if(MainActivity.getInstance().isServiceBound()){
- for(String protocol: getResources().getStringArray(R.array.protocols)){
- if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
- hasActiveListeners = true;
- int attacks = MainActivity.getInstance().getHoneyService().getNumberOfActiveConnections(protocol);
- if(attacks == 0){
- int log_attacks = dbh.numBssidSeen(protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
- totalLogged += log_attacks;
- if(log_attacks > 0){
- DANGER_LEVEL = Math.max(1, DANGER_LEVEL);
- } else {
- DANGER_LEVEL = Math.max(0, DANGER_LEVEL);
- }
- } else {
- totalAttacks += attacks;
- DANGER_LEVEL = Math.max(2, DANGER_LEVEL);
- }
- }
- }
- }
- if(hasActiveListeners){
- setStateActive(true);
- if(DANGER_LEVEL == 0){
- 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));
- } else if(DANGER_LEVEL == 1){
- mHomeTextAttacks.setText(totalLogged + (totalLogged == 1 ? " attack" : " attacks") + " logged");
- mHomeTextSecurity.setText("Insecure");
- mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
- mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
- } else if(DANGER_LEVEL == 2){
- mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? " attack" : " attacks") + " recorded");
- mHomeTextSecurity.setText("Insecure");
- mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
- mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
- }
- ThreatIndicatorGLRenderer.setThreatLevel(DANGER_LEVEL + 1);
- } else {
- mHomeSwitchConnection.setOnCheckedChangeListener(null);
- setStateNotActive();
- DANGER_LEVEL = 0;
- if(!HelperUtils.isWifiConnected(getActivity())){
- setStateNotConnected();
- }
- mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- dbh = new UglyDbHelper(getActivity());
- mProfileManager = ProfileManager.getInstance();
- mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
- rootView = inflater.inflate(R.layout.fragment_home, container, false);
- assignViews();
- defaultTextColor = mHomeTextName.getCurrentTextColor();
- setStateNotActive(true);
- setStateNotConnected();
- registerBroadcastReceiver();
- updateUI();
- final String[] protocols = getResources().getStringArray(R.array.protocols);
- 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){
- for(String protocol: protocols){
- MainActivity.getInstance().getHoneyService().startListener(protocol);
- }
- setStateActive();
- } else {
- MainActivity.getInstance().getHoneyService().stopListeners();
- MainActivity.getInstance().stopAndUnbind();
- setStateNotActive();
- }
- }
- }
- };
- }
- mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
- return rootView;
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
- }
- }
|