Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2006 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 android.content.ComponentName;
     20 import android.content.Intent;
     21 import android.content.pm.ActivityInfo;
     22 import android.graphics.Bitmap;
     23 
     24 import java.io.PrintWriter;
     25 
     26 class TaskRecord extends ThumbnailHolder {
     27     final int taskId;       // Unique identifier for this task.
     28     final String affinity;  // The affinity name for this task, or null.
     29     Intent intent;          // The original intent that started the task.
     30     Intent affinityIntent;  // Intent of affinity-moved activity that started this task.
     31     ComponentName origActivity; // The non-alias activity component of the intent.
     32     ComponentName realActivity; // The actual activity component that started the task.
     33     int numActivities;      // Current number of activities in this task.
     34     long lastActiveTime;    // Last time this task was active, including sleep.
     35     boolean rootWasReset;   // True if the intent at the root of the task had
     36                             // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
     37     boolean askedCompatMode;// Have asked the user about compat mode for this task.
     38 
     39     String stringName;      // caching of toString() result.
     40 
     41     TaskRecord(int _taskId, ActivityInfo info, Intent _intent) {
     42         taskId = _taskId;
     43         affinity = info.taskAffinity;
     44         setIntent(_intent, info);
     45     }
     46 
     47     void touchActiveTime() {
     48         lastActiveTime = android.os.SystemClock.elapsedRealtime();
     49     }
     50 
     51     long getInactiveDuration() {
     52         return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
     53     }
     54 
     55     void setIntent(Intent _intent, ActivityInfo info) {
     56         stringName = null;
     57 
     58         if (info.targetActivity == null) {
     59             intent = _intent;
     60             realActivity = _intent != null ? _intent.getComponent() : null;
     61             origActivity = null;
     62         } else {
     63             ComponentName targetComponent = new ComponentName(
     64                     info.packageName, info.targetActivity);
     65             if (_intent != null) {
     66                 Intent targetIntent = new Intent(_intent);
     67                 targetIntent.setComponent(targetComponent);
     68                 intent = targetIntent;
     69                 realActivity = targetComponent;
     70                 origActivity = _intent.getComponent();
     71             } else {
     72                 intent = null;
     73                 realActivity = targetComponent;
     74                 origActivity = new ComponentName(info.packageName, info.name);
     75             }
     76         }
     77 
     78         if (intent != null &&
     79                 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
     80             // Once we are set to an Intent with this flag, we count this
     81             // task as having a true root activity.
     82             rootWasReset = true;
     83         }
     84     }
     85 
     86     void dump(PrintWriter pw, String prefix) {
     87         if (numActivities != 0 || rootWasReset) {
     88             pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
     89                     pw.print(" rootWasReset="); pw.println(rootWasReset);
     90         }
     91         if (affinity != null) {
     92             pw.print(prefix); pw.print("affinity="); pw.println(affinity);
     93         }
     94         if (intent != null) {
     95             StringBuilder sb = new StringBuilder(128);
     96             sb.append(prefix); sb.append("intent={");
     97             intent.toShortString(sb, false, true, false);
     98             sb.append('}');
     99             pw.println(sb.toString());
    100         }
    101         if (affinityIntent != null) {
    102             StringBuilder sb = new StringBuilder(128);
    103             sb.append(prefix); sb.append("affinityIntent={");
    104             affinityIntent.toShortString(sb, false, true, false);
    105             sb.append('}');
    106             pw.println(sb.toString());
    107         }
    108         if (origActivity != null) {
    109             pw.print(prefix); pw.print("origActivity=");
    110             pw.println(origActivity.flattenToShortString());
    111         }
    112         if (realActivity != null) {
    113             pw.print(prefix); pw.print("realActivity=");
    114             pw.println(realActivity.flattenToShortString());
    115         }
    116         if (!askedCompatMode) {
    117             pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
    118         }
    119         pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
    120                 pw.print(" lastDescription="); pw.println(lastDescription);
    121         pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
    122                 pw.print(" (inactive for ");
    123                 pw.print((getInactiveDuration()/1000)); pw.println("s)");
    124     }
    125 
    126     public String toString() {
    127         if (stringName != null) {
    128             return stringName;
    129         }
    130         StringBuilder sb = new StringBuilder(128);
    131         sb.append("TaskRecord{");
    132         sb.append(Integer.toHexString(System.identityHashCode(this)));
    133         sb.append(" #");
    134         sb.append(taskId);
    135         if (affinity != null) {
    136             sb.append(" A ");
    137             sb.append(affinity);
    138         } else if (intent != null) {
    139             sb.append(" I ");
    140             sb.append(intent.getComponent().flattenToShortString());
    141         } else if (affinityIntent != null) {
    142             sb.append(" aI ");
    143             sb.append(affinityIntent.getComponent().flattenToShortString());
    144         } else {
    145             sb.append(" ??");
    146         }
    147         sb.append('}');
    148         return stringName = sb.toString();
    149     }
    150 }
    151