Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a474716f authored by Jaewan Kim's avatar Jaewan Kim Committed by android-build-merger
Browse files

PIP: Handle layoutDirection changes

am: 73ef3516

Change-Id: I75b84d9f142f17f4fa19492f668dd3bb302c43cf
parents 1fe4acdc 73ef3516
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -388,7 +388,7 @@
            android:theme="@style/PipTheme"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|locale|layoutDirection"
            android:resizeableActivity="true"
            android:supportsPictureInPicture="true"
            androidprv:alwaysFocusable="true"
@@ -398,7 +398,7 @@
            android:exported="true"
            android:theme="@style/PipTheme"
            android:taskAffinity=""
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|locale|layoutDirection"
            android:resizeableActivity="true"
            android:supportsPictureInPicture="true"
            android:excludeFromRecents="true" />
+21 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 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.
-->

<resources>
    <!-- Bounds [left top right bottom] on screen for picture-in-picture (PIP) windows,
         when the PIP menu is shown with settings. -->
    <string translatable="false" name="pip_settings_bounds">"778 54 1258 324"</string>
</resources>
+56 −47
Original line number Diff line number Diff line
@@ -196,7 +196,23 @@ public class PipManager {
        }
        mInitialized = true;
        mContext = context;
        Resources res = context.getResources();

        mActivityManager = ActivityManagerNative.getDefault();
        SystemServicesProxy.getInstance(context).registerTaskStackListener(mTaskStackListener);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
        mContext.registerReceiver(mBroadcastReceiver, intentFilter);
        mOnboardingShown = Prefs.getBoolean(
                mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, false);

        loadConfigurationsAndApply();
        mPipRecentsOverlayManager = new PipRecentsOverlayManager(context);
        mMediaSessionManager =
                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
    }

    private void loadConfigurationsAndApply() {
        Resources res = mContext.getResources();
        mDefaultPipBounds = Rect.unflattenFromString(res.getString(
                com.android.internal.R.string.config_defaultPictureInPictureBounds));
        mSettingsPipBounds = Rect.unflattenFromString(res.getString(
@@ -209,25 +225,19 @@ public class PipManager {
                R.string.pip_recents_focused_bounds));
        mRecentsFocusChangedAnimationDurationMs = res.getInteger(
                R.integer.recents_tv_pip_focus_anim_duration);
        mPipBounds = mDefaultPipBounds;

        mActivityManager = ActivityManagerNative.getDefault();
        SystemServicesProxy.getInstance(context).registerTaskStackListener(mTaskStackListener);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
        mContext.registerReceiver(mBroadcastReceiver, intentFilter);
        mOnboardingShown = Prefs.getBoolean(
                mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, false);

        mPipRecentsOverlayManager = new PipRecentsOverlayManager(context);
        mMediaSessionManager =
                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
        // Reset the PIP bounds and apply. PIP bounds can be changed by two reasons.
        //   1. Configuration changed due to the language change (RTL <-> RTL)
        //   2. SystemUI restarts after the crash
        mPipBounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds;
        resizePinnedStack(getPinnedStackInfo() == null ? STATE_NO_PIP : STATE_PIP_OVERLAY);
    }

    /**
     * Updates the PIP per configuration changed.
     */
    void onConfigurationChanged() {
        loadConfigurationsAndApply();
        mPipRecentsOverlayManager.onConfigurationChanged(mContext);
    }

@@ -443,6 +453,16 @@ public class PipManager {
        return mState != STATE_NO_PIP;
    }

    private StackInfo getPinnedStackInfo() {
        StackInfo stackInfo = null;
        try {
            stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
        } catch (RemoteException e) {
            Log.e(TAG, "getStackInfo failed", e);
        }
        return stackInfo;
    }

    private void handleMediaResourceGranted(String[] packageNames) {
        if (mState == STATE_NO_PIP) {
            mLastPackagesResourceGranted = packageNames;
@@ -525,7 +545,18 @@ public class PipManager {
        return PLAYBACK_STATE_UNAVAILABLE;
    }

    private static boolean isSettingsShown(ComponentName topActivity) {
    private boolean isSettingsShown() {
        List<RunningTaskInfo> runningTasks;
        try {
            runningTasks = mActivityManager.getTasks(1, 0);
            if (runningTasks == null || runningTasks.size() == 0) {
                return false;
            }
        } catch (RemoteException e) {
            Log.d(TAG, "Failed to detect top activity", e);
            return false;
        }
        ComponentName topActivity = runningTasks.get(0).topActivity;
        for (Pair<String, String> componentName : sSettingsPackageAndClassNamePairList) {
            String packageName = componentName.first;
            if (topActivity.getPackageName().equals(packageName)) {
@@ -544,18 +575,12 @@ public class PipManager {
            if (mState != STATE_NO_PIP) {
                boolean hasPip = false;

                StackInfo stackInfo = null;
                try {
                    stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
                    if (stackInfo == null) {
                        Log.w(TAG, "There is no pinned stack");
                StackInfo stackInfo = getPinnedStackInfo();
                if (stackInfo == null || stackInfo.taskIds == null) {
                    Log.w(TAG, "There is nothing in pinned stack");
                    closePipInternal(false);
                    return;
                }
                } catch (RemoteException e) {
                    Log.e(TAG, "getStackInfo failed", e);
                    return;
                }
                for (int i = stackInfo.taskIds.length - 1; i >= 0; --i) {
                    if (stackInfo.taskIds[i] == mPipTaskId) {
                        // PIP task is still alive.
@@ -570,38 +595,22 @@ public class PipManager {
                }
            }
            if (mState == STATE_PIP_OVERLAY) {
                try {
                    List<RunningTaskInfo> runningTasks = mActivityManager.getTasks(1, 0);
                    if (runningTasks == null || runningTasks.size() == 0) {
                        return;
                    }
                    RunningTaskInfo topTask = runningTasks.get(0);
                    Rect bounds = isSettingsShown(topTask.topActivity)
                          ? mSettingsPipBounds : mDefaultPipBounds;
                Rect bounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds;
                if (mPipBounds != bounds) {
                    mPipBounds = bounds;
                    resizePinnedStack(STATE_PIP_OVERLAY);
                }
                } catch (RemoteException e) {
                    Log.d(TAG, "Failed to detect top activity", e);
                }
            }
        }

        @Override
        public void onActivityPinned() {
            if (DEBUG) Log.d(TAG, "onActivityPinned()");
            StackInfo stackInfo = null;
            try {
                stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
            StackInfo stackInfo = getPinnedStackInfo();
            if (stackInfo == null) {
                Log.w(TAG, "Cannot find pinned stack");
                return;
            }
            } catch (RemoteException e) {
                Log.e(TAG, "getStackInfo failed", e);
                return;
            }
            if (DEBUG) Log.d(TAG, "PINNED_STACK:" + stackInfo);
            mPipTaskId = stackInfo.taskIds[stackInfo.taskIds.length - 1];
            mPipComponentName = ComponentName.unflattenFromString(