Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2018 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 com.android.server.am;
     18 
     19 import android.annotation.Nullable;
     20 import android.app.ActivityOptions;
     21 import android.os.Handler;
     22 import android.util.ArrayMap;
     23 import android.view.RemoteAnimationAdapter;
     24 
     25 /**
     26  * Registry to keep track of remote animations to be run for activity starts from a certain package.
     27  *
     28  * @see ActivityManagerService#registerRemoteAnimationForNextActivityStart
     29  */
     30 class PendingRemoteAnimationRegistry {
     31 
     32     private static final long TIMEOUT_MS = 3000;
     33 
     34     private final ArrayMap<String, Entry> mEntries = new ArrayMap<>();
     35     private final Handler mHandler;
     36     private final ActivityManagerService mService;
     37 
     38     PendingRemoteAnimationRegistry(ActivityManagerService service, Handler handler) {
     39         mService = service;
     40         mHandler = handler;
     41     }
     42 
     43     /**
     44      * Adds a remote animation to be run for all activity starts originating from a certain package.
     45      */
     46     void addPendingAnimation(String packageName, RemoteAnimationAdapter adapter) {
     47         mEntries.put(packageName, new Entry(packageName, adapter));
     48     }
     49 
     50     /**
     51      * Overrides the activity options with a registered remote animation for a certain calling
     52      * package if such a remote animation is registered.
     53      */
     54     ActivityOptions overrideOptionsIfNeeded(String callingPackage,
     55             @Nullable ActivityOptions options) {
     56         final Entry entry = mEntries.get(callingPackage);
     57         if (entry == null) {
     58             return options;
     59         }
     60         if (options == null) {
     61             options = ActivityOptions.makeRemoteAnimation(entry.adapter);
     62         } else {
     63             options.setRemoteAnimationAdapter(entry.adapter);
     64         }
     65         mEntries.remove(callingPackage);
     66         return options;
     67     }
     68 
     69     private class Entry {
     70         final String packageName;
     71         final RemoteAnimationAdapter adapter;
     72 
     73         Entry(String packageName, RemoteAnimationAdapter adapter) {
     74             this.packageName = packageName;
     75             this.adapter = adapter;
     76             mHandler.postDelayed(() -> {
     77                 synchronized (mService) {
     78                     final Entry entry = mEntries.get(packageName);
     79                     if (entry == this) {
     80                         mEntries.remove(packageName);
     81                     }
     82                 }
     83             }, TIMEOUT_MS);
     84         }
     85     }
     86 }
     87