[Large screen] Check if it's in 2-pane before 2-pane deep link flow
The callback of SplitStateObserver.SplitStateListener can have one or more active splits when there is already 2-pane in Activity stack. This change prevent unnecessary 2-pane deep link flow if the Activity is already in 2-pane activity stack. This solution does not work if the Activity was started in a new task. (e.g., Intent.FLAG_ACTIVITY_NEW_TASK, launchMode singleTask). Bug: 201379454 Bug: 201620626 Bug: 204398432 Bug: 204397936 Bug: 197609195 Bug: 197609197 Bug: 204501179 Bug: 204959335 Bug: 204845334 Test: manual 1. Settings -> Apps > Default apps > Opening links. 2. Click back button should back to Default apps page. Change-Id: I04aaceed47a8f2754a4e17c53b49252f61e0a1d1
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.activityembedding;
|
||||
|
||||
import static androidx.lifecycle.Lifecycle.Event.ON_START;
|
||||
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.util.Consumer;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.window.embedding.SplitController;
|
||||
import androidx.window.embedding.SplitInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** A lifecycle-aware observer listens to active split state. */
|
||||
public class SplitStateObserver implements LifecycleObserver, Consumer<List<SplitInfo>> {
|
||||
|
||||
private final Activity mActivity;
|
||||
private final boolean mListenOnce;
|
||||
private final SplitStateListener mListener;
|
||||
private final SplitController mSplitController;
|
||||
|
||||
public SplitStateObserver(@NonNull Activity activity, boolean listenOnce,
|
||||
@NonNull SplitStateListener listener) {
|
||||
mActivity = activity;
|
||||
mListenOnce = listenOnce;
|
||||
mListener = listener;
|
||||
mSplitController = SplitController.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start lifecycle event.
|
||||
*/
|
||||
@OnLifecycleEvent(ON_START)
|
||||
public void onStart() {
|
||||
mSplitController.addSplitListener(mActivity, ContextCompat.getMainExecutor(mActivity),
|
||||
this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop lifecycle event.
|
||||
*/
|
||||
@OnLifecycleEvent(ON_STOP)
|
||||
public void onStop() {
|
||||
mSplitController.removeSplitListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(List<SplitInfo> splitInfos) {
|
||||
if (mListenOnce) {
|
||||
mSplitController.removeSplitListener(this);
|
||||
}
|
||||
mListener.onSplitInfoChanged(splitInfos);
|
||||
}
|
||||
|
||||
/** This interface makes as class that it wants to listen to {@link SplitInfo} changes. */
|
||||
public interface SplitStateListener {
|
||||
|
||||
/** Receive a set of split info change */
|
||||
void onSplitInfoChanged(List<SplitInfo> splitInfos);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user