diff options
Diffstat (limited to 'src/com/android/tv/data/ChannelNumber.java')
-rw-r--r-- | src/com/android/tv/data/ChannelNumber.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/com/android/tv/data/ChannelNumber.java b/src/com/android/tv/data/ChannelNumber.java new file mode 100644 index 00000000..8e9c3cb2 --- /dev/null +++ b/src/com/android/tv/data/ChannelNumber.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tv.data; + +import android.support.annotation.NonNull; +import android.view.KeyEvent; + +/** + * A convenience class to handle channel number. + */ +public final class ChannelNumber implements Comparable<ChannelNumber> { + public static final String PRIMARY_CHANNEL_DELIMITER = "-"; + public static final String[] CHANNEL_DELIMITERS = {"-", ".", " "}; + + private static final int[] CHANNEL_DELIMITER_KEYCODES = { + KeyEvent.KEYCODE_MINUS, KeyEvent.KEYCODE_NUMPAD_SUBTRACT, KeyEvent.KEYCODE_PERIOD, + KeyEvent.KEYCODE_NUMPAD_DOT, KeyEvent.KEYCODE_SPACE + }; + + public String majorNumber; + public boolean hasDelimiter; + public String minorNumber; + + public ChannelNumber() { + reset(); + } + + public ChannelNumber(String major, boolean hasDelimiter, String minor) { + setChannelNumber(major, hasDelimiter, minor); + } + + public void reset() { + setChannelNumber("", false, ""); + } + + public void setChannelNumber(String majorNumber, boolean hasDelimiter, String minorNumber) { + this.majorNumber = majorNumber; + this.hasDelimiter = hasDelimiter; + this.minorNumber = minorNumber; + } + + @Override + public String toString() { + if (hasDelimiter) { + return majorNumber + PRIMARY_CHANNEL_DELIMITER + minorNumber; + } + return majorNumber; + } + + @Override + public int compareTo(@NonNull ChannelNumber another) { + int major = Integer.parseInt(majorNumber); + int minor = hasDelimiter ? Integer.parseInt(minorNumber) : 0; + + int opponentMajor = Integer.parseInt(another.majorNumber); + int opponentMinor = another.hasDelimiter + ? Integer.parseInt(another.minorNumber) : 0; + if (major == opponentMajor) { + return minor - opponentMinor; + } + return major - opponentMajor; + } + + public static boolean isChannelNumberDelimiterKey(int keyCode) { + for (int delimiterKeyCode : CHANNEL_DELIMITER_KEYCODES) { + if (delimiterKeyCode == keyCode) { + return true; + } + } + return false; + } + + public static ChannelNumber parseChannelNumber(String number) { + if (number == null) { + return null; + } + ChannelNumber ret = new ChannelNumber(); + int indexOfDelimiter = -1; + for (String delimiter : CHANNEL_DELIMITERS) { + indexOfDelimiter = number.indexOf(delimiter); + if (indexOfDelimiter >= 0) { + break; + } + } + if (indexOfDelimiter == 0 || indexOfDelimiter == number.length() - 1) { + return null; + } + if (indexOfDelimiter < 0) { + ret.majorNumber = number; + return ret; + } + ret.hasDelimiter = true; + ret.majorNumber = number.substring(0, indexOfDelimiter); + ret.minorNumber = number.substring(indexOfDelimiter + 1); + return ret; + } + + public static int compare(String lhs, String rhs) { + if (lhs == null && rhs == null) { + return 0; + } else if (lhs == null /* && rhs != null */) { + return -1; + } else if (lhs != null && rhs == null) { + return 1; + } + return parseChannelNumber(lhs).compareTo(parseChannelNumber(rhs)); + } +} |