diff options
author | Mike Lockwood <lockwood@google.com> | 2015-01-21 11:11:14 -0800 |
---|---|---|
committer | Mike Lockwood <lockwood@google.com> | 2015-01-21 11:11:14 -0800 |
commit | a1329aebe7ef21f18826d495b0df7fdbec1ae096 (patch) | |
tree | 7b91954cd172ea8ba41ca720f850a79512e7c19e | |
parent | ab75f637878ce3237e675cf3e1a5a43c3c009b09 (diff) | |
download | frameworks_base-a1329aebe7ef21f18826d495b0df7fdbec1ae096.zip frameworks_base-a1329aebe7ef21f18826d495b0df7fdbec1ae096.tar.gz frameworks_base-a1329aebe7ef21f18826d495b0df7fdbec1ae096.tar.bz2 |
Watch for NumberFormatExceptions in AlsaCardsParser and AlsaDevicesParser
Bug: 19082426
Change-Id: I130d4df290716339326a28d249217a8a04220176
-rw-r--r-- | core/java/android/alsa/AlsaCardsParser.java | 15 | ||||
-rw-r--r-- | core/java/android/alsa/AlsaDevicesParser.java | 101 |
2 files changed, 65 insertions, 51 deletions
diff --git a/core/java/android/alsa/AlsaCardsParser.java b/core/java/android/alsa/AlsaCardsParser.java index 8f9c56c..2c7d502 100644 --- a/core/java/android/alsa/AlsaCardsParser.java +++ b/core/java/android/alsa/AlsaCardsParser.java @@ -31,6 +31,8 @@ public class AlsaCardsParser { private static final String TAG = "AlsaCardsParser"; protected static final boolean DEBUG = true; + private static final String kCardsFilePath = "/proc/asound/cards"; + private static LineTokenizer mTokenizer = new LineTokenizer(" :[]"); private ArrayList<AlsaCardRecord> mCardRecords = new ArrayList<AlsaCardRecord>(); @@ -56,8 +58,14 @@ public class AlsaCardsParser { tokenIndex = mTokenizer.nextToken(line, tokenIndex); delimIndex = mTokenizer.nextDelimiter(line, tokenIndex); - // mCardNum - mCardNum = Integer.parseInt(line.substring(tokenIndex, delimIndex)); + try { + // mCardNum + mCardNum = Integer.parseInt(line.substring(tokenIndex, delimIndex)); + } catch (NumberFormatException e) { + Slog.e(TAG, "Failed to parse line " + lineIndex + " of " + kCardsFilePath + + ": " + line.substring(tokenIndex, delimIndex)); + return false; + } // mField1 tokenIndex = mTokenizer.nextToken(line, delimIndex); @@ -93,8 +101,7 @@ public class AlsaCardsParser { } mCardRecords = new ArrayList<AlsaCardRecord>(); - final String cardsFilePath = "/proc/asound/cards"; - File cardsFile = new File(cardsFilePath); + File cardsFile = new File(kCardsFilePath); try { FileReader reader = new FileReader(cardsFile); BufferedReader bufferedReader = new BufferedReader(reader); diff --git a/core/java/android/alsa/AlsaDevicesParser.java b/core/java/android/alsa/AlsaDevicesParser.java index 3581cb6..b140d3d 100644 --- a/core/java/android/alsa/AlsaDevicesParser.java +++ b/core/java/android/alsa/AlsaDevicesParser.java @@ -31,6 +31,8 @@ public class AlsaDevicesParser { private static final String TAG = "AlsaDevicesParser"; protected static final boolean DEBUG = false; + private static final String kDevicesFilePath = "/proc/asound/devices"; + private static final int kIndex_CardDeviceField = 5; private static final int kStartIndex_CardNum = 6; private static final int kEndIndex_CardNum = 8; // one past @@ -89,51 +91,57 @@ public class AlsaDevicesParser { } String token = line.substring(tokenOffset, delimOffset); - switch (tokenIndex) { - case kToken_LineNum: - // ignore - break; - - case kToken_CardNum: - mCardNum = Integer.parseInt(token); - if (line.charAt(delimOffset) != '-') { - tokenIndex++; // no device # in the token stream - } - break; - - case kToken_DeviceNum: - mDeviceNum = Integer.parseInt(token); - break; - - case kToken_Type0: - if (token.equals("digital")) { - // NOP - } else if (token.equals("control")) { - mDeviceType = kDeviceType_Control; - } else if (token.equals("raw")) { - // NOP - } - break; - - case kToken_Type1: - if (token.equals("audio")) { - mDeviceType = kDeviceType_Audio; - } else if (token.equals("midi")) { - mDeviceType = kDeviceType_MIDI; - mHasMIDIDevices = true; - } - break; - - case kToken_Type2: - if (token.equals("capture")) { - mDeviceDir = kDeviceDir_Capture; - mHasCaptureDevices = true; - } else if (token.equals("playback")) { - mDeviceDir = kDeviceDir_Playback; - mHasPlaybackDevices = true; - } - break; - } // switch (tokenIndex) + try { + switch (tokenIndex) { + case kToken_LineNum: + // ignore + break; + + case kToken_CardNum: + mCardNum = Integer.parseInt(token); + if (line.charAt(delimOffset) != '-') { + tokenIndex++; // no device # in the token stream + } + break; + + case kToken_DeviceNum: + mDeviceNum = Integer.parseInt(token); + break; + + case kToken_Type0: + if (token.equals("digital")) { + // NOP + } else if (token.equals("control")) { + mDeviceType = kDeviceType_Control; + } else if (token.equals("raw")) { + // NOP + } + break; + + case kToken_Type1: + if (token.equals("audio")) { + mDeviceType = kDeviceType_Audio; + } else if (token.equals("midi")) { + mDeviceType = kDeviceType_MIDI; + mHasMIDIDevices = true; + } + break; + + case kToken_Type2: + if (token.equals("capture")) { + mDeviceDir = kDeviceDir_Capture; + mHasCaptureDevices = true; + } else if (token.equals("playback")) { + mDeviceDir = kDeviceDir_Playback; + mHasPlaybackDevices = true; + } + break; + } // switch (tokenIndex) + } catch (NumberFormatException e) { + Slog.e(TAG, "Failed to parse token " + tokenIndex + " of " + kDevicesFilePath + + " token: " + token); + return false; + } tokenIndex++; } // while (true) @@ -245,8 +253,7 @@ public class AlsaDevicesParser { public void scan() { mDeviceRecords.clear(); - final String devicesFilePath = "/proc/asound/devices"; - File devicesFile = new File(devicesFilePath); + File devicesFile = new File(kDevicesFilePath); try { FileReader reader = new FileReader(devicesFile); BufferedReader bufferedReader = new BufferedReader(reader); |