package com.twilio.twiliochat.chat.accesstoken;

import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.twilio.twiliochat.R;
import com.twilio.twiliochat.application.SessionManager;
import com.twilio.twiliochat.application.TwilioChatApplication;
import com.twilio.twiliochat.chat.listeners.TaskCompletionListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AccessTokenFetcher {

  private Context context;

  public AccessTokenFetcher(Context context) {
    this.context = context;
  }

  public void fetch(final TaskCompletionListener<String, String> listener) {
    JSONObject obj = new JSONObject(getTokenRequestParams(context));
    String requestUrl = getStringResource(R.string.token_url);

    JsonObjectRequest jsonObjReq =
        new JsonObjectRequest(Request.Method.POST, requestUrl, obj, new Response.Listener<JSONObject>() {

          @Override
          public void onResponse(JSONObject response) {
            String token = null;
            try {
              token = response.getString("token");
            } catch (JSONException e) {
              e.printStackTrace();
              listener.onError("Failed to parse token JSON response");
            }
            listener.onSuccess(token);
          }
        }, new Response.ErrorListener() {

          @Override
          public void onErrorResponse(VolleyError error) {

            listener.onError("Failed to fetch token");
          }
        });
    jsonObjReq.setShouldCache(false);
    TokenRequest.getInstance().addToRequestQueue(jsonObjReq);
  }

  private Map<String, String> getTokenRequestParams(Context context) {
    String androidId =
        Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    Map<String, String> params = new HashMap<>();
    params.put("deviceId", androidId);
    params.put("identity", SessionManager.getInstance().getUsername());
    return params;
  }

  private String getStringResource(int id) {
    Resources resources = TwilioChatApplication.get().getResources();
    return resources.getString(id);
  }

}

@@ -131,25 +131,16 @@
 oid.
-provider.Settings
+util.Log
 ;%0A%0Ai
@@ -936,55 +936,232 @@
 ing 
-requestUrl = getStringResource(R.string.token_u
+identity = SessionManager.getInstance().getUsername();%0A    String requestUrl = getStringResource(R.string.token_url) + %22?identity=%22 + identity;%0A    Log.d(TwilioChatApplication.TAG, %22Requesting access token from: %22 + requestU
 rl);
@@ -1246,11 +1246,10 @@
 hod.
-POS
+GE
 T, r
@@ -1539,26 +1539,67 @@
     
-e.printStackTrace(
+Log.e(TwilioChatApplication.TAG, e.getLocalizedMessage(), e
 );%0A 
@@ -1851,24 +1851,105 @@
 or error) %7B%0A
+            Log.e(TwilioChatApplication.TAG, error.getLocalizedMessage(), error);
 %0A           
@@ -2196,124 +2196,8 @@
 ) %7B%0A
-    String androidId =%0A        Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);%0A
     
@@ -2246,47 +2246,8 @@
 ();%0A
-    params.put(%22deviceId%22, androidId);%0A
     

Improve access token retrieval code