• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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.annotation.CallSuper;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.UnsupportedAppUsage;
23 import android.content.ComponentCallbacks;
24 import android.content.ComponentCallbacks2;
25 import android.content.Context;
26 import android.content.ContextWrapper;
27 import android.content.Intent;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.autofill.AutofillManager;
32 
33 import java.util.ArrayList;
34 
35 /**
36  * Base class for maintaining global application state. You can provide your own
37  * implementation by creating a subclass and specifying the fully-qualified name
38  * of this subclass as the <code>"android:name"</code> attribute in your
39  * AndroidManifest.xml's <code>&lt;application&gt;</code> tag. The Application
40  * class, or your subclass of the Application class, is instantiated before any
41  * other class when the process for your application/package is created.
42  *
43  * <p class="note"><strong>Note: </strong>There is normally no need to subclass
44  * Application.  In most situations, static singletons can provide the same
45  * functionality in a more modular way.  If your singleton needs a global
46  * context (for example to register broadcast receivers), include
47  * {@link android.content.Context#getApplicationContext() Context.getApplicationContext()}
48  * as a {@link android.content.Context} argument when invoking your singleton's
49  * <code>getInstance()</code> method.
50  * </p>
51  */
52 public class Application extends ContextWrapper implements ComponentCallbacks2 {
53     private static final String TAG = "Application";
54     @UnsupportedAppUsage
55     private ArrayList<ComponentCallbacks> mComponentCallbacks =
56             new ArrayList<ComponentCallbacks>();
57     @UnsupportedAppUsage
58     private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
59             new ArrayList<ActivityLifecycleCallbacks>();
60     @UnsupportedAppUsage
61     private ArrayList<OnProvideAssistDataListener> mAssistCallbacks = null;
62 
63     /** @hide */
64     @UnsupportedAppUsage
65     public LoadedApk mLoadedApk;
66 
67     public interface ActivityLifecycleCallbacks {
68 
69         /**
70          * Called as the first step of the Activity being created. This is always called before
71          * {@link Activity#onCreate}.
72          */
onActivityPreCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)73         default void onActivityPreCreated(@NonNull Activity activity,
74                 @Nullable Bundle savedInstanceState) {
75         }
76 
77         /**
78          * Called when the Activity calls {@link Activity#onCreate super.onCreate()}.
79          */
onActivityCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)80         void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState);
81 
82         /**
83          * Called as the last step of the Activity being created. This is always called after
84          * {@link Activity#onCreate}.
85          */
onActivityPostCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)86         default void onActivityPostCreated(@NonNull Activity activity,
87                 @Nullable Bundle savedInstanceState) {
88         }
89 
90         /**
91          * Called as the first step of the Activity being started. This is always called before
92          * {@link Activity#onStart}.
93          */
onActivityPreStarted(@onNull Activity activity)94         default void onActivityPreStarted(@NonNull Activity activity) {
95         }
96 
97         /**
98          * Called when the Activity calls {@link Activity#onStart super.onStart()}.
99          */
onActivityStarted(@onNull Activity activity)100         void onActivityStarted(@NonNull Activity activity);
101 
102         /**
103          * Called as the last step of the Activity being started. This is always called after
104          * {@link Activity#onStart}.
105          */
onActivityPostStarted(@onNull Activity activity)106         default void onActivityPostStarted(@NonNull Activity activity) {
107         }
108 
109         /**
110          * Called as the first step of the Activity being resumed. This is always called before
111          * {@link Activity#onResume}.
112          */
onActivityPreResumed(@onNull Activity activity)113         default void onActivityPreResumed(@NonNull Activity activity) {
114         }
115 
116         /**
117          * Called when the Activity calls {@link Activity#onResume super.onResume()}.
118          */
onActivityResumed(@onNull Activity activity)119         void onActivityResumed(@NonNull Activity activity);
120 
121         /**
122          * Called as the last step of the Activity being resumed. This is always called after
123          * {@link Activity#onResume} and {@link Activity#onPostResume}.
124          */
onActivityPostResumed(@onNull Activity activity)125         default void onActivityPostResumed(@NonNull Activity activity) {
126         }
127 
128         /**
129          * Called as the first step of the Activity being paused. This is always called before
130          * {@link Activity#onPause}.
131          */
onActivityPrePaused(@onNull Activity activity)132         default void onActivityPrePaused(@NonNull Activity activity) {
133         }
134 
135         /**
136          * Called when the Activity calls {@link Activity#onPause super.onPause()}.
137          */
onActivityPaused(@onNull Activity activity)138         void onActivityPaused(@NonNull Activity activity);
139 
140         /**
141          * Called as the last step of the Activity being paused. This is always called after
142          * {@link Activity#onPause}.
143          */
onActivityPostPaused(@onNull Activity activity)144         default void onActivityPostPaused(@NonNull Activity activity) {
145         }
146 
147         /**
148          * Called as the first step of the Activity being stopped. This is always called before
149          * {@link Activity#onStop}.
150          */
onActivityPreStopped(@onNull Activity activity)151         default void onActivityPreStopped(@NonNull Activity activity) {
152         }
153 
154         /**
155          * Called when the Activity calls {@link Activity#onStop super.onStop()}.
156          */
onActivityStopped(@onNull Activity activity)157         void onActivityStopped(@NonNull Activity activity);
158 
159         /**
160          * Called as the last step of the Activity being stopped. This is always called after
161          * {@link Activity#onStop}.
162          */
onActivityPostStopped(@onNull Activity activity)163         default void onActivityPostStopped(@NonNull Activity activity) {
164         }
165 
166         /**
167          * Called as the first step of the Activity saving its instance state. This is always
168          * called before {@link Activity#onSaveInstanceState}.
169          */
onActivityPreSaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)170         default void onActivityPreSaveInstanceState(@NonNull Activity activity,
171                 @NonNull Bundle outState) {
172         }
173 
174         /**
175          * Called when the Activity calls
176          * {@link Activity#onSaveInstanceState super.onSaveInstanceState()}.
177          */
onActivitySaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)178         void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState);
179 
180         /**
181          * Called as the last step of the Activity saving its instance state. This is always
182          * called after{@link Activity#onSaveInstanceState}.
183          */
onActivityPostSaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)184         default void onActivityPostSaveInstanceState(@NonNull Activity activity,
185                 @NonNull Bundle outState) {
186         }
187 
188         /**
189          * Called as the first step of the Activity being destroyed. This is always called before
190          * {@link Activity#onDestroy}.
191          */
onActivityPreDestroyed(@onNull Activity activity)192         default void onActivityPreDestroyed(@NonNull Activity activity) {
193         }
194 
195         /**
196          * Called when the Activity calls {@link Activity#onDestroy super.onDestroy()}.
197          */
onActivityDestroyed(@onNull Activity activity)198         void onActivityDestroyed(@NonNull Activity activity);
199 
200         /**
201          * Called as the last step of the Activity being destroyed. This is always called after
202          * {@link Activity#onDestroy}.
203          */
onActivityPostDestroyed(@onNull Activity activity)204         default void onActivityPostDestroyed(@NonNull Activity activity) {
205         }
206     }
207 
208     /**
209      * Callback interface for use with {@link Application#registerOnProvideAssistDataListener}
210      * and {@link Application#unregisterOnProvideAssistDataListener}.
211      */
212     public interface OnProvideAssistDataListener {
213         /**
214          * This is called when the user is requesting an assist, to build a full
215          * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
216          * application.  You can override this method to place into the bundle anything
217          * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
218          * of the assist Intent.
219          */
onProvideAssistData(Activity activity, Bundle data)220         public void onProvideAssistData(Activity activity, Bundle data);
221     }
222 
Application()223     public Application() {
224         super(null);
225     }
226 
227     /**
228      * Called when the application is starting, before any activity, service,
229      * or receiver objects (excluding content providers) have been created.
230      *
231      * <p>Implementations should be as quick as possible (for example using
232      * lazy initialization of state) since the time spent in this function
233      * directly impacts the performance of starting the first activity,
234      * service, or receiver in a process.</p>
235      *
236      * <p>If you override this method, be sure to call {@code super.onCreate()}.</p>
237      *
238      * <p class="note">Be aware that direct boot may also affect callback order on
239      * Android {@link android.os.Build.VERSION_CODES#N} and later devices.
240      * Until the user unlocks the device, only direct boot aware components are
241      * allowed to run. You should consider that all direct boot unaware
242      * components, including such {@link android.content.ContentProvider}, are
243      * disabled until user unlock happens, especially when component callback
244      * order matters.</p>
245      */
246     @CallSuper
onCreate()247     public void onCreate() {
248     }
249 
250     /**
251      * This method is for use in emulated process environments.  It will
252      * never be called on a production Android device, where processes are
253      * removed by simply killing them; no user code (including this callback)
254      * is executed when doing so.
255      */
256     @CallSuper
onTerminate()257     public void onTerminate() {
258     }
259 
260     @CallSuper
onConfigurationChanged(@onNull Configuration newConfig)261     public void onConfigurationChanged(@NonNull Configuration newConfig) {
262         Object[] callbacks = collectComponentCallbacks();
263         if (callbacks != null) {
264             for (int i=0; i<callbacks.length; i++) {
265                 ((ComponentCallbacks)callbacks[i]).onConfigurationChanged(newConfig);
266             }
267         }
268     }
269 
270     @CallSuper
onLowMemory()271     public void onLowMemory() {
272         Object[] callbacks = collectComponentCallbacks();
273         if (callbacks != null) {
274             for (int i=0; i<callbacks.length; i++) {
275                 ((ComponentCallbacks)callbacks[i]).onLowMemory();
276             }
277         }
278     }
279 
280     @CallSuper
onTrimMemory(int level)281     public void onTrimMemory(int level) {
282         Object[] callbacks = collectComponentCallbacks();
283         if (callbacks != null) {
284             for (int i=0; i<callbacks.length; i++) {
285                 Object c = callbacks[i];
286                 if (c instanceof ComponentCallbacks2) {
287                     ((ComponentCallbacks2)c).onTrimMemory(level);
288                 }
289             }
290         }
291     }
292 
registerComponentCallbacks(ComponentCallbacks callback)293     public void registerComponentCallbacks(ComponentCallbacks callback) {
294         synchronized (mComponentCallbacks) {
295             mComponentCallbacks.add(callback);
296         }
297     }
298 
unregisterComponentCallbacks(ComponentCallbacks callback)299     public void unregisterComponentCallbacks(ComponentCallbacks callback) {
300         synchronized (mComponentCallbacks) {
301             mComponentCallbacks.remove(callback);
302         }
303     }
304 
registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback)305     public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
306         synchronized (mActivityLifecycleCallbacks) {
307             mActivityLifecycleCallbacks.add(callback);
308         }
309     }
310 
unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback)311     public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
312         synchronized (mActivityLifecycleCallbacks) {
313             mActivityLifecycleCallbacks.remove(callback);
314         }
315     }
316 
registerOnProvideAssistDataListener(OnProvideAssistDataListener callback)317     public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
318         synchronized (this) {
319             if (mAssistCallbacks == null) {
320                 mAssistCallbacks = new ArrayList<OnProvideAssistDataListener>();
321             }
322             mAssistCallbacks.add(callback);
323         }
324     }
325 
unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback)326     public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
327         synchronized (this) {
328             if (mAssistCallbacks != null) {
329                 mAssistCallbacks.remove(callback);
330             }
331         }
332     }
333 
334     /**
335      * Returns the name of the current process. A package's default process name
336      * is the same as its package name. Non-default processes will look like
337      * "$PACKAGE_NAME:$NAME", where $NAME corresponds to an android:process
338      * attribute within AndroidManifest.xml.
339      */
getProcessName()340     public static String getProcessName() {
341         return ActivityThread.currentProcessName();
342     }
343 
344     // ------------------ Internal API ------------------
345 
346     /**
347      * @hide
348      */
349     @UnsupportedAppUsage
attach(Context context)350     /* package */ final void attach(Context context) {
351         attachBaseContext(context);
352         mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
353     }
354 
355     @UnsupportedAppUsage
dispatchActivityPreCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)356         /* package */ void dispatchActivityPreCreated(@NonNull Activity activity,
357             @Nullable Bundle savedInstanceState) {
358         Object[] callbacks = collectActivityLifecycleCallbacks();
359         if (callbacks != null) {
360             for (int i = 0; i < callbacks.length; i++) {
361                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(activity,
362                         savedInstanceState);
363             }
364         }
365     }
366 
367     @UnsupportedAppUsage
dispatchActivityCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)368     /* package */ void dispatchActivityCreated(@NonNull Activity activity,
369             @Nullable Bundle savedInstanceState) {
370         Object[] callbacks = collectActivityLifecycleCallbacks();
371         if (callbacks != null) {
372             for (int i=0; i<callbacks.length; i++) {
373                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
374                         savedInstanceState);
375             }
376         }
377     }
378 
379     @UnsupportedAppUsage
dispatchActivityPostCreated(@onNull Activity activity, @Nullable Bundle savedInstanceState)380         /* package */ void dispatchActivityPostCreated(@NonNull Activity activity,
381             @Nullable Bundle savedInstanceState) {
382         Object[] callbacks = collectActivityLifecycleCallbacks();
383         if (callbacks != null) {
384             for (int i = 0; i < callbacks.length; i++) {
385                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(activity,
386                         savedInstanceState);
387             }
388         }
389     }
390 
391     @UnsupportedAppUsage
dispatchActivityPreStarted(@onNull Activity activity)392         /* package */ void dispatchActivityPreStarted(@NonNull Activity activity) {
393         Object[] callbacks = collectActivityLifecycleCallbacks();
394         if (callbacks != null) {
395             for (int i = 0; i < callbacks.length; i++) {
396                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(activity);
397             }
398         }
399     }
400 
401     @UnsupportedAppUsage
dispatchActivityStarted(@onNull Activity activity)402     /* package */ void dispatchActivityStarted(@NonNull Activity activity) {
403         Object[] callbacks = collectActivityLifecycleCallbacks();
404         if (callbacks != null) {
405             for (int i=0; i<callbacks.length; i++) {
406                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStarted(activity);
407             }
408         }
409     }
410 
411     @UnsupportedAppUsage
dispatchActivityPostStarted(@onNull Activity activity)412         /* package */ void dispatchActivityPostStarted(@NonNull Activity activity) {
413         Object[] callbacks = collectActivityLifecycleCallbacks();
414         if (callbacks != null) {
415             for (int i = 0; i < callbacks.length; i++) {
416                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostStarted(activity);
417             }
418         }
419     }
420 
421     @UnsupportedAppUsage
dispatchActivityPreResumed(@onNull Activity activity)422         /* package */ void dispatchActivityPreResumed(@NonNull Activity activity) {
423         Object[] callbacks = collectActivityLifecycleCallbacks();
424         if (callbacks != null) {
425             for (int i = 0; i < callbacks.length; i++) {
426                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(activity);
427             }
428         }
429     }
430 
431     @UnsupportedAppUsage
dispatchActivityResumed(@onNull Activity activity)432     /* package */ void dispatchActivityResumed(@NonNull Activity activity) {
433         Object[] callbacks = collectActivityLifecycleCallbacks();
434         if (callbacks != null) {
435             for (int i=0; i<callbacks.length; i++) {
436                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityResumed(activity);
437             }
438         }
439     }
440 
441     @UnsupportedAppUsage
dispatchActivityPostResumed(@onNull Activity activity)442         /* package */ void dispatchActivityPostResumed(@NonNull Activity activity) {
443         Object[] callbacks = collectActivityLifecycleCallbacks();
444         if (callbacks != null) {
445             for (int i = 0; i < callbacks.length; i++) {
446                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(activity);
447             }
448         }
449     }
450 
451     @UnsupportedAppUsage
dispatchActivityPrePaused(@onNull Activity activity)452         /* package */ void dispatchActivityPrePaused(@NonNull Activity activity) {
453         Object[] callbacks = collectActivityLifecycleCallbacks();
454         if (callbacks != null) {
455             for (int i = 0; i < callbacks.length; i++) {
456                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(activity);
457             }
458         }
459     }
460 
461     @UnsupportedAppUsage
dispatchActivityPaused(@onNull Activity activity)462     /* package */ void dispatchActivityPaused(@NonNull Activity activity) {
463         Object[] callbacks = collectActivityLifecycleCallbacks();
464         if (callbacks != null) {
465             for (int i=0; i<callbacks.length; i++) {
466                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity);
467             }
468         }
469     }
470 
471     @UnsupportedAppUsage
dispatchActivityPostPaused(@onNull Activity activity)472         /* package */ void dispatchActivityPostPaused(@NonNull Activity activity) {
473         Object[] callbacks = collectActivityLifecycleCallbacks();
474         if (callbacks != null) {
475             for (int i = 0; i < callbacks.length; i++) {
476                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(activity);
477             }
478         }
479     }
480 
481     @UnsupportedAppUsage
dispatchActivityPreStopped(@onNull Activity activity)482         /* package */ void dispatchActivityPreStopped(@NonNull Activity activity) {
483         Object[] callbacks = collectActivityLifecycleCallbacks();
484         if (callbacks != null) {
485             for (int i = 0; i < callbacks.length; i++) {
486                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(activity);
487             }
488         }
489     }
490 
491     @UnsupportedAppUsage
dispatchActivityStopped(@onNull Activity activity)492     /* package */ void dispatchActivityStopped(@NonNull Activity activity) {
493         Object[] callbacks = collectActivityLifecycleCallbacks();
494         if (callbacks != null) {
495             for (int i=0; i<callbacks.length; i++) {
496                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStopped(activity);
497             }
498         }
499     }
500 
501     @UnsupportedAppUsage
dispatchActivityPostStopped(@onNull Activity activity)502         /* package */ void dispatchActivityPostStopped(@NonNull Activity activity) {
503         Object[] callbacks = collectActivityLifecycleCallbacks();
504         if (callbacks != null) {
505             for (int i = 0; i < callbacks.length; i++) {
506                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostStopped(activity);
507             }
508         }
509     }
510 
511     @UnsupportedAppUsage
dispatchActivityPreSaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)512         /* package */ void dispatchActivityPreSaveInstanceState(@NonNull Activity activity,
513             @NonNull Bundle outState) {
514         Object[] callbacks = collectActivityLifecycleCallbacks();
515         if (callbacks != null) {
516             for (int i = 0; i < callbacks.length; i++) {
517                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreSaveInstanceState(
518                         activity, outState);
519             }
520         }
521     }
522 
523     @UnsupportedAppUsage
dispatchActivitySaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)524     /* package */ void dispatchActivitySaveInstanceState(@NonNull Activity activity,
525             @NonNull Bundle outState) {
526         Object[] callbacks = collectActivityLifecycleCallbacks();
527         if (callbacks != null) {
528             for (int i=0; i<callbacks.length; i++) {
529                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivitySaveInstanceState(activity,
530                         outState);
531             }
532         }
533     }
534 
535     @UnsupportedAppUsage
dispatchActivityPostSaveInstanceState(@onNull Activity activity, @NonNull Bundle outState)536         /* package */ void dispatchActivityPostSaveInstanceState(@NonNull Activity activity,
537             @NonNull Bundle outState) {
538         Object[] callbacks = collectActivityLifecycleCallbacks();
539         if (callbacks != null) {
540             for (int i = 0; i < callbacks.length; i++) {
541                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostSaveInstanceState(
542                         activity, outState);
543             }
544         }
545     }
546 
547     @UnsupportedAppUsage
dispatchActivityPreDestroyed(@onNull Activity activity)548         /* package */ void dispatchActivityPreDestroyed(@NonNull Activity activity) {
549         Object[] callbacks = collectActivityLifecycleCallbacks();
550         if (callbacks != null) {
551             for (int i = 0; i < callbacks.length; i++) {
552                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPreDestroyed(activity);
553             }
554         }
555     }
556 
557     @UnsupportedAppUsage
dispatchActivityDestroyed(@onNull Activity activity)558     /* package */ void dispatchActivityDestroyed(@NonNull Activity activity) {
559         Object[] callbacks = collectActivityLifecycleCallbacks();
560         if (callbacks != null) {
561             for (int i=0; i<callbacks.length; i++) {
562                 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
563             }
564         }
565     }
566 
567     @UnsupportedAppUsage
dispatchActivityPostDestroyed(@onNull Activity activity)568         /* package */ void dispatchActivityPostDestroyed(@NonNull Activity activity) {
569         Object[] callbacks = collectActivityLifecycleCallbacks();
570         if (callbacks != null) {
571             for (int i = 0; i < callbacks.length; i++) {
572                 ((ActivityLifecycleCallbacks) callbacks[i]).onActivityPostDestroyed(activity);
573             }
574         }
575     }
576 
collectComponentCallbacks()577     private Object[] collectComponentCallbacks() {
578         Object[] callbacks = null;
579         synchronized (mComponentCallbacks) {
580             if (mComponentCallbacks.size() > 0) {
581                 callbacks = mComponentCallbacks.toArray();
582             }
583         }
584         return callbacks;
585     }
586 
587     @UnsupportedAppUsage
collectActivityLifecycleCallbacks()588     private Object[] collectActivityLifecycleCallbacks() {
589         Object[] callbacks = null;
590         synchronized (mActivityLifecycleCallbacks) {
591             if (mActivityLifecycleCallbacks.size() > 0) {
592                 callbacks = mActivityLifecycleCallbacks.toArray();
593             }
594         }
595         return callbacks;
596     }
597 
dispatchOnProvideAssistData(Activity activity, Bundle data)598     /* package */ void dispatchOnProvideAssistData(Activity activity, Bundle data) {
599         Object[] callbacks;
600         synchronized (this) {
601             if (mAssistCallbacks == null) {
602                 return;
603             }
604             callbacks = mAssistCallbacks.toArray();
605         }
606         if (callbacks != null) {
607             for (int i=0; i<callbacks.length; i++) {
608                 ((OnProvideAssistDataListener)callbacks[i]).onProvideAssistData(activity, data);
609             }
610         }
611     }
612 
613     /** @hide */
614     @Override
getAutofillClient()615     public AutofillManager.AutofillClient getAutofillClient() {
616         final AutofillManager.AutofillClient client = super.getAutofillClient();
617         if (client != null) {
618             return client;
619         }
620         if (android.view.autofill.Helper.sVerbose) {
621             Log.v(TAG, "getAutofillClient(): null on super, trying to find activity thread");
622         }
623         // Okay, ppl use the application context when they should not. This breaks
624         // autofill among other things. We pick the focused activity since autofill
625         // interacts only with the currently focused activity and we need the fill
626         // client only if a call comes from the focused activity. Sigh...
627         final ActivityThread activityThread = ActivityThread.currentActivityThread();
628         if (activityThread == null) {
629             return null;
630         }
631         final int activityCount = activityThread.mActivities.size();
632         for (int i = 0; i < activityCount; i++) {
633             final ActivityThread.ActivityClientRecord record =
634                     activityThread.mActivities.valueAt(i);
635             if (record == null) {
636                 continue;
637             }
638             final Activity activity = record.activity;
639             if (activity == null) {
640                 continue;
641             }
642             if (activity.getWindow().getDecorView().hasFocus()) {
643                 if (android.view.autofill.Helper.sVerbose) {
644                     Log.v(TAG, "getAutofillClient(): found activity for " + this + ": " + activity);
645                 }
646                 return activity;
647             }
648         }
649         if (android.view.autofill.Helper.sVerbose) {
650             Log.v(TAG, "getAutofillClient(): none of the " + activityCount + " activities on "
651                     + this + " have focus");
652         }
653         return null;
654     }
655 }
656