Merge "Remove compiler warnings from emoji related code"

main
Tadashi G. Takaoka 2013-11-27 03:44:33 +00:00 committed by Android (Google) Code Review
commit c144299f70
5 changed files with 180 additions and 160 deletions

View File

@ -31,17 +31,17 @@ public class EmojiCategoryPageIndicatorView extends LinearLayout {
private int mCurrentCategoryPageId = 0;
private float mOffset = 0.0f;
public EmojiCategoryPageIndicatorView(Context context) {
public EmojiCategoryPageIndicatorView(final Context context) {
this(context, null /* attrs */);
}
public EmojiCategoryPageIndicatorView(Context context, AttributeSet attrs) {
public EmojiCategoryPageIndicatorView(final Context context, final AttributeSet attrs) {
super(context, attrs);
mPaint.setColor(context.getResources().getColor(
R.color.emoji_category_page_id_view_foreground));
}
public void setCategoryPageId(int size, int id, float offset) {
public void setCategoryPageId(final int size, final int id, final float offset) {
mCategoryPageSize = size;
mCurrentCategoryPageId = id;
mOffset = offset;
@ -49,7 +49,7 @@ public class EmojiCategoryPageIndicatorView extends LinearLayout {
}
@Override
protected void onDraw(Canvas canvas) {
protected void onDraw(final Canvas canvas) {
if (mCategoryPageSize <= 1) {
// If the category is not set yet or contains only one category,
// just clear and return.

View File

@ -44,6 +44,7 @@ import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.android.inputmethod.keyboard.internal.DynamicGridKeyboard;
import com.android.inputmethod.keyboard.internal.EmojiLayoutParams;
import com.android.inputmethod.keyboard.internal.EmojiPageKeyboardView;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R;
@ -72,14 +73,14 @@ import java.util.concurrent.ConcurrentHashMap;
public final class EmojiPalettesView extends LinearLayout implements OnTabChangeListener,
ViewPager.OnPageChangeListener, View.OnClickListener,
EmojiPageKeyboardView.OnKeyClickListener {
private static final String TAG = EmojiPalettesView.class.getSimpleName();
static final String TAG = EmojiPalettesView.class.getSimpleName();
private static final boolean DEBUG_PAGER = false;
private final int mKeyBackgroundId;
private final int mEmojiFunctionalKeyBackgroundId;
private final KeyboardLayoutSet mLayoutSet;
private final ColorStateList mTabLabelColor;
private final DeleteKeyOnTouchListener mDeleteKeyOnTouchListener;
private EmojiPalettesAdapter mEmojiPalettesAdapter;
private final EmojiLayoutParams mEmojiLayoutParams;
private TabHost mTabHost;
private ViewPager mEmojiPager;
@ -173,7 +174,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
.loadRecentKeys(mCategoryKeyboardMap.values());
}
private void addShownCategoryId(int categoryId) {
private void addShownCategoryId(final int categoryId) {
// Load a keyboard of categoryId
getKeyboard(categoryId, 0 /* cagetoryPageId */);
final CategoryProperties properties =
@ -181,20 +182,20 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
mShownCategories.add(properties);
}
public String getCategoryName(int categoryId, int categoryPageId) {
public String getCategoryName(final int categoryId, final int categoryPageId) {
return sCategoryName[categoryId] + "-" + categoryPageId;
}
public int getCategoryId(String name) {
public int getCategoryId(final String name) {
final String[] strings = name.split("-");
return mCategoryNameToIdMap.get(strings[0]);
}
public int getCategoryIcon(int categoryId) {
public int getCategoryIcon(final int categoryId) {
return sCategoryIcon[categoryId];
}
public String getCategoryLabel(int categoryId) {
public String getCategoryLabel(final int categoryId) {
return sCategoryLabel[categoryId];
}
@ -210,7 +211,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return getCategoryPageSize(mCurrentCategoryId);
}
public int getCategoryPageSize(int categoryId) {
public int getCategoryPageSize(final int categoryId) {
for (final CategoryProperties prop : mShownCategories) {
if (prop.mCategoryId == categoryId) {
return prop.mPageCount;
@ -221,12 +222,12 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return 0;
}
public void setCurrentCategoryId(int categoryId) {
public void setCurrentCategoryId(final int categoryId) {
mCurrentCategoryId = categoryId;
Settings.writeLastShownEmojiCategoryId(mPrefs, categoryId);
}
public void setCurrentCategoryPageId(int id) {
public void setCurrentCategoryPageId(final int id) {
mCurrentCategoryPageId = id;
}
@ -243,7 +244,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return mCurrentCategoryId == CATEGORY_ID_RECENTS;
}
public int getTabIdFromCategoryId(int categoryId) {
public int getTabIdFromCategoryId(final int categoryId) {
for (int i = 0; i < mShownCategories.size(); ++i) {
if (mShownCategories.get(i).mCategoryId == categoryId) {
return i;
@ -254,7 +255,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
}
// Returns the view pager's page position for the categoryId
public int getPageIdFromCategoryId(int categoryId) {
public int getPageIdFromCategoryId(final int categoryId) {
final int lastSavedCategoryPageId =
Settings.readLastTypedEmojiCategoryPageId(mPrefs, categoryId);
int sum = 0;
@ -273,7 +274,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return getTabIdFromCategoryId(CATEGORY_ID_RECENTS);
}
private int getCategoryPageCount(int categoryId) {
private int getCategoryPageCount(final int categoryId) {
final Keyboard keyboard = mLayoutSet.getKeyboard(sCategoryElementId[categoryId]);
return (keyboard.getKeys().length - 1) / mMaxPageKeyCount + 1;
}
@ -282,9 +283,9 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
// position. The category page id is numbered in each category. And the view page position
// is the position of the current shown page in the view pager which contains all pages of
// all categories.
public Pair<Integer, Integer> getCategoryIdAndPageIdFromPagePosition(int position) {
public Pair<Integer, Integer> getCategoryIdAndPageIdFromPagePosition(final int position) {
int sum = 0;
for (CategoryProperties properties : mShownCategories) {
for (final CategoryProperties properties : mShownCategories) {
final int temp = sum;
sum += properties.mPageCount;
if (sum > position) {
@ -295,7 +296,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
}
// Returns a keyboard from the view pager's page position.
public DynamicGridKeyboard getKeyboardFromPagePosition(int position) {
public DynamicGridKeyboard getKeyboardFromPagePosition(final int position) {
final Pair<Integer, Integer> categoryAndId =
getCategoryIdAndPageIdFromPagePosition(position);
if (categoryAndId != null) {
@ -304,39 +305,41 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return null;
}
public DynamicGridKeyboard getKeyboard(int categoryId, int id) {
synchronized(mCategoryKeyboardMap) {
final long key = (((long) categoryId) << Constants.MAX_INT_BIT_COUNT) | id;
final DynamicGridKeyboard kbd;
if (!mCategoryKeyboardMap.containsKey(key)) {
if (categoryId != CATEGORY_ID_RECENTS) {
final Keyboard keyboard =
mLayoutSet.getKeyboard(sCategoryElementId[categoryId]);
final Key[][] sortedKeys = sortKeys(keyboard.getKeys(), mMaxPageKeyCount);
for (int i = 0; i < sortedKeys.length; ++i) {
final DynamicGridKeyboard tempKbd = new DynamicGridKeyboard(mPrefs,
private static final Long getCategoryKeyboardMapKey(final int categoryId, final int id) {
return (((long) categoryId) << Constants.MAX_INT_BIT_COUNT) | id;
}
public DynamicGridKeyboard getKeyboard(final int categoryId, final int id) {
synchronized (mCategoryKeyboardMap) {
final Long categotyKeyboardMapKey = getCategoryKeyboardMapKey(categoryId, id);
if (mCategoryKeyboardMap.containsKey(categotyKeyboardMapKey)) {
return mCategoryKeyboardMap.get(categotyKeyboardMapKey);
}
if (categoryId == CATEGORY_ID_RECENTS) {
final DynamicGridKeyboard kbd = new DynamicGridKeyboard(mPrefs,
mLayoutSet.getKeyboard(KeyboardId.ELEMENT_EMOJI_RECENTS),
mMaxPageKeyCount, categoryId, i /* categoryPageId */);
for (Key emojiKey : sortedKeys[i]) {
mMaxPageKeyCount, categoryId);
mCategoryKeyboardMap.put(categotyKeyboardMapKey, kbd);
return kbd;
}
final Keyboard keyboard = mLayoutSet.getKeyboard(sCategoryElementId[categoryId]);
final Key[][] sortedKeys = sortKeysIntoPages(keyboard.getKeys(), mMaxPageKeyCount);
for (int pageId = 0; pageId < sortedKeys.length; ++pageId) {
final DynamicGridKeyboard tempKeyboard = new DynamicGridKeyboard(mPrefs,
mLayoutSet.getKeyboard(KeyboardId.ELEMENT_EMOJI_RECENTS),
mMaxPageKeyCount, categoryId);
for (final Key emojiKey : sortedKeys[pageId]) {
if (emojiKey == null) {
break;
}
tempKbd.addKeyLast(emojiKey);
tempKeyboard.addKeyLast(emojiKey);
}
mCategoryKeyboardMap.put((((long) categoryId)
<< Constants.MAX_INT_BIT_COUNT) | i, tempKbd);
mCategoryKeyboardMap.put(
getCategoryKeyboardMapKey(categoryId, pageId), tempKeyboard);
}
kbd = mCategoryKeyboardMap.get(key);
} else {
kbd = new DynamicGridKeyboard(mPrefs,
mLayoutSet.getKeyboard(KeyboardId.ELEMENT_EMOJI_RECENTS),
mMaxPageKeyCount, categoryId, 0 /* categoryPageId */);
mCategoryKeyboardMap.put(key, kbd);
}
} else {
kbd = mCategoryKeyboardMap.get(key);
}
return kbd;
return mCategoryKeyboardMap.get(categotyKeyboardMapKey);
}
}
@ -348,11 +351,9 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
return sum;
}
private Key[][] sortKeys(Key[] inKeys, int maxPageCount) {
Key[] keys = Arrays.copyOf(inKeys, inKeys.length);
Arrays.sort(keys, 0, keys.length, new Comparator<Key>() {
private static Comparator<Key> EMOJI_KEY_COMPARATOR = new Comparator<Key>() {
@Override
public int compare(Key lhs, Key rhs) {
public int compare(final Key lhs, final Key rhs) {
final Rect lHitBox = lhs.getHitBox();
final Rect rHitBox = rhs.getHitBox();
if (lHitBox.top < rHitBox.top) {
@ -370,7 +371,11 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
}
return lhs.getCode() < rhs.getCode() ? -1 : 1;
}
});
};
private static Key[][] sortKeysIntoPages(final Key[] inKeys, final int maxPageCount) {
final Key[] keys = Arrays.copyOf(inKeys, inKeys.length);
Arrays.sort(keys, 0, keys.length, EMOJI_KEY_COMPARATOR);
final int pageCount = (keys.length - 1) / maxPageCount + 1;
final Key[][] retval = new Key[pageCount][maxPageCount];
for (int i = 0; i < keys.length; ++i) {
@ -403,12 +408,11 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
context, null /* editorInfo */);
final Resources res = context.getResources();
final EmojiLayoutParams emojiLp = new EmojiLayoutParams(res);
mEmojiLayoutParams = new EmojiLayoutParams(res);
builder.setSubtype(SubtypeSwitcher.getInstance().getEmojiSubtype());
builder.setKeyboardGeometry(ResourceUtils.getDefaultKeyboardWidth(res),
emojiLp.mEmojiKeyboardHeight);
mEmojiLayoutParams.mEmojiKeyboardHeight);
builder.setOptions(false, false, false /* lanuageSwitchKeyEnabled */);
mLayoutSet = builder.build();
mEmojiCategory = new EmojiCategory(PreferenceManager.getDefaultSharedPreferences(context),
context.getResources(), builder.build());
mDeleteKeyOnTouchListener = new DeleteKeyOnTouchListener(context);
@ -457,25 +461,23 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
mTabHost.setOnTabChangedListener(this);
mTabHost.getTabWidget().setStripEnabled(true);
mEmojiPalettesAdapter = new EmojiPalettesAdapter(mEmojiCategory, mLayoutSet, this);
mEmojiPalettesAdapter = new EmojiPalettesAdapter(mEmojiCategory, this);
mEmojiPager = (ViewPager)findViewById(R.id.emoji_keyboard_pager);
mEmojiPager.setAdapter(mEmojiPalettesAdapter);
mEmojiPager.setOnPageChangeListener(this);
mEmojiPager.setOffscreenPageLimit(0);
mEmojiPager.setPersistentDrawingCache(ViewPager.PERSISTENT_NO_CACHE);
final Resources res = getResources();
final EmojiLayoutParams emojiLp = new EmojiLayoutParams(res);
emojiLp.setPagerProperties(mEmojiPager);
mEmojiPager.setPersistentDrawingCache(PERSISTENT_NO_CACHE);
mEmojiLayoutParams.setPagerProperties(mEmojiPager);
mEmojiCategoryPageIndicatorView =
(EmojiCategoryPageIndicatorView)findViewById(R.id.emoji_category_page_id_view);
emojiLp.setCategoryPageIdViewProperties(mEmojiCategoryPageIndicatorView);
mEmojiLayoutParams.setCategoryPageIdViewProperties(mEmojiCategoryPageIndicatorView);
setCurrentCategoryId(mEmojiCategory.getCurrentCategoryId(), true /* force */);
final LinearLayout actionBar = (LinearLayout)findViewById(R.id.emoji_action_bar);
emojiLp.setActionBarProperties(actionBar);
mEmojiLayoutParams.setActionBarProperties(actionBar);
final ImageView deleteKey = (ImageView)findViewById(R.id.emoji_keyboard_delete);
deleteKey.setTag(Constants.CODE_DELETE);
@ -488,7 +490,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
spaceKey.setBackgroundResource(mKeyBackgroundId);
spaceKey.setTag(Constants.CODE_SPACE);
spaceKey.setOnClickListener(this);
emojiLp.setKeyProperties(spaceKey);
mEmojiLayoutParams.setKeyProperties(spaceKey);
final ImageView alphabetKey2 = (ImageView)findViewById(R.id.emoji_keyboard_alphabet2);
alphabetKey2.setBackgroundResource(mEmojiFunctionalKeyBackgroundId);
alphabetKey2.setTag(Constants.CODE_SWITCH_ALPHA_SYMBOL);
@ -635,7 +637,6 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
private int mActivePosition = 0;
public EmojiPalettesAdapter(final EmojiCategory emojiCategory,
final KeyboardLayoutSet layoutSet,
final EmojiPageKeyboardView.OnKeyClickListener listener) {
mEmojiCategory = emojiCategory;
mListener = listener;
@ -670,7 +671,8 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
}
@Override
public void setPrimaryItem(final View container, final int position, final Object object) {
public void setPrimaryItem(final ViewGroup container, final int position,
final Object object) {
if (mActivePosition == position) {
return;
}
@ -789,7 +791,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
}
}
public void pressDelete(int repeatCount) {
public void pressDelete(final int repeatCount) {
mKeyboardActionListener.onPressKey(
Constants.CODE_DELETE, repeatCount, true /* isSinglePointer */);
mKeyboardActionListener.onCodeInput(
@ -798,12 +800,12 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
Constants.CODE_DELETE, false /* withSliding */);
}
public void setKeyboardActionListener(KeyboardActionListener listener) {
public void setKeyboardActionListener(final KeyboardActionListener listener) {
mKeyboardActionListener = listener;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
public boolean onTouch(final View v, final MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setBackgroundColor(mDeleteKeyPressedBackgroundColor);

View File

@ -53,7 +53,7 @@ public class DynamicGridKeyboard extends Keyboard {
private Key[] mCachedGridKeys;
public DynamicGridKeyboard(final SharedPreferences prefs, final Keyboard templateKeyboard,
final int maxKeyCount, final int categoryId, final int categoryPageId) {
final int maxKeyCount, final int categoryId) {
super(templateKeyboard);
final Key key0 = getTemplateKey(TEMPLATE_KEY_CODE_0);
final Key key1 = getTemplateKey(TEMPLATE_KEY_CODE_1);
@ -124,7 +124,7 @@ public class DynamicGridKeyboard extends Keyboard {
final int keyY0 = getKeyY0(index);
final int keyX1 = getKeyX1(index);
final int keyY1 = getKeyY1(index);
gridKey.updateCorrdinates(keyX0, keyY0, keyX1, keyY1);
gridKey.updateCoordinates(keyX0, keyY0, keyX1, keyY1);
index++;
}
}
@ -143,32 +143,44 @@ public class DynamicGridKeyboard extends Keyboard {
Settings.writeEmojiRecentKeys(mPrefs, jsonStr);
}
private static Key getKey(final Collection<DynamicGridKeyboard> keyboards, final Object o) {
for (final DynamicGridKeyboard kbd : keyboards) {
if (o instanceof Integer) {
final int code = (Integer) o;
final Key key = kbd.getKey(code);
private static Key getKeyByCode(final Collection<DynamicGridKeyboard> keyboards,
final int code) {
for (final DynamicGridKeyboard keyboard : keyboards) {
final Key key = keyboard.getKey(code);
if (key != null) {
return key;
}
} else if (o instanceof String) {
final String outputText = (String) o;
final Key key = kbd.getKeyFromOutputText(outputText);
if (key != null) {
return key;
}
} else {
Log.w(TAG, "Invalid object: " + o);
}
}
return null;
}
public void loadRecentKeys(Collection<DynamicGridKeyboard> keyboards) {
private static Key getKeyByOutputText(final Collection<DynamicGridKeyboard> keyboards,
final String outputText) {
for (final DynamicGridKeyboard kbd : keyboards) {
final Key key = kbd.getKeyFromOutputText(outputText);
if (key != null) {
return key;
}
}
return null;
}
public void loadRecentKeys(final Collection<DynamicGridKeyboard> keyboards) {
final String str = Settings.readEmojiRecentKeys(mPrefs);
final List<Object> keys = StringUtils.jsonStrToList(str);
for (final Object o : keys) {
addKeyLast(getKey(keyboards, o));
final Key key;
if (o instanceof Integer) {
final int code = (Integer)o;
key = getKeyByCode(keyboards, code);
} else if (o instanceof String) {
final String outputText = (String)o;
key = getKeyByOutputText(keyboards, outputText);
} else {
Log.w(TAG, "Invalid object: " + o);
continue;
}
addKeyLast(key);
}
}
@ -217,7 +229,7 @@ public class DynamicGridKeyboard extends Keyboard {
super(originalKey);
}
public void updateCorrdinates(final int x0, final int y0, final int x1, final int y1) {
public void updateCoordinates(final int x0, final int y0, final int x1, final int y1) {
mCurrentX = x0;
mCurrentY = y0;
getHitBox().set(x0, y0, x1, y1);

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.inputmethod.keyboard;
package com.android.inputmethod.keyboard.internal;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.ResourceUtils;
@ -37,22 +37,22 @@ public class EmojiLayoutParams {
private final int mBottomPadding;
private final int mTopPadding;
public EmojiLayoutParams(Resources res) {
public EmojiLayoutParams(final Resources res) {
final int defaultKeyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
final int defaultKeyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
mKeyVerticalGap = (int) res.getFraction(R.fraction.key_bottom_gap_holo,
(int) defaultKeyboardHeight, (int) defaultKeyboardHeight);
defaultKeyboardHeight, defaultKeyboardHeight);
mBottomPadding = (int) res.getFraction(R.fraction.keyboard_bottom_padding_holo,
(int) defaultKeyboardHeight, (int) defaultKeyboardHeight);
defaultKeyboardHeight, defaultKeyboardHeight);
mTopPadding = (int) res.getFraction(R.fraction.keyboard_top_padding_holo,
(int) defaultKeyboardHeight, (int) defaultKeyboardHeight);
defaultKeyboardHeight, defaultKeyboardHeight);
mKeyHorizontalGap = (int) (res.getFraction(R.fraction.key_horizontal_gap_holo,
defaultKeyboardWidth, defaultKeyboardWidth));
mEmojiCategoryPageIdViewHeight =
(int) (res.getDimension(R.dimen.emoji_category_page_id_height));
final int baseheight = defaultKeyboardHeight - mBottomPadding - mTopPadding
+ mKeyVerticalGap;
mEmojiActionBarHeight = ((int) baseheight) / DEFAULT_KEYBOARD_ROWS
mEmojiActionBarHeight = baseheight / DEFAULT_KEYBOARD_ROWS
- (mKeyVerticalGap - mBottomPadding) / 2;
mEmojiPagerHeight = defaultKeyboardHeight - mEmojiActionBarHeight
- mEmojiCategoryPageIdViewHeight;
@ -60,26 +60,26 @@ public class EmojiLayoutParams {
mEmojiKeyboardHeight = mEmojiPagerHeight - mEmojiPagerBottomMargin - 1;
}
public void setPagerProperties(ViewPager vp) {
public void setPagerProperties(final ViewPager vp) {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vp.getLayoutParams();
lp.height = mEmojiKeyboardHeight;
lp.bottomMargin = mEmojiPagerBottomMargin;
vp.setLayoutParams(lp);
}
public void setCategoryPageIdViewProperties(LinearLayout ll) {
public void setCategoryPageIdViewProperties(final LinearLayout ll) {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.height = mEmojiCategoryPageIdViewHeight;
ll.setLayoutParams(lp);
}
public void setActionBarProperties(LinearLayout ll) {
public void setActionBarProperties(final LinearLayout ll) {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.height = mEmojiActionBarHeight - mBottomPadding;
ll.setLayoutParams(lp);
}
public void setKeyProperties(ImageView ib) {
public void setKeyProperties(final ImageView ib) {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ib.getLayoutParams();
lp.leftMargin = mKeyHorizontalGap / 2;
lp.rightMargin = mKeyHorizontalGap / 2;

View File

@ -16,15 +16,16 @@
package com.android.inputmethod.latin.utils;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.settings.SettingsValues;
import android.text.TextUtils;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.settings.SettingsValues;
import java.io.Closeable;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
@ -383,7 +384,7 @@ public final class StringUtils {
return false;
}
public static boolean isEmptyStringOrWhiteSpaces(String s) {
public static boolean isEmptyStringOrWhiteSpaces(final String s) {
final int N = codePointCount(s);
for (int i = 0; i < N; ++i) {
if (!Character.isWhitespace(s.codePointAt(i))) {
@ -394,7 +395,7 @@ public final class StringUtils {
}
@UsedForTesting
public static String byteArrayToHexString(byte[] bytes) {
public static String byteArrayToHexString(final byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return EMPTY_STRING;
}
@ -409,7 +410,7 @@ public final class StringUtils {
* Convert hex string to byte array. The string length must be an even number.
*/
@UsedForTesting
public static byte[] hexStringToByteArray(String hexString) {
public static byte[] hexStringToByteArray(final String hexString) {
if (TextUtils.isEmpty(hexString)) {
return null;
}
@ -426,19 +427,22 @@ public final class StringUtils {
return bytes;
}
public static List<Object> jsonStrToList(String s) {
final ArrayList<Object> retval = CollectionUtils.newArrayList();
private static final String INTEGER_CLASS_NAME = Integer.class.getSimpleName();
private static final String STRING_CLASS_NAME = String.class.getSimpleName();
public static List<Object> jsonStrToList(final String s) {
final ArrayList<Object> list = CollectionUtils.newArrayList();
final JsonReader reader = new JsonReader(new StringReader(s));
try {
reader.beginArray();
while(reader.hasNext()) {
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
final String name = reader.nextName();
if (name.equals(Integer.class.getSimpleName())) {
retval.add(reader.nextInt());
} else if (name.equals(String.class.getSimpleName())) {
retval.add(reader.nextString());
if (name.equals(INTEGER_CLASS_NAME)) {
list.add(reader.nextInt());
} else if (name.equals(STRING_CLASS_NAME)) {
list.add(reader.nextString());
} else {
Log.w(TAG, "Invalid name: " + name);
reader.skipValue();
@ -447,18 +451,15 @@ public final class StringUtils {
reader.endObject();
}
reader.endArray();
return retval;
} catch (IOException e) {
return list;
} catch (final IOException e) {
} finally {
try {
reader.close();
} catch (IOException e) {
}
close(reader);
}
return Collections.<Object>emptyList();
}
public static String listToJsonStr(List<Object> list) {
public static String listToJsonStr(final List<Object> list) {
if (list == null || list.isEmpty()) {
return EMPTY_STRING;
}
@ -469,23 +470,28 @@ public final class StringUtils {
for (final Object o : list) {
writer.beginObject();
if (o instanceof Integer) {
writer.name(Integer.class.getSimpleName()).value((Integer)o);
writer.name(INTEGER_CLASS_NAME).value((Integer)o);
} else if (o instanceof String) {
writer.name(String.class.getSimpleName()).value((String)o);
writer.name(STRING_CLASS_NAME).value((String)o);
}
writer.endObject();
}
writer.endArray();
return sw.toString();
} catch (IOException e) {
} catch (final IOException e) {
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
}
close(writer);
}
return EMPTY_STRING;
}
private static void close(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (final IOException e) {
// Ignore
}
}
}