public static void buildComplexMissionItem(MavLinkDrone drone, Bundle itemBundle) {
    MissionItem missionItem = MissionItemType.restoreMissionItemFromBundle(itemBundle);
    if (missionItem == null || !(missionItem instanceof MissionItem.ComplexItem)) return;

    final MissionItemType itemType = missionItem.getType();
    switch (itemType) {
      case SURVEY:
        Survey updatedSurvey = buildSurvey(drone, (Survey) missionItem);
        if (updatedSurvey != null) itemType.storeMissionItem(updatedSurvey, itemBundle);
        break;

      case SPLINE_SURVEY:
        Survey updatedSplineSurvey = buildSplineSurvey(drone, (Survey) missionItem);
        if (updatedSplineSurvey != null) itemType.storeMissionItem(updatedSplineSurvey, itemBundle);
        break;

      case STRUCTURE_SCANNER:
        StructureScanner updatedScanner =
            buildStructureScanner(drone, (StructureScanner) missionItem);
        if (updatedScanner != null) itemType.storeMissionItem(updatedScanner, itemBundle);
        break;

      default:
        Timber.w("Unrecognized complex mission item.");
        break;
    }
  }
  @Override
  public void onSpinnerItemSelected(Spinner spinner, int position) {
    final MissionItemType selectedType = commandAdapter.getItem(position);

    try {
      if (mSelectedProxies.isEmpty()) return;

      final List<Pair<MissionItemProxy, MissionItemProxy>> updatesList =
          new ArrayList<Pair<MissionItemProxy, MissionItemProxy>>(mSelectedProxies.size());

      for (MissionItemProxy missionItemProxy : mSelectedProxies) {
        final MissionItem oldItem = missionItemProxy.getMissionItem();
        if (oldItem.getType() != selectedType) {
          final MissionItem newItem = selectedType.getNewItem();

          if (oldItem instanceof MissionItem.SpatialItem
              && newItem instanceof MissionItem.SpatialItem) {
            ((MissionItem.SpatialItem) newItem)
                .setCoordinate(((MissionItem.SpatialItem) oldItem).getCoordinate());
          }

          updatesList.add(
              Pair.create(missionItemProxy, new MissionItemProxy(mMissionProxy, newItem)));
        }
      }

      if (!updatesList.isEmpty()) {
        mListener.onWaypointTypeChanged(selectedType, updatesList);
        dismiss();
      }
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
  }
 void onRestoreInstanceState(Bundle savedState) {
   super.onRestoreInstanceState(savedState);
   final String selectedTypeName =
       savedState.getString(
           EXTRA_SELECTED_MARKER_MISSION_ITEM_TYPE, MARKER_ITEMS_TYPE[0].name());
   selectedType = MissionItemType.valueOf(selectedTypeName);
 }
    @Override
    public void onMapClick(LatLong point) {
      if (missionProxy == null) return;

      // If an mission item is selected, unselect it.
      missionProxy.selection.clearSelection();

      if (selectedType == null) return;

      BaseSpatialItem spatialItem = (BaseSpatialItem) selectedType.getNewItem();
      missionProxy.addSpatialWaypoint(spatialItem, point);
    }
  @Override
  public void onApiConnected() {
    mMissionProxy = getMissionProxy();

    mSelectedProxies.clear();
    mSelectedProxies.addAll(mMissionProxy.selection.getSelected());

    mSelectedItems.clear();
    for (MissionItemProxy mip : mSelectedProxies) {
      mSelectedItems.add(mip.getMissionItem());
    }

    final View view = getView();
    if (view == null) return;

    List<MissionItemType> list =
        new LinkedList<MissionItemType>(Arrays.asList(MissionItemType.values()));

    if (mSelectedProxies.size() == 1) {
      final MissionItemProxy itemProxy = mSelectedProxies.get(0);
      final MissionItem currentItem = itemProxy.getMissionItem();

      if (currentItem instanceof Survey) {
        list.clear();
        list.add(MissionItemType.SURVEY);
      } else {
        list.remove(MissionItemType.SURVEY);
      }

      if ((currentItem instanceof StructureScanner)) {
        list.clear();
        list.add(MissionItemType.STRUCTURE_SCANNER);
      }

      if (mMissionProxy.getItems().indexOf(itemProxy) != 0) {
        list.remove(MissionItemType.TAKEOFF);
      }

      if (mMissionProxy.getItems().indexOf(itemProxy) != (mMissionProxy.getItems().size() - 1)) {
        list.remove(MissionItemType.LAND);
        list.remove(MissionItemType.RETURN_TO_LAUNCH);
      }

      if (currentItem instanceof MissionItem.Command) {
        list.remove(MissionItemType.LAND);
        list.remove(MissionItemType.SPLINE_WAYPOINT);
        list.remove(MissionItemType.CIRCLE);
        list.remove(MissionItemType.REGION_OF_INTEREST);
        list.remove(MissionItemType.WAYPOINT);
        list.remove(MissionItemType.STRUCTURE_SCANNER);
      }

      final TextView waypointIndex = (TextView) view.findViewById(R.id.WaypointIndex);
      if (waypointIndex != null) {
        final int itemOrder = mMissionProxy.getOrder(itemProxy);
        waypointIndex.setText(String.valueOf(itemOrder));
      }

      final TextView distanceView = (TextView) view.findViewById(R.id.DistanceValue);
      if (distanceView != null) {
        distanceView.setText(
            getLengthUnitProvider()
                .boxBaseValueToTarget(mMissionProxy.getDistanceFromLastWaypoint(itemProxy))
                .toString());
      }

      final TextView distanceLabelView = (TextView) view.findViewById(R.id.DistanceLabel);
      if (distanceLabelView != null) {
        distanceLabelView.setVisibility(View.VISIBLE);
      }
    } else if (mSelectedProxies.size() > 1) {
      // Remove the mission item types that don't apply to multiple items.
      list.removeAll(typeWithNoMultiEditSupport);
    } else {
      // Invalid state. We should not have been able to get here.
      throw new IllegalStateException(
          "Mission Detail Fragment cannot be shown when no " + "mission items is selected.");
    }

    commandAdapter =
        new AdapterMissionItems(
            getActivity(),
            android.R.layout.simple_list_item_1,
            list.toArray(new MissionItemType[list.size()]));

    typeSpinner = (SpinnerSelfSelect) view.findViewById(R.id.spinnerWaypointType);
    typeSpinner.setAdapter(commandAdapter);
    typeSpinner.setOnSpinnerItemSelectedListener(this);
  }
 void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   if (selectedType != null)
     outState.putString(EXTRA_SELECTED_DRAW_MISSION_ITEM_TYPE, selectedType.name());
 }
 @Override
 public int hashCode() {
   return type != null ? type.hashCode() : 0;
 }
 protected MissionItem(Parcel in) {
   this.type = MissionItemType.values()[in.readInt()];
 }