UserState.java revision bd91e2f3f6aca512a02be645b2515b5e3331e177
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 17package com.android.server.am; 18 19import java.io.PrintWriter; 20import java.util.ArrayList; 21 22import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_MU; 23import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; 24import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; 25 26import android.app.IStopUserCallback; 27import android.os.UserHandle; 28import android.util.ArrayMap; 29import android.util.Slog; 30 31public final class UserState { 32 private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM; 33 34 // User is first coming up. 35 public final static int STATE_BOOTING = 0; 36 // User is in the locked state. 37 public final static int STATE_RUNNING_LOCKED = 1; 38 // User is in the unlocking state. 39 public final static int STATE_RUNNING_UNLOCKING = 2; 40 // User is in the running state. 41 public final static int STATE_RUNNING_UNLOCKED = 3; 42 // User is in the initial process of being stopped. 43 public final static int STATE_STOPPING = 4; 44 // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN. 45 public final static int STATE_SHUTDOWN = 5; 46 47 public final UserHandle mHandle; 48 public final ArrayList<IStopUserCallback> mStopCallbacks 49 = new ArrayList<IStopUserCallback>(); 50 51 public int state = STATE_BOOTING; 52 public int lastState = STATE_BOOTING; 53 public boolean switching; 54 public boolean initializing; 55 56 /** 57 * The last time that a provider was reported to usage stats as being brought to important 58 * foreground procstate. 59 */ 60 public final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>(); 61 62 public UserState(UserHandle handle) { 63 mHandle = handle; 64 } 65 66 public boolean setState(int oldState, int newState) { 67 if (state == oldState) { 68 setState(newState); 69 return true; 70 } else { 71 Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state " 72 + stateToString(oldState) + " but was in state " + stateToString(state)); 73 return false; 74 } 75 } 76 77 public void setState(int newState) { 78 if (DEBUG_MU) { 79 Slog.i(TAG, "User " + mHandle.getIdentifier() + " state changed from " 80 + stateToString(state) + " to " + stateToString(newState)); 81 } 82 lastState = state; 83 state = newState; 84 } 85 86 private static String stateToString(int state) { 87 switch (state) { 88 case STATE_BOOTING: return "BOOTING"; 89 case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED"; 90 case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING"; 91 case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED"; 92 case STATE_STOPPING: return "STOPPING"; 93 case STATE_SHUTDOWN: return "SHUTDOWN"; 94 default: return Integer.toString(state); 95 } 96 } 97 98 void dump(String prefix, PrintWriter pw) { 99 pw.print(prefix); 100 pw.print("state="); pw.print(stateToString(state)); 101 if (switching) pw.print(" SWITCHING"); 102 if (initializing) pw.print(" INITIALIZING"); 103 pw.println(); 104 } 105} 106