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