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.DEBUG_MU; |
| 20 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 21 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 23 | import android.app.IStopUserCallback; |
| 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; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 28 | import com.android.internal.util.ProgressReporter; |
| 29 | |
| 30 | import java.io.PrintWriter; |
| 31 | import java.util.ArrayList; |
| 32 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 33 | public final class UserState { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 34 | private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM; |
| 35 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 36 | // User is first coming up. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 37 | public final static int STATE_BOOTING = 0; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 38 | // User is in the locked state. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 39 | public final static int STATE_RUNNING_LOCKED = 1; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 40 | // User is in the unlocking state. |
| 41 | public final static int STATE_RUNNING_UNLOCKING = 2; |
| 42 | // User is in the running state. |
| 43 | public final static int STATE_RUNNING_UNLOCKED = 3; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 44 | // User is in the initial process of being stopped. |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 45 | public final static int STATE_STOPPING = 4; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 46 | // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN. |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 47 | public final static int STATE_SHUTDOWN = 5; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 48 | |
| 49 | public final UserHandle mHandle; |
| 50 | public final ArrayList<IStopUserCallback> mStopCallbacks |
| 51 | = new ArrayList<IStopUserCallback>(); |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 52 | public final ProgressReporter mUnlockProgress; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 53 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 54 | public int state = STATE_BOOTING; |
| 55 | public int lastState = STATE_BOOTING; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 56 | public boolean switching; |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 57 | public boolean tokenProvided; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 58 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 59 | /** |
| 60 | * The last time that a provider was reported to usage stats as being brought to important |
| 61 | * foreground procstate. |
| 62 | */ |
| 63 | public final ArrayMap<String,Long> mProviderLastReportedFg = new ArrayMap<>(); |
| 64 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 65 | public UserState(UserHandle handle) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 66 | mHandle = handle; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 67 | mUnlockProgress = new ProgressReporter(handle.getIdentifier()); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 70 | public boolean setState(int oldState, int newState) { |
| 71 | if (state == oldState) { |
| 72 | setState(newState); |
Jeff Sharkey | 97c6d3d | 2016-11-09 13:31:36 -0700 | [diff] [blame] | 73 | EventLogTags.writeAmUserStateChanged(mHandle.getIdentifier(), newState); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 74 | return true; |
| 75 | } else { |
| 76 | Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state " |
| 77 | + stateToString(oldState) + " but was in state " + stateToString(state)); |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 82 | public void setState(int newState) { |
| 83 | if (DEBUG_MU) { |
| 84 | Slog.i(TAG, "User " + mHandle.getIdentifier() + " state changed from " |
| 85 | + stateToString(state) + " to " + stateToString(newState)); |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 86 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 87 | lastState = state; |
| 88 | state = newState; |
| 89 | } |
| 90 | |
Sudheer Shanka | 28537b6 | 2016-09-07 11:12:31 -0700 | [diff] [blame] | 91 | static String stateToString(int state) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 92 | switch (state) { |
| 93 | case STATE_BOOTING: return "BOOTING"; |
| 94 | case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED"; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 95 | case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING"; |
| 96 | case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED"; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 97 | 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 Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 106 | if (switching) pw.print(" SWITCHING"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 107 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 108 | } |
| 109 | } |