blob: 6e2342b2556b95bcf144f78276204621e3cd3afe [file] [log] [blame]
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001/*
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
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070022import 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
Dianne Hackborn80a4af22012-08-27 19:18:31 -070026import android.app.IStopUserCallback;
27import android.os.UserHandle;
Amith Yamasani37a40c22015-06-17 13:25:42 -070028import android.util.ArrayMap;
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070029import android.util.Slog;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070030
Amith Yamasani37a40c22015-06-17 13:25:42 -070031public final class UserState {
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070032 private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM;
33
Dianne Hackborn36d337a2012-10-08 14:33:47 -070034 // User is first coming up.
Dianne Hackborn80a4af22012-08-27 19:18:31 -070035 public final static int STATE_BOOTING = 0;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060036 // User is in the locked state.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070037 public final static int STATE_RUNNING_LOCKED = 1;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060038 // 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;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070042 // User is in the initial process of being stopped.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060043 public final static int STATE_STOPPING = 4;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070044 // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060045 public final static int STATE_SHUTDOWN = 5;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070046
47 public final UserHandle mHandle;
48 public final ArrayList<IStopUserCallback> mStopCallbacks
49 = new ArrayList<IStopUserCallback>();
50
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070051 public int state = STATE_BOOTING;
52 public int lastState = STATE_BOOTING;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070053 public boolean switching;
54 public boolean initializing;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070055
Amith Yamasani37a40c22015-06-17 13:25:42 -070056 /**
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
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -080062 public UserState(UserHandle handle) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070063 mHandle = handle;
64 }
65
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060066 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
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070077 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));
Dianne Hackborn36d337a2012-10-08 14:33:47 -070081 }
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070082 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";
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060090 case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING";
91 case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED";
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070092 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));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700101 if (switching) pw.print(" SWITCHING");
102 if (initializing) pw.print(" INITIALIZING");
103 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700104 }
105}