com.github.jthuraisamy.yellowusage.ui.InvalidNetworkDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.github.jthuraisamy.yellowusage.ui.InvalidNetworkDialog.java

Source

/*
 * 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.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;

@SuppressLint("ValidFragment")
public class InvalidNetworkDialog extends DialogFragment {
    public static final String TAG = InvalidNetworkDialog.class.getSimpleName();

    private final String networkOperatorName;

    @SuppressLint("ValidFragment")
    public InvalidNetworkDialog(final String networkOperatorName) {
        this.networkOperatorName = networkOperatorName;
    }

    public static InvalidNetworkDialog create(final String networkOperatorName) {
        return new InvalidNetworkDialog(networkOperatorName);
    }

    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final MainActivity ctx = (MainActivity) getActivity();
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctx);

        // Set title and message.
        alertDialog.setTitle(R.string.invalid_network_title);
        alertDialog.setMessage(String.format(getString(R.string.invalid_network_message), networkOperatorName));

        // Set OnClickListener for negative button.
        alertDialog.setNegativeButton(R.string.action_exit, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ctx.finish();
            }
        });

        // Set OnClickListener for positive button.
        alertDialog.setPositiveButton(R.string.action_uninstall, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Uri packageURI = Uri.parse("package:com.github.jthuraisamy.yellowusage");
                Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
                startActivity(uninstallIntent);

                // Show the dialog again in case the user clicks cancel in the uninstall dialog.
                InvalidNetworkDialog invalidNetworkDialog = InvalidNetworkDialog.create(networkOperatorName);
                invalidNetworkDialog.show(getFragmentManager(), InvalidNetworkDialog.TAG);
            }
        });

        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) {
                final MainActivity ctx = (MainActivity) getActivity();
                ctx.finish();
            }
        });
    }
}