blob: 4f5c59ce0634fa4727c039cc323e3d2edee260bc [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;
Jeff Sharkey1c7736d2017-07-06 10:33:25 -060023import android.os.Trace;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070024import android.os.UserHandle;
Amith Yamasani37a40c22015-06-17 13:25:42 -070025import android.util.ArrayMap;
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070026import android.util.Slog;
Yi Jin148d7f42017-11-28 14:23:56 -080027import android.util.proto.ProtoOutputStream;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070028
Jeff Sharkey84a4c972016-04-18 15:36:39 -060029import com.android.internal.util.ProgressReporter;
30
31import java.io.PrintWriter;
32import java.util.ArrayList;
33
Amith Yamasani37a40c22015-06-17 13:25:42 -070034public final class UserState {
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070035 private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM;
36
Dianne Hackborn36d337a2012-10-08 14:33:47 -070037 // User is first coming up.
Dianne Hackborn80a4af22012-08-27 19:18:31 -070038 public final static int STATE_BOOTING = 0;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060039 // User is in the locked state.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070040 public final static int STATE_RUNNING_LOCKED = 1;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060041 // User is in the unlocking state.
42 public final static int STATE_RUNNING_UNLOCKING = 2;
43 // User is in the running state.
44 public final static int STATE_RUNNING_UNLOCKED = 3;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070045 // User is in the initial process of being stopped.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060046 public final static int STATE_STOPPING = 4;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070047 // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN.
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060048 public final static int STATE_SHUTDOWN = 5;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070049
50 public final UserHandle mHandle;
51 public final ArrayList<IStopUserCallback> mStopCallbacks
52 = new ArrayList<IStopUserCallback>();
Jeff Sharkey84a4c972016-04-18 15:36:39 -060053 public final ProgressReporter mUnlockProgress;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070054
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070055 public int state = STATE_BOOTING;
56 public int lastState = STATE_BOOTING;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070057 public boolean switching;
Jeff Sharkey24d94912016-07-07 12:33:48 -060058 public boolean tokenProvided;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070059
Amith Yamasani37a40c22015-06-17 13:25:42 -070060 /**
61 * The last time that a provider was reported to usage stats as being brought to important
62 * foreground procstate.
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -070063 * <p><strong>Important: </strong>Only access this field when holding ActivityManagerService
64 * lock.
Amith Yamasani37a40c22015-06-17 13:25:42 -070065 */
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -070066 final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>();
Amith Yamasani37a40c22015-06-17 13:25:42 -070067
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -080068 public UserState(UserHandle handle) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070069 mHandle = handle;
Jeff Sharkey84a4c972016-04-18 15:36:39 -060070 mUnlockProgress = new ProgressReporter(handle.getIdentifier());
Dianne Hackborn80a4af22012-08-27 19:18:31 -070071 }
72
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060073 public boolean setState(int oldState, int newState) {
74 if (state == oldState) {
75 setState(newState);
76 return true;
77 } else {
78 Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state "
79 + stateToString(oldState) + " but was in state " + stateToString(state));
80 return false;
81 }
82 }
83
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070084 public void setState(int newState) {
Fyodor Kupolovfa0edc82017-03-01 15:53:57 -080085 if (newState == state) {
86 return;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070087 }
Jeff Sharkey1c7736d2017-07-06 10:33:25 -060088 final int userId = mHandle.getIdentifier();
89 if (state != STATE_BOOTING) {
90 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
91 stateToString(state) + " " + userId, userId);
92 }
93 if (newState != STATE_SHUTDOWN) {
94 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
95 stateToString(newState) + " " + userId, userId);
96 }
97 Slog.i(TAG, "User " + userId + " state changed from "
Fyodor Kupolovfa0edc82017-03-01 15:53:57 -080098 + stateToString(state) + " to " + stateToString(newState));
Jeff Sharkey1c7736d2017-07-06 10:33:25 -060099 EventLogTags.writeAmUserStateChanged(userId, newState);
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700100 lastState = state;
101 state = newState;
102 }
103
Makoto Onuki88aef752017-03-29 16:52:03 -0700104 public static String stateToString(int state) {
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700105 switch (state) {
106 case STATE_BOOTING: return "BOOTING";
107 case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED";
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600108 case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING";
109 case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED";
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700110 case STATE_STOPPING: return "STOPPING";
111 case STATE_SHUTDOWN: return "SHUTDOWN";
112 default: return Integer.toString(state);
113 }
114 }
115
Yi Jin148d7f42017-11-28 14:23:56 -0800116 public static int stateToProtoEnum(int state) {
117 switch (state) {
118 case STATE_BOOTING: return UserStateProto.STATE_BOOTING;
119 case STATE_RUNNING_LOCKED: return UserStateProto.STATE_RUNNING_LOCKED;
120 case STATE_RUNNING_UNLOCKING: return UserStateProto.STATE_RUNNING_UNLOCKING;
121 case STATE_RUNNING_UNLOCKED: return UserStateProto.STATE_RUNNING_UNLOCKED;
122 case STATE_STOPPING: return UserStateProto.STATE_STOPPING;
123 case STATE_SHUTDOWN: return UserStateProto.STATE_SHUTDOWN;
124 default: return state;
125 }
126 }
127
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700128 void dump(String prefix, PrintWriter pw) {
129 pw.print(prefix);
130 pw.print("state="); pw.print(stateToString(state));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700131 if (switching) pw.print(" SWITCHING");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700132 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700133 }
Yi Jin148d7f42017-11-28 14:23:56 -0800134
135 void writeToProto(ProtoOutputStream proto, long fieldId) {
136 final long token = proto.start(fieldId);
137 proto.write(UserStateProto.STATE, stateToProtoEnum(state));
138 proto.write(UserStateProto.SWITCHING, switching);
139 proto.end(token);
140 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700141}