1. e_voloshchak's Avatar
    Hello everybody!
    I`m developing a simple messaging/calling app, using Android`s self managed connection service.
    So, when I want to display incoming call, i create notification with two action buttons (Accept and Decline).
    The problem is, i cannot call setDisconnected method of my Connection when users clicks "Decline" button, because, AFAIK, notification actions can only launch pending intents.
    Here`s my Connection class
    Code:
    public class Connection extends android.telecom.Connection {
        private static final String TAG = "Connection";
     
        public Connection() {
            super();
            setConnectionProperties(android.telecom.Connection.PROPERTY_SELF_MANAGED);
        }
     
        @override
        public void onStateChanged(int state) {
            super.onStateChanged(state);
            Log.i(TAG, "onStateChanged state=" + android.telecom.Connection.stateToString(state));
        }
     
        public void setConnectionDisconnected(int cause) {
            setDisconnected(new DisconnectCause(cause));
            destroy();
            Log.i(TAG, "disconnected");
        }
     
     
        public void displayIncomingCallNotification() {
            final NotificationManager notificationManager = (NotificationManager) Config.context.getSystemService(Config.context.NOTIFICATION_SERVICE);
            String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
            Handler.Callback callback = new Handler.Callback() {
                public boolean handleMessage(Message msg) {
                    Log.d("Call", "back");
                    return true;
                }
            };
            PendingIntent dismissIntent = NotificationActivity.getDismissIntent(1, Config.context, callback);
     
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
     
                notificationChannel.setDescription("Channel description");
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                notificationChannel.setSound(ringtoneUri, new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .build());
                notificationManager.createNotificationChannel(notificationChannel);
            }
     
     
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Config.context, NOTIFICATION_CHANNEL_ID);
            Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(Config.context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(Config.context, 1, intent, 0);
     
            notificationBuilder.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(android.R.drawable.sym_action_call)
                    .setTicker("Hearty365")
                    //.setPriority(Notification.PRIORITY_HIGH)
                    .setContentTitle("Default notification")
                    .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
                    .setContentInfo("Info")
                    .setOngoing(true)
                    .setContentIntent(pendingIntent)
                    .setFullScreenIntent(pendingIntent, true)
                    .addAction(android.R.drawable.sym_call_incoming, "Accept", null)
                    .addAction(android.R.drawable.sym_call_missed, "Decline", dismissIntent);
            final Notification notification = notificationBuilder.build();
            notification.flags |= Notification.FLAG_INSISTENT;
     
            new android.os.Handler().postDelayed(
                    new Runnable() {
                        public void run() {
                            notificationManager.notify(1, notification);
                        }
                    },
                    4200);
        }
       
     
        @override
        public void onShowIncomingCallUi() {
            displayIncomingCallNotification();
     
            Log.i(TAG, "onShowIncomingCallUi ");
        }
     
        @override
        public void onDisconnect() {
            setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
            destroy();
            Log.i(TAG, "disconnected local");
        }
     
     
        @override
        public void onReject() {
            setDisconnected(new DisconnectCause(DisconnectCause.REJECTED));
            destroy();
            Log.i(TAG, "disconnected reject");
        }
     
        @override
        public void onAnswer() {
            Log.i(TAG, "Call answered");
        }
    }
    When user clicks "Dismiss" button, dismissIntent is called

    Code:
    public class NotificationActivity extends Activity {
     
        public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
     
        @override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
     
            finish();
        }
     
        public static PendingIntent getDismissIntent(int notificationId, Context context) {
           
            Intent intent = new Intent(context, NotificationActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.putExtra(NOTIFICATION_ID, notificationId);
            PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
     
            return dismissIntent;
        }
    }
    Ideally, i would want to call Connection.setConnectionDisconnected() in NotificationActivity`s onCreate()
    07-08-2019 12:09 PM

Similar Threads

  1. How did my Oneplus 5 instantly delete all media on my device?
    By Android Central Question in forum OnePlus 5 & 5T
    Replies: 10
    Last Post: 07-12-2019, 04:54 AM
  2. How to make pre-installed apps on Android 9 pie
    By Android Central Question in forum Ask a Question
    Replies: 2
    Last Post: 07-08-2019, 03:06 PM
  3. How did my Oneplus 5 instantly delete all media on my device?
    By Android Central Question in forum Ask a Question
    Replies: 0
    Last Post: 07-08-2019, 11:50 AM
LINK TO POST COPIED TO CLIPBOARD