123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import com.google.android.gms.analytics.HitBuilders;
- import com.google.android.gms.analytics.Tracker;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Fragment;
- import android.app.FragmentManager;
- 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.Uri;
- 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.Hostage;
- import de.tudarmstadt.informatik.hostage.HostageApplication;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
- import de.tudarmstadt.informatik.hostage.ui2.model.LogFilter;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
- import de.tudarmstadt.informatik.hostage.system.Device;
- /**
- * This fragments displays the current hostage state and attacks on the device in form of an animation and simple view components
- *
- * @author Alexander Brakowski
- * @created 13.01.14 19:06
- */
- public class HomeFragment extends TrackerFragment {
- /**
- * View objects from the layout
- */
- private Switch mHomeSwitchConnection;
- private TextView mHomeTextName;
- private TextView mHomeTextSecurity;
- private TextView mHomeTextAttacks;
- private TextView mHomeTextProfile;
- private TextView mHomeTextProfileHeader;
- private ImageView mHomeProfileImage;
- private ImageView mHomeConnectionInfoButton;
- private View mRootView;
- /**
- * This handles all the broadcasts from the Hostage service
- */
- private BroadcastReceiver mReceiver;
- /**
- * A change listener for the monitor switch
- */
- private CompoundButton.OnCheckedChangeListener mSwitchChangeListener = null;
- private int mDefaultTextColor;
- /**
- * A reference to the profile manager
- */
- private ProfileManager mProfileManager;
- /**
- * A shared preference that holds all the connection info of the current network connection
- */
- private SharedPreferences mConnectionInfo;
- /**
- * Hold the shared preferences for the app
- */
- private SharedPreferences mSharedPreferences;
- /**
- * An Helper to access the sqllite database with all the records
- */
- private HostageDBOpenHelper mDbHelper;
- /**
- * Holds a state if the broadcast receiver is registered to the hostage service
- */
- private boolean mReceiverRegistered;
- /**
- * Holds a state if the hostage service is active
- */
- private boolean isActive = false;
- /**
- * Holds a state if the device is currently connected to a network
- */
- private boolean isConnected = false;
- public HomeFragment() {
- }
- /**
- * Looks up all the neccessary views in the layout
- */
- private void assignViews() {
- mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
- mHomeTextName = (TextView) mRootView.findViewById(R.id.home_text_name);
- mHomeTextSecurity = (TextView) mRootView.findViewById(R.id.home_text_security);
- mHomeTextAttacks = (TextView) mRootView.findViewById(R.id.home_text_attacks);
- mHomeTextProfile = (TextView) mRootView.findViewById(R.id.home_text_profile);
- mHomeTextProfileHeader = (TextView) mRootView.findViewById(R.id.home_text_profile_header);
- mHomeProfileImage = (ImageView) mRootView.findViewById(R.id.home_image_profile);
- mHomeConnectionInfoButton = (ImageView) mRootView.findViewById(R.id.home_button_connection_info);
- }
- /**
- * Registers the broadcast receiver with the hostage service
- */
- private void registerBroadcastReceiver() {
- if (!mReceiverRegistered) {
- LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
- this.mReceiverRegistered = true;
- }
- }
- /**
- * Unregisters the broadcast receiver
- */
- private void unregisterBroadcastReceiver() {
- if (mReceiverRegistered) {
- LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
- this.mReceiverRegistered = false;
- }
- }
- /**
- * Sets the view state to not active.
- * This means hiding and graying out all the informations in the view, that are not important, if the service is not active.
- *
- * @param initial indicates that the method was called on creation of view
- */
- 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.ThreatLevel.NOT_MONITORING);
- }
- mHomeSwitchConnection.setChecked(false);
- isActive = false;
- }
- /**
- * Alias for calling setStateNotActive with the initial value being false
- */
- public void setStateNotActive() {
- setStateNotActive(false);
- }
- /**
- * Alias for calling setStateActive with the initial value being false
- */
- public void setStateActive() {
- setStateActive(false);
- }
- /**
- * Sets the state of the view to active.
- * That means that all the information showing the active state of the hostage service is being displayed.
- *
- * @param initial indicates that the method was called on creation of view
- */
- public void setStateActive(boolean initial) {
- mHomeTextAttacks.setVisibility(View.VISIBLE);
- mHomeTextSecurity.setVisibility(View.VISIBLE);
- mHomeTextName.setTextColor(mDefaultTextColor);
- mHomeTextProfile.setTextColor(mDefaultTextColor);
- mHomeTextProfileHeader.setTextColor(mDefaultTextColor);
- if (!initial) {
- ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
- }
- mHomeSwitchConnection.setChecked(true);
- isActive = true;
- }
- /**
- * Sets the state of the view to not connected by hiding information about the attacks.
- */
- public void setStateNotConnected() {
- mHomeTextSecurity.setVisibility(View.INVISIBLE);
- mHomeTextAttacks.setVisibility(View.INVISIBLE);
- mHomeTextProfile.setVisibility(View.INVISIBLE);
- mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
- mHomeProfileImage.setVisibility(View.INVISIBLE);
- mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
- mHomeTextName.setText(R.string.not_connected);
- isConnected = false;
- }
- /**
- * Sets the state of the view to connected by showing informations about attacks
- */
- public void setStateConnected() {
- mHomeTextProfile.setVisibility(View.VISIBLE);
- mHomeTextProfileHeader.setVisibility(View.VISIBLE);
- mHomeProfileImage.setVisibility(View.VISIBLE);
- mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
- isConnected = true;
- }
- /**
- * Updates the view.
- *
- * That means: updating the number of attacks on the view,
- * updating the threat level,
- * updating the connection state,
- * updating the monitoring state
- */
- public void updateUI() {
- Profile profile = mProfileManager.getCurrentActivatedProfile();
- if (profile != null) {
- mHomeTextProfile.setText(profile.mLabel);
- mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
- }
- // if the device is connected to an network display the network name
- //SK: Temporary Bugfix
- //if (HelperUtils.isNetworkAvailable(getActivity())) {
- if (HelperUtils.isWifiConnected(getActivity())) {
- setStateConnected();
- String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "\"\"");
- mHomeTextName.setText(ssid.substring(1,ssid.length() - 1));
- } else {
- setStateNotConnected();
- }
- boolean hasActiveListeners = false;
- int totalAttacks = mDbHelper.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
- ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
- // decides which threat level to display
- if (MainActivity.getInstance().getHostageService() != null) {
- if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
- hasActiveListeners = true;
- if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
- threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
- } else if (totalAttacks > 0) {
- threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
- } else {
- threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
- }
- }
- }
- // if the monitoring is running show the information
- if (hasActiveListeners) {
- setStateActive(true);
- if(!isConnected){
- ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
- mHomeTextAttacks.setText("");
- mHomeTextSecurity.setText("");
- } else {
- switch (threatLevel) {
- case NO_THREAT:
- mHomeTextAttacks.setText(R.string.zero_attacks);
- mHomeTextSecurity.setText(R.string.secure);
- mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
- mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
- break;
- case PAST_THREAT:
- mHomeTextAttacks.setText(totalAttacks
- + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
- + getResources().getString(R.string.recorded));
- mHomeTextSecurity.setText(R.string.insecure);
- mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
- mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
- break;
- case LIVE_THREAT:
- mHomeTextAttacks.setText(totalAttacks
- + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
- + getResources().getString(R.string.recorded));
- mHomeTextSecurity.setText(R.string.insecure);
- mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
- mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
- break;
- }
- ThreatIndicatorGLRenderer.setThreatLevel(threatLevel);
- }
- } else {
- setStateNotActive();
- }
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- final Activity activity = getActivity();
- if (activity != null) {
- activity.setTitle(getResources().getString(R.string.drawer_overview));
- }
- mDbHelper = new HostageDBOpenHelper(getActivity());
- mProfileManager = ProfileManager.getInstance();
- mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
- mRootView = inflater.inflate(R.layout.fragment_home, container, false);
- assignViews();
- // hook up the connection info button
- mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- final FragmentManager fragmentManager = getFragmentManager();
- if (fragmentManager != null) {
- ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
- connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
- }
- }
- });
- mDefaultTextColor = mHomeTextName.getCurrentTextColor();
- // sets state and connection initially to off
- setStateNotActive(true);
- setStateNotConnected();
- // register the broadcast receiver
- mReceiver = new BroadcastReceiver() {
- @SuppressLint("NewApi")
- @Override
- public void onReceive(Context context, Intent intent) {
- if (getUserVisibleHint())
- updateUI();
- }
- };
- registerBroadcastReceiver();
- updateUI();
- // connects the switch listener to the switch button
- mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
- mHomeSwitchConnection.setSaveEnabled(false);
- if (mSwitchChangeListener == null) {
- mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // displays a alert dialog if no network is available
- //SK: Temporary bugfix to limit to wifi
- //if (!HelperUtils.isNetworkAvailable(getActivity())) {
- if (!HelperUtils.isWifiConnected(getActivity())) {
- //new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
- new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.wifi_not_connected_msg)
- .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) {
- boolean protocolActivated = false;
- mSharedPreferences = getActivity().getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE);
- if(mSharedPreferences.getBoolean( "isFirstEmulation", true)) {
- //Notify Porthack is not installed to user
- if(!Device.isPorthackInstalled()) {
- new AlertDialog.Builder(getActivity())
- .setTitle(R.string.information)
- .setMessage(R.string.no_portbinder_msg2)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .setNegativeButton(R.string.help, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- MainActivity.getInstance().displayView(MainActivity.MainMenuItem.SETTINGS.getValue());
- }
- })
- .setIcon(android.R.drawable.ic_dialog_info).show();
- }
- // Disable hint for 'first' time emulation notification
- SharedPreferences.Editor editor = mSharedPreferences.edit();
- editor.putBoolean("isFirstEmulation", false);
- editor.commit();
- }
- if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
- // starts all services
- MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
- } else {
- // starts the services that are actived in the current profile
- ProfileManager profileManager = ProfileManager.getInstance();
- if (profileManager.isRandomActive()) {
- profileManager.randomizeProtocols(profileManager.getRandomProfile());
- }
- Profile currentProfile = profileManager.getCurrentActivatedProfile();
- List<String> protocols = currentProfile.getActiveProtocols();
- if(protocols.size() > 0 || currentProfile.mGhostActive){
- protocols.add("GHOST");
- MainActivity.getInstance().startMonitorServices(protocols);
- protocolActivated = true;
- }
- }
- if (protocolActivated) {
- setStateActive();
- } else {
- // no protocol was started, so show alert dialog
- new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- }).setIcon(android.R.drawable.ic_dialog_info).show();
- setStateNotActive();
- }
- } else {
- // stop hostage service and all listeners
- if (MainActivity.getInstance().getHostageService() != null) {
- MainActivity.getInstance().getHostageService().stopListeners();
- MainActivity.getInstance().stopAndUnbind();
- }
- setStateNotActive();
- }
- }
- }
- };
- }
- mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
- // connects the profile text an click listener
- mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment fragment = new ProfileManagerFragment();
- MainActivity.getInstance().injectFragment(fragment);
- }
- });
- // connect the attacks text to an click listener
- View.OnClickListener attackClickListener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
- if (!ssid.isEmpty()) {
- ArrayList<String> ssids = new ArrayList<String>();
- ssids.add(ssid);
- LogFilter filter = new LogFilter();
- filter.setESSIDs(ssids);
- RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
- recordOverviewFragment.setFilter(filter);
- recordOverviewFragment.setGroupKey("ESSID");
- MainActivity.getInstance().injectFragment(recordOverviewFragment);
- }
- }
- };
- mHomeTextAttacks.setOnClickListener(attackClickListener);
- mHomeTextSecurity.setOnClickListener(attackClickListener);
- return mRootView;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onStop() {
- super.onStop();
- unregisterBroadcastReceiver();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onStart() {
- super.onStart();
- registerBroadcastReceiver();
- updateUI();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onDestroy() {
- super.onDestroy();
- unregisterBroadcastReceiver();
- }
- }
|