1 /*
2  * Copyright (C) 2012 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.example.android.lifecycle.util;
18 
19 import java.util.*;
20 
21 public class StatusTracker {
22   private Map<String, String> mStatusMap;
23   private List<String> mMethodList;
24   private static StatusTracker ourInstance = new StatusTracker();
25   private static final String STATUS_SUFFIX = "ed";
26 
getInstance()27   public static StatusTracker getInstance() {
28     return ourInstance;
29   }
30 
StatusTracker()31   private StatusTracker() {
32     mStatusMap = new LinkedHashMap<String, String>();
33     mMethodList = new ArrayList<String>();
34   }
35 
getMethodList()36   public List<String> getMethodList() {
37     return mMethodList;
38   }
39 
clear()40   public void clear() {
41     mMethodList.clear();
42     mStatusMap.clear();
43   }
44 
45   /**
46    * Adds the status value for the given activityName into the Map.
47    *
48    * @param activityName
49    * @param status
50    */
setStatus(String activityName, String status)51   public void setStatus(String activityName, String status) {
52     mMethodList.add(activityName + "." + status + "()");
53     if (mStatusMap.containsKey(activityName)) mStatusMap.remove(activityName);
54     mStatusMap.put(activityName, status);
55   }
56 
57   /**
58    * Gets the status value for the given activityName.
59    *
60    * @param activityName
61    * @return
62    */
getStatus(String activityName)63   public String getStatus(String activityName) {
64     String status = mStatusMap.get(activityName);
65     status = status.substring(2, status.length());
66 
67     // String manipulation to ensure the status value is spelled correctly.
68     if (status.endsWith("e")) {
69       status = status.substring(0, status.length() - 1);
70     }
71     if (status.endsWith("p")) {
72       status = status + "p";
73     }
74     status = status + STATUS_SUFFIX;
75     return status;
76   }
77 
keySet()78   public Set<String> keySet() {
79     return mStatusMap.keySet();
80   }
81 }
82