Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.cts;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 /**
     23  * CommandHistory holds max 50 executed command.
     24  * User can view what has been input and select one to execute again.
     25  * Example:
     26  * <ul>
     27  *    <li> "cts_host > h"  to view latest 50 executed command.
     28  *    <li> "cts_host > h 10" to view latest 10 executed command.
     29  *    <li> "cts_host > h -e 5" to execute the 5th executed command.
     30  * </ul>
     31  */
     32 public class CommandHistory {
     33     private static final int CMD_RECORD_DEPTH = 50;
     34 
     35     private List<String> mCmdRecords;
     36 
     37     public CommandHistory() {
     38         mCmdRecords = new ArrayList<String>();
     39     }
     40 
     41     /**
     42      * Check if the command is history command.
     43      *
     44      * @param cmd The command string.
     45      * @return If it's history command, return true; else return false.
     46      */
     47     public boolean isHistoryCommand(final String cmd) {
     48         return CTSCommand.HISTORY.equals(cmd) || CTSCommand.H.equals(cmd);
     49     }
     50 
     51     /**
     52      * Get the number of commands recorded.
     53      *
     54      * @return The number of commands recorded.
     55      */
     56     public int size() {
     57         return mCmdRecords.size();
     58     }
     59 
     60     /**
     61      * Get command by index from command history cache.
     62      *
     63      * @param index The command index.
     64      * @return The command corresponding to the command index.
     65      */
     66     public String get(final int index) {
     67         return mCmdRecords.get(index);
     68     }
     69 
     70     /**
     71      * display specified number of commands from command history cache onto CTS console.
     72      *
     73      * @param cmdCount The command count requested.
     74      */
     75     public void show(final int cmdCount) {
     76         int cmdSize = mCmdRecords.size();
     77         int start = 0;
     78 
     79         if (cmdSize == 0) {
     80             CUIOutputStream.println("no history command list");
     81             return;
     82         }
     83         if (cmdCount < cmdSize) {
     84             start = cmdSize - cmdCount;
     85         }
     86 
     87         for (; start < cmdSize; start ++) {
     88             String cmdLine = mCmdRecords.get(start);
     89             CUIOutputStream.println("  " + Long.toString(start) + "\t" + cmdLine);
     90         }
     91     }
     92 
     93     /**
     94      * Add a command to the command cache.
     95      *
     96      * @param cp The command container.
     97      * @param cmdLine The command line.
     98      */
     99     public void addCommand(final CommandParser cp,
    100             final String cmdLine) {
    101         if ((cmdLine == null) || (cmdLine.length() == 0)) {
    102             return;
    103         }
    104 
    105         if (isValidCommand(cp.getAction()) && (!hasCommand(cmdLine))) {
    106             mCmdRecords.add(cmdLine);
    107             if (mCmdRecords.size() > CMD_RECORD_DEPTH) {
    108                 mCmdRecords.remove(0);
    109             }
    110         }
    111     }
    112 
    113     /**
    114      * Check if the command contains valid action.
    115      *
    116      * @param action The action contained in the command.
    117      * @return If valid, return true; else, return false.
    118      */
    119     private boolean isValidCommand(final String action) {
    120         if (!(CTSCommand.HISTORY.equals(action) || CTSCommand.H.equals(action))) {
    121             if (CTSCommand.ADD.equals(action)
    122                     || CTSCommand.EXIT.equals(action)
    123                     || CTSCommand.HELP.equals(action)
    124                     || CTSCommand.LIST.equals(action)
    125                     || CTSCommand.REMOVE.equals(action)
    126                     || CTSCommand.START.equals(action)) {
    127                 return true;
    128             }
    129         }
    130         return false;
    131     }
    132 
    133     /**
    134      * Check if the command is a duplicate one.
    135      *
    136      * @param cmdLine The command to be checked against the commands recorded.
    137      * @return If duplicated, return true; else, return false.
    138      */
    139     private boolean hasCommand(final String cmdLine) {
    140         for(String cmd : mCmdRecords) {
    141             if (cmd.equals(cmdLine)) {
    142                 return true;
    143             }
    144         }
    145         return false;
    146     }
    147 }
    148