Java tutorial
/* * Copyright (C) 2013 Fabien Barbero Permission is hereby granted, free of * charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, subject to * the following conditions: The above copyright notice and this permission * notice shall be included in all copies or substantial portions of the * Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package com.pickr.activities; import java.util.LinkedList; import java.util.List; import android.app.ActionBar; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Handler.Callback; import android.os.Message; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; import com.pickr.R; import com.pickr.entites.PhotoInformations; import com.pickr.error.ErrorDisplayer; import com.pickr.fragments.PhotoFragment; import com.pickr.tasks.PhotoInfosLoader; import com.pickr.tasks.SavePhotoTask; import com.pickr.ui.adapters.PhotosPagerAdapter; import com.pickr.ui.dialogs.CommentsDialog; import com.pickr.ui.dialogs.ExifDialog; import com.pickr.ui.dialogs.PhotoInfosDialog; import org.ez.flickr.api.entities.Paginated; import org.ez.flickr.api.entities.Photo; import org.ez.flickr.api.entities.PhotoInfos; import org.ez.flickr.api.entities.PhotoLocation; public abstract class PhotosPagerActivity extends FragmentActivity implements OnPageChangeListener, Callback { protected ViewPager mPager; private List<PhotoFragment> mFragments; private int mStartIndex = -1; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(this); requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_photos_pager); ActionBar actionBar = getActionBar(); actionBar.setTitle(null); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99333333"))); mPager = (ViewPager) findViewById(R.id.photosPager); mPager.setOnPageChangeListener(this); mPager.setPageMargin(10); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState != null) { mStartIndex = savedInstanceState.getInt("pager.position"); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (outState != null && mPager != null) { outState.putInt("pager.position", mPager.getCurrentItem()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_photo, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; case R.id.photoInfosItem: PhotoFragment fragment = mFragments.get(mPager.getCurrentItem()); new PhotoInfosLoader(mHandler, fragment.getPhoto()).execute(); return true; case R.id.photoCommentsItem: fragment = mFragments.get(mPager.getCurrentItem()); Photo photo = fragment.getPhoto(); CommentsDialog dialog = new CommentsDialog(photo, this); dialog.show(); return true; case R.id.photoSaveItem: new SavePhotoTask(mFragments.get(mPager.getCurrentItem()).getPhoto(), false, mHandler, this).execute(); return true; case R.id.photoSetWallpaperItem: // new SavePhotoTask(mFragments.get(mPager.getCurrentItem()).getPhoto(), true, mHandler, this).execute(); return true; case R.id.photoStatsItem: return true; case R.id.photoTechnicalInfosItem: fragment = mFragments.get(mPager.getCurrentItem()); photo = fragment.getPhoto(); new ExifDialog(photo, this).show(); return true; } return super.onOptionsItemSelected(item); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageSelected(int position) { if (mFragments != null) { getActionBar().setTitle(mFragments.get(position).getPhoto().getTitle()); } } protected void showPhoto(Photo photo) { if (mFragments != null) { if (mStartIndex != -1) { // Dirty! mPager.setCurrentItem(mStartIndex); mStartIndex = -1; } else { for (int i = 0; i < mFragments.size(); i++) { if (mFragments.get(i).getPhoto().equals(photo)) { mPager.setCurrentItem(i); return; } } } } } protected void setPhotos(Paginated<Photo> photos) { mFragments = new LinkedList<PhotoFragment>(); PhotoFragment fragment; Bundle args; for (Photo photo : photos) { args = new Bundle(); args.putSerializable(PhotoFragment.PARAM_PHOTO, photo); fragment = new PhotoFragment(); fragment.setArguments(args); mFragments.add(fragment); } mPager.setAdapter(new PhotosPagerAdapter(getSupportFragmentManager(), mFragments)); mPager.setCurrentItem(mStartIndex); } @Override public boolean handleMessage(Message msg) { switch (msg.what) { case SavePhotoTask.PHOTO_SAVED: Toast.makeText(this, R.string.photo_saved, Toast.LENGTH_LONG).show(); return true; case SavePhotoTask.PHOTO_SAVE_ERROR: ErrorDisplayer.displayToast(msg, this); return true; case PhotoInfosLoader.PHOTO_INFOS_LOADED: PhotoInformations infos = (PhotoInformations) msg.obj; PhotoInfosDialog dialog = new PhotoInfosDialog(infos, this) { @Override protected void onShowLocationMap(PhotoLocation location, PhotoInfos infos) { Uri uri = Uri.parse("geo:0,0?q=" + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()) + "(" + infos.getTitle() + ")"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } } }; dialog.show(); } return false; } }