am.roadpolice.roadpolice.dialogs.DialogRateUs.java Source code

Java tutorial

Introduction

Here is the source code for am.roadpolice.roadpolice.dialogs.DialogRateUs.java

Source

/*
 * Copyright (C) 2015 Victor Apoyan.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package am.roadpolice.roadpolice.dialogs;

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.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import am.roadpolice.roadpolice.R;
import am.roadpolice.roadpolice.utils.PreferenceUtils;

public class DialogRateUs extends DialogFragment {

    private final static String APP_PNAME = "am.roadpolice.roadpolice"; // Package Name

    private final static int DAYS_UNTIL_PROMPT = 3; // Min number of days
    private final static int LAUNCHES_UNTIL_PROMPT = 3; // Min number of launches

    public static void showRateUs(final Context context, final FragmentManager fragmentManager) {
        final boolean dontShowAgain = PreferenceUtils.getDataBool(context, PreferenceUtils.KEY_DO_NOT_SHOW_AGAIN,
                false);
        if (dontShowAgain)
            return;

        // Increment launch counter
        long launch_count = PreferenceUtils.getDataLong(context, PreferenceUtils.KEY_LAUNCH_COUNT, 0) + 1;
        PreferenceUtils.storeDataLong(context, PreferenceUtils.KEY_LAUNCH_COUNT, launch_count);

        // Get date of first launch
        Long date_firstLaunch = PreferenceUtils.getDataLong(context, PreferenceUtils.KEY_DATA_OF_FIRST_LAUNCH, 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            PreferenceUtils.storeDataLong(context, PreferenceUtils.KEY_DATA_OF_FIRST_LAUNCH, date_firstLaunch);
        }

        // Wait at least n days before opening
        if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
            if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
                showDialog(fragmentManager);
            }
        }
    }

    private static void showDialog(final FragmentManager fragmentManager) {

        if (fragmentManager == null)
            return;

        DialogRateUs dialog = (DialogRateUs) fragmentManager.findFragmentByTag("rate_us");
        if (dialog == null)
            dialog = new DialogRateUs();

        dialog.setCancelable(false);
        // Start showing progress dialog fragment.
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(dialog, "rate_us");
        transaction.commitAllowingStateLoss();
    }

    @SuppressWarnings("NullableProblems")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.txtRateUsTitle))
                .setMessage(getString(R.string.txtRateUsMessage))
                .setPositiveButton(R.string.txtRate, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(
                                new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
                    }
                }).setNegativeButton(R.string.txtRateNo, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PreferenceUtils.storeDataBool(getActivity(), PreferenceUtils.KEY_DO_NOT_SHOW_AGAIN, true);
                    }
                }).setNeutralButton(R.string.txtRateLater, null).create();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onDestroy() {
        Dialog dialog = getDialog();
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();

        super.onDestroy();
    }

}