Home | History | Annotate | Download | only in util
      1 /**
      2  * Copyright (C) 2009 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.internal.util;
     18 
     19 import android.os.Message;
     20 
     21 /**
     22  * {@hide}
     23  *
     24  * The class for implementing states in a HierarchicalStateMachine
     25  */
     26 public class HierarchicalState {
     27 
     28     /**
     29      * Constructor
     30      */
     31     protected HierarchicalState() {
     32     }
     33 
     34     /**
     35      * Called when a state is entered.
     36      */
     37     protected void enter() {
     38     }
     39 
     40     /**
     41      * Called when a message is to be processed by the
     42      * state machine.
     43      *
     44      * This routine is never reentered thus no synchronization
     45      * is needed as only one processMessage method will ever be
     46      * executing within a state machine at any given time. This
     47      * does mean that processing by this routine must be completed
     48      * as expeditiously as possible as no subsequent messages will
     49      * be processed until this routine returns.
     50      *
     51      * @param msg to process
     52      * @return true if processing has completed and false
     53      *         if the parent state's processMessage should
     54      *         be invoked.
     55      */
     56     protected boolean processMessage(Message msg) {
     57         return false;
     58     }
     59 
     60     /**
     61      * Called when a state is exited.
     62      */
     63     protected void exit() {
     64     }
     65 
     66     /**
     67      * @return name of state, but default returns the states
     68      * class name. An instance name would be better but requiring
     69      * it seems unnecessary.
     70      */
     71     public String getName() {
     72         String name = getClass().getName();
     73         int lastDollar = name.lastIndexOf('$');
     74         return name.substring(lastDollar + 1);
     75     }
     76 }
     77