content: /*
   *    Copyright (C) 2016 Amit Shekhar
   *    Copyright (C) 2011 Android Open Source Project
   *
   *    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.networking;
  
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.ImageView;
  
  import com.androidnetworking.AndroidNetworking;
  import com.androidnetworking.common.Priority;
  import com.androidnetworking.error.ANError;
  import com.androidnetworking.interfaces.AnalyticsListener;
  import com.androidnetworking.interfaces.BitmapRequestListener;
  import com.androidnetworking.interfaces.JSONArrayRequestListener;
  import com.androidnetworking.interfaces.JSONObjectRequestListener;
  import com.androidnetworking.internal.ANImageLoader;
  import com.androidnetworking.widget.ANImageView;
  import com.networking.provider.Images;
  
  import org.json.JSONArray;
  import org.json.JSONObject;
  
  public class MainActivity extends AppCompatActivity {
  
      private static final String TAG = MainActivity.class.getSimpleName();
      private static final String URL_IMAGE = "http://i.imgur.com/2M7Hasn.png";
      private static final String URL_IMAGE_LOADER = "http://i.imgur.com/52md06W.jpg";
  
      private ImageView imageView;
      private ANImageView ANImageView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          imageView = (ImageView) findViewById(R.id.imageView);
          ANImageView = (ANImageView) findViewById(R.id.greatImageView);
          ANImageView.setDefaultImageResId(R.drawable.ic_toys_black_24dp);
          ANImageView.setErrorImageResId(R.drawable.ic_error_outline_black_24dp);
          ANImageView.setImageUrl(Images.imageThumbUrls[0]);
          makeJSONArrayRequest();
          makeJSONObjectRequest();
      }
  
      private void makeJSONArrayRequest() {
          AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY)
                  .setTag(this)
                  .addPathParameter("pageNumber", "0")
                  .addQueryParameter("limit", "3")
                  .setPriority(Priority.LOW)
                  .build()
                  .setAnalyticsListener(new AnalyticsListener() {
                      @Override
                      public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
                          Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                          Log.d(TAG, " bytesSent : " + bytesSent);
                          Log.d(TAG, " bytesReceived : " + bytesReceived);
                          Log.d(TAG, " isFromCache : " + isFromCache);
                      }
                  })
                  .getAsJSONArray(new JSONArrayRequestListener() {
                      @Override
                      public void onResponse(JSONArray response) {
                          Log.d(TAG, "onResponse array : " + response.toString());
                      }
  
                      @Override
                      public void onError(ANError error) {
                          if (error.getErrorCode() != 0) {
                              // received ANError from server
                              // error.getErrorCode() - the ANError code from server
                              // error.getErrorBody() - the ANError body from server
                              // error.getErrorDetail() - just a ANError detail
                              Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                              Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          } else {
                              // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          }
                      }
                  });
      }
  
      private void makeJSONObjectRequest() {
          AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_OBJECT)
                  .setTag(this)
                  .addPathParameter("userId", "1")
                  .setPriority(Priority.HIGH)
                  .build()
                  .setAnalyticsListener(new AnalyticsListener() {
                      @Override
                      public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
                          Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                          Log.d(TAG, " bytesSent : " + bytesSent);
                          Log.d(TAG, " bytesReceived : " + bytesReceived);
                          Log.d(TAG, " isFromCache : " + isFromCache);
                      }
                  })
                  .getAsJSONObject(new JSONObjectRequestListener() {
                      @Override
                      public void onResponse(JSONObject response) {
                          Log.d(TAG, "onResponse object : " + response.toString());
                      }
  
                      @Override
                      public void onError(ANError error) {
                          if (error.getErrorCode() != 0) {
                              // received ANError from server
                              // error.getErrorCode() - the ANError code from server
                              // error.getErrorBody() - the ANError body from server
                              // error.getErrorDetail() - just a ANError detail
                              Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                              Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          } else {
                              // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          }
                      }
                  });
      }
  
      public void makeRequests(View view) {
          for (int i = 0; i < 10; i++) {
              makeJSONArrayRequest();
              makeJSONObjectRequest();
          }
      }
  
      public void cancelAllRequests(View view) {
          Log.d(TAG, "isRequestRunning before cancel : " + AndroidNetworking.isRequestRunning(this));
          AndroidNetworking.cancel(this);
          Log.d(TAG, "isRequestRunning after cancel : " + AndroidNetworking.isRequestRunning(this));
      }
  
      public void loadImageDirect(View view) {
          AndroidNetworking.get(URL_IMAGE)
                  .setTag("imageRequestTag")
                  .setPriority(Priority.MEDIUM)
                  .setImageScaleType(null)
                  .setBitmapMaxHeight(0)
                  .setBitmapMaxWidth(0)
                  .setBitmapConfig(Bitmap.Config.ARGB_8888)
                  .build()
                  .setAnalyticsListener(new AnalyticsListener() {
                      @Override
                      public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
                          Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                          Log.d(TAG, " bytesSent : " + bytesSent);
                          Log.d(TAG, " bytesReceived : " + bytesReceived);
                          Log.d(TAG, " isFromCache : " + isFromCache);
                      }
                  })
                  .getAsBitmap(new BitmapRequestListener() {
                      @Override
                      public void onResponse(Bitmap response) {
                          Log.d(TAG, "onResponse Bitmap");
                          imageView.setImageBitmap(response);
                      }
  
                      @Override
                      public void onError(ANError error) {
                          if (error.getErrorCode() != 0) {
                              // received ANError from server
                              // error.getErrorCode() - the ANError code from server
                              // error.getErrorBody() - the ANError body from server
                              // error.getErrorDetail() - just a ANError detail
                              Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                              Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          } else {
                              // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                              Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                          }
                      }
                  });
      }
  
      public void loadImageFromImageLoader(View view) {
          ANImageLoader.getInstance().get(URL_IMAGE_LOADER, ANImageLoader.getImageListener(imageView,
                  R.drawable.ic_toys_black_24dp, R.drawable.ic_error_outline_black_24dp));
      }
  
      public void startGridActivity(View view) {
          startActivity(new Intent(MainActivity.this, ImageGridActivity.class));
      }
  
      public void startApiTestActivity(View view) {
          startActivity(new Intent(MainActivity.this, ApiTestActivity.class));
      }
  
      public void startOkHttpResponseTestActivity(View view) {
          startActivity(new Intent(MainActivity.this, OkHttpResponseTestActivity.class));
      }
  
      public void startWebSocketActivity(View view) {
          startActivity(new Intent(MainActivity.this, WebSocketActivity.class));
      }
  }
  