1 /*
2  * Copyright (C) 2019 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 
17 package com.android.systemui.statusbar.notification.collection.listbuilder;
18 
19 import android.annotation.IntDef;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.systemui.statusbar.notification.collection.ShadeListBuilder;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Used by {@link ShadeListBuilder} to track its internal state machine.
30  */
31 public class PipelineState {
32 
33     private @StateName int mState = STATE_IDLE;
34 
35     /** Returns true if the current state matches <code>state</code> */
is(@tateName int state)36     public boolean is(@StateName int state) {
37         return state == mState;
38     }
39 
40     /** Get the current state's integer representation */
getState()41     public @StateName int getState() {
42         return mState;
43     }
44 
setState(@tateName int state)45     public void setState(@StateName int state) {
46         mState = state;
47     }
48 
49     /**
50      * Increments the state from <code>(to - 1)</code> to <code>to</code>. If the current state
51      * isn't <code>(to - 1)</code>, throws an exception.
52      */
incrementTo(@tateName int to)53     public void incrementTo(@StateName int to) {
54         if (mState != to - 1) {
55             throw new IllegalStateException(
56                     "Cannot increment from state " + mState + " to state " + to);
57         }
58         mState = to;
59     }
60 
61     /**
62      * Throws an exception if the current state is not <code>state</code>.
63      */
requireState(@tateName int state)64     public void requireState(@StateName int state) {
65         if (state != mState) {
66             throw new IllegalStateException(
67                     "Required state is <" + state + " but actual state is " + mState);
68         }
69     }
70 
71     /**
72      * Throws an exception if the current state is >= <code>state</code>.
73      */
requireIsBefore(@tateName int state)74     public void requireIsBefore(@StateName int state) {
75         if (mState >= state) {
76             throw new IllegalStateException(
77                     "Required state is <" + state + " but actual state is " + mState);
78         }
79     }
80 
81     /** Get the current state's string representation */
82     @NonNull
getStateName()83     public String getStateName() {
84         return getStateName(mState);
85     }
86 
87     /** Get the given state's string representation */
88     @NonNull
getStateName(@tateName int state)89     public static String getStateName(@StateName int state) {
90         switch (state) {
91             case STATE_IDLE:
92                 return "STATE_IDLE";
93             case STATE_BUILD_STARTED:
94                 return "STATE_BUILD_STARTED";
95             case STATE_RESETTING:
96                 return "STATE_RESETTING";
97             case STATE_PRE_GROUP_FILTERING:
98                 return "STATE_PRE_GROUP_FILTERING";
99             case STATE_GROUPING:
100                 return "STATE_GROUPING";
101             case STATE_TRANSFORMING:
102                 return "STATE_TRANSFORMING";
103             case STATE_GROUP_STABILIZING:
104                 return "STATE_GROUP_STABILIZING";
105             case STATE_SORTING:
106                 return "STATE_SORTING";
107             case STATE_FINALIZE_FILTERING:
108                 return "STATE_FINALIZE_FILTERING";
109             case STATE_FINALIZING:
110                 return "STATE_FINALIZING";
111             default:
112                 return "STATE:" + state;
113         }
114     }
115 
116     public static final int STATE_IDLE = 0;
117     public static final int STATE_BUILD_STARTED = 1;
118     public static final int STATE_RESETTING = 2;
119     public static final int STATE_PRE_GROUP_FILTERING = 3;
120     public static final int STATE_GROUPING = 4;
121     public static final int STATE_TRANSFORMING = 5;
122     public static final int STATE_GROUP_STABILIZING = 6;
123     public static final int STATE_SORTING = 7;
124     public static final int STATE_FINALIZE_FILTERING = 8;
125     public static final int STATE_FINALIZING = 9;
126 
127     @IntDef(prefix = { "STATE_" }, value = {
128             STATE_IDLE,
129             STATE_BUILD_STARTED,
130             STATE_RESETTING,
131             STATE_PRE_GROUP_FILTERING,
132             STATE_GROUPING,
133             STATE_TRANSFORMING,
134             STATE_GROUP_STABILIZING,
135             STATE_SORTING,
136             STATE_FINALIZE_FILTERING,
137             STATE_FINALIZING,
138     })
139     @Retention(RetentionPolicy.SOURCE)
140     public @interface StateName {}
141 }
142