I walked out of the interview immediately.
Looking back, I think I might have reacted rudely. Generally, how to handle these situations gracefully?
If you were no longer interested in interviewing and the interviewer wasn't paying attention, it's reasonable to say something like "I can see that we don't have a good fit here. Thanks for your time." and then walkedwalk out.
If you were interested in continuing to interview, you could have stopped talking and waited for his attention to return to the interview. You could have said something like "Is this a bad time for our discussion? Should we reschedule to a time when you aren't as busy?"
It's entirely your choice. The interviewer acted rudely. And then you reacted. If you don't care to ever interview there in the future, your reaction isn't a huge deal anyway and certainly nothing to worry about.