package com.epson.mobilephone.common.wifidirect;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;

import java.lang.ref.WeakReference;

public class ActivityRequestLocationPermission extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    public static final int ACCESS_BLE = 4;
    public static final int ACCESS_SIMPLEPAP = 1;
    public static final int ACCESS_WIFI = 1;
    public static final int ACCESS_WIFIP2P = 16;
    private static final String DONOTASKAGAIN = "DONOTASKAGAIN";
    private static final int ID_CHECK_LOCATION_SETTINGS = 1;
    private static final int ID_ENABLED_LOCATION_SETTINGS = 1;
    private static final int ID_ENABLED_PERMISSION = 1;
    private static final int ID_RECHECK_LOCATION_SETTINGS = 2;
    private static final int ID_RESULT_APPSETTINGS = 3;
    private static final String INTENT_FORCE_REQUEST = "INTENT_FORCE_REQUEST";
    private static final String REJECTED_REQUEST = "REJECTED_REQUEST";
    private static final String TAG = "ActivityRequestLocationPermission";
    private Handler mHandler = new innerHandler(this);
    private String requestPermission;

    public void onConnected(@Nullable Bundle bundle) {
    }

    public void onConnectionSuspended(int i) {
    }

    /* access modifiers changed from: protected */
    @SuppressLint({"NewApi"})
    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        Intent intent = getIntent();
        boolean booleanExtra = intent != null ? intent.getBooleanExtra(INTENT_FORCE_REQUEST, false) : false;
        if (!isRejectedRequestPermission() || booleanExtra) {
            requestWindowFeature(1);
            if (isNeedLocationPermission(this)) {
                if (!hasLocationPermission(this)) {
                    requestPermission = getNeededPermissionString(this);
                    final boolean z = getPreferences(0).getBoolean(DONOTASKAGAIN, false);
                    new AlertDialog.Builder(this).setCustomTitle(getTitleLayout(this, R.string.str_need_location_setting_title)).setMessage(R.string.str_need_location_permission).setPositiveButton(R.string.str_next, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (!z) {
                                ActivityRequestLocationPermission activityRequestLocationPermission = ActivityRequestLocationPermission.this;
                                activityRequestLocationPermission.requestPermissions(new String[]{activityRequestLocationPermission.requestPermission}, 1);
                            } else {
                                ActivityRequestLocationPermission.this.startActivityForResult(new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.parse("package:" + ActivityRequestLocationPermission.this.getPackageName())), 3);
                            }
                            dialogInterface.dismiss();
                        }
                    }).setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ActivityRequestLocationPermission.this.setRejectRequestPermission();
                            dialogInterface.dismiss();
                            ActivityRequestLocationPermission.this.onError();
                        }
                    }).setCancelable(false).create().show();
                    return;
                }
                mHandler.sendEmptyMessage(1);
            } else if (Build.VERSION.SDK_INT > 28) {
                mHandler.sendEmptyMessage(1);
            } else {
                onSuccess();
            }
        } else {
            onError();
        }
    }

    /* access modifiers changed from: private */
    @NonNull
    public static LinearLayout getTitleLayout(Context context, int i) {
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
        layoutParams.setMargins(40, 25, 40, 25);
        TextView textView = new TextView(context);
        textView.setText(context.getResources().getText(i));
        textView.setTextSize(17.0f);
        textView.setMaxLines(5);
        textView.setLayoutParams(layoutParams);
        linearLayout.addView(textView);
        return linearLayout;
    }

    private boolean isRejectedRequestPermission() {
        ComponentName callingActivity = getCallingActivity();
        if (callingActivity != null) {
            SharedPreferences preferences = getPreferences(0);
            if (preferences.getBoolean("REJECTED_REQUEST_" + callingActivity.getClassName(), false)) {
                return true;
            }
        }
        return false;
    }

    private void setRejectRequestPermission() {
        ComponentName callingActivity = getCallingActivity();
        if (callingActivity != null) {
            SharedPreferences.Editor edit = getPreferences(0).edit();
            edit.putBoolean("REJECTED_REQUEST_" + callingActivity.getClassName(), true);
            edit.apply();
        }
    }

    @SuppressLint({"NewApi"})
    public void onRequestPermissionsResult(int i, @NonNull String[] strArr, @NonNull int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 1) {
            int i2 = 0;
            while (i2 < strArr.length) {
                if (!this.requestPermission.equals(strArr[i2]) || iArr[i2] != 0) {
                    i2++;
                } else {
                    EPLog.d(TAG, "PERMISSION_GRANTED " + requestPermission);
                    mHandler.sendEmptyMessage(1);
                    return;
                }
            }
            if (!shouldShowRequestPermissionRationale(requestPermission)) {
                SharedPreferences.Editor edit = getPreferences(0).edit();
                edit.putBoolean(DONOTASKAGAIN, true);
                edit.apply();
            }
            onError();
        }
    }

    private static class innerHandler extends Handler {
        private final WeakReference<ActivityRequestLocationPermission> mActivity;

        innerHandler(ActivityRequestLocationPermission activityRequestLocationPermission) {
            mActivity = new WeakReference<>(activityRequestLocationPermission);
        }

        /* JADX WARNING: Code restructure failed: missing block: B:2:0x0009, code lost:
            r4 = (com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission) r3.mActivity.get();
         */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void handleMessage(android.os.Message r4) {
            /*
                r3 = this;
                super.handleMessage(r4)
                int r4 = r4.what
                r0 = 1
                if (r4 == r0) goto L_0x0009
                goto L_0x0054
            L_0x0009:
                java.lang.ref.WeakReference<com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission> r4 = r3.mActivity
                java.lang.Object r4 = r4.get()
                com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission r4 = (com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission) r4
                if (r4 == 0) goto L_0x0054
                boolean r0 = com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission.isNeedChangeLocationSettings(r4)
                if (r0 == 0) goto L_0x0051
                android.app.AlertDialog$Builder r0 = new android.app.AlertDialog$Builder
                r0.<init>(r4)
                int r1 = com.epson.mobilephone.common.wifidirect.R.string.str_need_location_setting_title
                android.widget.LinearLayout r1 = com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission.getTitleLayout(r4, r1)
                android.app.AlertDialog$Builder r0 = r0.setCustomTitle(r1)
                int r1 = com.epson.mobilephone.common.wifidirect.R.string.str_need_location_setting
                android.app.AlertDialog$Builder r0 = r0.setMessage(r1)
                int r1 = com.epson.mobilephone.common.wifidirect.R.string.str_next
                com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission$innerHandler$2 r2 = new com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission$innerHandler$2
                r2.<init>(r4)
                android.app.AlertDialog$Builder r0 = r0.setPositiveButton(r1, r2)
                int r1 = com.epson.mobilephone.common.wifidirect.R.string.str_cancel
                com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission$innerHandler$1 r2 = new com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission$innerHandler$1
                r2.<init>(r4)
                android.app.AlertDialog$Builder r4 = r0.setNegativeButton(r1, r2)
                r0 = 0
                android.app.AlertDialog$Builder r4 = r4.setCancelable(r0)
                android.app.AlertDialog r4 = r4.create()
                r4.show()
                goto L_0x0054
            L_0x0051:
                r4.onSuccess()
            L_0x0054:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission.innerHandler.handleMessage(android.os.Message):void");
        }
    }

    private void onSuccess() {
        setResult(-1);
        finish();
    }

    private void onError() {
        setResult(0);
        finish();
    }

    private void checkLocationPreference() {
        if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == 0) {
            GoogleApiClient build = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
            try {
                build.connect();
                LocationServices.SettingsApi.checkLocationSettings(build, new LocationSettingsRequest.Builder().setAlwaysShow(true).addLocationRequest(new LocationRequest().setPriority(102)).build()).setResultCallback(new ResultCallback<LocationSettingsResult>() {
                    public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
                        Status status = locationSettingsResult.getStatus();
                        int statusCode = status.getStatusCode();
                        if (statusCode == 0) {
                            ActivityRequestLocationPermission.this.onSuccess();
                        } else if (statusCode == 6) {
                            try {
                                status.startResolutionForResult(ActivityRequestLocationPermission.this, 1);
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                                ActivityRequestLocationPermission.this.onError();
                            }
                        } else if (statusCode == 8502) {
                            EPLog.e(ActivityRequestLocationPermission.TAG, "Receive LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE;");
                            ActivityRequestLocationPermission.this.onError();
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                onError();
            }
        } else {
            Intent intent = new Intent();
            intent.setAction("android.settings.LOCATION_SOURCE_SETTINGS");
            try {
                startActivityForResult(intent, 2);
            } catch (ActivityNotFoundException e2) {
                e2.printStackTrace();
                onError();
            }
        }
    }

    /* access modifiers changed from: protected */
    @SuppressLint({"NewApi"})
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        switch (i) {
            case 1:
                if (i2 != -1) {
                    onError();
                    return;
                } else {
                    onSuccess();
                    return;
                }
            case 2:
                if (i2 == -1) {
                    mHandler.sendEmptyMessage(1);
                    return;
                } else if (!isNeedChangeLocationSettings(this)) {
                    onSuccess();
                    return;
                } else {
                    onError();
                    return;
                }
            case 3:
                if (hasLocationPermission(this)) {
                    mHandler.sendEmptyMessage(1);
                    return;
                } else {
                    onError();
                    return;
                }
            default:
                return;
        }
    }

    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        EPLog.e(TAG, "onConnectionFailed() connectionResult=" + connectionResult.toString());
        if (connectionResult.hasResolution()) {
            EPLog.e(TAG, "connectionResult has resolution. Try resolution.");
            try {
                connectionResult.startResolutionForResult(this, 2);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
                onError();
            }
        } else {
            EPLog.e(TAG, "connectionResult do'nt has resolution");
            GoogleApiAvailability instance = GoogleApiAvailability.getInstance();
            if (instance != null) {
                int isGooglePlayServicesAvailable = instance.isGooglePlayServicesAvailable(this);
                if (isGooglePlayServicesAvailable == 0) {
                    EPLog.e(TAG, "resultCode is SUCCESS. No resolution");
                    onError();
                } else if (instance.isUserResolvableError(isGooglePlayServicesAvailable)) {
                    instance.showErrorDialogFragment(this, isGooglePlayServicesAvailable, 2, new DialogInterface.OnCancelListener() {
                        public void onCancel(DialogInterface dialogInterface) {
                            ActivityRequestLocationPermission.this.onError();
                        }
                    });
                } else {
                    EPLog.e(TAG, "No userResolvableError.");
                    onError();
                }
            } else {
                EPLog.e(TAG, "GoogleApiAvailability.getInstance() failed.");
                onError();
            }
        }
    }

    public static void requestLocationPermission(Activity activity, int i) {
        activity.startActivityForResult(new Intent(activity, ActivityRequestLocationPermission.class), i);
    }

    public static void requestLocationPermissionForce(Activity activity, int i) {
        Intent intent = new Intent(activity, ActivityRequestLocationPermission.class);
        intent.putExtra(INTENT_FORCE_REQUEST, true);
        activity.startActivityForResult(intent, i);
    }

    public static void requestLocationPermission(Fragment fragment, int i) {
        fragment.startActivityForResult(new Intent(fragment.getActivity(), ActivityRequestLocationPermission.class), i);
    }

    private static boolean isNeedLocationPermission(Context context) {
        int i;
        if (Build.VERSION.SDK_INT < 23) {
            return false;
        }
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
            i = applicationInfo != null ? applicationInfo.targetSdkVersion : 0;
        } catch (Exception unused) {
            i = 0;
        }
        if (i >= 23) {
            return true;
        }
        return false;
    }

    private static boolean hasLocationPermission(Context context) {
        if (Build.VERSION.SDK_INT < 23 || context.checkSelfPermission(getNeededPermissionString(context)) == 0) {
            return true;
        }
        return false;
    }

    @NonNull
    private static String getNeededPermissionString(Context context) {
        return Build.VERSION.SDK_INT > 28 ? "android.permission.ACCESS_FINE_LOCATION" : "android.permission.ACCESS_COARSE_LOCATION";
    }

    private static boolean isNeedChangeLocationSettings(Context context) {
        if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT >= 19) {
            try {
                int i = Settings.Secure.getInt(context.getContentResolver(), "location_mode");
                if (i == 0 || i == 1) {
                    EPLog.i(TAG, "isNeedChangeLocationSettings() return true;");
                    return true;
                }
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean canAccessWiFiInfo(Context context, int i) {
        if (Build.VERSION.SDK_INT > 28) {
            if ((i & 21) == 0) {
                return true;
            }
            if ((!isNeedLocationPermission(context) || hasLocationPermission(context)) && !isNeedChangeLocationSettings(context)) {
                return true;
            }
            return false;
        } else if ((i & 5) == 0 || !isNeedLocationPermission(context)) {
            return true;
        } else {
            if (hasLocationPermission(context) && !isNeedChangeLocationSettings(context)) {
                return true;
            }
            return false;
        }
    }
}
