Search code examples
javaandroidgoogle-mapsgoogle-places-apigoogle-places-autocomplete

Does public static AutocompleteSessionToken newInstance () return same instance?


Does this method behave on singleton pattern? or does it create new session for the user each time?

the below method is called on edittext change listener.

   @NonNull
   private ArrayList<PlaceAutocomplete> getPredictions(@NonNull CharSequence constraint) {

       final ArrayList<PlaceAutocomplete> resultList = new ArrayList<>();

       // Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
       // and once again when the user makes a selection (for example when calling fetchPlace()).
       AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();

       //https://gist.github.com/graydon/11198540
       // Use the builder to create a FindAutocompletePredictionsRequest.
       FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
              .setLocationBias(bounds)
               .setCountry("PK")
               .setSessionToken(token)
               .setQuery(constraint.toString())
               .build();

       Task<FindAutocompletePredictionsResponse> autocompletePredictions = placesClient.findAutocompletePredictions(request);

       // This method should have been called off the main UI thread. Block and wait for at most
       // 60s for a result from the API.
       try {
           Tasks.await(autocompletePredictions, 60, TimeUnit.SECONDS);
       } catch (@NonNull ExecutionException | InterruptedException | TimeoutException e) {
           e.printStackTrace();
       }

       if (autocompletePredictions.isSuccessful()) {
           FindAutocompletePredictionsResponse findAutocompletePredictionsResponse = autocompletePredictions.getResult();
           if (findAutocompletePredictionsResponse != null)
               for (AutocompletePrediction prediction : findAutocompletePredictionsResponse.getAutocompletePredictions()) {
                   Log.i(TAG, prediction.getPlaceId());
                   resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getPrimaryText(STYLE_NORMAL).toString(), prediction.getFullText(STYLE_BOLD).toString()));
               }

           return resultList;
       } else {
           return resultList;
       }

   }

the method calls on each text change in editText.


Solution

  • The AutocompleteSessionToken.newInstance() method returns a new instance, i.e. a new session token. Each time you call this method, you are creating a new session token.

    Google explains how autocomplete sessions work here:

    Session tokens group the query and selection phases of a user autocomplete search into a discrete session for billing purposes. The session begins when the user starts typing a query, and concludes when they select a place. Each session can have multiple queries, followed by one place selection. Once a session has concluded, the token is no longer valid; your app must generate a fresh token for each session.

    The text in editText changes when the user has selected a place from autocomplete predictions (the session ends, a new one will be created when the user selects a new place).

    Hope this helps!