blob: 7b18a1783aaef8001011cad04e9ddc071bc40eb8 [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 Sharkeybedbaa92015-12-02 16:42:25 -070036 // User is in the locked running state.
37 public final static int STATE_RUNNING_LOCKED = 1;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070038 // User is in the normal running state.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070039 public final static int STATE_RUNNING = 2;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070040 // User is in the initial process of being stopped.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070041 public final static int STATE_STOPPING = 3;
Dianne Hackborn36d337a2012-10-08 14:33:47 -070042 // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070043 public final static int STATE_SHUTDOWN = 4;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070044
45 public final UserHandle mHandle;
46 public final ArrayList<IStopUserCallback> mStopCallbacks
47 = new ArrayList<IStopUserCallback>();
48
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070049 public int state = STATE_BOOTING;
50 public int lastState = STATE_BOOTING;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070051 public boolean switching;
52 public boolean initializing;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070053
Amith Yamasani37a40c22015-06-17 13:25:42 -070054 /**
55 * The last time that a provider was reported to usage stats as being brought to important
56 * foreground procstate.
57 */
58 public final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>();
59
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -080060 public UserState(UserHandle handle) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070061 mHandle = handle;
62 }
63
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070064 public void setState(int newState) {
65 if (DEBUG_MU) {
66 Slog.i(TAG, "User " + mHandle.getIdentifier() + " state changed from "
67 + stateToString(state) + " to " + stateToString(newState));
Dianne Hackborn36d337a2012-10-08 14:33:47 -070068 }
Jeff Sharkeybedbaa92015-12-02 16:42:25 -070069 lastState = state;
70 state = newState;
71 }
72
73 private static String stateToString(int state) {
74 switch (state) {
75 case STATE_BOOTING: return "BOOTING";
76 case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED";
77 case STATE_RUNNING: return "RUNNING";
78 case STATE_STOPPING: return "STOPPING";
79 case STATE_SHUTDOWN: return "SHUTDOWN";
80 default: return Integer.toString(state);
81 }
82 }
83
84 void dump(String prefix, PrintWriter pw) {
85 pw.print(prefix);
86 pw.print("state="); pw.print(stateToString(state));
87 pw.print(" lastState="); pw.print(stateToString(lastState));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070088 if (switching) pw.print(" SWITCHING");
89 if (initializing) pw.print(" INITIALIZING");
90 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -070091 }
92}