blob: 9970c82e2c75f81993a97ff50c68f9098acc0e14 [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
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070019import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
20import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
21
Dianne Hackborn80a4af22012-08-27 19:18:31 -070022import android.app.IStopUserCallback;
23import android.os.UserHandle;
Amith Yamasani37a40c22015-06-17 13:25:42 -070024import android.util.ArrayMap;
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070025import android.util.Slog;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070026
Jeff Sharkey84a4c972016-04-18 15:36:39 -060027import com.android.internal.util.ProgressReporter;
28
29import java.io.PrintWriter;
30import java.util.ArrayList;
31
Amith Yamasani37a40c22015-06-17 13:25:42 -070032public final class UserState {
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070033 private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM;
34
Dianne Hackborn36d337a2012-10-08 14:33:47 -070035 // User is first coming up.
Dianne Hackborn80a4af22012-08-27 19:18:31 -070036 public final static int STATE_BOOTING = 0;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060037 // User is in the locked state.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070038 public final static int STATE_RUNNING_LOCKED = 1;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060039 // User is in the unlocking state.
40 public final static int STATE_RUNNING_UNLOCKING = 2;
41 // User is in the running state.
42 public final static int STATE_RUNNING_UNLOCKED = 3;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070043 // User is in the initial process of being stopped.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060044 public final static int STATE_STOPPING = 4;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070045 // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060046 public final static int STATE_SHUTDOWN = 5;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070047
48 public final UserHandle mHandle;
49 public final ArrayList<IStopUserCallback> mStopCallbacks
50 = new ArrayList<IStopUserCallback>();
Jeff Sharkey84a4c972016-04-18 15:36:39 -060051 public final ProgressReporter mUnlockProgress;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070052
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070053 public int state = STATE_BOOTING;
54 public int lastState = STATE_BOOTING;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070055 public boolean switching;
Jeff Sharkey24d94912016-07-07 12:33:48 -060056 public boolean tokenProvided;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070057
Amith Yamasani37a40c22015-06-17 13:25:42 -070058 /**
59 * The last time that a provider was reported to usage stats as being brought to important
60 * foreground procstate.
61 */
62 public final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>();
63
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -080064 public UserState(UserHandle handle) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070065 mHandle = handle;
Jeff Sharkey84a4c972016-04-18 15:36:39 -060066 mUnlockProgress = new ProgressReporter(handle.getIdentifier());
Dianne Hackborn80a4af22012-08-27 19:18:31 -070067 }
68
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060069 public boolean setState(int oldState, int newState) {
70 if (state == oldState) {
71 setState(newState);
Jeff Sharkey97c6d3d2016-11-09 13:31:36 -070072 EventLogTags.writeAmUserStateChanged(mHandle.getIdentifier(), newState);
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060073 return true;
74 } else {
75 Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state "
76 + stateToString(oldState) + " but was in state " + stateToString(state));
77 return false;
78 }
79 }
80
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070081 public void setState(int newState) {
Fyodor Kupolovfa0edc82017-03-01 15:53:57 -080082 if (newState == state) {
83 return;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070084 }
Fyodor Kupolovfa0edc82017-03-01 15:53:57 -080085 Slog.i(TAG, "User " + mHandle.getIdentifier() + " state changed from "
86 + stateToString(state) + " to " + stateToString(newState));
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070087 lastState = state;
88 state = newState;
89 }
90
Makoto Onuki88aef752017-03-29 16:52:03 -070091 public static String stateToString(int state) {
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070092 switch (state) {
93 case STATE_BOOTING: return "BOOTING";
94 case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED";
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060095 case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING";
96 case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED";
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070097 case STATE_STOPPING: return "STOPPING";
98 case STATE_SHUTDOWN: return "SHUTDOWN";
99 default: return Integer.toString(state);
100 }
101 }
102
103 void dump(String prefix, PrintWriter pw) {
104 pw.print(prefix);
105 pw.print("state="); pw.print(stateToString(state));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700106 if (switching) pw.print(" SWITCHING");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700107 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700108 }
109}