@Override
    public void onWifiSelected(int selectedIndex, List<ScanResult> wifiList) {
      if (wifiList == null) {
        return;
      }

      String bssidToAdd = wifiList.get(selectedIndex).BSSID;
      String nameToAdd = wifiList.get(selectedIndex).SSID;

      // Add bssid to shown list of trusted networks
      mArrayAdapter.add(new String[] {nameToAdd, bssidToAdd});
      FilesystemIterface.saveTrustedNetworks(getActivity(), mArrayList);
    }
    @Override
    public View onCreateView(
        LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      mWifiWatcherServiceIntent = new Intent(getActivity(), WifiWatcherService.class);
      View rootView = inflater.inflate(R.layout.fragment_lockscreen, container, false);

      Button btn = (Button) rootView.findViewById(R.id.button_add_wifi);
      btn.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              new SelectNetworkDialog(getActivity(), mSelf).show();
            }
          });

      // Depending on the user's last choice, the switch needs to show started or not
      // And the UI needs to toggle the switch accordingly
      SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE);
      boolean serviceState = sp.getBoolean("SERVICE_ACTIVATED", false);
      Switch toggleSwitch = (Switch) rootView.findViewById(R.id.switch_toggle_service);
      toggleSwitch.setChecked(serviceState);

      toggleSwitch.setOnCheckedChangeListener(
          new Switch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
              if (isChecked) {
                // Start service
                getActivity().startService(mWifiWatcherServiceIntent);
                getActivity()
                    .registerReceiver(
                        new OnWifiConnectionChangedReceiver(),
                        new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

              } else {
                // Stop service
                getActivity().stopService(mWifiWatcherServiceIntent);
              }
            }
          });

      ListView listView = (ListView) rootView.findViewById(R.id.listview_networks);
      mArrayList = FilesystemIterface.loadTrustedNetworks(getActivity());
      mArrayAdapter =
          new WifiAdapter(getActivity(), android.R.layout.simple_list_item_1, mArrayList);
      listView.setAdapter(mArrayAdapter);
      registerForContextMenu(listView);

      return rootView;
    }
    private void removeItem(int index) {
      // Remove from list
      mArrayList.remove(index);

      // Remove from UI list
      getActivity()
          .runOnUiThread(
              new Runnable() {
                public void run() {
                  mArrayAdapter.notifyDataSetChanged();
                }
              });

      // TODO better do in onStop only
      FilesystemIterface.saveTrustedNetworks(getActivity(), mArrayList);
    }