@Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof DrawerNavigationUI) navActivity = (DrawerNavigationUI) activity;

    if (activity instanceof SlidingUpPanelLayout.PanelSlideListener)
      slidingPanelListenerMgr.addPanelSlideListener(
          parentActivityPanelListenerLabel, (SlidingUpPanelLayout.PanelSlideListener) activity);
  }
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (!(activity instanceof EditorToolListener)) {
      throw new IllegalStateException(
          "Parent activity must be an instance of " + EditorToolListener.class.getName());
    }

    listener = (EditorToolListener) activity;
  }
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    // Store the currently selected tool
    savedInstanceState.putString(STATE_SELECTED_TOOL, tool.name());

    for (EditorToolsImpl toolImpl : editorToolsImpls) {
      toolImpl.onSaveInstanceState(savedInstanceState);
    }
  }
 @Override
 public void onStart() {
   super.onStart();
   setupMapFragment();
   updateMapLocationButtons(getAppPrefs().getAutoPanMode());
 }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final Bundle arguments = getArguments();
    final boolean showActionDrawerToggle =
        arguments == null
            ? DEFAULT_SHOW_ACTION_DRAWER_TOGGLE
            : arguments.getBoolean(
                EXTRA_SHOW_ACTION_DRAWER_TOGGLE, DEFAULT_SHOW_ACTION_DRAWER_TOGGLE);

    actionbarShadow = view.findViewById(R.id.actionbar_shadow);

    final FragmentManager fm = getChildFragmentManager();

    mSlidingPanel = (SlidingUpPanelLayout) view.findViewById(R.id.slidingPanelContainer);
    mSlidingPanel.addPanelSlideListener(slidingPanelListenerMgr);

    warningText = (TextView) view.findViewById(R.id.failsafeTextView);
    warningContainer = view.findViewById(R.id.warningContainer);
    ImageView closeWarningView = (ImageView) view.findViewById(R.id.close_warning_view);
    closeWarningView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            hideWarningView();
          }
        });

    setupMapFragment();

    mGoToMyLocation = (FloatingActionButton) view.findViewById(R.id.my_location_button);
    mGoToDroneLocation = (FloatingActionButton) view.findViewById(R.id.drone_location_button);
    actionDrawerToggle = (FloatingActionButton) view.findViewById(R.id.toggle_action_drawer);

    if (showActionDrawerToggle) {
      actionDrawerToggle.setVisibility(View.VISIBLE);

      actionDrawerToggle.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (navActivity == null) return;

              if (navActivity.isActionDrawerOpened()) navActivity.closeActionDrawer();
              else navActivity.openActionDrawer();
            }
          });
    }

    mGoToMyLocation.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mapFragment != null) {
              mapFragment.goToMyLocation();
              updateMapLocationButtons(AutoPanMode.DISABLED);
            }
          }
        });
    mGoToMyLocation.setOnLongClickListener(
        new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
            if (mapFragment != null) {
              mapFragment.goToMyLocation();
              updateMapLocationButtons(AutoPanMode.USER);
              return true;
            }
            return false;
          }
        });

    mGoToDroneLocation.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mapFragment != null) {
              mapFragment.goToDroneLocation();
              updateMapLocationButtons(AutoPanMode.DISABLED);
            }
          }
        });
    mGoToDroneLocation.setOnLongClickListener(
        new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
            if (mapFragment != null) {
              mapFragment.goToDroneLocation();
              updateMapLocationButtons(AutoPanMode.DRONE);
              return true;
            }
            return false;
          }
        });

    flightActions = (FlightControlManagerFragment) fm.findFragmentById(R.id.flightActionsFragment);
    if (flightActions == null) {
      flightActions = new FlightControlManagerFragment();
      fm.beginTransaction().add(R.id.flightActionsFragment, flightActions).commit();
    }

    // Add the mode info panel fragment
    FlightModePanel flightModePanel =
        (FlightModePanel) fm.findFragmentById(R.id.sliding_drawer_content);
    if (flightModePanel == null) {
      flightModePanel = new FlightModePanel();
      fm.beginTransaction().add(R.id.sliding_drawer_content, flightModePanel).commit();
    }
  }
 @Override
 public void onDetach() {
   super.onDetach();
   navActivity = null;
   slidingPanelListenerMgr.removePanelSlideListener(parentActivityPanelListenerLabel);
 }
 @Override
 public void onDetach() {
   super.onDetach();
   listener = null;
 }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (savedInstanceState != null) {
      // Retrieve the tool that was last selected before the fragment was destroyed.
      final String toolName = savedInstanceState.getString(STATE_SELECTED_TOOL, tool.name());
      tool = EditorTools.valueOf(toolName);

      for (EditorToolsImpl toolImpl : editorToolsImpls)
        toolImpl.onRestoreInstanceState(savedInstanceState);
    }

    final Resources res = getResources();
    popupLeftMargin =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics());

    final Context context = getContext();
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    final int popupWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
    final int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    final Drawable popupBg = res.getDrawable(android.R.color.transparent);

    mEditorRadioGroup = (RadioGroup) view.findViewById(R.id.editor_tools_layout);

    final DrawToolsImpl drawToolImpl = (DrawToolsImpl) editorToolsImpls[EditorTools.DRAW.ordinal()];
    final RadioButtonCenter buttonDraw =
        (RadioButtonCenter) view.findViewById(R.id.editor_tools_draw);
    final View drawPopupView =
        inflater.inflate(R.layout.popup_editor_tool_draw, (ViewGroup) view, false);
    final AdapterMissionItems drawItemsAdapter =
        new AdapterMissionItems(
            context, R.layout.spinner_drop_down_flight_mode, DrawToolsImpl.DRAW_ITEMS_TYPE);
    final Spinner drawItemsSpinner = (Spinner) drawPopupView.findViewById(R.id.draw_items_spinner);
    drawItemsSpinner.setAdapter(drawItemsAdapter);
    drawItemsSpinner.setSelection(drawItemsAdapter.getPosition(drawToolImpl.getSelected()));
    drawItemsSpinner.setOnItemSelectedListener(drawToolImpl);
    drawPopup = new PopupWindow(drawPopupView, popupWidth, popupHeight, true);
    drawPopup.setBackgroundDrawable(popupBg);

    final MarkerToolsImpl markerToolImpl =
        (MarkerToolsImpl) editorToolsImpls[EditorTools.MARKER.ordinal()];
    final RadioButtonCenter buttonMarker =
        (RadioButtonCenter) view.findViewById(R.id.editor_tools_marker);
    final View markerPopupView =
        inflater.inflate(R.layout.popup_editor_tool_marker, (ViewGroup) view, false);
    final AdapterMissionItems markerItemsAdapter =
        new AdapterMissionItems(
            context, R.layout.spinner_drop_down_flight_mode, MarkerToolsImpl.MARKER_ITEMS_TYPE);
    final Spinner markerItemsSpinner =
        (Spinner) markerPopupView.findViewById(R.id.marker_items_spinner);
    markerItemsSpinner.setAdapter(markerItemsAdapter);
    markerItemsSpinner.setSelection(markerItemsAdapter.getPosition(markerToolImpl.getSelected()));
    markerItemsSpinner.setOnItemSelectedListener(markerToolImpl);
    markerPopup = new PopupWindow(markerPopupView, popupWidth, popupHeight, true);
    markerPopup.setBackgroundDrawable(popupBg);

    final RadioButtonCenter buttonTrash =
        (RadioButtonCenter) view.findViewById(R.id.editor_tools_trash);
    final View trashPopupView =
        inflater.inflate(R.layout.popup_editor_tool_trash, (ViewGroup) view, false);
    final TrashToolsImpl trashToolImpl =
        (TrashToolsImpl) editorToolsImpls[EditorTools.TRASH.ordinal()];
    final TextView clearMission = (TextView) trashPopupView.findViewById(R.id.clear_mission_button);
    clearMission.setOnClickListener(trashToolImpl);
    trashPopup = new PopupWindow(trashPopupView, popupWidth, popupHeight, true);
    trashPopup.setBackgroundDrawable(popupBg);

    final RadioButtonCenter buttonSelector =
        (RadioButtonCenter) view.findViewById(R.id.editor_tools_selector);
    final View selectorPopupView =
        inflater.inflate(R.layout.popup_editor_tool_selector, (ViewGroup) view, false);
    final SelectorToolsImpl selectorToolImpl =
        (SelectorToolsImpl) editorToolsImpls[EditorTools.SELECTOR.ordinal()];
    final TextView selectAll = (TextView) selectorPopupView.findViewById(R.id.select_all_button);
    selectAll.setOnClickListener(selectorToolImpl);
    selectorPopup = new PopupWindow(selectorPopupView, popupWidth, popupHeight, true);
    selectorPopup.setBackgroundDrawable(popupBg);

    for (View vv : new View[] {buttonDraw, buttonMarker, buttonTrash, buttonSelector}) {
      vv.setOnClickListener(this);
    }
  }