Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins { #270

Open
Appdesign327 opened this issue Apr 16, 2024 · 0 comments
Open

plugins { #270

Appdesign327 opened this issue Apr 16, 2024 · 0 comments

Comments

@Appdesign327
Copy link

package com.callguard.calleridblock.usa.calleridnameaddress;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import com.callguard.calleridblock.usa.R;
import com.my.sdclass.AppManage;
import com.my.sdclass.MyCallback;

import static android.content.Context.MODE_PRIVATE;

public class PermissionUtils {

Activity activity;
Intent intent;
String[] permissionsRequired;
private SharedPreferences permissionStatus;
private static final int PERMISSION_CALLBACK_CONSTANT = 100;
private static final int REQUEST_PERMISSION_SETTING = 101;
private boolean sentToSettings = false;
private boolean writeflag = false;
private boolean overflag = false;
Dialog dialog1, dialog2, dialog3, dialog4;
boolean isFinish;

public PermissionUtils(Activity activity, Intent intent, boolean isFinish) {
    this.activity = activity;
    this.intent = intent;
    this.isFinish = isFinish;
}

public void grantPermition() {

    permissionStatus = activity.getSharedPreferences("permissionStatus", MODE_PRIVATE);
    permissionsRequired = new String[]{ "android.permission.CALL_PHONE", "android.permission.WRITE_CONTACTS"
    };
    if (Build.VERSION.SDK_INT >= 31) {
        permissionsRequired = new String[]{ "android.permission.READ_CONTACTS",
                 "android.permission.CALL_PHONE", "android.permission.WRITE_CONTACTS"
        };
    }
    if (Build.VERSION.SDK_INT >= 31) {
        if (ActivityCompat.checkSelfPermission(activity, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, permissionsRequired[2]) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[0])
                    || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[1])
                    || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[2])) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle("Need Multiple Permissions");
                builder.setMessage("This app needs to all permissions.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

                Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                b.setTextColor(Color.parseColor("#0034ff"));
                b1.setTextColor(Color.parseColor("#0034ff"));

            } else if (permissionStatus.getBoolean(permissionsRequired[0], false)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle("Need Multiple Permissions");
                builder.setMessage("This app needs to all permissions.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        sentToSettings = true;
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                        intent.setData(uri);
                        activity.startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

                Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                b.setTextColor(Color.parseColor("#0034ff"));
                b1.setTextColor(Color.parseColor("#0034ff"));
            } else {
                ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
            }

            SharedPreferences.Editor editor = permissionStatus.edit();
            editor.putBoolean(permissionsRequired[0], true);
            editor.commit();
        } else {
            proceedAfterPermission();
        }
    } else {
        if (ActivityCompat.checkSelfPermission(activity, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[0])
                    || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[1])) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle("Need Multiple Permissions");
                builder.setMessage("This app needs to all permissions.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

                Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                b.setTextColor(Color.parseColor("#0034ff"));
                b1.setTextColor(Color.parseColor("#0034ff"));

            } else if (permissionStatus.getBoolean(permissionsRequired[0], false)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle("Need Multiple Permissions");
                builder.setMessage("This app needs to all permissions.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        sentToSettings = true;
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                        intent.setData(uri);
                        activity.startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

                Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                b.setTextColor(Color.parseColor("#0034ff"));
                b1.setTextColor(Color.parseColor("#0034ff"));
            } else {
                ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
            }

            SharedPreferences.Editor editor = permissionStatus.edit();
            editor.putBoolean(permissionsRequired[0], true);
            editor.commit();
        } else {
            proceedAfterPermission();
        }
    }
}


public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == PERMISSION_CALLBACK_CONSTANT) {
        boolean allgranted = false;
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                allgranted = true;
            } else {
                allgranted = false;
                break;
            }
        }

        if (allgranted) {
            proceedAfterPermission();
        } else {
            if (Build.VERSION.SDK_INT >= 31) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[0])
                        || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[1])
                        || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[2])) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                    builder.setTitle("Need Multiple Permissions");
                    builder.setMessage("This app needs to all permissions.");
                    builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();

                    Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                    b.setTextColor(Color.parseColor("#0034ff"));
                    b1.setTextColor(Color.parseColor("#0034ff"));
                } else {
                }
            } else {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[0])
                        || ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionsRequired[1])) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                    builder.setTitle("Need Multiple Permissions");
                    builder.setMessage("This app needs to all permissions.");
                    builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            ActivityCompat.requestPermissions(activity, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();

                    Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    Button b1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

                    b.setTextColor(Color.parseColor("#0034ff"));
                    b1.setTextColor(Color.parseColor("#0034ff"));
                } else {
                }
            }

        }
    }
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_PERMISSION_SETTING) {
        if (Build.VERSION.SDK_INT >= 31) {
            if (ActivityCompat.checkSelfPermission(activity, permissionsRequired[0]) == PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(activity, permissionsRequired[1]) == PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(activity, permissionsRequired[2]) == PackageManager.PERMISSION_GRANTED ) {
                proceedAfterPermission();
            }
        } else {
            if (ActivityCompat.checkSelfPermission(activity, permissionsRequired[0]) == PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(activity, permissionsRequired[1]) == PackageManager.PERMISSION_GRANTED ) {
                proceedAfterPermission();
            }
        }

    }
}

