Java tutorial
/* * Copyright (C) 2015 Jackson Thuraisamy. * * 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 3 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, see <http://www.gnu.org/licenses/>. */ package com.github.jthuraisamy.yellowusage.ui; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import com.github.jthuraisamy.yellowusage.R; import com.github.jthuraisamy.yellowusage.receivers.CallListener; import com.github.jthuraisamy.yellowusage.utils.OverageFeesHelper; import com.github.jthuraisamy.yellowusage.utils.PreferenceHelper; @SuppressLint("ValidFragment") public class VoiceOverageOutgoingCallWarningDialog extends DialogFragment { private final String phoneNumber; @SuppressLint("ValidFragment") public VoiceOverageOutgoingCallWarningDialog(final String phoneNumber) { this.phoneNumber = phoneNumber; } @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { Context context = getActivity(); float totalOverageFees = OverageFeesHelper.calculateTotal(context); float peakTimeVoiceOverageFeePricePerMin = PreferenceHelper.getPeakTimeVoiceOverageFeePricePerMin(context); AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); // Set message. String alertMessage = String.format(getString(R.string.voice_overage_alert_message), totalOverageFees, peakTimeVoiceOverageFeePricePerMin); alertDialog.setMessage(alertMessage); // Set OnClickListener for negative button. alertDialog.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { getActivity().finish(); } }); // Set OnClickListener for positive button. alertDialog.setPositiveButton(R.string.action_continue, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Uri uri = Uri.fromParts("tel", phoneNumber + CallListener.IGNORE_SUFFIX, null); Intent intent = new Intent("android.intent.action.CALL", uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); getActivity().finish(); } }); return alertDialog.create(); } @Override public void onResume() { super.onResume(); final AlertDialog alertDialog = (AlertDialog) getDialog(); // Set OnCancelListener. alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialogInterface) { getActivity().finish(); } }); } }