|
| 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 | + |
| 17 | +package android.app; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.os.Bundle; |
| 21 | + |
| 22 | +/** |
| 23 | + * Helper class for building an options Bundle that can be used with |
| 24 | + * {@link android.content.Context#startActivity(android.content.Intent, android.os.Bundle) |
| 25 | + * Context.startActivity(Intent, Bundle)} and related methods. |
| 26 | + */ |
| 27 | +public class ActivityOptions { |
| 28 | + /** |
| 29 | + * The package name that created the options. |
| 30 | + * @hide |
| 31 | + */ |
| 32 | + public static final String KEY_PACKAGE_NAME = "android:packageName"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Custom enter animation resource ID. |
| 36 | + * @hide |
| 37 | + */ |
| 38 | + public static final String KEY_ANIM_ENTER_RES_ID = "android:animEnterRes"; |
| 39 | + |
| 40 | + /** |
| 41 | + * Custom exit animation resource ID. |
| 42 | + * @hide |
| 43 | + */ |
| 44 | + public static final String KEY_ANIM_EXIT_RES_ID = "android:animExitRes"; |
| 45 | + |
| 46 | + private String mPackageName; |
| 47 | + private boolean mIsCustomAnimation; |
| 48 | + private int mCustomEnterResId; |
| 49 | + private int mCustomExitResId; |
| 50 | + |
| 51 | + /** |
| 52 | + * Create an ActivityOptions specifying a custom animation to run when |
| 53 | + * the activity is displayed. |
| 54 | + * |
| 55 | + * @param context Who is defining this. This is the application that the |
| 56 | + * animation resources will be loaded from. |
| 57 | + * @param enterResId A resource ID of the animation resource to use for |
| 58 | + * the incoming activity. Use 0 for no animation. |
| 59 | + * @param exitResId A resource ID of the animation resource to use for |
| 60 | + * the outgoing activity. Use 0 for no animation. |
| 61 | + * @return Returns a new ActivityOptions object that you can use to |
| 62 | + * supply these options as the options Bundle when starting an activity. |
| 63 | + */ |
| 64 | + public static ActivityOptions makeCustomAnimation(Context context, |
| 65 | + int enterResId, int exitResId) { |
| 66 | + ActivityOptions opts = new ActivityOptions(); |
| 67 | + opts.mPackageName = context.getPackageName(); |
| 68 | + opts.mIsCustomAnimation = true; |
| 69 | + opts.mCustomEnterResId = enterResId; |
| 70 | + opts.mCustomExitResId = exitResId; |
| 71 | + return opts; |
| 72 | + } |
| 73 | + |
| 74 | + private ActivityOptions() { |
| 75 | + } |
| 76 | + |
| 77 | + /** @hide */ |
| 78 | + public ActivityOptions(Bundle opts) { |
| 79 | + mPackageName = opts.getString(KEY_PACKAGE_NAME); |
| 80 | + if (opts.containsKey(KEY_ANIM_ENTER_RES_ID)) { |
| 81 | + mIsCustomAnimation = true; |
| 82 | + mCustomEnterResId = opts.getInt(KEY_ANIM_ENTER_RES_ID, 0); |
| 83 | + mCustomExitResId = opts.getInt(KEY_ANIM_EXIT_RES_ID, 0); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** @hide */ |
| 88 | + public String getPackageName() { |
| 89 | + return mPackageName; |
| 90 | + } |
| 91 | + |
| 92 | + /** @hide */ |
| 93 | + public boolean isCustomAnimation() { |
| 94 | + return mIsCustomAnimation; |
| 95 | + } |
| 96 | + |
| 97 | + /** @hide */ |
| 98 | + public int getCustomEnterResId() { |
| 99 | + return mCustomEnterResId; |
| 100 | + } |
| 101 | + |
| 102 | + /** @hide */ |
| 103 | + public int getCustomExitResId() { |
| 104 | + return mCustomExitResId; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Join the values in <var>otherOptions</var> in to this one. Any values |
| 109 | + * defined in <var>otherOptions</var> replace those in the base options. |
| 110 | + */ |
| 111 | + public void join(ActivityOptions otherOptions) { |
| 112 | + if (otherOptions.mPackageName != null) { |
| 113 | + mPackageName = otherOptions.mPackageName; |
| 114 | + } |
| 115 | + if (otherOptions.mIsCustomAnimation) { |
| 116 | + mIsCustomAnimation = true; |
| 117 | + mCustomEnterResId = otherOptions.mCustomEnterResId; |
| 118 | + mCustomExitResId = otherOptions.mCustomExitResId; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the created options as a Bundle, which can be passed to |
| 124 | + * {@link android.content.Context#startActivity(android.content.Intent, android.os.Bundle) |
| 125 | + * Context.startActivity(Intent, Bundle)} and related methods. |
| 126 | + * Note that the returned Bundle is still owned by the ActivityOptions |
| 127 | + * object; you must not modify it, but can supply it to the startActivity |
| 128 | + * methods that take an options Bundle. |
| 129 | + */ |
| 130 | + public Bundle toBundle() { |
| 131 | + Bundle b = new Bundle(); |
| 132 | + if (mPackageName != null) { |
| 133 | + b.putString(KEY_PACKAGE_NAME, mPackageName); |
| 134 | + } |
| 135 | + if (mIsCustomAnimation) { |
| 136 | + b.putInt(KEY_ANIM_ENTER_RES_ID, mCustomEnterResId); |
| 137 | + b.putInt(KEY_ANIM_EXIT_RES_ID, mCustomExitResId); |
| 138 | + } |
| 139 | + return b; |
| 140 | + } |
| 141 | +} |
0 commit comments