package edu.alaska.gina.feeder.puffinfeeder;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

/**
 * Fragment containing WebView that displays full-sized image.
 * Created by bobby on 7/1/13.
 */
public class ImageViewerFragment extends SherlockFragment{
    protected String image_url_small;
    protected String image_url_med;
    protected String image_url_large;
    protected String title;
    protected WebView image_frame;
    protected SharedPreferences sharedPreferences;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_image_viewer, container, false);
        setHasOptionsMenu(true);
        return v;
    }

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

        image_frame = (WebView) getActivity().findViewById(R.id.fragment_feed_image_webview);
        image_frame.getSettings().setBuiltInZoomControls(true);
        image_frame.getSettings().setLoadWithOverviewMode(true);


        image_frame.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                getSherlockActivity().setSupportProgress(newProgress * 100);
                if (newProgress >= 0)
                    getSherlockActivity().setSupportProgressBarIndeterminate(false);
                if (newProgress >= 100) {
                    getSherlockActivity().setSupportProgressBarVisibility(false);
                    getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
                }
            }
        });

        Bundle extra = getArguments();
        if (extra != null) {
            image_url_small = extra.getString("image_url_small");
            image_url_med = extra.getString("image_url_med");
            image_url_large = extra.getString("image_url_large");
            title = extra.getString("bar_title");
        }
        else {
            Log.d(getString(R.string.app_tag), "No Image URL. Please Fix that...");
            Toast.makeText(getActivity(), "No Image URL. Please fix that...", Toast.LENGTH_SHORT).show();
            return;
        }

        getSherlockActivity().getSupportActionBar().setTitle(title);

        getSherlockActivity().setSupportProgressBarVisibility(true);
        getSherlockActivity().setSupportProgressBarIndeterminate(true);
        getSherlockActivity().setSupportProgressBarIndeterminateVisibility(true);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
        sharedPreferences.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
                loadme();
            }
        });

        loadme();
    }

    private void loadme() {
        if (sharedPreferences.getString("pref_viewer_image_size", "med").equals("small"))
            image_frame.getSettings().setUseWideViewPort(false);
        else
            image_frame.getSettings().setUseWideViewPort(true);

        image_frame.loadUrl(getImageUrl(sharedPreferences.getString("pref_viewer_image_size", "med")));
    }

    private String getImageUrl(String sizeString) {
        if (sizeString.equals("small"))
            return image_url_small;
        if (sizeString.equals("med"))
            return image_url_med;
        if (sizeString.equals("large"))
            return image_url_large;

        return null;
    }
}

@@ -81,16 +81,88 @@
 rences;%0A
+import android.net.ConnectivityManager;%0Aimport android.net.NetworkInfo;%0A
 import a
@@ -3292,24 +3292,38 @@
      loadme(
+pickLoadSize()
 );%0A         
@@ -3355,16 +3355,30 @@
  loadme(
+pickLoadSize()
 );%0A    %7D
@@ -3395,96 +3395,1015 @@
 ate 
-void loadme() %7B%0A        if (sharedPreferences.getString(%22pref_viewer_image_size%22, %22med%22)
+boolean isMetered(NetworkInfo net1) %7B%0A        int type = net1.getType();%0A        switch (type) %7B%0A            case ConnectivityManager.TYPE_MOBILE:%0A            case ConnectivityManager.TYPE_MOBILE_DUN:%0A            case ConnectivityManager.TYPE_MOBILE_HIPRI:%0A            case ConnectivityManager.TYPE_MOBILE_MMS:%0A            case ConnectivityManager.TYPE_MOBILE_SUPL:%0A                return true;%0A            default:%0A                return false;%0A        %7D%0A    %7D%0A%0A    private String pickLoadSize() %7B%0A        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);%0A        NetworkInfo nf = cm.getActiveNetworkInfo();%0A%0A        if (sharedPreferences.getBoolean(%22pref_smart_sizing_on/off%22, true) && isMetered(nf))%0A            return sharedPreferences.getString(%22pref_smart_sizing_size%22, %22small%22);%0A        else%0A            return sharedPreferences.getString(%22pref_viewer_image_size%22, %22med%22);%0A    %7D%0A%0A    private void loadme(String size) %7B%0A        if (size
 .equ
@@ -4604,67 +4604,11 @@
 rl(s
-haredPreferences.getString(%22pref_viewer_image_size%22, %22med%22)
+ize
 ));%0A

Implement basic framework for smart sizing.