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