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 com.android.systemui.statusbar.notification.collection.ShadeListBuilder; 22 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 25 26 /** 27 * Used by {@link ShadeListBuilder} to track its internal state machine. 28 */ 29 public class PipelineState { 30 31 private @StateName int mState = STATE_IDLE; 32 33 /** Returns true if the current state matches <code>state</code> */ is(@tateName int state)34 public boolean is(@StateName int state) { 35 return state == mState; 36 } 37 getState()38 public @StateName int getState() { 39 return mState; 40 } 41 setState(@tateName int state)42 public void setState(@StateName int state) { 43 mState = state; 44 } 45 46 /** 47 * Increments the state from <code>(to - 1)</code> to <code>to</code>. If the current state 48 * isn't <code>(to - 1)</code>, throws an exception. 49 */ incrementTo(@tateName int to)50 public void incrementTo(@StateName int to) { 51 if (mState != to - 1) { 52 throw new IllegalStateException( 53 "Cannot increment from state " + mState + " to state " + to); 54 } 55 mState = to; 56 } 57 58 /** 59 * Throws an exception if the current state is not <code>state</code>. 60 */ requireState(@tateName int state)61 public void requireState(@StateName int state) { 62 if (state != mState) { 63 throw new IllegalStateException( 64 "Required state is <" + state + " but actual state is " + mState); 65 } 66 } 67 68 /** 69 * Throws an exception if the current state is >= <code>state</code>. 70 */ requireIsBefore(@tateName int state)71 public void requireIsBefore(@StateName int state) { 72 if (mState >= state) { 73 throw new IllegalStateException( 74 "Required state is <" + state + " but actual state is " + mState); 75 } 76 } 77 78 public static final int STATE_IDLE = 0; 79 public static final int STATE_BUILD_STARTED = 1; 80 public static final int STATE_RESETTING = 2; 81 public static final int STATE_PRE_GROUP_FILTERING = 3; 82 public static final int STATE_GROUPING = 4; 83 public static final int STATE_TRANSFORMING = 5; 84 public static final int STATE_GROUP_STABILIZING = 6; 85 public static final int STATE_SORTING = 7; 86 public static final int STATE_FINALIZE_FILTERING = 8; 87 public static final int STATE_FINALIZING = 9; 88 89 @IntDef(prefix = { "STATE_" }, value = { 90 STATE_IDLE, 91 STATE_BUILD_STARTED, 92 STATE_RESETTING, 93 STATE_PRE_GROUP_FILTERING, 94 STATE_GROUPING, 95 STATE_TRANSFORMING, 96 STATE_GROUP_STABILIZING, 97 STATE_SORTING, 98 STATE_FINALIZE_FILTERING, 99 STATE_FINALIZING, 100 }) 101 @Retention(RetentionPolicy.SOURCE) 102 public @interface StateName {} 103 } 104