1/* 2 * Copyright (C) 2017 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.Context; 20import android.content.Intent; 21import android.content.IntentSender; 22import android.support.annotation.Nullable; 23import android.support.annotation.RequiresApi; 24import android.util.AttributeSet; 25import android.view.View; 26 27@RequiresApi(14) 28abstract class BaseFragmentActivityApi14 extends SupportActivity { 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 View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 39 final View v = dispatchFragmentsOnCreateView(parent, name, context, attrs); 40 if (v == null) { 41 return super.onCreateView(parent, name, context, attrs); 42 } 43 return v; 44 } 45 46 @Override 47 public View onCreateView(String name, Context context, AttributeSet attrs) { 48 final View v = dispatchFragmentsOnCreateView(null, name, context, attrs); 49 if (v == null) { 50 return super.onCreateView(name, context, attrs); 51 } 52 return v; 53 } 54 55 abstract View dispatchFragmentsOnCreateView(View parent, String name, 56 Context context, AttributeSet attrs); 57 58 @Override 59 public void startIntentSenderForResult(IntentSender intent, int requestCode, 60 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 61 throws IntentSender.SendIntentException { 62 // If this was started from a Fragment we've already checked the upper 16 bits were not in 63 // use, and then repurposed them for the Fragment's index. 64 if (!mStartedIntentSenderFromFragment) { 65 if (requestCode != -1) { 66 checkForValidRequestCode(requestCode); 67 } 68 } 69 super.startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, flagsValues, 70 extraFlags); 71 } 72 73 /** 74 * Checks whether the given request code is a valid code by masking it with 0xffff0000. Throws 75 * an {@link IllegalArgumentException} if the code is not valid. 76 */ 77 static void checkForValidRequestCode(int requestCode) { 78 if ((requestCode & 0xffff0000) != 0) { 79 throw new IllegalArgumentException("Can only use lower 16 bits for requestCode"); 80 } 81 } 82} 83