<commit_before>/* Copyright 2020 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.
 */
// Copyright 2020 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
//
//      http://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.mapsplatform.transportation.sample;

import static com.google.mapsplatform.transportation.sample.state.AppStates.INITIALIZED;
import static com.google.mapsplatform.transportation.sample.state.AppStates.JOURNEY_SHARING;
import static com.google.mapsplatform.transportation.sample.state.AppStates.SELECTING_PICKUP;
import static com.google.mapsplatform.transportation.sample.state.AppStates.UNINITIALIZED;

import android.app.Application;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.libraries.mapsplatform.transportation.consumer.managers.TripModel;
import com.google.android.libraries.mapsplatform.transportation.consumer.managers.TripModelCallback;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.Trip;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.TripInfo;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.TripName;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.VehicleLocation;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.mapsplatform.transportation.sample.provider.ProviderUtils;
import com.google.mapsplatform.transportation.sample.provider.model.TripData;
import com.google.mapsplatform.transportation.sample.provider.model.TripStatus;
import com.google.mapsplatform.transportation.sample.provider.model.WaypointData;
import com.google.mapsplatform.transportation.sample.provider.response.TripResponse;
import com.google.mapsplatform.transportation.sample.provider.response.WaypointResponse;
import com.google.mapsplatform.transportation.sample.provider.service.LocalProviderService;
import com.google.mapsplatform.transportation.sample.state.AppStates;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * ViewModel for the Consumer Sample application. It provides observables for the current
 * application status such as trip completed and journey sharing which it maintains using
 * StateChangeCallback. When in journey sharing it has observables for the details of the trip
 * including eta, distance remaining and waypoint data.
 */
public class ConsumerViewModel extends AndroidViewModel {

  /** Amount of time until an idle state reset should be delayed before applying changes. */
  private static final int IDLE_STATE_RESET_DELAY_SECONDS = 3;

  interface JourneySharingListener {

    /** Starts journey sharing rendering and return that {@link TripModel}. */
    TripModel startJourneySharing(TripData tripData);

    /** Stops journey sharing rendering. */
    void stopJourneySharing();

    /** Updates current location. */
    void updateCurrentLocation(LatLng latLang);
  }

  private static final String TAG = "ConsumerViewModel";

  // LiveData for current application map state.
  private final MutableLiveData<Integer> appState = new MutableLiveData<>();

  // LiveData for dropoff location.
  private final MutableLiveData<LatLng> pickupLocation = new MutableLiveData<>();

  // LiveData for pickup location.
  private final MutableLiveData<LatLng> dropoffLocation = new MutableLiveData<>();

  // LiveData for cost for a route.
  private final MutableLiveData<Double> costData = new MutableLiveData<>();

  // LiveData of the current trip info from a trip refresh.
  private final MutableLiveData<Integer> tripStatus = new MutableLiveData<>();

  // LiveData of the current trip Id.
  private final MutableLiveData<String> tripId = new MutableLiveData<>();

  // LiveData of the remaining distance to the next waypoint.
  private final MutableLiveData<Integer> remainingDistanceMeters = new MutableLiveData<>();

  // LiveData of the ETA to the next waypoint.
  private final MutableLiveData<Long> nextWaypointEta = new MutableLiveData<>();

  // Latest trip data including: time remaining, distance remaining and etc.
  private final MutableLiveData<TripInfo> tripInfo = new MutableLiveData<>();

  // The current active trip, meant for create journey sharing session and observe session.
  private final MutableLiveData<TripModel> trip = new MutableLiveData<>();

  private final LocalProviderService providerService;
  private final ExecutorService executor = Executors.newCachedThreadPool();
  private final Executor mainExecutor;
  private final ScheduledExecutorService scheduledExecutor =
      Executors.newSingleThreadScheduledExecutor();

  private WeakReference<JourneySharingListener> journeySharingListener = new WeakReference<>(null);

  /**
   * Initializes the ConsumerApi and if successful initiates TripManager configuration.
   *
   * @param application Application context.
   */
  public ConsumerViewModel(Application application) {
    super(application);
    providerService =
        new LocalProviderService(
            LocalProviderService.createRestProvider(ProviderUtils.getProviderBaseUrl(application)),
            executor,
            scheduledExecutor);
    appState.setValue(UNINITIALIZED);
    mainExecutor = ContextCompat.getMainExecutor(application);
  }

