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             if (_intent != null) {
     60                 // If this Intent has a selector, we want to clear it for the
     61                 // recent task since it is not relevant if the user later wants
     62                 // to re-launch the app.
     63                 if (_intent.getSelector() != null) {
     64                     _intent = new Intent(_intent);
     65                     _intent.setSelector(null);
     66                 }
     67             }
     68             intent = _intent;
     69             realActivity = _intent != null ? _intent.getComponent() : null;
     70             origActivity = null;
     71         } else {
     72             ComponentName targetComponent = new ComponentName(
     73                     info.packageName, info.targetActivity);
     74             if (_intent != null) {
     75                 Intent targetIntent = new Intent(_intent);
     76                 targetIntent.setComponent(targetComponent);
     77                 targetIntent.setSelector(null);
     78                 intent = targetIntent;
     79                 realActivity = targetComponent;
     80                 origActivity = _intent.getComponent();
     81             } else {
     82                 intent = null;
     83                 realActivity = targetComponent;
     84                 origActivity = new ComponentName(info.packageName, info.name);
     85             }
     86         }
     87 
     88         if (intent != null &&
     89                 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
     90             // Once we are set to an Intent with this flag, we count this
     91             // task as having a true root activity.
     92             rootWasReset = true;
     93         }
     94     }
     95 
     96     void dump(PrintWriter pw, String prefix) {
     97         if (numActivities != 0 || rootWasReset) {
     98             pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
     99                     pw.print(" rootWasReset="); pw.println(rootWasReset);
    100         }
    101         if (affinity != null) {
    102             pw.print(prefix); pw.print("affinity="); pw.println(affinity);
    103         }
    104         if (intent != null) {
    105             StringBuilder sb = new StringBuilder(128);
    106             sb.append(prefix); sb.append("intent={");
    107             intent.toShortString(sb, false, true, false);
    108             sb.append('}');
    109             pw.println(sb.toString());
    110         }
    111         if (affinityIntent != null) {
    112             StringBuilder sb = new StringBuilder(128);
    113             sb.append(prefix); sb.append("affinityIntent={");
    114             affinityIntent.toShortString(sb, false, true, false);
    115             sb.append('}');
    116             pw.println(sb.toString());
    117         }
    118         if (origActivity != null) {
    119             pw.print(prefix); pw.print("origActivity=");
    120             pw.println(origActivity.flattenToShortString());
    121         }
    122         if (realActivity != null) {
    123             pw.print(prefix); pw.print("realActivity=");
    124             pw.println(realActivity.flattenToShortString());
    125         }
    126         if (!askedCompatMode) {
    127             pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
    128         }
    129         pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
    130                 pw.print(" lastDescription="); pw.println(lastDescription);
    131         pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
    132                 pw.print(" (inactive for ");
    133                 pw.print((getInactiveDuration()/1000)); pw.println("s)");
    134     }
    135 
    136     public String toString() {
    137         if (stringName != null) {
    138             return stringName;
    139         }
    140         StringBuilder sb = new StringBuilder(128);
    141         sb.append("TaskRecord{");
    142         sb.append(Integer.toHexString(System.identityHashCode(this)));
    143         sb.append(" #");
    144         sb.append(taskId);
    145         if (affinity != null) {
    146             sb.append(" A ");
    147             sb.append(affinity);
    148         } else if (intent != null) {
    149             sb.append(" I ");
    150             sb.append(intent.getComponent().flattenToShortString());
    151         } else if (affinityIntent != null) {
    152             sb.append(" aI ");
    153             sb.append(affinityIntent.getComponent().flattenToShortString());
    154         } else {
    155             sb.append(" ??");
    156         }
    157         sb.append('}');
    158         return stringName = sb.toString();
    159     }
    160 }
    161