• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.support.v4.app;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentSender;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.support.annotation.Nullable;
26 import android.util.AttributeSet;
27 import android.view.View;
28 
29 /**
30  * Base class for {@code FragmentActivity} to be able to use Gingerbread APIs.
31  *
32  * @hide
33  */
34 abstract class BaseFragmentActivityGingerbread extends Activity {
35 
36     // We need to keep track of whether startIntentSenderForResult originated from a Fragment, so we
37     // can conditionally check whether the requestCode collides with our reserved ID space for the
38     // request index (see above). Unfortunately we can't just call
39     // super.startIntentSenderForResult(...) to bypass the check when the call didn't come from a
40     // fragment, since we need to use the ActivityCompat version for backward compatibility.
41     boolean mStartedIntentSenderFromFragment;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         if (Build.VERSION.SDK_INT < 11 && getLayoutInflater().getFactory() == null) {
46             // On pre-HC devices we need to manually install ourselves as a Factory.
47             // On HC and above, we are automatically installed as a private factory
48             getLayoutInflater().setFactory(this);
49         }
50 
51         super.onCreate(savedInstanceState);
52     }
53 
54     @Override
onCreateView(String name, Context context, AttributeSet attrs)55     public View onCreateView(String name, Context context, AttributeSet attrs) {
56         final View v = dispatchFragmentsOnCreateView(null, name, context, attrs);
57         if (v == null) {
58             return super.onCreateView(name, context, attrs);
59         }
60         return v;
61     }
62 
dispatchFragmentsOnCreateView(View parent, String name, Context context, AttributeSet attrs)63     abstract View dispatchFragmentsOnCreateView(View parent, String name,
64             Context context, AttributeSet attrs);
65 
66 
67     @Override
startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)68     public void startIntentSenderForResult(IntentSender intent, int requestCode,
69             @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
70             throws IntentSender.SendIntentException {
71         // If this was started from a Fragment we've already checked the upper 16 bits were not in
72         // use, and then repurposed them for the Fragment's index.
73         if (!mStartedIntentSenderFromFragment) {
74             if (requestCode != -1) {
75                 checkForValidRequestCode(requestCode);
76             }
77         }
78         super.startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, flagsValues,
79                 extraFlags);
80     }
81 
82     /**
83      * Checks whether the given request code is a valid code by masking it with 0xffff0000. Throws
84      * an {@link IllegalArgumentException} if the code is not valid.
85      */
checkForValidRequestCode(int requestCode)86     static void checkForValidRequestCode(int requestCode) {
87         if ((requestCode & 0xffff0000) != 0) {
88             throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
89         }
90     }
91 }
92