Nirmal singh
Nirmal singh

Reputation: 197

popup window on incoming call screen like truecaller

i am trying to add popup window on incoming call screen as true caller but fail to implement .let me know what is logic behind this and how i can implement this

  public void onReceive(Context context, Intent intent) {

    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);


            }
    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

Upvotes: 4

Views: 11894

Answers (2)

Karmelina Michael
Karmelina Michael

Reputation: 192

try this

AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
            LayoutInflater inflater = LayoutInflater.from(context);
            View dialogView = inflater.inflate(R.layout.caller_dialog, null);

            ImageView button = dialogView.findViewById(R.id.close_btn);

            builder.setView(dialogView);
            final AlertDialog alert = builder.create();
            alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
            alert.setCanceledOnTouchOutside(true);
            alert.show();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = alert.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setGravity(Gravity.TOP);
            lp.copyFrom(window.getAttributes());
            //This makes the dialog take up the full width
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //close the service and remove the from from the window
                    alert.dismiss();
                }
            });

And add a permission check in your onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(getActivity())) {
        //If the draw over permission is not available open the settings screen
        //to grant the permission.
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getActivity().getPackageName()));
        startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
    }

And the onActivityResult()

private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084; 
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {

        //Check if the permission is granted or not.
        if (resultCode == RESULT_OK) {

        }
        else
            {
                // Permission is not available
            }

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

And don't forget to add permission in the manifest

<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />

Upvotes: 2

Basher51
Basher51

Reputation: 1329

I have used the below approach for my app,wherein I wanted to show a view ontop of the Dialer app when it launches(something similar to that in Truecaller).For this,create a Broadcast Receiver which helps to accept various device events as described below.

Broadcast Receiver :

        private WindowManager wm;
        private static LinearLayout ly1;
        private WindowManager.LayoutParams params1;

        // onReceive function of the Broadcast Receiver
        public void onReceive(Context arg0, Intent arg1) {
                String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);

                // Adds a view on top of the dialer app when it launches.
                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                    params1 = new WindowManager.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
                            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                            PixelFormat.TRANSPARENT);

                    params1.height = 75;
                    params1.width = 512;
                    params1.x = 265; 
                    params1.y = 400;
                    params1.format = PixelFormat.TRANSLUCENT;

                    ly1 = new LinearLayout(context);
                    ly1.setBackgroundColor(Color.BLACK);
                    ly1.setOrientation(LinearLayout.VERTICAL);

                    wm.addView(ly1, params1);
                }

                // To remove the view once the dialer app is closed.
                if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){
                    String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);
                    if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                        if(ly1!=null)
                        {
                            wm.removeView(ly1);
                            ly1 = null;
                        }
                    }
                }
            }

PS: The above eg generated a view having a layout with black background,having dimension as shown above.You have the liberty to add any layout within this view.For eg :To include a layout within the view you can modify the above code to include the following:

        ly1 = new LinearLayout(getApplicationContext());
        ly1.setOrientation(LinearLayout.HORIZONTAL);


        View hiddenInfo = getLayoutInflater().inflate(R.layout.layout1, ly1, false);
        ly1.addView(hiddenInfo);

        wm.addView(ly1, params1);

PS: Layout1 is a layout which you need to create in your layout folder and reference it here.

Addtionally , in your manifest you need to include the following permissions.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<action android:name="android.intent.action.PHONE_STATE" /> (within intent filter of Broadcast Receiver)

Upvotes: 13

Related Questions