1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.exoplayer2.util;
17 
18 import androidx.annotation.IntDef;
19 import com.google.android.exoplayer2.Player;
20 import java.lang.annotation.Documented;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 
24 /**
25  * Util class for repeat mode handling.
26  */
27 public final class RepeatModeUtil {
28 
29   // LINT.IfChange
30   /**
31    * Set of repeat toggle modes. Can be combined using bit-wise operations. Possible flag values are
32    * {@link #REPEAT_TOGGLE_MODE_NONE}, {@link #REPEAT_TOGGLE_MODE_ONE} and {@link
33    * #REPEAT_TOGGLE_MODE_ALL}.
34    */
35   @Documented
36   @Retention(RetentionPolicy.SOURCE)
37   @IntDef(
38       flag = true,
39       value = {REPEAT_TOGGLE_MODE_NONE, REPEAT_TOGGLE_MODE_ONE, REPEAT_TOGGLE_MODE_ALL})
40   public @interface RepeatToggleModes {}
41   /**
42    * All repeat mode buttons disabled.
43    */
44   public static final int REPEAT_TOGGLE_MODE_NONE = 0;
45   /**
46    * "Repeat One" button enabled.
47    */
48   public static final int REPEAT_TOGGLE_MODE_ONE = 1;
49   /** "Repeat All" button enabled. */
50   public static final int REPEAT_TOGGLE_MODE_ALL = 1 << 1; // 2
51   // LINT.ThenChange(../../../../../../../../../ui/src/main/res/values/attrs.xml)
52 
RepeatModeUtil()53   private RepeatModeUtil() {
54     // Prevent instantiation.
55   }
56 
57   /**
58    * Gets the next repeat mode out of {@code enabledModes} starting from {@code currentMode}.
59    *
60    * @param currentMode The current repeat mode.
61    * @param enabledModes Bitmask of enabled modes.
62    * @return The next repeat mode.
63    */
getNextRepeatMode(@layer.RepeatMode int currentMode, int enabledModes)64   public static @Player.RepeatMode int getNextRepeatMode(@Player.RepeatMode int currentMode,
65       int enabledModes) {
66     for (int offset = 1; offset <= 2; offset++) {
67       @Player.RepeatMode int proposedMode = (currentMode + offset) % 3;
68       if (isRepeatModeEnabled(proposedMode, enabledModes)) {
69         return proposedMode;
70       }
71     }
72     return currentMode;
73   }
74 
75   /**
76    * Verifies whether a given {@code repeatMode} is enabled in the bitmask {@code enabledModes}.
77    *
78    * @param repeatMode The mode to check.
79    * @param enabledModes The bitmask representing the enabled modes.
80    * @return {@code true} if enabled.
81    */
isRepeatModeEnabled(@layer.RepeatMode int repeatMode, int enabledModes)82   public static boolean isRepeatModeEnabled(@Player.RepeatMode int repeatMode, int enabledModes) {
83     switch (repeatMode) {
84       case Player.REPEAT_MODE_OFF:
85         return true;
86       case Player.REPEAT_MODE_ONE:
87         return (enabledModes & REPEAT_TOGGLE_MODE_ONE) != 0;
88       case Player.REPEAT_MODE_ALL:
89         return (enabledModes & REPEAT_TOGGLE_MODE_ALL) != 0;
90       default:
91         return false;
92     }
93   }
94 
95 }
96