Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 19 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 20 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 21 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 22 | import android.app.IStopUserCallback; |
Jeff Sharkey | 1c7736d | 2017-07-06 10:33:25 -0600 | [diff] [blame] | 23 | import android.os.Trace; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 25 | import android.util.ArrayMap; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame^] | 27 | import android.util.proto.ProtoOutputStream; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 28 | |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 29 | import com.android.internal.util.ProgressReporter; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame^] | 30 | import com.android.server.am.proto.UserStateProto; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 31 | |
| 32 | import java.io.PrintWriter; |
| 33 | import java.util.ArrayList; |
| 34 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 35 | public final class UserState { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 36 | private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM; |
| 37 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 38 | // User is first coming up. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 39 | public final static int STATE_BOOTING = 0; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 40 | // User is in the locked state. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 41 | public final static int STATE_RUNNING_LOCKED = 1; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 42 | // User is in the unlocking state. |
| 43 | public final static int STATE_RUNNING_UNLOCKING = 2; |
| 44 | // User is in the running state. |
| 45 | public final static int STATE_RUNNING_UNLOCKED = 3; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 46 | // User is in the initial process of being stopped. |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 47 | public final static int STATE_STOPPING = 4; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 48 | // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN. |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 49 | public final static int STATE_SHUTDOWN = 5; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 50 | |
| 51 | public final UserHandle mHandle; |
| 52 | public final ArrayList<IStopUserCallback> mStopCallbacks |
| 53 | = new ArrayList<IStopUserCallback>(); |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 54 | public final ProgressReporter mUnlockProgress; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 55 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 56 | public int state = STATE_BOOTING; |
| 57 | public int lastState = STATE_BOOTING; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 58 | public boolean switching; |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 59 | public boolean tokenProvided; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 60 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 61 | /** |
| 62 | * The last time that a provider was reported to usage stats as being brought to important |
| 63 | * foreground procstate. |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 64 | * <p><strong>Important: </strong>Only access this field when holding ActivityManagerService |
| 65 | * lock. |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 66 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 67 | final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>(); |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 68 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 69 | public UserState(UserHandle handle) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 70 | mHandle = handle; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 71 | mUnlockProgress = new ProgressReporter(handle.getIdentifier()); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 74 | public boolean setState(int oldState, int newState) { |
| 75 | if (state == oldState) { |
| 76 | setState(newState); |
| 77 | return true; |
| 78 | } else { |
| 79 | Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state " |
| 80 | + stateToString(oldState) + " but was in state " + stateToString(state)); |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 85 | public void setState(int newState) { |
Fyodor Kupolov | fa0edc8 | 2017-03-01 15:53:57 -0800 | [diff] [blame] | 86 | if (newState == state) { |
| 87 | return; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 88 | } |
Jeff Sharkey | 1c7736d | 2017-07-06 10:33:25 -0600 | [diff] [blame] | 89 | final int userId = mHandle.getIdentifier(); |
| 90 | if (state != STATE_BOOTING) { |
| 91 | Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 92 | stateToString(state) + " " + userId, userId); |
| 93 | } |
| 94 | if (newState != STATE_SHUTDOWN) { |
| 95 | Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 96 | stateToString(newState) + " " + userId, userId); |
| 97 | } |
| 98 | Slog.i(TAG, "User " + userId + " state changed from " |
Fyodor Kupolov | fa0edc8 | 2017-03-01 15:53:57 -0800 | [diff] [blame] | 99 | + stateToString(state) + " to " + stateToString(newState)); |
Jeff Sharkey | 1c7736d | 2017-07-06 10:33:25 -0600 | [diff] [blame] | 100 | EventLogTags.writeAmUserStateChanged(userId, newState); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 101 | lastState = state; |
| 102 | state = newState; |
| 103 | } |
| 104 | |
Makoto Onuki | 88aef75 | 2017-03-29 16:52:03 -0700 | [diff] [blame] | 105 | public static String stateToString(int state) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 106 | switch (state) { |
| 107 | case STATE_BOOTING: return "BOOTING"; |
| 108 | case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED"; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 109 | case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING"; |
| 110 | case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED"; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 111 | case STATE_STOPPING: return "STOPPING"; |
| 112 | case STATE_SHUTDOWN: return "SHUTDOWN"; |
| 113 | default: return Integer.toString(state); |
| 114 | } |
| 115 | } |
| 116 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame^] | 117 | public static int stateToProtoEnum(int state) { |
| 118 | switch (state) { |
| 119 | case STATE_BOOTING: return UserStateProto.STATE_BOOTING; |
| 120 | case STATE_RUNNING_LOCKED: return UserStateProto.STATE_RUNNING_LOCKED; |
| 121 | case STATE_RUNNING_UNLOCKING: return UserStateProto.STATE_RUNNING_UNLOCKING; |
| 122 | case STATE_RUNNING_UNLOCKED: return UserStateProto.STATE_RUNNING_UNLOCKED; |
| 123 | case STATE_STOPPING: return UserStateProto.STATE_STOPPING; |
| 124 | case STATE_SHUTDOWN: return UserStateProto.STATE_SHUTDOWN; |
| 125 | default: return state; |
| 126 | } |
| 127 | } |
| 128 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 129 | void dump(String prefix, PrintWriter pw) { |
| 130 | pw.print(prefix); |
| 131 | pw.print("state="); pw.print(stateToString(state)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 132 | if (switching) pw.print(" SWITCHING"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 133 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 134 | } |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame^] | 135 | |
| 136 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 137 | final long token = proto.start(fieldId); |
| 138 | proto.write(UserStateProto.STATE, stateToProtoEnum(state)); |
| 139 | proto.write(UserStateProto.SWITCHING, switching); |
| 140 | proto.end(token); |
| 141 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 142 | } |