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 
19 import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
20 import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
21 
22 import android.app.IStopUserCallback;
23 import android.os.UserHandle;
24 import android.util.ArrayMap;
25 import android.util.Slog;
26 
27 import com.android.internal.util.ProgressReporter;
28 
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 
32 public final class UserState {
33     private static final String TAG = TAG_WITH_CLASS_NAME ? "UserState" : TAG_AM;
34 
35     // User is first coming up.
36     public final static int STATE_BOOTING = 0;
37     // User is in the locked state.
38     public final static int STATE_RUNNING_LOCKED = 1;
39     // 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;
43     // User is in the initial process of being stopped.
44     public final static int STATE_STOPPING = 4;
45     // User is in the final phase of stopping, sending Intent.ACTION_SHUTDOWN.
46     public final static int STATE_SHUTDOWN = 5;
47 
48     public final UserHandle mHandle;
49     public final ArrayList<IStopUserCallback> mStopCallbacks
50             = new ArrayList<IStopUserCallback>();
51     public final ProgressReporter mUnlockProgress;
52 
53     public int state = STATE_BOOTING;
54     public int lastState = STATE_BOOTING;
55     public boolean switching;
56     public boolean tokenProvided;
57 
58     /**
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 
UserState(UserHandle handle)64     public UserState(UserHandle handle) {
65         mHandle = handle;
66         mUnlockProgress = new ProgressReporter(handle.getIdentifier());
67     }
68 
setState(int oldState, int newState)69     public boolean setState(int oldState, int newState) {
70         if (state == oldState) {
71             setState(newState);
72             return true;
73         } else {
74             Slog.w(TAG, "Expected user " + mHandle.getIdentifier() + " in state "
75                     + stateToString(oldState) + " but was in state " + stateToString(state));
76             return false;
77         }
78     }
79 
setState(int newState)80     public void setState(int newState) {
81         if (newState == state) {
82             return;
83         }
84         Slog.i(TAG, "User " + mHandle.getIdentifier() + " state changed from "
85                 + stateToString(state) + " to " + stateToString(newState));
86         EventLogTags.writeAmUserStateChanged(mHandle.getIdentifier(), newState);
87         lastState = state;
88         state = newState;
89     }
90 
stateToString(int state)91     public static String stateToString(int state) {
92         switch (state) {
93             case STATE_BOOTING: return "BOOTING";
94             case STATE_RUNNING_LOCKED: return "RUNNING_LOCKED";
95             case STATE_RUNNING_UNLOCKING: return "RUNNING_UNLOCKING";
96             case STATE_RUNNING_UNLOCKED: return "RUNNING_UNLOCKED";
97             case STATE_STOPPING: return "STOPPING";
98             case STATE_SHUTDOWN: return "SHUTDOWN";
99             default: return Integer.toString(state);
100         }
101     }
102 
dump(String prefix, PrintWriter pw)103     void dump(String prefix, PrintWriter pw) {
104         pw.print(prefix);
105         pw.print("state="); pw.print(stateToString(state));
106         if (switching) pw.print(" SWITCHING");
107         pw.println();
108     }
109 }
110