Home | History | Annotate | Download | only in launcher3
      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 package com.android.launcher3;
     17 
     18 import android.annotation.TargetApi;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.Build;
     22 import android.os.Bundle;
     23 import android.os.CancellationSignal;
     24 import android.os.Handler;
     25 
     26 import com.android.launcher3.states.InternalStateHandler;
     27 import com.android.quickstep.ActivityControlHelper.ActivityInitListener;
     28 import com.android.quickstep.OverviewCallbacks;
     29 import com.android.quickstep.util.RemoteAnimationProvider;
     30 
     31 import java.util.function.BiPredicate;
     32 
     33 @TargetApi(Build.VERSION_CODES.P)
     34 public class LauncherInitListener extends InternalStateHandler implements ActivityInitListener {
     35 
     36     private final BiPredicate<Launcher, Boolean> mOnInitListener;
     37 
     38     private RemoteAnimationProvider mRemoteAnimationProvider;
     39 
     40     public LauncherInitListener(BiPredicate<Launcher, Boolean> onInitListener) {
     41         mOnInitListener = onInitListener;
     42     }
     43 
     44     @Override
     45     protected boolean init(Launcher launcher, boolean alreadyOnHome) {
     46         if (mRemoteAnimationProvider != null) {
     47             LauncherAppTransitionManagerImpl appTransitionManager =
     48                     (LauncherAppTransitionManagerImpl) launcher.getAppTransitionManager();
     49 
     50             // Set a one-time animation provider. After the first call, this will get cleared.
     51             // TODO: Probably also check the intended target id.
     52             CancellationSignal cancellationSignal = new CancellationSignal();
     53             appTransitionManager.setRemoteAnimationProvider((targets) -> {
     54 
     55                 // On the first call clear the reference.
     56                 cancellationSignal.cancel();
     57                 RemoteAnimationProvider provider = mRemoteAnimationProvider;
     58                 mRemoteAnimationProvider = null;
     59 
     60                 if (provider != null && launcher.getStateManager().getState().overviewUi) {
     61                     return provider.createWindowAnimation(targets);
     62                 }
     63                 return null;
     64             }, cancellationSignal);
     65         }
     66         OverviewCallbacks.get(launcher).onInitOverviewTransition();
     67         return mOnInitListener.test(launcher, alreadyOnHome);
     68     }
     69 
     70     @Override
     71     public void register() {
     72         initWhenReady();
     73     }
     74 
     75     @Override
     76     public void unregister() {
     77         mRemoteAnimationProvider = null;
     78         clearReference();
     79     }
     80 
     81     @Override
     82     public void registerAndStartActivity(Intent intent, RemoteAnimationProvider animProvider,
     83             Context context, Handler handler, long duration) {
     84         mRemoteAnimationProvider = animProvider;
     85 
     86         register();
     87 
     88         Bundle options = animProvider.toActivityOptions(handler, duration).toBundle();
     89         context.startActivity(addToIntent(new Intent((intent))), options);
     90     }
     91 }
     92