zatamos
zatamos

Reputation: 93

Adding several events in Google Calendar on Android

I'm writting ab android application that handles the end of a call:

My problem is:

If you are called by numer +442070313000 call -> notification pop up, and when you click, it creates an event with title +442070313000

But if then number 666 calls -> notification pop up, but calendar event is filled with the number +442070313000!

I debugged the application and the intent passed to the google calendar is ok. But it seems to keep only the first intent and ignore other ones

Here is the code that handle event :

package com.amos.addincalendar.facade;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiverIncoming extends BroadcastReceiver {

    String LOG_TAG = "CallReceiver";
    String EVENT_TYPE_PHONE_STATE_CHANGED = "phone state changed";
    private static int MIN_TOTAL_TIME = 0;
    static String LOG_TAG = "CallReceiver";

    TelephonyManager tm;
    private String sPhoneNumber = "";
    private boolean bAgenda = false;
    private long lStartTime;
    private long lEndTime;

    private void onReceivePhoneStateChanged(final Context context,
              final Intent intent) {

        final String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        final StringBuilder sb = new StringBuilder();

        if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState)) {
            Log.d(LOG_TAG, "event : idle");
            bAgenda = true;
            lEndTime = System.currentTimeMillis();
        } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState)) {
            Log.d(LOG_TAG, "event : offhook");
            bAgenda = true;
            lEndTime = System.currentTimeMillis();
        } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState)) {
            Log.d(LOG_TAG, "event : ringing");
            lStartTime = System.currentTimeMillis();
        }

    }



    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if(null == bundle){
            return;         
        }

        final String action = intent.getAction();
        sPhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
            onReceivePhoneStateChanged(context, intent);
        } else {
            final String data = intent.getDataString();

            Log.v(LOG_TAG, "broadcast : action=" + action + ", data=" + data);
        }


        if(!bAgenda){
            CallReceiver.agendaFishAndChips(context, sPhoneNumber, lStartTime, lEndTime);
        }

    }


    public static void agendaFishAndChips(Context context, String sPhoneNumber, long lStartTime, long lEndTime){

        StringBuilder sbTitleEvent  = new StringBuilder();
        StringBuilder sbDescription = new StringBuilder();
        StringBuilder sbAddress     = new StringBuilder();
        StringBuilder sbName        = new StringBuilder();

        if(sPhoneNumber != null && !sPhoneNumber.equals("")){
            sbTitleEvent.append(sPhoneNumber);

            CallReceiver.sendNotificationAgenda(context, sbTitleEvent.toString(), sbAddress.toString(), sbDescription.toString(), sPhoneNumber, sbName.toString());

        }
    }

    public static String getCaracteresNumeriques (String chaine){
        String sChaineNumerique = "";

        Matcher matcher = Pattern.compile("[0-9]*").matcher(chaine);
        while ( matcher.find() ) {
            sChaineNumerique += matcher.group();
        }
        return sChaineNumerique;
    }

    /*
     * Send a notification and add event in agenda
     */
    public static void sendNotificationAgenda(Context context, String sTitleEvent, String sAddress, String sDescription, String sPhoneNumber, String sName){
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        sName = (sName != null && !sName.equals("") ? sName : sPhoneNumber);
        String sNotificationTitle = "Rendezvous with "+ sName;
        Notification notification = new Notification(R.drawable.ic_launcher, sNotificationTitle, System.currentTimeMillis());

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        Log.d(LOG_TAG, sName + " : " + getCaracteresNumeriques(sPhoneNumber));

        Log.d(LOG_TAG, sTitleEvent);

    //Create the google event intent
    Intent intentNotification = new Intent(Intent.ACTION_EDIT);
        intentNotification.setType("vnd.android.cursor.item/event");

        intentNotification.putExtra("title", sTitleEvent);
        intentNotification.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Bundle objetbunble = new Bundle();
    objetbunble.putString("TitleEvent"  , sTitleEvent);
    objetbunble.putString("Address"     , sAddress);
    objetbunble.putString("Description", sDescription);
    intentNotification.putExtras(objetbunble);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentNotification, 0);
        notification.setLatestEventInfo(context,
                    sNotificationTitle,
                    context.getText(R.string.notification_new_rdv) + " " + sName,   pendingIntent);
        notificationManager.notify( Integer.parseInt(getCaracteresNumeriques(sPhoneNumber)), notification);


    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        sPhoneNumber = "";
    }
}

And here is the manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amos.addincalendar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.SET_DEBUG_APP"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >

          <activity
            android:name=".action.CalendarMain"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".facade.CallReceiverIncoming">
            <intent-filter> 
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter> 
        </receiver>

        <activity android:name=".dao.CalendarDAO"></activity>
        <activity android:name=".facade.CallReceiver"></activity>
        <activity android:name=".facade.CallActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"></activity>

    </application>
</manifest>

Upvotes: 2

Views: 451

Answers (1)

THelper
THelper

Reputation: 15619

I think in this case you have to provide each PendingIntent with an unique id. Try replacing the 0 in

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentNotification, 0); 

with a unique ID (perhaps the phone number?). If you provide the same number to each PendingIntent then the same intent (first one sent) will be read.

Upvotes: 1

Related Questions