Java tutorial
/* * Copyright (C) 2012 Davy Leggieri * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.bydavy.card.receipts.fragments; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.bydavy.card.receipts.R; import com.bydavy.card.receipts.fragments.PicturePickerDialogFragment.OnPictureChoseListener; import com.bydavy.card.receipts.providers.Receipts; import com.bydavy.card.receipts.providers.access.ReceiptProviderAccess; import com.bydavy.card.receipts.ui.BitmapWorkerTask; import com.bydavy.card.receipts.utils.LogHelper; import com.bydavy.card.receipts.utils.LogHelper.ErrorID; public class PreviewPictureFragment extends StateSherlockFragment { /** Change first statement of if condition to enable class debug **/ private static final boolean DEBUG = LogHelper.DEBUG ? false : LogHelper.DEFAULT_DEBUG; private static final String DEBUG_PREFIX = PreviewPictureFragment.class.getName(); private static final String ARG_RECEIPT_ID = "receiptId"; private static final int CURSOR_LOADER_RECEIPT = 0; private static final String INSERT_PICTURE_DIALOG_FRAGMENT_TAG = "insertPictureDialog"; private final MyReceiptLoader mLoaderCallBack = new MyReceiptLoader(); private ImageView mPicture; private String mPicturePath; private BitmapWorkerTask mViewPictureWorker; public static PreviewPictureFragment newInstance(long receiptId) { if (receiptId <= 0) { throw new IllegalArgumentException("receipt id must be > 0"); } final PreviewPictureFragment f = new PreviewPictureFragment(); final Bundle args = new Bundle(); args.putLong(ARG_RECEIPT_ID, receiptId); f.setArguments(args); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_preview_picture, null); mPicture = (ImageView) v; return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Load the receipt in a dedicated thread final Bundle b = mLoaderCallBack.createBundle(getReceiptId()); getLoaderManager().initLoader(CURSOR_LOADER_RECEIPT, b, mLoaderCallBack); } /* * Retrieve the receipt id currently displayed. * * This method is thread safe and can be invoked right after fragments * instantiation * * @return shown receipt id or -1 if no receipt id was set */ public long getReceiptId() { final Bundle args = getArguments(); if (args != null) { return args.getLong(ARG_RECEIPT_ID); } return -1; } /* * Must be executed by the UIThread */ private void updateView(Cursor c) { final boolean hasReceipt = ((c != null) && c.moveToFirst()); if (hasReceipt) { final int picturePathColumnIndex = c.getColumnIndex(Receipts.COLUMN_NAME_PICTURE_PATH); mPicturePath = c.getString(picturePathColumnIndex); final Display display = getActivity().getWindowManager().getDefaultDisplay(); // Cancel picture task if (mViewPictureWorker != null) { mViewPictureWorker.cancel(true); } mViewPictureWorker = new BitmapWorkerTask(mPicture, display.getWidth(), display.getHeight()) { @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (getState() != StateFragmentListener.STATE_COMPLETED) { setState(StateFragmentListener.STATE_COMPLETED); } } }; mViewPictureWorker.execute(mPicturePath); } } private final class MyReceiptLoader implements LoaderCallbacks<Cursor> { private static final String BUNDLE_RECEIPT_ID = "receiptId"; /* Not possible to create a static method =0/ */ public Bundle createBundle(long receiptId) { final Bundle bundle = new Bundle(); bundle.putLong(BUNDLE_RECEIPT_ID, receiptId); return bundle; } /* Dedicated Thread */ @Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { switch (id) { case CURSOR_LOADER_RECEIPT: { if (bundle == null) { throw new IllegalArgumentException("Must set a bundle"); } final long receiptId = bundle.getLong(BUNDLE_RECEIPT_ID); if (receiptId <= 0L) { throw new IllegalArgumentException("Must set " + BUNDLE_RECEIPT_ID); } final Uri uriSingleReceipt = Uri.withAppendedPath(Receipts.CONTENT_URI, String.valueOf(receiptId)); return new CursorLoader(getActivity(), uriSingleReceipt, null, null, null, null); } default: throw new IllegalArgumentException("The cursor loader " + id + " doesn't exist"); } } /* UI Thread */ @Override public void onLoadFinished(Loader<Cursor> cl, final Cursor cursor) { updateView(cursor); } /* UI Thread */ @Override public void onLoaderReset(Loader<Cursor> cl) { } } public void delete() { ReceiptProviderAccess.updatePicture(getActivity(), getReceiptId(), null, mPicturePath, false); } public void edit() { final FragmentTransaction ft = getFragmentManager().beginTransaction(); final Fragment prev = getFragmentManager().findFragmentByTag(INSERT_PICTURE_DIALOG_FRAGMENT_TAG); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); final PicturePickerDialogFragment dialog = PicturePickerDialogFragment.newInstance(); dialog.show(ft, INSERT_PICTURE_DIALOG_FRAGMENT_TAG); } public void onPictureChose(String path, int from) { if ((from != OnPictureChoseListener.GALLERY) && (from != OnPictureChoseListener.TEMPORARY)) { LogHelper.e(ErrorID.IMPLEMENTATION, "A new value was added to " + OnPictureChoseListener.class.getName() + " from, and it's not handled.", null); return; } mPicturePath = ReceiptProviderAccess.updatePicture(getActivity(), getReceiptId(), path, mPicturePath, from != OnPictureChoseListener.GALLERY); } }