package epson.print;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.p000v4.app.ActivityCompat;

import java.io.Serializable;
import java.util.ArrayList;

import epson.print.Util.EPLog;

public class ActivityRequestPermissions extends Activity {
    private static final int APPLICATION_DETAILS_SETTINGS = 0;
    private static final int ID_ENABLED_PERMISSION = 1;
    private static final String KEY_PERMISSIONS = "KEY_PERMISSIONS";
    private static final String TAG = "ActivityRequestPermission";
    Permission mPermissionObject;

    /* access modifiers changed from: protected */
    @TargetApi(23)
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Intent intent = getIntent();
        if (intent != null) {
            mPermissionObject = (Permission) intent.getSerializableExtra(KEY_PERMISSIONS);
            requestPermissions(mPermissionObject.getPermissionKey(), 1);
            return;
        }
        finish();
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 1) {
            int i2 = 0;
            for (int i3 = 0; i3 < strArr.length; i3++) {
                if (mPermissionObject.getPermissionKey(i3).equals(strArr[i3])) {
                    if (iArr[i3] == 0) {
                        EPLog.d(TAG, "PERMISSION_GRANTED " + strArr[i3]);
                        i2++;
                    } else if (!ActivityCompat.shouldShowRequestPermissionRationale(this, strArr[i3])) {
                        PermisionAlertDialog(strArr[i3], true, mPermissionObject.getDialogTitle(i3, 1), mPermissionObject.getDialogMessage(i3, 1));
                    } else {
                        PermisionAlertDialog(strArr[i3], false, mPermissionObject.getDialogTitle(i3, 0), mPermissionObject.getDialogMessage(i3, 0));
                    }
                }
            }
            if (i2 == strArr.length) {
                setResult(-1);
                finish();
            }
        }
    }

    /* access modifiers changed from: protected */
    @TargetApi(23)
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i == 0) {
            requestPermissions(mPermissionObject.getPermissionKey(), 1);
        }
        super.onActivityResult(i, i2, intent);
    }

    @TargetApi(23)
    public static boolean checkPermission(Context context, String[] strArr) {
        for (String checkSelfPermission : strArr) {
            if (context.checkSelfPermission(checkSelfPermission) != 0) {
                return false;
            }
        }
        return true;
    }

    public static void requestPermission(Activity activity, Permission permission, int i) {
        Intent intent = new Intent(activity, ActivityRequestPermissions.class);
        intent.putExtra(KEY_PERMISSIONS, permission);
        activity.startActivityForResult(intent, i);
    }

    public static void requestPermission(Fragment fragment, Permission permission, int i) {
        Intent intent = new Intent(fragment.getActivity(), ActivityRequestPermissions.class);
        intent.putExtra(KEY_PERMISSIONS, permission);
        fragment.startActivityForResult(intent, i);
    }

    private void startApplicationManagementActivity(String str) {
        Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.parse("package:" + str));
        startActivityForResult(intent, 0);
    }

    public static boolean isRuntimePermissionSupported() {
        return Build.VERSION.SDK_INT >= 23;
    }

    private void PermisionAlertDialog(final String str, final boolean z, String str2, String str3) {
        new AlertDialog.Builder(this).setCancelable(false).setTitle(str2).setMessage(str3).setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
            @TargetApi(23)
            public void onClick(DialogInterface dialogInterface, int i) {
                if (z) {
                    ActivityRequestPermissions activityRequestPermissions = ActivityRequestPermissions.this;
                    activityRequestPermissions.startApplicationManagementActivity(activityRequestPermissions.getPackageName());
                    return;
                }
                ActivityRequestPermissions.this.requestPermissions(new String[]{str}, 1);
            }
        }).setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                ActivityRequestPermissions.this.finish();
            }
        }).show();
    }

    public static class Permission implements Serializable {
        static final int DIALOG_MESSAGE = 2;
        static final int DIALOG_TITLE = 1;
        static final int PARAMETER_SIZE = 3;
        static final int PERMISSION_KEY = 0;
        private ArrayList<String[][]> mArrayPermission = new ArrayList<>();

        public Permission(String str, @Nullable String[] strArr, @Nullable String[] strArr2) {
            addPermission(str, strArr, strArr2);
        }

        public void addPermission(String str, @Nullable String[] strArr, @Nullable String[] strArr2) {
            mArrayPermission.add(new String[][]{new String[]{str}, strArr, strArr2});
        }

        public ArrayList<String[][]> getArrayPermission() {
            return mArrayPermission;
        }

        public int getPermissionSize() {
            return mArrayPermission.size();
        }

        public String[] getPermissionKey() {
            int size = mArrayPermission.size();
            String[] strArr = new String[size];
            for (int i = 0; i < size; i++) {
                strArr[i] = getPermissionKey(i);
            }
            return strArr;
        }

        public String getPermissionKey(int i) {
            try {
                return mArrayPermission.get(i)[0][0];
            } catch (NullPointerException unused) {
                return null;
            }
        }

        public String getDialogTitle(int i, int i2) {
            try {
                return mArrayPermission.get(i)[1][i2];
            } catch (NullPointerException unused) {
                return null;
            }
        }

        public String getDialogMessage(int i, int i2) {
            try {
                return mArrayPermission.get(i)[2][i2];
            } catch (NullPointerException unused) {
                return null;
            }
        }
    }

    public static class DialogParameter {
        public static String setMessage2(Fragment fragment, String str) {
            return setMessage2(fragment.getContext(), str);
        }

        public static String setMessage3A(Fragment fragment, String str, String str2) {
            return setMessage3A(fragment.getContext(), str, str2);
        }

        public static String setMessage2(Context context, String str) {
            return getMessageTop(context, str) + "\n \n" + context.getString(R.string.permission_dialog_message_bottom);
        }

        public static String setMessage3A(Context context, String str, String str2) {
            return getMessageTop(context, str) + "\n \n" + String.format(context.getString(R.string.permission_dialog_message_prompt_setting), new Object[]{str2});
        }

        private static String getMessageTop(Context context, String str) {
            return context.getString(R.string.permission_dialog_message_top) + "\n \n" + str;
        }
    }
}
