How do I create an app that gives caller id using a CallDirectoryExtension in Xamarin?


I am following the Xamarin guide to create an iPhone app that can block phone numbers or display caller id by creating a Call Directory Extension:

The code in Xamarin's documentation is not completely updated, but if you just create a Call Directory Extension in Xamarin Studio for OS X, you get some sample code to get you starting.

Below is the simplest possible code to block phone number 22334455:

public class CallDirectoryHandler : CXCallDirectoryProvider, ICXCallDirectoryExtensionContextDelegate
    protected CallDirectoryHandler(IntPtr handle) : base(handle) { }

    public override void BeginRequestWithExtensionContext(NSExtensionContext context)
        var cxContext = (CXCallDirectoryExtensionContext)context;
        cxContext.Delegate = this;

        //cxContext.AddIdentificationEntry(22334455, "Telemarketer");


    public void RequestFailed(CXCallDirectoryExtensionContext extensionContext, NSError error) { }

From the sample code it seems it should be just as easy to display caller id for the same number, simply use the method AddIdentificationEntry instead of AddBlockingEntry, but I cannot get it to work.

What am I missing?


The answer was frustratingly simple.

AddIdentificationEntry() requires the country code, AddBlockingEntry() does not.

When I added 47 (Norway's country code) to the beginning of the phone number, it worked. Here is the working code to display caller id for Norwegian phone number 22334455:

public override void BeginRequestWithExtensionContext(NSExtensionContext context)
  var cxContext = (CXCallDirectoryExtensionContext)context;
  cxContext.Delegate = this;

  cxContext.AddIdentificationEntry(4722334455, "Telemarketer");


addBlockingEntry() works with both 22334455 and 4722334455 as input.

There is a standard trick for computing the Fibonacci numbers that can easily be adapted to your problem. The naive definition for Fibonacci numbers is:

fibFunction :: Int -> Integer
fibFunction 0 = 1
fibFunction 1 = 1
fibFunction n = fibFunction (n-2) + fibFunction (n-1)

However, this is very costly: since all the leaves of the recursion are 1, if fib x = y, then we must perform y recursive calls! Since the Fibonacci numbers grow exponentially, this is a bad state of affairs to be in. But with dynamic programming, we can share the computations needed in the two recursive calls. The pleasing one-liner for this looks like this:

fibList :: [Integer]
fibList = 1 : 1 : zipWith (+) fibList (tail fibList)

This may look a bit puzzling at first; here the fibList argument to zipWith serves as the recursion on two indices ago, while the tail fibList argument serves as the recursion on one index ago, which gives us both the fib (n-2) and fib (n-1) values. The two 1s at the beginning are of course the base cases. There are other good questions here on SO that explain this technique in further detail, and you should study this code and those answers until you feel you understand how it works and why it is very fast.

If necessary, one can recover the Int -> Integer type signature from this using (!!).

Let's try to apply this technique to your function. As with computing Fibonacci numbers, you need the previous and second-to-last values; and additionally need the current index. That can be done by including [2..] in the call to zipWith. Here's how it would look:

waves :: [Integer]
waves = 1 : 1 : zipWith3 thisWave [2..] waves (tail waves) where
    thisWave n back2 back1 = ((3 * n - 3) * back2 + (2 * n + 1) * back1) `div` (n + 2)

As before, one can recover the function version with (!!) or genericIndex (if one really needs Integer indices). We can confirm that it computes the same function (but faster, and using less memory) in ghci:

> :set +s
> map wave [0..30]
(6.00 secs, 3,334,097,776 bytes)
> take 31 waves
(0.00 secs, 300,696 bytes)

With n=30, you need to compute wave 29 and wave 28, which, in turn, needs to compute wave 28, wave 27 twice and wave 26 and so forth, this quickly goes in the billions.

You can employ the same trick that is used in computation of the fibonacci numbers:

wave 0 = 1
wave 1 = 1
wave n = helper 1 1 2
       helper x y k | k <n      = helper y z (k+1)
                    | otherwise = z
                    where z = ((3*k-3) * x + (2*k+1) * y) `div` (k+2)

This runs in linear time, and the helper has, for every k the values for wave (k-2) and wave (k-1) ready.

By : Ingo

This video can help you solving your question :)
By: admin