TaskStackBuilder.java revision 3c464bdefe37f75153e8a8c978c3457ae4d1f37e
1dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell/*
2dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * Copyright (C) 2012 The Android Open Source Project
3dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
4dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * you may not use this file except in compliance with the License.
6dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * You may obtain a copy of the License at
7dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
8dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
10dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * Unless required by applicable law or agreed to in writing, software
11dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * See the License for the specific language governing permissions and
14dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * limitations under the License.
15dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell */
16dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
17dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellpackage android.app;
18dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
19dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.ComponentName;
20dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.Context;
21dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.Intent;
22dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.pm.ActivityInfo;
23dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.pm.PackageManager;
24dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.content.pm.PackageManager.NameNotFoundException;
25dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport android.util.Log;
26dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
27dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport java.util.ArrayList;
28dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellimport java.util.Iterator;
29dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
30dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell/**
31dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * Utility class for constructing synthetic back stacks for cross-task navigation
32dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * on Android 3.0 and newer.
33dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
34dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for
35dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * app navigation using the back key changed. The back key's behavior is local
36dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * to the current task and does not capture navigation across different tasks.
37dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * Navigating across tasks and easily reaching the previous task is accomplished
38dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * through the "recents" UI, accessible through the software-provided Recents key
39dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * on the navigation or system bar. On devices with the older hardware button configuration
40dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * the recents UI can be accessed with a long press on the Home key.</p>
41dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
42dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <p>When crossing from one task stack to another post-Android 3.0,
43dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * the application should synthesize a back stack/history for the new task so that
44dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * the user may navigate out of the new task and back to the Launcher by repeated
45dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * presses of the back key. Back key presses should not navigate across task stacks.</p>
46dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
47dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <p>TaskStackBuilder provides a way to obey the correct conventions
48dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * around cross-task navigation.</p>
49dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell *
50dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <div class="special reference">
51dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <h3>About Navigation</h3>
52dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * For more detailed information about tasks, the back stack, and navigation design guidelines,
53dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * please read
54dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
55dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
56dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * from the design guide.
57dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell * </div>
58dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell */
59dd8fab2629131b09367df747afd9a61e42dd1992Adam Powellpublic class TaskStackBuilder implements Iterable<Intent> {
60dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    private static final String TAG = "TaskStackBuilder";
61dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
62dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    private final ArrayList<Intent> mIntents = new ArrayList<Intent>();
63dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    private final Context mSourceContext;
64dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
65dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    private TaskStackBuilder(Context a) {
66dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        mSourceContext = a;
67dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
68dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
69dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
70dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Return a new TaskStackBuilder for launching a fresh task stack consisting
71dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * of a series of activities.
72dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
73dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param context The context that will launch the new task stack or generate a PendingIntent
74dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return A new TaskStackBuilder
75dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
76dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public static TaskStackBuilder from(Context context) {
77dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return new TaskStackBuilder(context);
78dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
79dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
80dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
81dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Add a new Intent to the task stack. The most recently added Intent will invoke
82dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * the Activity at the top of the final task stack.
83dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
84dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param nextIntent Intent for the next Activity in the synthesized task stack
85dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return This TaskStackBuilder for method chaining
86dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
87dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public TaskStackBuilder addNextIntent(Intent nextIntent) {
88dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        mIntents.add(nextIntent);
89dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return this;
90dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
91dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
92dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
93dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Add the activity parent chain as specified by the
943c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * {@link Activity#getParentActivityIntent() getParentActivityIntent()} method of the activity
953c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * specified and the {@link android.R.attr#parentActivityName parentActivityName} attributes
963c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * of each successive activity (or activity-alias) element in the application's manifest
973c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * to the task stack builder.
98dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
99dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param sourceActivity All parents of this activity will be added
100dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return This TaskStackBuilder for method chaining
101dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
102dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public TaskStackBuilder addParentStack(Activity sourceActivity) {
103dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        final int insertAt = mIntents.size();
104dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        Intent parent = sourceActivity.getParentActivityIntent();
105dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        PackageManager pm = sourceActivity.getPackageManager();
106dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        while (parent != null) {
107dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            mIntents.add(insertAt, parent);
108dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            try {
109dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                ActivityInfo info = pm.getActivityInfo(parent.getComponent(), 0);
110dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                String parentActivity = info.parentActivityName;
111dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                if (parentActivity != null) {
112dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    parent = new Intent().setComponent(
113dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                            new ComponentName(mSourceContext, parentActivity));
114dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                } else {
115dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    parent = null;
116dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                }
117dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            } catch (NameNotFoundException e) {
118dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
119dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                throw new IllegalArgumentException(e);
120dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            }
121dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        }
122dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return this;
123dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
124dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
125dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
126dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Add the activity parent chain as specified by the
127dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
128dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * (or activity-alias) element in the application's manifest to the task stack builder.
129dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
130dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param sourceActivityClass All parents of this activity will be added
131dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return This TaskStackBuilder for method chaining
132dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
133dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public TaskStackBuilder addParentStack(Class<?> sourceActivityClass) {
134dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        final int insertAt = mIntents.size();
135dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        PackageManager pm = mSourceContext.getPackageManager();
136dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        try {
137dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            ActivityInfo info = pm.getActivityInfo(
138dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    new ComponentName(mSourceContext, sourceActivityClass), 0);
139dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            String parentActivity = info.parentActivityName;
140dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            Intent parent = new Intent().setComponent(
141dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    new ComponentName(mSourceContext, parentActivity));
142dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            while (parent != null) {
143dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                mIntents.add(insertAt, parent);
144dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                info = pm.getActivityInfo(parent.getComponent(), 0);
145dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                parentActivity = info.parentActivityName;
146dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                if (parentActivity != null) {
147dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    parent = new Intent().setComponent(
148dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                            new ComponentName(mSourceContext, parentActivity));
149dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                } else {
150dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    parent = null;
151dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                }
152dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            }
153dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        } catch (NameNotFoundException e) {
154dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
155dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            throw new IllegalArgumentException(e);
156dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        }
157dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return this;
158dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
159dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
160dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
1613c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * Add the activity parent chain as specified by the
1623c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
1633c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * (or activity-alias) element in the application's manifest to the task stack builder.
1643c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     *
1653c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * @param sourceActivityName Must specify an Activity component. All parents of
1663c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     *                           this activity will be added
1673c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     * @return This TaskStackBuilder for method chaining
1683c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell     */
1693c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell    public TaskStackBuilder addParentStack(ComponentName sourceActivityName) {
1703c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        final int insertAt = mIntents.size();
1713c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        PackageManager pm = mSourceContext.getPackageManager();
1723c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        try {
1733c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            ActivityInfo info = pm.getActivityInfo(sourceActivityName, 0);
1743c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            String parentActivity = info.parentActivityName;
1753c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            Intent parent = new Intent().setComponent(
1763c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                    new ComponentName(info.packageName, parentActivity));
1773c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            while (parent != null) {
1783c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                mIntents.add(insertAt, parent);
1793c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                info = pm.getActivityInfo(parent.getComponent(), 0);
1803c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                parentActivity = info.parentActivityName;
1813c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                if (parentActivity != null) {
1823c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                    parent = new Intent().setComponent(
1833c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                            new ComponentName(info.packageName, parentActivity));
1843c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                } else {
1853c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                    parent = null;
1863c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell                }
1873c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            }
1883c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        } catch (NameNotFoundException e) {
1893c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
1903c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell            throw new IllegalArgumentException(e);
1913c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        }
1923c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell        return this;
1933c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell    }
1943c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell
1953c464bdefe37f75153e8a8c978c3457ae4d1f37eAdam Powell    /**
196dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return the number of intents added so far.
197dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
198dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public int getIntentCount() {
199dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return mIntents.size();
200dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
201dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
202dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
203dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Get the intent at the specified index.
204dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Useful if you need to modify the flags or extras of an intent that was previously added,
205dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * for example with {@link #addParentStack(Activity)}.
206dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
207dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param index Index from 0-getIntentCount()
208dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return the intent at position index
209dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
210dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public Intent getIntent(int index) {
211dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return mIntents.get(index);
212dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
213dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
214dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public Iterator<Intent> iterator() {
215dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return mIntents.iterator();
216dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
217dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
218dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
219dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Start the task stack constructed by this builder.
220dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
221dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public void startActivities() {
222dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        if (mIntents.isEmpty()) {
223dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell            throw new IllegalStateException(
224dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                    "No intents added to TaskStackBuilder; cannot startActivities");
225dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        }
226dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
227dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
228dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
229dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                Intent.FLAG_ACTIVITY_CLEAR_TASK |
230dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
231dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        mSourceContext.startActivities(intents);
232dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
233dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell
234dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    /**
235dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * Obtain a {@link PendingIntent} for launching the task constructed by this builder so far.
236dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *
237dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param requestCode Private request code for the sender
238dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT},
239dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *              {@link PendingIntent#FLAG_NO_CREATE}, {@link PendingIntent#FLAG_CANCEL_CURRENT},
240dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *              {@link PendingIntent#FLAG_UPDATE_CURRENT}, or any of the flags supported by
241dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *              {@link Intent#fillIn(Intent, int)} to control which unspecified parts of the
242dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     *              intent that can be supplied when the actual send happens.
243dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     * @return The obtained PendingIntent
244dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell     */
245dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    public PendingIntent getPendingIntent(int requestCode, int flags) {
2468ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell        if (mIntents.isEmpty()) {
2478ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell            throw new IllegalStateException(
2488ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell                    "No intents added to TaskStackBuilder; cannot getPendingIntent");
2498ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell        }
2508ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell
251dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
2528ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell        intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2538ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell                Intent.FLAG_ACTIVITY_CLEAR_TASK |
2548ab700cbbab60f7a01a86cbf16496e1f34236dd6Adam Powell                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
255dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell        return PendingIntent.getActivities(mSourceContext, requestCode, intents, flags);
256dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell    }
257dd8fab2629131b09367df747afd9a61e42dd1992Adam Powell}
258