/* Copyright (c) 2014 Andrea Zoli. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file. */

package it.inaf.android;

import android.app.Application;
import android.content.res.Configuration;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;
import org.json.JSONArray;
import org.json.JSONObject;

@ReportsCrashes(
        formKey = "",
        formUri = "http://app.media.inaf.it/android/report.php"
)

public class INAF extends Application {

    public static RequestQueue requestQueue;
    public static ImageLoader imageLoader;

    private static BitmapLruCache mBitmapCache;

    public static int width;
    public static int height;
    public static float aspectRatio;
    public static boolean landscape;

    public static String aboutUrl = "http://app.media.inaf.it/GetAbout.php";
    public static JSONArray jsonAbout;
    public static String homeImageUrl = "http://app.media.inaf.it/GetSplashImage.php";
    public static JSONObject jsonHomeImage;
    public static BitmapDrawable homeBackground;
    public static String locationsUrl = "http://app.media.inaf.it/GetLocations.php";
    public static JSONArray jsonLocations;
    public static String appsUrl = "http://app.media.inaf.it/GetApps.php";
    public static JSONArray jsonApps;
    public static String telescopesUrl = "http://app.media.inaf.it/GetTelescopes.php";
    public static String telescopeImagePrefixUrl = "http://www.media.inaf.it/wp-content/themes/mediainaf/images/tags/";
    public static String telescopeDetailPrefixUrl = "http://www.media.inaf.it/tag/";
    public static JSONArray jsonTelescopes;
    public static String satellitesUrl = "http://app.media.inaf.it/GetSatellites.php";
    public static String satelliteImagePrefixUrl = "http://www.media.inaf.it/wp-content/themes/mediainaf/images/tags/";
    public static String satelliteDetailPrefixUrl = "http://www.media.inaf.it/tag/";
    public static JSONArray jsonSatellites;

    @Override
    public void onCreate() {
        super.onCreate();

        requestQueue = Volley.newRequestQueue(this, null);

        ACRA.init(this);

        imageLoader = new ImageLoader(requestQueue, getBitmapCache());
        VolleyLog.setTag("MyAppTag");
        // http://stackoverflow.com/a/17035814
        imageLoader.setBatchedResponseDelay(0);

        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
            width = size.x;
            height = size.y;
        }
        else {
            width = display.getWidth();  // deprecated
            height = display.getHeight();  // deprecated
        }
        landscape = false;
        int r = display.getRotation();
        if(r == Surface.ROTATION_90 || r == Surface.ROTATION_270) {
            int tmp = width;
            width = height;
            height = tmp;
            landscape = true;
        }

        if(width > height)
            aspectRatio = width / (float)height;
        else
            aspectRatio = height / (float)width;

        Log.i("Info", "Width: " + width + " Height: " + height + "Aspect Ratio: " + aspectRatio);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            landscape = true;
        }else{
            landscape = false;
        }
    }

    public static BitmapLruCache getBitmapCache() {
        if (mBitmapCache == null) {
            // The cache size will be measured in kilobytes rather than
            // number of items. See http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
            int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            int cacheSize = maxMemory / 8;  //Use 1/8th of the available memory for this memory cache.
            mBitmapCache = new BitmapLruCache(cacheSize);
        }
        return mBitmapCache;
    }
}

@@ -3224,32 +3224,33 @@
 cated%0A        %7D%0A
+%0A
         landscap
@@ -3257,244 +3257,111 @@
 e = 
-false;%0A        int r = display.getRotation();%0A        if(r == Surface.ROTATION_90 %7C%7C r == Surface.ROTATION_270) %7B%0A            int tmp = width;%0A            width = height;%0A            height = tmp;%0A            landscape = true;%0A        %7D
+(getResources().getConfiguration().orientation%0A                == Configuration.ORIENTATION_LANDSCAPE);
 %0A%0A  

Fix initial landscape value.