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.activities; import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.View; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.bydavy.card.receipts.R; import com.bydavy.card.receipts.fragments.ReceiptPagerFragment; import com.bydavy.card.receipts.fragments.PicturePickerDialogFragment.OnPictureChoseListener; import com.bydavy.card.receipts.fragments.StateFragment; import com.bydavy.card.receipts.fragments.StateFragment.StateFragmentListener; import com.bydavy.card.receipts.metrics.MetricsSherlockFragmentActivity; import com.bydavy.card.receipts.providers.Receipts; import com.bydavy.card.receipts.utils.AndroidUtils; import com.bydavy.card.receipts.utils.LogHelper; import com.bydavy.card.receipts.utils.LogHelper.ErrorID; public final class ReceiptPagerActivity extends MetricsSherlockFragmentActivity implements ReceiptPagerFragment.OnReceiptPageChangeListener, StateFragmentListener, OnPictureChoseListener { private static final int FRAGMENT_PAGER_FRAGMENT_ID = R.id.fragment_receipt_pager_container; public static final String INTENT_BUNDLE_INDEX = "index"; public static final String INTENT_BUNDLE_CURSOR_SELECTION = "selection"; public static final String INTENT_BUNDLE_CURSOR_SELECTION_ARGS = "selectionArgs"; public static final String INTENT_BUNDLE_CURSOR_SORT_ORDER = "sortOrder"; public static final String RESULT_INDEX = "pagerIndex"; private ReceiptPagerFragment mReceiptPagerFragment; private View mLoadingView; private View mContentView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receipt_pager); mLoadingView = findViewById(R.id.loading_container); mContentView = findViewById(R.id.content_container); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mReceiptPagerFragment = (ReceiptPagerFragment) getSupportFragmentManager() .findFragmentById(FRAGMENT_PAGER_FRAGMENT_ID); if (mReceiptPagerFragment == null) { final Bundle bundle = getIntent().getExtras(); if (bundle == null) { LogHelper.e(ErrorID.EXTRA_IS_NULL, "Attempt to open " + ReceiptPagerActivity.class.getName() + " without extras in intent", null); finish(); return; } // Load data from the intent final int index = bundle.getInt(INTENT_BUNDLE_INDEX); final String selection = bundle.getString(INTENT_BUNDLE_CURSOR_SELECTION); final String[] selectionArgs = bundle.getStringArray(INTENT_BUNDLE_CURSOR_SELECTION_ARGS); final String sortOrder = bundle.getString(INTENT_BUNDLE_CURSOR_SORT_ORDER); // Create the fragment mReceiptPagerFragment = ReceiptPagerFragment.newInstance(index, selection, selectionArgs, sortOrder); // Publish the fragment final FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); fm.add(FRAGMENT_PAGER_FRAGMENT_ID, mReceiptPagerFragment); fm.commit(); } mReceiptPagerFragment.setStateListener(this, true); // If Landscape && screen lize >= Large, finish the activity if ((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) && AndroidUtils.isScreenSizeBiggerOrEqual(getResources(), Configuration.SCREENLAYOUT_SIZE_LARGE)) { setActivityResult(); finish(); } } @Override public void onFragmentStateChanged(int newState) { if (newState == StateFragment.StateFragmentListener.STATE_COMPLETED) { mLoadingView.setVisibility(View.GONE); mContentView.setVisibility(View.VISIBLE); } } @Override public boolean onCreateOptionsMenu(Menu menu) { final MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.activity_receipt_pager, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: { final Intent intent = new Intent(this, ReceiptListActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; } case R.id.menu_edit: { final Intent intent = new Intent(this, ReceiptEditActivity.class); intent.putExtra(ReceiptEditActivity.INTENT_BUNDLE_RECEIPT_ID, mReceiptPagerFragment.getPageReceiptId()); startActivity(intent); return true; } case R.id.menu_delete: { final long id = mReceiptPagerFragment.getPageReceiptId(); final Uri uri = Uri.withAppendedPath(Receipts.CONTENT_URI, String.valueOf(id)); final int rows = getContentResolver().delete(uri, null, null); if (rows != 1) { LogHelper.e(ErrorID.CONTENT_PROVIDER, "Error when deleting a receipt (id:" + id + ", delete count: " + rows, null); } finish(); return true; } default: return super.onOptionsItemSelected(item); } } @Override public void onBackPressed() { setActivityResult(); super.onBackPressed(); } @Override public void onReceiptPageSelected(int index) { getSupportActionBar().setTitle(mReceiptPagerFragment.getPageTitle(index)); } private void setActivityResult() { final Intent intent = new Intent(); intent.putExtra(RESULT_INDEX, mReceiptPagerFragment.getPageIndex()); setResult(RESULT_OK, intent); } @Override public void onPictureChose(String path, int from) { final Fragment f = mReceiptPagerFragment.getPrimaryFragment(); if (f instanceof OnPictureChoseListener) { ((OnPictureChoseListener) f).onPictureChose(path, from); } // TODO Log not handled cases } }