com.leo.runningman.ui.ImageGridFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.leo.runningman.ui.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.leo.runningman.ui;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdView;
import com.leo.runningman.provider.ImageProvider;
import com.leo.runningman.util.DiskLruCache;
import com.leo.runningman.util.ImageCache;
import com.leo.runningman.util.ImageCache.ImageCacheParams;
import com.leo.runningman.util.ImageFetcher;
import com.leo.runningman.util.ImageResizer;
import com.leo.runningman.util.Network;
import com.leo.runningman.util.Utils;

/**
 * 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 as the user rotates the device.
 */
public class ImageGridFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final String TAG = "ImageGridFragment";
    private static final String IMAGE_CACHE_DIR = "thumbs";

    private int mImageThumbSize;
    private int mImageThumbSpacing;
    private ImageAdapter mAdapter;
    private ImageResizer mImageWorker;

    private RelativeLayout layout_bottom;
    private Thread showThread;
    private Thread hideThread;

    private AdView adview_first;
    private int start = 0; //the start position to require data
    private int count = 90; //dynamic add data num

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

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

        mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
        mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);

        mAdapter = new ImageAdapter(getActivity());

        ImageCacheParams cacheParams = new ImageCacheParams(IMAGE_CACHE_DIR);

        // Allocate a third of the per-app memory limit to the bitmap memory cache. This value
        // should be chosen carefully based on a number of factors. Refer to the corresponding
        // Android Training class for more discussion:
        // http://developer.android.com/training/displaying-bitmaps/
        // In this case, we aren't using memory for much else other than this activity and the
        // ImageDetailActivity so a third lets us keep all our sample image thumbnails in memory
        // at once.
        cacheParams.memCacheSize = 1024 * 1024 * Utils.getMemoryClass(getActivity()) / 3;

        // The ImageWorker takes care of loading images into our ImageView children asynchronously
        mImageWorker = new ImageFetcher(getActivity(), mImageThumbSize);
        mImageWorker.setAdapter(ImageProvider.imageThumbWorkerUrlsAdapter);
        mImageWorker.setLoadingImage(R.drawable.empty_photo); //
        mImageWorker.setImageCache(ImageCache.findOrCreateCache(getActivity(), cacheParams));

    }

    public void autoHideview(long millions) {
        if (hideThread == null) {
            hideThread = new Thread() {
                public void run() {
                    sendHandlerMessage(HIDE_BOTTOM, 0, 0, null);
                }
            };
        }
        if (layout_bottom.getVisibility() == View.VISIBLE) {
            cancelAutoHideview();
            layout_bottom.postDelayed(hideThread, millions);
        }
    }

    public void autoShowview(long millions) {
        if (showThread == null) {
            showThread = new Thread() {
                public void run() {
                    sendHandlerMessage(SHOW_BOTTOM, 0, 0, null);
                }
            };
        }
        if (layout_bottom.getVisibility() != View.VISIBLE) {
            cancelAutoShow();
            layout_bottom.postDelayed(showThread, millions);
        }
    }

    public void cancelAutoShow() {
        if (showThread != null) {
            layout_bottom.removeCallbacks(showThread);
        }
    }

    public void cancelAutoHideview() {
        if (hideThread != null) {
            layout_bottom.removeCallbacks(hideThread);
        }
    }

    public boolean sendHandlerMessage(int what, int arg1, int arg2, Object obj) {
        Message msg = Message.obtain();
        if (msg != null) {
            msg.what = what;
            msg.arg1 = arg1;
            msg.arg2 = arg2;
            msg.obj = obj;
            return mHandler.sendMessage(msg);
        }
        return false;
    }

    private static final int SHOW_BOTTOM = 0;
    private static final int HIDE_BOTTOM = 1;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case SHOW_BOTTOM:
                showBottomLayout();
                break;
            case HIDE_BOTTOM:
                hideBottomLayout();
                break;
            default:
                break;
            }
        }
    };

    private void showBottomLayout() {
        if (layout_bottom != null && layout_bottom.getVisibility() != View.VISIBLE) {
            layout_bottom.setVisibility(View.VISIBLE);
        }
        cancelAutoShow();
        autoHideview(4000);
    }

    private void hideBottomLayout() {
        if (layout_bottom != null && layout_bottom.getVisibility() != View.GONE) {
            layout_bottom.setVisibility(View.GONE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getActivity().getWindow();
        window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.layout_title);

        final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);

        TextView txt_title = (TextView) this.getActivity().findViewById(R.id.txt_title);
        txt_title.setText(getActivity().getResources().getString(R.string.app_name));
        Button btn_delete = (Button) this.getActivity().findViewById(R.id.btn_delete);
        TextView txt_delete_confirm = (TextView) this.getActivity().findViewById(R.id.txt_delete_confirm);
        btn_delete.setVisibility(View.GONE);
        txt_delete_confirm.setVisibility(View.GONE);

        layout_bottom = (RelativeLayout) v.findViewById(R.id.layout_bottom);
        autoHideview(4000);

        final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);

        // 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() {
            @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);
                        }
                    }
                }
            }
        });
        mGridView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                case OnScrollListener.SCROLL_STATE_IDLE:
                    if (loadMore) {
                        new HttpGetTask(1000L).execute();
                    }
                    break;
                case OnScrollListener.SCROLL_STATE_FLING:
                    break;
                case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    break;
                }
                hideBottomLayout();
                cancelAutoHideview();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.i(TAG, "firstVisibleItem:--->" + firstVisibleItem + "visibleItemCount:----->" + visibleItemCount
                        + "totalItemCount:" + totalItemCount);
                loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
                start = totalItemCount;
                autoShowview(2000);
            }
        });

        adview_first = (AdView) v.findViewById(R.id.adview_first); //TODO
        return v;
    }

    boolean loadMore = false;

    class HttpGetTask extends AsyncTask<String, Void, Void> {
        private Long time = -1L;

        public HttpGetTask(Long time) {
            this.time = time;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }

        @Override
        protected Void doInBackground(String... params) {
            Network.getInstance(getActivity()).getUrls(1, start, count);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mImageWorker.setExitTasksEarly(false);
        if (mAdapter != null && mAdapter.getCount() > 3) { //TODO  numColum:3
            mAdapter.notifyDataSetChanged();
        } else if (mAdapter.getCount() < 3) {
            new HttpGetTask(1000L).execute();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mImageWorker.setExitTasksEarly(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
        i.putExtra(ImageDetailActivity.EXTRA_IMAGE, (int) id);
        startActivity(i);
    }

    @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:
            final ImageCache cache = mImageWorker.getImageCache();
            if (cache != null) {
                mImageWorker.getImageCache().clearCaches();
                DiskLruCache.clearCache(getActivity(), ImageFetcher.HTTP_CACHE_DIR);
                Toast.makeText(getActivity(), R.string.clear_cache_complete, Toast.LENGTH_SHORT).show();
            }
            return true;
        //            case R.id.local:
        //               Intent intent = new Intent(this.getActivity(),LocalImageActivity.class);
        //               startActivity(intent);
        //                return true;
        case R.id.evaluate:
            launchMarket();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void launchMarket() {
        Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName());
        Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            startActivity(myAppLinkToMarket);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), " unable to find market app", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 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 = -1;
        private GridView.LayoutParams mImageViewLayoutParams;

        public ImageAdapter(Context context) {
            super();
            mContext = context;
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        @Override
        public int getCount() {
            // Size of adapter + number of columns for top empty row
            return mImageWorker.getAdapter().getSize() + mNumColumns;
        }

        @Override
        public Object getItem(int position) {
            return position < mNumColumns ? null : mImageWorker.getAdapter().getItem(position - mNumColumns);
        }

        @Override
        public long getItemId(int position) {
            return position < mNumColumns ? 0 : position - mNumColumns;
        }

        @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) {
            // First check if this is the top row
            if (position < mNumColumns) {
                if (convertView == null) {
                    convertView = new View(mContext);
                }
                // Calculate ActionBar height
                //                if (mActionBarHeight < 0) {
                //                    TypedValue tv = new TypedValue();
                //                    if (mContext.getTheme().resolveAttribute(
                //                            android.R.attr.actionBarSize, tv, true)) {
                //                        mActionBarHeight = TypedValue.complexToDimensionPixelSize(
                //                                tv.data, mContext.getResources().getDisplayMetrics());
                //                    } else {
                //                        // No ActionBar style (pre-Honeycomb or ActionBar not in theme)
                //                        mActionBarHeight = 0;
                //                    }
                //                }
                mActionBarHeight = 0;
                // Set empty view with height of ActionBar
                convertView.setLayoutParams(
                        new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));
                return convertView;
            }

            // Now handle the main ImageView thumbnails
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, instantiate and initialize
                imageView = new ImageView(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
            mImageWorker.loadImage(position - mNumColumns, imageView);
            return imageView;
        }

        /**
         * 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);
            mImageWorker.setImageSize(height);
            notifyDataSetChanged();
        }

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

        public int getNumColumns() {
            return mNumColumns;
        }
    }
}