1 /*
2  * Copyright (C) 2020 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.Nullable;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.content.res.Configuration;
23 import android.os.Bundle;
24 import android.os.IBinder;
25 import android.os.PersistableBundle;
26 import android.os.RemoteException;
27 import android.util.Singleton;
28 import android.view.RemoteAnimationDefinition;
29 import android.window.SizeConfigurationBuckets;
30 
31 import com.android.internal.policy.IKeyguardDismissCallback;
32 
33 /**
34  * Provides the activity associated operations that communicate with system.
35  *
36  * @hide
37  */
38 public class ActivityClient {
ActivityClient()39     private ActivityClient() {}
40 
41     /** Reports the main thread is idle after the activity is resumed. */
activityIdle(IBinder token, Configuration config, boolean stopProfiling)42     public void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
43         try {
44             getActivityClientController().activityIdle(token, config, stopProfiling);
45         } catch (RemoteException e) {
46             e.rethrowFromSystemServer();
47         }
48     }
49 
50     /** Reports {@link Activity#onResume()} is done. */
activityResumed(IBinder token, boolean handleSplashScreenExit)51     public void activityResumed(IBinder token, boolean handleSplashScreenExit) {
52         try {
53             getActivityClientController().activityResumed(token, handleSplashScreenExit);
54         } catch (RemoteException e) {
55             e.rethrowFromSystemServer();
56         }
57     }
58 
59     /**
60      * Reports after {@link Activity#onTopResumedActivityChanged(boolean)} is called for losing the
61      * top most position.
62      */
activityTopResumedStateLost()63     public void activityTopResumedStateLost() {
64         try {
65             getActivityClientController().activityTopResumedStateLost();
66         } catch (RemoteException e) {
67             e.rethrowFromSystemServer();
68         }
69     }
70 
71     /** Reports {@link Activity#onPause()} is done. */
activityPaused(IBinder token)72     public void activityPaused(IBinder token) {
73         try {
74             getActivityClientController().activityPaused(token);
75         } catch (RemoteException e) {
76             e.rethrowFromSystemServer();
77         }
78     }
79 
80     /** Reports {@link Activity#onStop()} is done. */
activityStopped(IBinder token, Bundle state, PersistableBundle persistentState, CharSequence description)81     public void activityStopped(IBinder token, Bundle state, PersistableBundle persistentState,
82             CharSequence description) {
83         try {
84             getActivityClientController().activityStopped(token, state, persistentState,
85                     description);
86         } catch (RemoteException e) {
87             e.rethrowFromSystemServer();
88         }
89     }
90 
91     /** Reports {@link Activity#onDestroy()} is done. */
activityDestroyed(IBinder token)92     public void activityDestroyed(IBinder token) {
93         try {
94             getActivityClientController().activityDestroyed(token);
95         } catch (RemoteException e) {
96             e.rethrowFromSystemServer();
97         }
98     }
99 
100     /** Reports the activity has completed relaunched. */
activityRelaunched(IBinder token)101     public void activityRelaunched(IBinder token) {
102         try {
103             getActivityClientController().activityRelaunched(token);
104         } catch (RemoteException e) {
105             e.rethrowFromSystemServer();
106         }
107     }
108 
reportSizeConfigurations(IBinder token, SizeConfigurationBuckets sizeConfigurations)109     void reportSizeConfigurations(IBinder token, SizeConfigurationBuckets sizeConfigurations) {
110         try {
111             getActivityClientController().reportSizeConfigurations(token, sizeConfigurations);
112         } catch (RemoteException e) {
113             e.rethrowFromSystemServer();
114         }
115     }
116 
moveActivityTaskToBack(IBinder token, boolean nonRoot)117     public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
118         try {
119             return getActivityClientController().moveActivityTaskToBack(token, nonRoot);
120         } catch (RemoteException e) {
121             throw e.rethrowFromSystemServer();
122         }
123     }
124 
shouldUpRecreateTask(IBinder token, String destAffinity)125     boolean shouldUpRecreateTask(IBinder token, String destAffinity) {
126         try {
127             return getActivityClientController().shouldUpRecreateTask(token, destAffinity);
128         } catch (RemoteException e) {
129             throw e.rethrowFromSystemServer();
130         }
131     }
132 
navigateUpTo(IBinder token, Intent destIntent, int resultCode, Intent resultData)133     boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
134             Intent resultData) {
135         try {
136             return getActivityClientController().navigateUpTo(token, destIntent, resultCode,
137                     resultData);
138         } catch (RemoteException e) {
139             throw e.rethrowFromSystemServer();
140         }
141     }
142 
releaseActivityInstance(IBinder token)143     boolean releaseActivityInstance(IBinder token) {
144         try {
145             return getActivityClientController().releaseActivityInstance(token);
146         } catch (RemoteException e) {
147             throw e.rethrowFromSystemServer();
148         }
149     }
150 
finishActivity(IBinder token, int resultCode, Intent resultData, int finishTask)151     public boolean finishActivity(IBinder token, int resultCode, Intent resultData,
152             int finishTask) {
153         try {
154             return getActivityClientController().finishActivity(token, resultCode, resultData,
155                     finishTask);
156         } catch (RemoteException e) {
157             throw e.rethrowFromSystemServer();
158         }
159     }
160 
finishActivityAffinity(IBinder token)161     boolean finishActivityAffinity(IBinder token) {
162         try {
163             return getActivityClientController().finishActivityAffinity(token);
164         } catch (RemoteException e) {
165             throw e.rethrowFromSystemServer();
166         }
167     }
168 
finishSubActivity(IBinder token, String resultWho, int requestCode)169     void finishSubActivity(IBinder token, String resultWho, int requestCode) {
170         try {
171             getActivityClientController().finishSubActivity(token, resultWho, requestCode);
172         } catch (RemoteException e) {
173             e.rethrowFromSystemServer();
174         }
175     }
176 
isTopOfTask(IBinder token)177     public boolean isTopOfTask(IBinder token) {
178         try {
179             return getActivityClientController().isTopOfTask(token);
180         } catch (RemoteException e) {
181             throw e.rethrowFromSystemServer();
182         }
183     }
184 
willActivityBeVisible(IBinder token)185     boolean willActivityBeVisible(IBinder token) {
186         try {
187             return getActivityClientController().willActivityBeVisible(token);
188         } catch (RemoteException e) {
189             throw e.rethrowFromSystemServer();
190         }
191     }
192 
getDisplayId(IBinder token)193     public int getDisplayId(IBinder token) {
194         try {
195             return getActivityClientController().getDisplayId(token);
196         } catch (RemoteException e) {
197             throw e.rethrowFromSystemServer();
198         }
199     }
200 
getTaskForActivity(IBinder token, boolean onlyRoot)201     public int getTaskForActivity(IBinder token, boolean onlyRoot) {
202         try {
203             return getActivityClientController().getTaskForActivity(token, onlyRoot);
204         } catch (RemoteException e) {
205             throw e.rethrowFromSystemServer();
206         }
207     }
208 
209     /**
210      * Returns the non-finishing activity token below in the same task if it belongs to the same
211      * process.
212      */
213     @Nullable
getActivityTokenBelow(IBinder activityToken)214     public IBinder getActivityTokenBelow(IBinder activityToken) {
215         try {
216             return getActivityClientController().getActivityTokenBelow(activityToken);
217         } catch (RemoteException e) {
218             throw e.rethrowFromSystemServer();
219         }
220     }
221 
getCallingActivity(IBinder token)222     ComponentName getCallingActivity(IBinder token) {
223         try {
224             return getActivityClientController().getCallingActivity(token);
225         } catch (RemoteException e) {
226             throw e.rethrowFromSystemServer();
227         }
228     }
229 
getCallingPackage(IBinder token)230     String getCallingPackage(IBinder token) {
231         try {
232             return getActivityClientController().getCallingPackage(token);
233         } catch (RemoteException e) {
234             throw e.rethrowFromSystemServer();
235         }
236     }
237 
getLaunchedFromUid(IBinder token)238     public int getLaunchedFromUid(IBinder token) {
239         try {
240             return getActivityClientController().getLaunchedFromUid(token);
241         } catch (RemoteException e) {
242             throw e.rethrowFromSystemServer();
243         }
244     }
245 
getLaunchedFromPackage(IBinder token)246     public String getLaunchedFromPackage(IBinder token) {
247         try {
248             return getActivityClientController().getLaunchedFromPackage(token);
249         } catch (RemoteException e) {
250             throw e.rethrowFromSystemServer();
251         }
252     }
253 
setRequestedOrientation(IBinder token, int requestedOrientation)254     public void setRequestedOrientation(IBinder token, int requestedOrientation) {
255         try {
256             getActivityClientController().setRequestedOrientation(token, requestedOrientation);
257         } catch (RemoteException e) {
258             e.rethrowFromSystemServer();
259         }
260     }
261 
getRequestedOrientation(IBinder token)262     int getRequestedOrientation(IBinder token) {
263         try {
264             return getActivityClientController().getRequestedOrientation(token);
265         } catch (RemoteException e) {
266             throw e.rethrowFromSystemServer();
267         }
268     }
269 
convertFromTranslucent(IBinder token)270     boolean convertFromTranslucent(IBinder token) {
271         try {
272             return getActivityClientController().convertFromTranslucent(token);
273         } catch (RemoteException e) {
274             throw e.rethrowFromSystemServer();
275         }
276     }
277 
convertToTranslucent(IBinder token, Bundle options)278     boolean convertToTranslucent(IBinder token, Bundle options) {
279         try {
280             return getActivityClientController().convertToTranslucent(token, options);
281         } catch (RemoteException e) {
282             throw e.rethrowFromSystemServer();
283         }
284     }
285 
reportActivityFullyDrawn(IBinder token, boolean restoredFromBundle)286     void reportActivityFullyDrawn(IBinder token, boolean restoredFromBundle) {
287         try {
288             getActivityClientController().reportActivityFullyDrawn(token, restoredFromBundle);
289         } catch (RemoteException e) {
290             e.rethrowFromSystemServer();
291         }
292     }
293 
isImmersive(IBinder token)294     boolean isImmersive(IBinder token) {
295         try {
296             return getActivityClientController().isImmersive(token);
297         } catch (RemoteException e) {
298             throw e.rethrowFromSystemServer();
299         }
300     }
301 
setImmersive(IBinder token, boolean immersive)302     void setImmersive(IBinder token, boolean immersive) {
303         try {
304             getActivityClientController().setImmersive(token, immersive);
305         } catch (RemoteException e) {
306             e.rethrowFromSystemServer();
307         }
308     }
309 
enterPictureInPictureMode(IBinder token, PictureInPictureParams params)310     boolean enterPictureInPictureMode(IBinder token, PictureInPictureParams params) {
311         try {
312             return getActivityClientController().enterPictureInPictureMode(token, params);
313         } catch (RemoteException e) {
314             throw e.rethrowFromSystemServer();
315         }
316     }
317 
setPictureInPictureParams(IBinder token, PictureInPictureParams params)318     void setPictureInPictureParams(IBinder token, PictureInPictureParams params) {
319         try {
320             getActivityClientController().setPictureInPictureParams(token, params);
321         } catch (RemoteException e) {
322             e.rethrowFromSystemServer();
323         }
324     }
325 
toggleFreeformWindowingMode(IBinder token)326     void toggleFreeformWindowingMode(IBinder token) {
327         try {
328             getActivityClientController().toggleFreeformWindowingMode(token);
329         } catch (RemoteException e) {
330             e.rethrowFromSystemServer();
331         }
332     }
333 
startLockTaskModeByToken(IBinder token)334     void startLockTaskModeByToken(IBinder token) {
335         try {
336             getActivityClientController().startLockTaskModeByToken(token);
337         } catch (RemoteException e) {
338             e.rethrowFromSystemServer();
339         }
340     }
341 
stopLockTaskModeByToken(IBinder token)342     void stopLockTaskModeByToken(IBinder token) {
343         try {
344             getActivityClientController().stopLockTaskModeByToken(token);
345         } catch (RemoteException e) {
346             e.rethrowFromSystemServer();
347         }
348     }
349 
showLockTaskEscapeMessage(IBinder token)350     void showLockTaskEscapeMessage(IBinder token) {
351         try {
352             getActivityClientController().showLockTaskEscapeMessage(token);
353         } catch (RemoteException e) {
354             e.rethrowFromSystemServer();
355         }
356     }
357 
setTaskDescription(IBinder token, ActivityManager.TaskDescription td)358     void setTaskDescription(IBinder token, ActivityManager.TaskDescription td) {
359         try {
360             getActivityClientController().setTaskDescription(token, td);
361         } catch (RemoteException e) {
362             e.rethrowFromSystemServer();
363         }
364     }
365 
showAssistFromActivity(IBinder token, Bundle args)366     boolean showAssistFromActivity(IBinder token, Bundle args) {
367         try {
368             return getActivityClientController().showAssistFromActivity(token, args);
369         } catch (RemoteException e) {
370             throw e.rethrowFromSystemServer();
371         }
372     }
373 
isRootVoiceInteraction(IBinder token)374     boolean isRootVoiceInteraction(IBinder token) {
375         try {
376             return getActivityClientController().isRootVoiceInteraction(token);
377         } catch (RemoteException e) {
378             throw e.rethrowFromSystemServer();
379         }
380     }
381 
startLocalVoiceInteraction(IBinder callingActivity, Bundle options)382     void startLocalVoiceInteraction(IBinder callingActivity, Bundle options) {
383         try {
384             getActivityClientController().startLocalVoiceInteraction(callingActivity, options);
385         } catch (RemoteException e) {
386             e.rethrowFromSystemServer();
387         }
388     }
389 
stopLocalVoiceInteraction(IBinder callingActivity)390     void stopLocalVoiceInteraction(IBinder callingActivity) {
391         try {
392             getActivityClientController().stopLocalVoiceInteraction(callingActivity);
393         } catch (RemoteException e) {
394             e.rethrowFromSystemServer();
395         }
396     }
397 
setShowWhenLocked(IBinder token, boolean showWhenLocked)398     void setShowWhenLocked(IBinder token, boolean showWhenLocked) {
399         try {
400             getActivityClientController().setShowWhenLocked(token, showWhenLocked);
401         } catch (RemoteException e) {
402             e.rethrowFromSystemServer();
403         }
404     }
405 
setInheritShowWhenLocked(IBinder token, boolean inheritShowWhenLocked)406     void setInheritShowWhenLocked(IBinder token, boolean inheritShowWhenLocked) {
407         try {
408             getActivityClientController().setInheritShowWhenLocked(token, inheritShowWhenLocked);
409         } catch (RemoteException e) {
410             e.rethrowFromSystemServer();
411         }
412     }
413 
setTurnScreenOn(IBinder token, boolean turnScreenOn)414     void setTurnScreenOn(IBinder token, boolean turnScreenOn) {
415         try {
416             getActivityClientController().setTurnScreenOn(token, turnScreenOn);
417         } catch (RemoteException e) {
418             e.rethrowFromSystemServer();
419         }
420     }
421 
setVrMode(IBinder token, boolean enabled, ComponentName packageName)422     int setVrMode(IBinder token, boolean enabled, ComponentName packageName) {
423         try {
424             return getActivityClientController().setVrMode(token, enabled, packageName);
425         } catch (RemoteException e) {
426             throw e.rethrowFromSystemServer();
427         }
428     }
429 
overridePendingTransition(IBinder token, String packageName, int enterAnim, int exitAnim)430     void overridePendingTransition(IBinder token, String packageName,
431             int enterAnim, int exitAnim) {
432         try {
433             getActivityClientController().overridePendingTransition(token, packageName,
434                     enterAnim, exitAnim);
435         } catch (RemoteException e) {
436             e.rethrowFromSystemServer();
437         }
438     }
439 
setDisablePreviewScreenshots(IBinder token, boolean disable)440     void setDisablePreviewScreenshots(IBinder token, boolean disable) {
441         try {
442             getActivityClientController().setDisablePreviewScreenshots(token, disable);
443         } catch (RemoteException e) {
444             e.rethrowFromSystemServer();
445         }
446     }
447 
448     /** Removes the snapshot of home task. */
invalidateHomeTaskSnapshot(IBinder homeToken)449     public void invalidateHomeTaskSnapshot(IBinder homeToken) {
450         try {
451             getActivityClientController().invalidateHomeTaskSnapshot(homeToken);
452         } catch (RemoteException e) {
453             e.rethrowFromSystemServer();
454         }
455     }
456 
dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message)457     void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback,
458             CharSequence message) {
459         try {
460             getActivityClientController().dismissKeyguard(token, callback, message);
461         } catch (RemoteException e) {
462             e.rethrowFromSystemServer();
463         }
464     }
465 
registerRemoteAnimations(IBinder token, RemoteAnimationDefinition definition)466     void registerRemoteAnimations(IBinder token, RemoteAnimationDefinition definition) {
467         try {
468             getActivityClientController().registerRemoteAnimations(token, definition);
469         } catch (RemoteException e) {
470             e.rethrowFromSystemServer();
471         }
472     }
473 
unregisterRemoteAnimations(IBinder token)474     void unregisterRemoteAnimations(IBinder token) {
475         try {
476             getActivityClientController().unregisterRemoteAnimations(token);
477         } catch (RemoteException e) {
478             e.rethrowFromSystemServer();
479         }
480     }
481 
onBackPressedOnTaskRoot(IBinder token, IRequestFinishCallback callback)482     void onBackPressedOnTaskRoot(IBinder token, IRequestFinishCallback callback) {
483         try {
484             getActivityClientController().onBackPressedOnTaskRoot(token, callback);
485         } catch (RemoteException e) {
486             e.rethrowFromSystemServer();
487         }
488     }
489 
490     /**
491      * Reports the splash screen view has attached to client.
492      */
reportSplashScreenAttached(IBinder token)493     void reportSplashScreenAttached(IBinder token) {
494         try {
495             getActivityClientController().splashScreenAttached(token);
496         } catch (RemoteException e) {
497             e.rethrowFromSystemServer();
498         }
499     }
500 
getInstance()501     public static ActivityClient getInstance() {
502         return sInstance.get();
503     }
504 
505     /**
506      * If system server has passed the controller interface, store it so the subsequent access can
507      * speed up.
508      */
setActivityClientController( IActivityClientController activityClientController)509     public static IActivityClientController setActivityClientController(
510             IActivityClientController activityClientController) {
511         // No lock because it is no harm to encounter race condition. The thread safe Singleton#get
512         // will take over that case.
513         return INTERFACE_SINGLETON.mKnownInstance = activityClientController;
514     }
515 
getActivityClientController()516     private static IActivityClientController getActivityClientController() {
517         final IActivityClientController controller = INTERFACE_SINGLETON.mKnownInstance;
518         return controller != null ? controller : INTERFACE_SINGLETON.get();
519     }
520 
521     private static final Singleton<ActivityClient> sInstance = new Singleton<ActivityClient>() {
522         @Override
523         protected ActivityClient create() {
524             return new ActivityClient();
525         }
526     };
527 
528     private static final ActivityClientControllerSingleton INTERFACE_SINGLETON =
529             new ActivityClientControllerSingleton();
530 
531     private static class ActivityClientControllerSingleton
532             extends Singleton<IActivityClientController> {
533         /**
534          * A quick look up to reduce potential extra binder transactions. E.g. getting activity
535          * task manager from service manager and controller from activity task manager.
536          */
537         IActivityClientController mKnownInstance;
538 
539         @Override
create()540         protected IActivityClientController create() {
541             try {
542                 return ActivityTaskManager.getService().getActivityClientController();
543             } catch (RemoteException e) {
544                 throw e.rethrowFromSystemServer();
545             }
546         }
547     }
548 }
549