Check if last shown Emoji category ID is still valid

When upgrading to a new version of APK we may find that the stored Emoji
category ID is no longer applicable. For example it happened when we
brought in the new Unicode 8.0 Emojis.

Also restore the "Symbols" emoji category on pre-kitkat devices which
was accidentally dropped when bringing in Unicode 8.0 Emojis.

b/25972978

Change-Id: I91c044603b0aac8757cb8597d3af995f84b822f3
diff --git a/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java b/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java
index 75b7962..b57e483 100644
--- a/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java
+++ b/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java
@@ -196,6 +196,8 @@
                     addShownCategoryId(EmojiCategory.ID_FLAGS);
                 }
             }
+        } else {
+            addShownCategoryId(EmojiCategory.ID_SYMBOLS);
         }
         addShownCategoryId(EmojiCategory.ID_EMOTICONS);
 
@@ -204,9 +206,14 @@
         recentsKbd.loadRecentKeys(mCategoryKeyboardMap.values());
 
         mCurrentCategoryId = Settings.readLastShownEmojiCategoryId(mPrefs, defaultCategoryId);
-        if (mCurrentCategoryId == EmojiCategory.ID_RECENTS &&
+        Log.i(TAG, "Last Emoji category id is " + mCurrentCategoryId);
+        if (!isShownCategoryId(mCurrentCategoryId)) {
+            Log.i(TAG, "Last emoji category " + mCurrentCategoryId +
+                    " is invalid, starting in " + defaultCategoryId);
+            mCurrentCategoryId = defaultCategoryId;
+        } else if (mCurrentCategoryId == EmojiCategory.ID_RECENTS &&
                 recentsKbd.getSortedKeys().isEmpty()) {
-            Log.i(TAG, "No recent emojis found, starting in category " + mCurrentCategoryId);
+            Log.i(TAG, "No recent emojis found, starting in category " + defaultCategoryId);
             mCurrentCategoryId = defaultCategoryId;
         }
     }
@@ -219,6 +226,15 @@
         mShownCategories.add(properties);
     }
 
+    private boolean isShownCategoryId(final int categoryId) {
+        for (final CategoryProperties prop : mShownCategories) {
+            if (prop.mCategoryId == categoryId) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public static String getCategoryName(final int categoryId, final int categoryPageId) {
         return sCategoryName[categoryId] + "-" + categoryPageId;
     }