  /** Creates a trip in the sample provider. */
  public void startSingleExclusiveTrip() {
    ListenableFuture<TripResponse> singleExclusiveTrip =
        providerService.createSingleExclusiveTrip(
            createWaypointData(pickupLocation.getValue(), dropoffLocation.getValue()));
    handleCreateSingleExclusiveTripResponse(singleExclusiveTrip);
    ListenableFuture<TripData> tripDataFuture =
        Futures.transformAsync(
            singleExclusiveTrip,
            tripResponse -> providerService.fetchMatchedTrip(tripResponse.getTripName()),
            executor);
    handleFetchMatchedTripResponse(tripDataFuture);
  }

  private void handleCreateSingleExclusiveTripResponse(
      ListenableFuture<TripResponse> singleExclusiveTrip) {
    Futures.addCallback(
        singleExclusiveTrip,
        new FutureCallback<TripResponse>() {
          @Override
          public void onSuccess(TripResponse result) {
            Log.i(TAG, String.format("Successfully created trip %s.", result.getTripName()));
            tripStatus.postValue(TripStatus.parse(result.getTripStatus()));
          }

          @Override
          public void onFailure(Throwable e) {
            Log.e(TAG, "Failed to create trip.", e);
          }
        },
        executor);
  }

  private void handleFetchMatchedTripResponse(ListenableFuture<TripData> tripDataFuture) {
    Futures.addCallback(
        tripDataFuture,
        new FutureCallback<TripData>() {
          @Override
          public void onSuccess(TripData tripData) {
            mainExecutor.execute(() -> startJourneySharing(tripData));
          }

          @Override
          public void onFailure(Throwable e) {
            Log.e(TAG, "Failed to match trip with a driver.", e);
          }
        },<commit_msg>Fix irrelevant error being thrown in consumer app<commit_after>/* Copyright 2020 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.
 */
// Copyright 2020 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
//
//      http://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.mapsplatform.transportation.sample;

import static com.google.mapsplatform.transportation.sample.state.AppStates.INITIALIZED;
import static com.google.mapsplatform.transportation.sample.state.AppStates.JOURNEY_SHARING;
import static com.google.mapsplatform.transportation.sample.state.AppStates.SELECTING_PICKUP;
import static com.google.mapsplatform.transportation.sample.state.AppStates.UNINITIALIZED;

import android.app.Application;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.libraries.mapsplatform.transportation.consumer.managers.TripModel;
import com.google.android.libraries.mapsplatform.transportation.consumer.managers.TripModelCallback;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.Trip;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.TripInfo;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.TripName;
import com.google.android.libraries.mapsplatform.transportation.consumer.model.VehicleLocation;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.mapsplatform.transportation.sample.provider.ProviderUtils;
import com.google.mapsplatform.transportation.sample.provider.model.TripData;
import com.google.mapsplatform.transportation.sample.provider.model.TripStatus;
import com.google.mapsplatform.transportation.sample.provider.model.WaypointData;
import com.google.mapsplatform.transportation.sample.provider.response.TripResponse;
import com.google.mapsplatform.transportation.sample.provider.response.WaypointResponse;
import com.google.mapsplatform.transportation.sample.provider.service.LocalProviderService;
import com.google.mapsplatform.transportation.sample.state.AppStates;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * ViewModel for the Consumer Sample application. It provides observables for the current
 * application status such as trip completed and journey sharing which it maintains using
 * StateChangeCallback. When in journey sharing it has observables for the details of the trip
 * including eta, distance remaining and waypoint data.
 */
public class ConsumerViewModel extends AndroidViewModel {

  /** Amount of time until an idle state reset should be delayed before applying changes. */
  private static final int IDLE_STATE_RESET_DELAY_SECONDS = 3;

  interface JourneySharingListener {

    /** Starts journey sharing rendering and return that {@link TripModel}. */
    TripModel startJourneySharing(TripData tripData);

    /** Stops journey sharing rendering. */
    void stopJourneySharing();

    /** Updates current location. */
    void updateCurrentLocation(LatLng latLang);
  }

  private static final String TAG = "ConsumerViewModel";

  // LiveData for current application map state.
  private final MutableLiveData<Integer> appState = new MutableLiveData<>();

  // LiveData for dropoff location.
  private final MutableLiveData<LatLng> pickupLocation = new MutableLiveData<>();

  // LiveData for pickup location.
  private final MutableLiveData<LatLng> dropoffLocation = new MutableLiveData<>();

  // LiveData for cost for a route.
  private final MutableLiveData<Double> costData = new MutableLiveData<>();

  // LiveData of the current trip info from a trip refresh.
  private final MutableLiveData<Integer> tripStatus = new MutableLiveData<>();

  // LiveData of the current trip Id.
  private final MutableLiveData<String> tripId = new MutableLiveData<>();

  // LiveData of the remaining distance to the next waypoint.
  private final MutableLiveData<Integer> remainingDistanceMeters = new MutableLiveData<>();

  // LiveData of the ETA to the next waypoint.
  private final MutableLiveData<Long> nextWaypointEta = new MutableLiveData<>();

  // Latest trip data including: time remaining, distance remaining and etc.
  private final MutableLiveData<TripInfo> tripInfo = new MutableLiveData<>();

  // The current active trip, meant for create journey sharing session and observe session.
  private final MutableLiveData<TripModel> trip = new MutableLiveData<>();

  private final LocalProviderService providerService;
  private final ExecutorService executor = Executors.newCachedThreadPool();
  private final Executor mainExecutor;
  private final ScheduledExecutorService scheduledExecutor =
      Executors.newSingleThreadScheduledExecutor();

  private WeakReference<JourneySharingListener> journeySharingListener = new WeakReference<>(null);

  /**
   * Initializes the ConsumerApi and if successful initiates TripManager configuration.
   *
   * @param application Application context.
   */
  public ConsumerViewModel(Application application) {
    super(application);
    providerService =
        new LocalProviderService(
            LocalProviderService.createRestProvider(ProviderUtils.getProviderBaseUrl(application)),
            executor,
            scheduledExecutor);
    appState.setValue(UNINITIALIZED);
    mainExecutor = ContextCompat.getMainExecutor(application);
  }

  /** Creates a trip in the sample provider. */
  public void startSingleExclusiveTrip() {
    ListenableFuture<TripResponse> singleExclusiveTrip =
        providerService.createSingleExclusiveTrip(
            createWaypointData(pickupLocation.getValue(), dropoffLocation.getValue()));
    handleCreateSingleExclusiveTripResponse(singleExclusiveTrip);
  }

  private void handleCreateSingleExclusiveTripResponse(
      ListenableFuture<TripResponse> singleExclusiveTrip) {
    Futures.addCallback(
        singleExclusiveTrip,
        new FutureCallback<TripResponse>() {
          @Override
          public void onSuccess(TripResponse result) {
            Log.i(TAG, String.format("Successfully created trip %s.", result.getTripName()));
            tripStatus.postValue(TripStatus.parse(result.getTripStatus()));

            ListenableFuture<TripData> tripDataFuture = providerService.fetchMatchedTrip(
                result.getTripName());
            handleFetchMatchedTripResponse(tripDataFuture);
          }

          @Override
          public void onFailure(Throwable e) {
            Log.e(TAG, "Failed to create trip.", e);
          }
        },
        executor);
  }

  private void handleFetchMatchedTripResponse(ListenableFuture<TripData> tripDataFuture) {
    Futures.addCallback(
        tripDataFuture,
        new FutureCallback<TripData>() {
          @Override
          public void onSuccess(TripData tripData) {
            mainExecutor.execute(() -> startJourneySharing(tripData));
          }

          @Override
          public void onFailure(Throwable e) {
            Log.e(TAG, "Failed to match trip with a driver.", e);
          }
        },
{'fraction_non_alphanumeric': 0.08242585727525487, 'fraction_numerical': 0.001969416126042632, 'mean_word_length': 4.3601366035392735, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 62, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4632541', 'n_tokens_mistral': 4520, 'n_tokens_neox': 4389, 'n_words': 1246}