com.hengtan.nanodegreeapp.stocount.ProductDetailGridPagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.hengtan.nanodegreeapp.stocount.ProductDetailGridPagerAdapter.java

Source

/*
 * Copyright (C) 2014 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.hengtan.nanodegreeapp.stocount;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.support.wearable.view.CardFragment;
import android.support.wearable.view.FragmentGridPagerAdapter;
import android.support.wearable.view.GridPagerAdapter;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Constructs fragments as requested by the GridViewPager. For each row a different background is
 * provided.
 * <p>
 * Always avoid loading resources from the main thread. In this sample, the background images are
 * loaded from an background task and then updated using {@link #notifyRowBackgroundChanged(int)}
 * and {@link #notifyPageBackgroundChanged(int, int)}.
 */
public class ProductDetailGridPagerAdapter extends FragmentGridPagerAdapter {
    private static final int TRANSITION_DURATION_MILLIS = 100;

    private final Context mContext;
    private List<Row> mRows;
    private Row row;
    private ColorDrawable mDefaultBg;
    private ColorDrawable mClearBg;

    private int mProductId;
    private String mProductName;
    private Integer mProductCountId;
    private String mAdditionalInfo;
    private double mCurrentCount;
    private Bitmap mThumbnail;
    private CustomFragment customeFrag;
    CardFragment cardFragment;

    public ProductDetailGridPagerAdapter(Context ctx, FragmentManager fm, CustomFragment cf, int id, String name,
            String additionalInfo, Integer prodCountId, double count, Bitmap image) {
        super(fm);
        mContext = ctx;
        mProductId = id;
        mProductName = name;
        mAdditionalInfo = additionalInfo;
        mProductCountId = prodCountId;
        mCurrentCount = count;
        mThumbnail = image;
        mRows = new ArrayList<Row>();

        customeFrag = cf;

        Bundle bundle = new Bundle();
        bundle.putInt("productId", mProductId);
        bundle.putInt("productCountId", mProductCountId);

        customeFrag.setArguments(bundle);

        CreateCardFragment(mProductName, GetCardFragmentDescription());

        row = new Row(cardFragment, customeFrag);
        mRows.add(row);

        /*
        mRows.add(new Row(cardFragment(R.string.welcome_title, R.string.welcome_text)));
        mRows.add(new Row(cardFragment(R.string.about_title, R.string.about_text)));
        mRows.add(new Row(
            cardFragment(R.string.cards_title, R.string.cards_text),
            cardFragment(R.string.expansion_title, R.string.expansion_text)));
        mRows.add(new Row(
            cardFragment(R.string.backgrounds_title, R.string.backgrounds_text),
            cardFragment(R.string.columns_title, R.string.columns_text)));
        mRows.add(new Row(new CustomFragment()));
        mRows.add(new Row(cardFragment(R.string.dismiss_title, R.string.dismiss_text)));
        */

        mDefaultBg = new ColorDrawable(mContext.getResources().getColor(android.R.color.darker_gray));
        mClearBg = new ColorDrawable(mContext.getResources().getColor(android.R.color.transparent));
    }

    LruCache<Integer, Drawable> mRowBackgrounds = new LruCache<Integer, Drawable>(3) {
        @Override
        protected Drawable create(final Integer row) {

            //int resid = BG_IMAGES[row % BG_IMAGES.length];
            int resid = R.mipmap.ic_launcher;

            new DrawableLoadingTask(mContext) {
                @Override
                protected void onPostExecute(Drawable result) {
                    TransitionDrawable background = new TransitionDrawable(new Drawable[] { mDefaultBg, result });
                    mRowBackgrounds.put(row, background);
                    notifyRowBackgroundChanged(row);
                    background.startTransition(TRANSITION_DURATION_MILLIS);
                }
            }.execute(resid);
            return mDefaultBg;
        }
    };

    LruCache<Point, Drawable> mPageBackgrounds = new LruCache<Point, Drawable>(3) {
        @Override
        protected Drawable create(final Point page) {
            // place bugdroid as the background at row 2, column 1
            if (page.y == 2 && page.x == 1) {
                int resid = R.mipmap.ic_launcher;
                new DrawableLoadingTask(mContext) {
                    @Override
                    protected void onPostExecute(Drawable result) {
                        TransitionDrawable background = new TransitionDrawable(new Drawable[] { mClearBg, result });
                        mPageBackgrounds.put(page, background);
                        notifyPageBackgroundChanged(page.y, page.x);
                        background.startTransition(TRANSITION_DURATION_MILLIS);
                    }
                }.execute(resid);
            }
            return GridPagerAdapter.BACKGROUND_NONE;
        }
    };

    private String GetCardFragmentDescription() {
        return mAdditionalInfo + "\n StoCount : " + Double.toString(mCurrentCount);
    }

    private void CreateCardFragment(String titleRes, String textRes) {
        Resources res = mContext.getResources();
        cardFragment = CardFragment.create(titleRes, textRes);
        // Add some extra bottom margin to leave room for the page indicator
        cardFragment.setCardMarginBottom(res.getDimensionPixelSize(R.dimen.card_margin_bottom));
    }

    /*static final int[] BG_IMAGES = new int[] {
        R.drawable.debug_background_1,
        R.drawable.debug_background_2,
        R.drawable.debug_background_3,
        R.drawable.debug_background_4,
        R.drawable.debug_background_5
    };*/

    /** A convenient container for a row of fragments. */
    private class Row {
        final List<Fragment> columns = new ArrayList<Fragment>();

        public Row(Fragment... fragments) {
            for (Fragment f : fragments) {
                add(f);
            }
        }

        public void add(Fragment f) {
            columns.add(f);
        }

        Fragment getColumn(int i) {
            return columns.get(i);
        }

        public int getColumnCount() {
            return columns.size();
        }
    }

    @Override
    public Fragment getFragment(int row, int col) {
        Row adapterRow = mRows.get(row);
        return adapterRow.getColumn(col);
    }

    @Override
    public Drawable getBackgroundForRow(final int row) {

        if (mThumbnail != null) {
            return new BitmapDrawable(mContext.getResources(), mThumbnail);
        } else {
            return mRowBackgrounds.get(row);
        }
    }

    @Override
    public Drawable getBackgroundForPage(final int row, final int column) {
        return mPageBackgrounds.get(new Point(column, row));
    }

    @Override
    public int getRowCount() {
        return mRows.size();
    }

    @Override
    public int getColumnCount(int rowNum) {
        return mRows.get(rowNum).getColumnCount();
    }

    public void UpdateQuantity(int pid, double quantity) {
        if (this.mProductId == pid && this.mCurrentCount != quantity) {
            this.mCurrentCount = quantity;

            CreateCardFragment(mProductName, GetCardFragmentDescription());

            row = new Row(cardFragment, customeFrag);

            mRows.set(0, row);

            notifyDataSetChanged();
        }
    }

    class DrawableLoadingTask extends AsyncTask<Integer, Void, Drawable> {
        private static final String TAG = "Loader";
        private Context context;

        DrawableLoadingTask(Context context) {
            this.context = context;
        }

        @Override
        protected Drawable doInBackground(Integer... params) {
            Log.d(TAG, "Loading asset 0x" + Integer.toHexString(params[0]));
            return context.getResources().getDrawable(params[0]);
        }
    }
}