// Path: app/src/main/java/com/google/audio/asr/RequestForRecognitionThread.java
// public enum Action {
//   NO_ACTION,
//   HANDLE_NETWORK_CONNECTION_FATAL_ERROR,
//   HANDLE_NON_NETWORK_CONNECTION_FATAL_ERROR,
//   OK_TO_TERMINATE_SESSION,
//   POST_RESULTS,
//   REQUEST_TO_END_SESSION,
//   RESET_SESSION,
//   RESET_SESSION_AND_CLEAR_TRANSCRIPT,
// }


import com.google.audio.asr.RequestForRecognitionThread.Action;
import com.google.common.base.Objects;
import com.google.common.flogger.FluentLogger;
import java.lang.ref.WeakReference;
/*
 * Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.audio.asr;


/**
 * Handles results as they come in from the recognition module and posts them back to the
 * RepeatingRecognitionSession.
 */
class TranscriptionResultReceiver implements SpeechSessionListener {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  private final WeakReference<RepeatingRecognitionSession.PostHandler> postHandlerRef;

  public TranscriptionResultReceiver(RepeatingRecognitionSession.PostHandler postHandler) {
    this.postHandlerRef = new WeakReference<>(postHandler);
  }

  @Override
  public void onSessionFatalError(int sessionID, Throwable error) {
    logger.atSevere().withCause(error).log("Session #%d ended fatally.", sessionID);
    post(
        RequestForRecognitionThread.newBuilder()
            .setAction(
                errorIndicatesLossOfConnection(error)
// Path: app/src/main/java/com/google/audio/asr/RequestForRecognitionThread.java
// public enum Action {
//   NO_ACTION,
//   HANDLE_NETWORK_CONNECTION_FATAL_ERROR,
//   HANDLE_NON_NETWORK_CONNECTION_FATAL_ERROR,
//   OK_TO_TERMINATE_SESSION,
//   POST_RESULTS,
//   REQUEST_TO_END_SESSION,
//   RESET_SESSION,
//   RESET_SESSION_AND_CLEAR_TRANSCRIPT,
// }


// Path: app/src/main/java/com/google/audio/asr/TranscriptionResultReceiver.java
import com.google.audio.asr.RequestForRecognitionThread.Action;
import com.google.common.base.Objects;
import com.google.common.flogger.FluentLogger;
import java.lang.ref.WeakReference;

/*
 * Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.audio.asr;


/**
 * Handles results as they come in from the recognition module and posts them back to the
 * RepeatingRecognitionSession.
 */
class TranscriptionResultReceiver implements SpeechSessionListener {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  private final WeakReference<RepeatingRecognitionSession.PostHandler> postHandlerRef;

  public TranscriptionResultReceiver(RepeatingRecognitionSession.PostHandler postHandler) {
    this.postHandlerRef = new WeakReference<>(postHandler);
  }

  @Override
  public void onSessionFatalError(int sessionID, Throwable error) {
    logger.atSevere().withCause(error).log("Session #%d ended fatally.", sessionID);
    post(
        RequestForRecognitionThread.newBuilder()
            .setAction(
                errorIndicatesLossOfConnection(error)
                    ? Action.HANDLE_NETWORK_CONNECTION_FATAL_ERROR