com.starup.traven.travelkorea.ImageGridFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.starup.traven.travelkorea.ImageGridFragment.java

Source

/*
 * Copyright (C) 2012 The 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.starup.traven.travelkorea;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.starup.traven.travelkorea.common.logger.Log;
import com.starup.traven.travelkorea.provider.Images;
import com.starup.traven.travelkorea.ui.RecyclingImageView;
import com.starup.traven.travelkorea.util.ImageCache;
import com.starup.traven.travelkorea.util.ImageFetcher;
import com.starup.traven.travelkorea.util.Utils;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.starup.traven.travelkorea.XMLParser.VisitKoreaXmlParser;
import com.starup.traven.travelkorea.XMLParser.VisitKoreaXmlParser.Entry;

/**
 * The main fragment that powers the ImageGridActivity screen. Fairly straight forward GridView
 * implementation with the key addition being the ImageWorker class w/ImageCache to load children
 * asynchronously, keeping the UI nice and smooth and caching thumbnails for quick retrieval. The
 * cache is retained over configuration changes like orientation change so the images are populated
 * quickly if, for example, the user rotates the device.
 */
public class ImageGridFragment extends Fragment implements AdapterView.OnItemClickListener {
    private GridView mGridView = null;

    private int mAge;
    private int mGender;
    private int mLanguage;

    private String langService = "EngService";
    private String contentID = "82";
    private String radius = "2000";
    private String mapX = "126.981106";
    private String mapY = "37.568477";

    private static final String service_key = "tcMsl%2FuAaldSmyvnY78FqTHtqWLrQUg%2FYLDcNX389OGxcA%2BpXV2ejk86zNsw1XJZXNwUiIw6F8e6BbToTVpblg%3D%3D";
    private static final String URL1 = "http://api.visitkorea.or.kr/openapi/service/rest/"; // +langService
    private static final String URL2 = "/locationBasedList?ServiceKey=" + service_key + "&contentTypeId="; // +contentID
    private static final String URL3 = "&mapX="; // +mapX
    private static final String URL4 = "&mapY="; // +mapY
    private static final String URL5 = "&radius=";//radius
    private static final String URl6 = "&listYN=Y&MobileOS=AND&MobileApp=TourAPI3.0_Guide&arrange=A&numOfRows=100&pageNo=1";

    public String getURL() {
        Location loc = ((MainActivity) getActivity()).mLastLocation;
        if (loc != null) {
            mapX = Double.toString(loc.getLongitude());
            mapY = Double.toString(loc.getLatitude());
        }

        String visitURL = URL1 + langService + URL2 + contentID + URL3 + mapX + URL4 + mapY + URL5 + radius + URl6;
        return visitURL;
    }

    private static final String TAG = "ImageGridFragment";
    private static final String IMAGE_CACHE_DIR = "thumbs";

    private int mImageThumbSize;
    private int mImageThumbSpacing;
    private ImageAdapter mAdapter;
    private ImageFetcher mImageFetcher;

    /**
     *  VisitKorea Open API connection.
     */
    public static final String WIFI = "Wi-Fi";
    public static final String ANY = "Any";

    // Whether there is a Wi-Fi connection.
    private static boolean wifiConnected = false;
    // Whether there is a mobile connection.
    private static boolean mobileConnected = false;
    // Whether the display should be refreshed.
    public static boolean refreshDisplay = true;

    // The user's current network preference setting.
    public static String sPref = null;

    private List<Entry> mGridData = null;

    ImageButton[] imageButton = new ImageButton[5];

    /**
     * Empty constructor as per the Fragment documentation
     */
    public ImageGridFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        radius = getResources().getString(R.string.distance);

