[go: nahoru, domu]

1/*
2 * Copyright (C) 2016 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.app;
18
19import android.content.Intent;
20import android.content.IntentSender;
21import android.support.annotation.Nullable;
22
23/**
24 * Base class for {@code FragmentActivity} to be able to use v5 APIs.
25 *
26 * @hide
27 */
28abstract class BaseFragmentActivityEclair extends BaseFragmentActivityDonut {
29
30    // We need to keep track of whether startIntentSenderForResult originated from a Fragment, so we
31    // can conditionally check whether the requestCode collides with our reserved ID space for the
32    // request index (see above). Unfortunately we can't just call
33    // super.startIntentSenderForResult(...) to bypass the check when the call didn't come from a
34    // fragment, since we need to use the ActivityCompat version for backward compatibility.
35    boolean mStartedIntentSenderFromFragment;
36
37    @Override
38    public void startIntentSenderForResult(IntentSender intent, int requestCode,
39            @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
40            throws IntentSender.SendIntentException {
41        // If this was started from a Fragment we've already checked the upper 16 bits were not in
42        // use, and then repurposed them for the Fragment's index.
43        if (!mStartedIntentSenderFromFragment) {
44            if (requestCode != -1) {
45                checkForValidRequestCode(requestCode);
46            }
47        }
48        super.startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, flagsValues,
49                extraFlags);
50    }
51
52    @Override
53    void onBackPressedNotHandled() {
54        // On v5+, delegate to the framework impl of onBackPressed()
55        super.onBackPressed();
56    }
57
58    /**
59     * Checks whether the given request code is a valid code by masking it with 0xffff0000. Throws
60     * an {@link IllegalArgumentException} if the code is not valid.
61     */
62    static void checkForValidRequestCode(int requestCode) {
63        if ((requestCode & 0xffff0000) != 0) {
64            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
65        }
66    }
67}
68