Why not compute this ratio for both width and height and take the minimum?  That seems safer to me.   For nexus4, you will have ensured that both quotients are the same, but for other devices, especially when using a 16:9 ratio, I think the width would give the smaller value.