int dia3_dis = 0;


protected void onResume() {

    if (writeflag == true) {
        boolean retVal = true;
        try {
            dialog1.dismiss();
            dialog3.dismiss();
        } catch (Exception e) {
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            retVal = Settings.System.canWrite(activity);
            if (retVal) {

                dialog4 = new Dialog(activity, R.style.s_permission);
                dialog4.setContentView(R.layout.dialog_write);
                dialog4.setCancelable(false);
                TextView write_ok = dialog4.findViewById(R.id.write_ok);
                TextView sort_sen = dialog4.findViewById(R.id.sort_sen);
                TextView long_sen = dialog4.findViewById(R.id.long_sen);
                sort_sen.setText("Required for Caller Id to system alert window");
                long_sen.setText("System Alert Window");
                write_ok.setText("Give Permission");
                write_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:" + activity.getPackageName()));
                        activity.startActivityForResult(intent, 848);
                        overflag = true;
                        writeflag = false;
                        dialog4.dismiss();

                    }
                });
                dialog4.show();

            } else {
                dialog1.dismiss();
                if (dia3_dis == 1) {
                    dialog3.dismiss();
                }
                dialog3 = new Dialog(activity, R.style.s_permission);
                dialog3.setContentView(R.layout.dialog_write);
                dialog3.setCancelable(false);
                TextView write_ok = dialog3.findViewById(R.id.write_ok);
                TextView sort_sen = dialog3.findViewById(R.id.sort_sen);
                TextView long_sen = dialog3.findViewById(R.id.long_sen);
                sort_sen.setText("Required for Caller Id to manage write setting");
                long_sen.setText("Write Setting");
                write_ok.setText("Give Permission");
                write_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        dia3_dis = 1;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (!Settings.System.canWrite(activity)) {
                                Intent intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
                                StringBuilder sb = new StringBuilder();
                                sb.append("package:");
                                sb.append(activity.getPackageName());
                                intent.setData(Uri.parse(sb.toString()));
                                activity.startActivityForResult(intent, 1112);
                                writeflag = true;
                                overflag = false;
                                dialog3.dismiss();
                            }
                        }

                    }
                });
                dialog3.show();

            }
        }
    }

    if (overflag == true) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            dialog4.dismiss();


            if (Settings.canDrawOverlays(activity)) {

                SharedPreferences.Editor editor = activity.getSharedPreferences("MyPrefFile1", MODE_PRIVATE).edit();
                editor.putBoolean("permission", true);
                editor.commit();
                AppManage.getInstance(activity).inScreen(activity, new MyCallback() {
                    public void callbackCall() {
                        activity.startActivity(intent);
                        if (isFinish) {
                            activity.finish();
                        }
                    }
                }, AppManage.go);


            } else {
                dialog1.dismiss();
                dialog2 = new Dialog(activity, R.style.s_permission);
                dialog2.setContentView(R.layout.dialog_write);
                dialog2.setCancelable(false);
                TextView write_ok = dialog2.findViewById(R.id.write_ok);
                TextView sort_sen = dialog2.findViewById(R.id.sort_sen);
                TextView long_sen = dialog2.findViewById(R.id.long_sen);
                sort_sen.setText("Required for Caller Id to system alert window");
                long_sen.setText("System Alert Window");
                write_ok.setText("Give Permission");
                write_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:" + activity.getPackageName()));
                        activity.startActivityForResult(intent, 848);
                        overflag = true;
                        writeflag = false;
                        dialog2.dismiss();
                    }
                });
                dialog2.show();
            }
        }
    }
}

private void proceedAfterPermission() {
    checkSystemWritePermission();
}

private boolean checkSystemWritePermission() {
    boolean retVal = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        retVal = Settings.System.canWrite(activity);
        if (retVal) {
            SharedPreferences.Editor editor = activity.getSharedPreferences("MyPrefFile1", MODE_PRIVATE).edit();
            editor.putBoolean("permission", true);
            editor.commit();
            AppManage.getInstance(activity).inScreen(activity, new MyCallback() {
                public void callbackCall() {
                    activity.startActivity(intent);
                    if (isFinish) {
                        activity.finish();
                    }
                }
            }, AppManage.go);
        } else {
            dialog1 = new Dialog(activity, R.style.s_permission);
            dialog1.setContentView(R.layout.dialog_write);
            dialog1.setCancelable(false);
            TextView write_ok = dialog1.findViewById(R.id.write_ok);
            TextView sort_sen = dialog1.findViewById(R.id.sort_sen);
            TextView long_sen = dialog1.findViewById(R.id.long_sen);
            sort_sen.setText("Required for Caller Id to manage write setting");
            long_sen.setText("Write Setting");
            write_ok.setText("Give Permission");
            write_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (!Settings.System.canWrite(activity)) {
                            Intent intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
                            StringBuilder sb = new StringBuilder();
                            sb.append("package:");
                            sb.append(activity.getPackageName());
                            intent.setData(Uri.parse(sb.toString()));
                            activity.startActivityForResult(intent, 1112);
                            writeflag = true;
                            overflag = false;
                            dialog1.dismiss();
                            try {
                                dialog3.dismiss();
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            });
            dialog1.show();
        }
    }
    return retVal;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant