Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2013 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.wm;
     18 
     19 import static com.android.server.wm.WindowManagerService.TAG;
     20 import static com.android.server.wm.WindowManagerService.DEBUG_STACK;
     21 
     22 import android.util.EventLog;
     23 import android.util.Slog;
     24 import com.android.server.EventLogTags;
     25 
     26 class Task {
     27     TaskStack mStack;
     28     final AppTokenList mAppTokens = new AppTokenList();
     29     final int mTaskId;
     30     final int mUserId;
     31     boolean mDeferRemoval = false;
     32     final WindowManagerService mService;
     33 
     34     Task(int taskId, TaskStack stack, int userId, WindowManagerService service) {
     35         mTaskId = taskId;
     36         mStack = stack;
     37         mUserId = userId;
     38         mService = service;
     39     }
     40 
     41     DisplayContent getDisplayContent() {
     42         return mStack.getDisplayContent();
     43     }
     44 
     45     void addAppToken(int addPos, AppWindowToken wtoken) {
     46         final int lastPos = mAppTokens.size();
     47         if (addPos >= lastPos) {
     48             addPos = lastPos;
     49         } else {
     50             for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
     51                 if (mAppTokens.get(pos).removed) {
     52                     // addPos assumes removed tokens are actually gone.
     53                     ++addPos;
     54                 }
     55             }
     56         }
     57         mAppTokens.add(addPos, wtoken);
     58         wtoken.mTask = this;
     59         mDeferRemoval = false;
     60     }
     61 
     62     void removeLocked() {
     63         if (!mAppTokens.isEmpty() && mStack.isAnimating()) {
     64             if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
     65             mDeferRemoval = true;
     66             return;
     67         }
     68         if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
     69         EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
     70         mDeferRemoval = false;
     71         mStack.removeTask(this);
     72         mService.mTaskIdToTask.delete(mTaskId);
     73     }
     74 
     75     void moveTaskToStack(TaskStack stack, boolean toTop) {
     76         if (stack == mStack) {
     77             return;
     78         }
     79         if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
     80                 + " from stack=" + mStack);
     81         EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
     82         if (mStack != null) {
     83             mStack.removeTask(this);
     84         }
     85         stack.addTask(this, toTop);
     86     }
     87 
     88     boolean removeAppToken(AppWindowToken wtoken) {
     89         boolean removed = mAppTokens.remove(wtoken);
     90         if (mAppTokens.size() == 0) {
     91             EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId,
     92                     "removeAppToken: last token");
     93             if (mDeferRemoval) {
     94                 removeLocked();
     95             }
     96         }
     97         wtoken.mTask = null;
     98         /* Leave mTaskId for now, it might be useful for debug
     99         wtoken.mTaskId = -1;
    100          */
    101         return removed;
    102     }
    103 
    104     void setSendingToBottom(boolean toBottom) {
    105         for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
    106             mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
    107         }
    108     }
    109 
    110     boolean showForAllUsers() {
    111         final int tokensCount = mAppTokens.size();
    112         return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
    113     }
    114 
    115     @Override
    116     public String toString() {
    117         return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
    118     }
    119 }
    120