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