        mGridData = new ArrayList<Entry>(0);
        mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
        mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);

        mAdapter = new ImageAdapter(getActivity());

        ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);

        cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory

        // The ImageFetcher takes care of loading images into our ImageView children asynchronously
        mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);
        mImageFetcher.setLoadingImage(R.drawable.empty_photo);
        mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(), cacheParams);

        // Gets the user's network preference settings
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

        // Retrieves a string value for the preferences. The second parameter
        // is the default value to use if a preference value is not found.
        sPref = sharedPrefs.getString("listPref", "Wi-Fi");

        updateConnectedFlags();
        updateOption();
    }

    public void updateOption() {
        mAge = ((MainActivity) getActivity()).myinfo.age;
        mGender = ((MainActivity) getActivity()).myinfo.gender;
        mLanguage = ((MainActivity) getActivity()).myinfo.language;

        if (mLanguage == 1) {
            langService = "KorService";
        } else if (mLanguage == 2) {
            langService = "ChsService";
        } else if (mLanguage == 3) {
            langService = "JpnService";
        } else {
            langService = "EngService";
        }

        if (langService.equals("KorService")) {
            if (contentID == "82") {
                contentID = "39";
            } else if (contentID == "80") {
                contentID = "32";
            } else if (contentID == "79") {
                contentID = "38";
            } else if (contentID == "88") {
                contentID = "14";
            } else if (contentID == "85") {
                contentID = "15";
            }
        }

        Location loc = ((MainActivity) getActivity()).mLastLocation;
        if (loc != null) {
            mapX = Double.toString(loc.getLongitude());
            mapY = Double.toString(loc.getLatitude());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
        mGridView = (GridView) v.findViewById(R.id.gridView);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);

        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                // Pause fetcher to ensure smoother scrolling when flinging
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                    // Before Honeycomb pause image loading on scroll to help with performance
                    if (!Utils.hasHoneycomb()) {
                        mImageFetcher.setPauseWork(true);
                    }
                } else {
                    mImageFetcher.setPauseWork(false);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
            }
        });

        // This listener is used to get the final width of the GridView and then calculate the
        // number of columns and the width of each column. The width of each column is variable
        // as the GridView has stretchMode=columnWidth. The column width is used to set the height
        // of each view so we get nice square thumbnails.
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                if (mAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math
                            .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                        mAdapter.setNumColumns(numColumns);
                        mAdapter.setItemHeight(columnWidth);
                        if (BuildConfig.DEBUG) {
                            Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                        }
                        if (Utils.hasJellyBean()) {
                            mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        } else {
                            mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }
                    }
                }
            }
        });

        addListenerOnButton(v);
        return v;
    }

    public void addListenerOnButton(View v) {
        imageButton[0] = (ImageButton) v.findViewById(R.id.foodButton);
        imageButton[1] = (ImageButton) v.findViewById(R.id.hotelButton);
        imageButton[2] = (ImageButton) v.findViewById(R.id.shoppingButton);
        imageButton[3] = (ImageButton) v.findViewById(R.id.cultureButton);
        imageButton[4] = (ImageButton) v.findViewById(R.id.concertButton);

        for (int i = 0; i < 5; i++) {
            imageButton[i].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    mAge = ((MainActivity) getActivity()).myinfo.age;
                    mGender = ((MainActivity) getActivity()).myinfo.gender;
                    mLanguage = ((MainActivity) getActivity()).myinfo.language;

                    if (mLanguage == 1) {
                        langService = "KorService";
                    } else if (mLanguage == 2) {
                        langService = "ChsService";
                    } else if (mLanguage == 3) {
                        langService = "JpnService";
                    } else {
                        langService = "EngService";
                    }

                    boolean temp = langService.equals("KorService");
                    String currentID = contentID;
                    if (arg0.getId() == R.id.foodButton) {
                        contentID = langService.equals("KorService") ? "39" : "82";
                    } else if (arg0.getId() == R.id.hotelButton) {
                        contentID = langService.equals("KorService") ? "32" : "80";
                    } else if (arg0.getId() == R.id.shoppingButton) {
                        contentID = langService.equals("KorService") ? "38" : "79";
                    } else if (arg0.getId() == R.id.cultureButton) {
                        contentID = langService.equals("KorService") ? "14" : "78";
                    } else if (arg0.getId() == R.id.concertButton) {
                        contentID = langService.equals("KorService") ? "15" : "85";
                    }

                    //                    if (contentID != currentID)
                    {
                        mGridData.clear();
                        loadPage();
                    }

                }

            });
        }
    }

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

        loadPage();

        //updateOption();
        mImageFetcher.setExitTasksEarly(false);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        mImageFetcher.setPauseWork(false);
        mImageFetcher.setExitTasksEarly(true);
        mImageFetcher.flushCache();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mImageFetcher.closeCache();
    }

    @TargetApi(VERSION_CODES.JELLY_BEAN)
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
        i.putExtra("langService", langService);
        i.putExtra("contenttypeId", mGridData.get(position).contenttypeId);
        i.putExtra("contentid", mGridData.get(position).contentid);
        startActivity(i);
    }

    // Checks the network connection and sets the wifiConnected and mobileConnected
    // variables accordingly.
    private void updateConnectedFlags() {
        ConnectivityManager connMgr = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
            mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
        } else {
            wifiConnected = false;
            mobileConnected = false;
        }
    }

    // Uses AsyncTask subclass to download the XML feed from stackoverflow.com.
    // This avoids UI lock up. To prevent network operations from
    // causing a delay that results in a poor user experience, always perform
    // network operations on a separate thread from the UI.
    private void loadPage() {
        updateOption();
        new DownloadXmlTask().execute(getURL());

        //        if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected))
        //                || ((sPref.equals(WIFI)) && (wifiConnected))) {
        //            // AsyncTask subclass
        //        } else {
        //            showErrorPage();
        //        }
    }

    private void showErrorPage() {
        ;
    }

    // Implementation of AsyncTask used to download XML feed from stackoverflow.com.
    private class DownloadXmlTask extends AsyncTask<String, Void, String> {

        static final String Tag = "DownloadTask";

        @Override
        protected String doInBackground(String... urls) {
            try {
                return loadXmlFromNetwork(urls[0]);
            } catch (IOException e) {
                return getResources().getString(R.string.connection_error);
            } catch (XmlPullParserException e) {
                return getResources().getString(R.string.xml_error);
            }
        }

        @Override
        protected void onPostExecute(String result) {
            //setContentView(R.layout.main);
            // Displays the HTML string in the UI via a WebView
            //WebView myWebView = (WebView) findViewById(R.id.webview);
            //myWebView.loadData(result, "text/html", null);

            android.util.Log.i(Tag, "onPoseExecute");

            if (result.equals("OK")) {
                //mGridAdapter.setGridData(mGridData);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    // Uploads XML from stackoverflow.com, parses it, and combines it with
    // HTML markup. Returns HTML string.
    private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
        InputStream stream = null;
        VisitKoreaXmlParser stackOverflowXmlParser = new VisitKoreaXmlParser();

        try {
            stream = downloadUrl(urlString);

            mGridData = null;
            mGridData = stackOverflowXmlParser.parse(stream);

            ListIterator<Entry> iter = mGridData.listIterator();

            while (iter.hasNext()) {
                Entry entry = iter.next();
                if (entry.firstimage == null) {
                    iter.remove();
                }
            }

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (stream != null) {
                stream.close();
            }
        }

        return "OK";
    }

    // Given a string representation of a URL, sets up a connection and gets
    // an input stream.
    private InputStream downloadUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        InputStream stream = conn.getInputStream();
        return stream;
    }

    //    @Override
    //    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //        inflater.inflate(R.menu.main_menu, menu);
    //    }

    //    @Override
    //    public boolean onOptionsItemSelected(MenuItem item) {
    //        switch (item.getItemId()) {
    //            case R.id.clear_cache:
    //                mImageFetcher.clearCache();
    //                Toast.makeText(getActivity(), R.string.clear_cache_complete_toast,
    //                        Toast.LENGTH_SHORT).show();
    //                return true;
    //        }
    //        return super.onOptionsItemSelected(item);
    //    }

    /**
     * The main adapter that backs the GridView. This is fairly standard except the number of
     * columns in the GridView is used to create a fake top row of empty views as we use a
     * transparent ActionBar and don't want the real top row of images to start off covered by it.
     */
    private class ImageAdapter extends BaseAdapter {

        private final Context mContext;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private int mActionBarHeight = 0;
        private GridView.LayoutParams mImageViewLayoutParams;

        public ImageAdapter(Context context) {
            super();
            mContext = context;

            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);

            // Calculate ActionBar height
            TypedValue tv = new TypedValue();
            if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                        context.getResources().getDisplayMetrics());
            }
        }

        @Override
        public int getCount() {
            int val = 0;
            if (mGridData != null)
                val = mGridData.size();

            return val;
        }

        @Override
        public Object getItem(int position) {
            return mGridData.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        //        @Override
        //        public int getViewTypeCount() {
        //            // Two types of views, the normal ImageView and the top row of empty views
        //            return 2;
        //        }

        //        @Override
        //        public int getItemViewType(int position) {
        //            return (position < mNumColumns) ? 1 : 0;
        //        }

        //        @Override
        //        public boolean hasStableIds() {
        //            return true;
        //        }

        @Override
        public View getView(int position, View convertView, ViewGroup container) {
            // Now handle the main ImageView thumbnails
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, instantiate and initialize
                imageView = new RecyclingImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setLayoutParams(mImageViewLayoutParams);
            } else { // Otherwise re-use the converted view
                imageView = (ImageView) convertView;
            }

            // Check the height matches our calculated column width
            if (imageView.getLayoutParams().height != mItemHeight) {
                imageView.setLayoutParams(mImageViewLayoutParams);
            }

            // Finally load the image asynchronously into the ImageView, this also takes care of
            // setting a placeholder image while the background thread runs
            //mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView);

            if (mGridData != null) {
                String image_url = mGridData.get(position).firstimage;

                if (image_url != null)
                    mImageFetcher.loadImage(image_url, imageView);
            }

            return imageView;
            //END_INCLUDE(load_gridview_item)
        }

        /**
         * Sets the item height. Useful for when we know the column width so the height can be set
         * to match.
         *
         * @param height
         */
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }
    }

}