Java tutorial
/* This file is part of PodemosQuotes. PodemosQuotes 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 3 of the License, or (at your option) any later version. PodemosQuotes 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 PodemosQuotes. If not, see <http://www.gnu.org/licenses/> */ package com.jjoseba.podemosquotes.fragment; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.DialogFragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.jjoseba.podemosquotes.R; import com.jjoseba.podemosquotes.application.SoundManager; import com.jjoseba.podemosquotes.model.Quote; import com.jjoseba.podemosquotes.model.QuoteAuthor; import com.jjoseba.podemosquotes.utils.ShareUtils; import com.squareup.picasso.Picasso; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import butterknife.Bind; import butterknife.BindString; import butterknife.ButterKnife; import butterknife.OnClick; public class QuoteDialog extends DialogFragment { public static String TAG = QuoteDialog.class.getName(); public static String DIALOG_QUOTE = "dialogQuote"; private static String TEMP_FILENAME = "temp"; private static final Uri AUDIO_PROVIDER = Uri.parse("content://com.jjoseba.podemosquotes.audioprovider/sounds"); private Quote quote; private boolean soundPlayed; @Bind(R.id.quoteText) TextView quoteText; @Bind(R.id.quoteImage) ImageView quoteImage; @Bind(R.id.authorName) TextView authorName; @Bind(R.id.authorPosition) TextView authorPosition; @Bind(R.id.fabShare) FloatingActionButton shareButton; @Bind(R.id.fabPlay) FloatingActionButton playButton; @BindString(R.string.share_audio) String shareAudioString; @BindString(R.string.share_quote) String shareQuoteString; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); Bundle args = getArguments(); quote = (Quote) args.getSerializable(DIALOG_QUOTE); soundPlayed = false; builder.setView(inflater.inflate(R.layout.fragment_quote_dialog, null)); return builder.create(); } @Override public void onDismiss(DialogInterface dialog) { if (soundPlayed) { SoundManager.getInstance(getActivity()).stopCurrentSound(); } super.onDismiss(dialog); } @Override public void onStart() { super.onStart(); final AlertDialog d = (AlertDialog) getDialog(); if (d == null) return; ButterKnife.bind(this, d); quoteText.setText(quote.getQuote()); authorName.setText(quote.getAuthor().getName()); Picasso.with(this.getActivity()).load(QuoteAuthor.imagesBasePath + quote.getAuthor().getImagePath()) .placeholder(R.drawable.quote_placeholder).error(R.drawable.error_sad).into(quoteImage); String position = quote.getAuthor().getPosition(); if ((position != null) && !position.equals("")) authorPosition.setText(quote.getAuthor().getPosition()); else authorPosition.setVisibility(View.GONE); } @OnClick(R.id.fabShare) public void shareButtonClicked(View v) { final CharSequence[] items = { shareAudioString, shareQuoteString }; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Intent share = (item == 0 ? ShareUtils.constructShareAudioIntent(getActivity(), quote) : ShareUtils.constructShareTextIntent(getActivity(), quote)); startActivity(Intent.createChooser(share, items[item])); dialog.dismiss(); } }).show(); } @OnClick(R.id.fabPlay) public void playButtonClicked(View v) { soundPlayed = true; SoundManager.getInstance(getActivity()).playSound(quote.getSoundPath()); } }