1/*
2 * Copyright (C) 2012 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
17package android.support.v4.content;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Build;
23import android.os.Bundle;
24
25/**
26 * Helper for accessing features in {@link android.content.Context}
27 * introduced after API level 4 in a backwards compatible fashion.
28 */
29public class ContextCompat {
30
31    /**
32     * Start a set of activities as a synthesized task stack, if able.
33     *
34     * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for
35     * app navigation using the back key changed. The back key's behavior is local
36     * to the current task and does not capture navigation across different tasks.
37     * Navigating across tasks and easily reaching the previous task is accomplished
38     * through the "recents" UI, accessible through the software-provided Recents key
39     * on the navigation or system bar. On devices with the older hardware button configuration
40     * the recents UI can be accessed with a long press on the Home key.</p>
41     *
42     * <p>When crossing from one task stack to another post-Android 3.0,
43     * the application should synthesize a back stack/history for the new task so that
44     * the user may navigate out of the new task and back to the Launcher by repeated
45     * presses of the back key. Back key presses should not navigate across task stacks.</p>
46     *
47     * <p>startActivities provides a mechanism for constructing a synthetic task stack of
48     * multiple activities. If the underlying API is not available on the system this method
49     * will return false.</p>
50     *
51     * @param context Start activities using this activity as the starting context
52     * @param intents Array of intents defining the activities that will be started. The element
53     *                length-1 will correspond to the top activity on the resulting task stack.
54     * @return true if the underlying API was available and the call was successful, false otherwise
55     */
56    public static boolean startActivities(Context context, Intent[] intents) {
57        return startActivities(context, intents, null);
58    }
59
60    /**
61     * Start a set of activities as a synthesized task stack, if able.
62     *
63     * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for
64     * app navigation using the back key changed. The back key's behavior is local
65     * to the current task and does not capture navigation across different tasks.
66     * Navigating across tasks and easily reaching the previous task is accomplished
67     * through the "recents" UI, accessible through the software-provided Recents key
68     * on the navigation or system bar. On devices with the older hardware button configuration
69     * the recents UI can be accessed with a long press on the Home key.</p>
70     *
71     * <p>When crossing from one task stack to another post-Android 3.0,
72     * the application should synthesize a back stack/history for the new task so that
73     * the user may navigate out of the new task and back to the Launcher by repeated
74     * presses of the back key. Back key presses should not navigate across task stacks.</p>
75     *
76     * <p>startActivities provides a mechanism for constructing a synthetic task stack of
77     * multiple activities. If the underlying API is not available on the system this method
78     * will return false.</p>
79     *
80     * @param context Start activities using this activity as the starting context
81     * @param intents Array of intents defining the activities that will be started. The element
82     *                length-1 will correspond to the top activity on the resulting task stack.
83     * @param options Additional options for how the Activity should be started.
84     * See {@link android.content.Context#startActivity(Intent, Bundle)
85     * @return true if the underlying API was available and the call was successful, false otherwise
86     */
87    public static boolean startActivities(Context context, Intent[] intents,
88            Bundle options) {
89        final int version = Build.VERSION.SDK_INT;
90        if (version >= 16) {
91            ContextCompatJellybean.startActivities(context, intents, options);
92            return true;
93        } else if (version >= 11) {
94            ContextCompatHoneycomb.startActivities(context, intents);
95            return true;
96        }
97        return false;
98    }
99}
100