1ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell/*
2ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Copyright (C) 2011 The Android Open Source Project
3ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell *
4ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * you may not use this file except in compliance with the License.
6ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * You may obtain a copy of the License at
7ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell *
8ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell *
10ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Unless required by applicable law or agreed to in writing, software
11ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * See the License for the specific language governing permissions and
14ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * limitations under the License.
15ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell */
16ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell
17ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellpackage android.support.v4.app;
18ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell
19ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellimport android.app.Activity;
203a96487b54eca412f51ad00b8f8096055e94dcbbJake Whartonimport android.content.Intent;
21ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellimport android.os.Build;
223a96487b54eca412f51ad00b8f8096055e94dcbbJake Whartonimport android.os.Bundle;
23575e098da5bc16ff8b95ca080284253fd206fe12Adam Powellimport android.support.v4.content.ContextCompat;
24ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell
25ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell/**
26ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Helper for accessing features in {@link android.app.Activity}
27ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * introduced after API level 4 in a backwards compatible fashion.
28ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell */
29575e098da5bc16ff8b95ca080284253fd206fe12Adam Powellpublic class ActivityCompat extends ContextCompat {
30ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell    /**
31ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * Invalidate the activity's options menu, if able.
32ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     *
33ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the
34ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * options menu was controlled primarily by the user's operation of
35ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * the hardware menu key. When the user presses down on the menu key
36ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * for the first time the menu was created and prepared by calls
37ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and
38ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively.
39ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * Subsequent presses of the menu key kept the existing instance of the
40ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)}
41ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * to give the activity an opportunity to contextually alter the menu
42ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * before the menu panel was shown.</p>
43ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     *
44ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * <p>In Android 3.0+ the Action Bar forces the options menu to be built early
45ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * so that items chosen to show as actions may be displayed when the activity
46ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * first becomes visible. The Activity method invalidateOptionsMenu forces
47ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * the entire menu to be destroyed and recreated from
48ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar
49ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * though heavier-weight opportunity to change the menu's contents. Normally
50ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * this functionality is used to support a changing configuration of Fragments.</p>
51ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     *
52ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * <p>Applications may use this support helper to signal a significant change in
53ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * activity state that should cause the options menu to be rebuilt. If the app
54ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * is running on an older platform version that does not support menu invalidation
55ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)}
56ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * the next time the user presses the menu key and this method will return false.
57ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * If this method returns true the options menu was successfully invalidated.</p>
58ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     *
59ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * @param activity Invalidate the options menu of this activity
60ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     * @return true if this operation was supported and it completed; false if it was not available.
61ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell     */
62ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell    public static boolean invalidateOptionsMenu(Activity activity) {
63ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell        if (Build.VERSION.SDK_INT >= 11) {
64ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell            ActivityCompatHoneycomb.invalidateOptionsMenu(activity);
65ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell            return true;
66ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell        }
67ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell        return false;
68ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell    }
693a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton
703a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    /**
713a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * Start an activity with additional launch information, if able.
723a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *
733a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * <p>In Android 4.1+ additional options were introduced to allow for more
743a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * control on activity launch animations. Applications can use this method
753a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * along with {@link ActivityOptionsCompat} to use these animations when
763a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * available. When run on versions of the platform where this feature does
773a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * not exist the activity will be launched normally.</p>
783a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *
793a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param activity Context to launch activity from.
803a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param intent The description of the activity to start.
813a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param options Additional options for how the Activity should be started.
823a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                May be null if there are no options. See
833a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                {@link ActivityOptionsCompat} for how to build the Bundle
843a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                supplied here; there are no supported definitions for
853a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                building it manually.
863a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     */
873a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    public static void startActivity(Activity activity, Intent intent, Bundle options) {
883a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        if (Build.VERSION.SDK_INT >= 16) {
893a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton            ActivityCompatJB.startActivity(activity, intent, options);
903a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        } else {
913a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton            activity.startActivity(intent);
923a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        }
933a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    }
943a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton
953a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    /**
963a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * Start new activity with options, if able, for which you would like a
973a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * result when it finished.
983a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *
993a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * <p>In Android 4.1+ additional options were introduced to allow for more
1003a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * control on activity launch animations. Applications can use this method
1013a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * along with {@link ActivityOptionsCompat} to use these animations when
1023a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * available. When run on versions of the platform where this feature does
1033a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * not exist the activity will be launched normally.</p>
1043a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *
1053a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param activity Origin activity to launch from.
1063a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param intent The description of the activity to start.
1073a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param requestCode If >= 0, this code will be returned in
1083a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                   onActivityResult() when the activity exits.
1093a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     * @param options Additional options for how the Activity should be started.
1103a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                May be null if there are no options. See
1113a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                {@link ActivityOptionsCompat} for how to build the Bundle
1123a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                supplied here; there are no supported definitions for
1133a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     *                building it manually.
1143a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton     */
1153a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    public static void startActivityForResult(Activity activity, Intent intent, int requestCode, Bundle options) {
1163a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        if (Build.VERSION.SDK_INT >= 16) {
1173a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton            ActivityCompatJB.startActivityForResult(activity, intent, requestCode, options);
1183a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        } else {
1193a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton            activity.startActivityForResult(intent, requestCode);
1203a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton        }
1213a96487b54eca412f51ad00b8f8096055e94dcbbJake Wharton    }
12251e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes
12351e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes    /**
12451e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     * Finish this activity, and tries to finish all activities immediately below it
12551e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     * in the current task that have the same affinity.
12651e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     *
12751e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     * <p>On Android 4.1+ calling this method will call through to the native version of this
12851e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     * method. For other platforms {@link Activity#finish()} will be called instead.</p>
12951e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes     */
13051e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes    public static void finishAffinity(Activity activity) {
13151e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes        if (Build.VERSION.SDK_INT >= 16) {
13251e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes            ActivityCompatJB.finishAffinity(activity);
13351e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes        } else {
13451e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes            activity.finish();
13551e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes        }
13651e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes    }
13751e35e07a00e4b56a1ca330323e69ef9258c4e57Chris Banes
138ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell}
139