/* Handles intent to show player from notification or from shortcut */
  private void handleShowPlayer(Intent intent) {
    // get station from intent
    Station station = null;
    if (intent.hasExtra(TransistorKeys.EXTRA_STATION)) {
      // get station from notification
      station = intent.getParcelableExtra(TransistorKeys.EXTRA_STATION);
    } else if (intent.hasExtra(TransistorKeys.EXTRA_STREAM_URI)) {
      // get Uri of station from home screen shortcut
      station =
          mCollectionAdapter.findStation(
              Uri.parse(intent.getStringExtra(TransistorKeys.EXTRA_STREAM_URI)));
    } else if (intent.hasExtra(TransistorKeys.EXTRA_LAST_STATION)
        && intent.getBooleanExtra(TransistorKeys.EXTRA_LAST_STATION, false)) {
      // try to get last station
      loadAppState(mActivity);
      if (mStationIDLast > -1 && mStationIDLast < mCollectionAdapter.getItemCount()) {
        station = mCollectionAdapter.getStation(mStationIDLast);
      }
    }

    if (station == null) {
      Toast.makeText(mActivity, getString(R.string.toastalert_station_not_found), Toast.LENGTH_LONG)
          .show();
    }

    // get playback action from intent
    boolean startPlayback;
    if (intent.hasExtra(TransistorKeys.EXTRA_PLAYBACK_STATE)) {
      startPlayback = intent.getBooleanExtra(TransistorKeys.EXTRA_PLAYBACK_STATE, false);
    } else {
      startPlayback = false;
    }

    // prepare arguments or intent
    if (mTwoPane && station != null) {
      mStationIDSelected = mCollectionAdapter.getStationID(station);
      mCollectionAdapter.setStationIDSelected(
          mStationIDSelected, station.getPlaybackState(), startPlayback);
    } else if (station != null) {
      // start player activity - on phone
      Intent playerIntent = new Intent(mActivity, PlayerActivity.class);
      playerIntent.setAction(TransistorKeys.ACTION_SHOW_PLAYER);
      playerIntent.putExtra(TransistorKeys.EXTRA_STATION, station);
      playerIntent.putExtra(TransistorKeys.EXTRA_PLAYBACK_STATE, startPlayback);
      startActivity(playerIntent);
    }
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // retrieve selected image Uri from image picker
    Uri newImageUri = null;
    if (null != data) {
      newImageUri = data.getData();
    }

    if (requestCode == TransistorKeys.REQUEST_LOAD_IMAGE
        && resultCode == Activity.RESULT_OK
        && newImageUri != null) {

      ImageHelper imageHelper = new ImageHelper(newImageUri, mActivity);
      Bitmap newImage = imageHelper.getInputImage();

      if (newImage != null && mTempStationID != -1) {
        // write image to storage
        File stationImageFile = mTempStation.getStationImageFile();
        try (FileOutputStream out = new FileOutputStream(stationImageFile)) {
          newImage.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (IOException e) {
          LogHelper.e(LOG_TAG, "Unable to save: " + newImage.toString());
        }
        // update adapter
        mCollectionAdapter.notifyItemChanged(mTempStationID);

      } else {
        LogHelper.e(
            LOG_TAG, "Unable to get image from media picker. Uri was:  " + newImageUri.toString());
      }

    } else {
      LogHelper.e(LOG_TAG, "Unable to get image from media picker. Did not receive an Uri");
    }
  }
  /* Handles adding, deleting and renaming of station */
  private void handleCollectionChanges(Intent intent) {

    // load app state
    loadAppState(mActivity);

    int newStationPosition;

    switch (intent.getIntExtra(TransistorKeys.EXTRA_COLLECTION_CHANGE, 1)) {

        // CASE: station was added
      case TransistorKeys.STATION_ADDED:
        if (intent.hasExtra(TransistorKeys.EXTRA_STATION)) {

          // get station from intent
          Station station = intent.getParcelableExtra(TransistorKeys.EXTRA_STATION);

          // add station to adapter, scroll to new position and update adapter
          newStationPosition = mCollectionAdapter.add(station);

          if (mCollectionAdapter.getItemCount() > 0) {
            toggleActionCall();
          }

          mLayoutManager.scrollToPosition(newStationPosition);
          mCollectionAdapter.setStationIDSelected(newStationPosition, mPlayback, false);
          mCollectionAdapter.notifyDataSetChanged(); // TODO Remove?
        }
        break;

        // CASE: station was renamed
      case TransistorKeys.STATION_RENAMED:
        if (intent.hasExtra(TransistorKeys.EXTRA_STATION_NEW_NAME)
            && intent.hasExtra(TransistorKeys.EXTRA_STATION)
            && intent.hasExtra(TransistorKeys.EXTRA_STATION_ID)) {

          // get new name, station and station ID from intent
          String newStationName = intent.getStringExtra(TransistorKeys.EXTRA_STATION_NEW_NAME);
          Station station = intent.getParcelableExtra(TransistorKeys.EXTRA_STATION);
          int stationID = intent.getIntExtra(TransistorKeys.EXTRA_STATION_ID, 0);

          // update notification
          if (station.getPlaybackState()) {
            NotificationHelper.update(station, stationID, null, null);
          }

          // change station within in adapter, scroll to new position and update adapter
          newStationPosition = mCollectionAdapter.rename(newStationName, station, stationID);
          mLayoutManager.scrollToPosition(newStationPosition);
          mCollectionAdapter.setStationIDSelected(newStationPosition, mPlayback, false);
          mCollectionAdapter.notifyDataSetChanged(); // TODO Remove?
        }
        break;

        // CASE: station was deleted
      case TransistorKeys.STATION_DELETED:
        if (intent.hasExtra(TransistorKeys.EXTRA_STATION)
            && intent.hasExtra(TransistorKeys.EXTRA_STATION_ID)) {

          // get station and station ID from intent
          Station station = intent.getParcelableExtra(TransistorKeys.EXTRA_STATION);
          int stationID = intent.getIntExtra(TransistorKeys.EXTRA_STATION_ID, 0);

          // dismiss notification
          NotificationHelper.stop();

          if (station.getPlaybackState()) {
            // stop player service and notification using intent
            Intent i = new Intent(mActivity, PlayerService.class);
            i.setAction(TransistorKeys.ACTION_DISMISS);
            mActivity.startService(i);
            LogHelper.v(LOG_TAG, "Stopping player service.");
          }

          // remove station from adapter and update
          newStationPosition = mCollectionAdapter.delete(station, stationID);
          if (newStationPosition == -1 || mCollectionAdapter.getItemCount() == 0) {
            // show call to action
            toggleActionCall();
          } else {
            // scroll to new position
            mCollectionAdapter.setStationIDSelected(newStationPosition, mPlayback, false);
            mLayoutManager.scrollToPosition(newStationPosition);
          }
          mCollectionAdapter.notifyDataSetChanged(); // TODO Remove?
        }
        break;
    }
  }