Merge "Revert "Revert "Implements the "Save App Pair" button in Overvie..."" into 24D1-dev
diff --git a/src/com/android/launcher3/apppairs/AppPairIcon.java b/src/com/android/launcher3/apppairs/AppPairIcon.java
index 48d0fbd..a3800f7 100644
--- a/src/com/android/launcher3/apppairs/AppPairIcon.java
+++ b/src/com/android/launcher3/apppairs/AppPairIcon.java
@@ -103,7 +103,7 @@
 
         // Set up icon drawable area
         icon.mIconGraphic = icon.findViewById(R.id.app_pair_icon_graphic);
-        icon.mIconGraphic.init(activity.getDeviceProfile(), icon);
+        icon.mIconGraphic.init(activity, icon);
 
         // Set up app pair title
         icon.mAppPairName = icon.findViewById(R.id.app_pair_icon_name);
diff --git a/src/com/android/launcher3/apppairs/AppPairIconGraphic.kt b/src/com/android/launcher3/apppairs/AppPairIconGraphic.kt
index 365edf8..a4ac4c8 100644
--- a/src/com/android/launcher3/apppairs/AppPairIconGraphic.kt
+++ b/src/com/android/launcher3/apppairs/AppPairIconGraphic.kt
@@ -25,17 +25,19 @@
 import android.view.Gravity
 import android.widget.FrameLayout
 import com.android.launcher3.DeviceProfile
+import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener
 import com.android.launcher3.icons.BitmapInfo
 import com.android.launcher3.icons.PlaceHolderIconDrawable
 import com.android.launcher3.model.data.WorkspaceItemInfo
 import com.android.launcher3.util.Themes
+import com.android.launcher3.views.ActivityContext
 
 /**
  * A FrameLayout marking the area on an [AppPairIcon] where the visual icon will be drawn. One of
  * two child UI elements on an [AppPairIcon], along with a BubbleTextView holding the text title.
  */
 class AppPairIconGraphic @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
-    FrameLayout(context, attrs) {
+    FrameLayout(context, attrs), OnDeviceProfileChangeListener {
     private val TAG = "AppPairIconGraphic"
 
     companion object {
@@ -69,14 +71,17 @@
     // The app pairs icon appears differently in portrait and landscape.
     var isLeftRightSplit = false
 
+    private lateinit var activityContext: ActivityContext
     private lateinit var parentIcon: AppPairIcon
     private lateinit var appPairBackground: Drawable
     private var appIcon1: Drawable? = null
     private var appIcon2: Drawable? = null
 
-    fun init(grid: DeviceProfile, icon: AppPairIcon) {
+    fun init(activity: ActivityContext, icon: AppPairIcon) {
+        activityContext = activity
+
         // Calculate device-specific measurements
-        val defaultIconSize = grid.iconSizePx
+        val defaultIconSize = activity.deviceProfile.iconSizePx
         outerPadding = OUTER_PADDING_SCALE * defaultIconSize
         innerPadding = INNER_PADDING_SCALE * defaultIconSize
         backgroundSize = defaultIconSize - outerPadding * 2
@@ -84,8 +89,8 @@
         centerChannelSize = CENTER_CHANNEL_SCALE * defaultIconSize
         bigRadius = BIG_RADIUS_SCALE * defaultIconSize
         smallRadius = SMALL_RADIUS_SCALE * defaultIconSize
-        isLeftRightSplit = grid.isLeftRightSplit
         parentIcon = icon
+        updateOrientation()
 
         appPairBackground = AppPairIconBackground(context, this)
         appPairBackground.setBounds(0, 0, backgroundSize.toInt(), backgroundSize.toInt())
@@ -100,6 +105,28 @@
         layoutParams = lp
     }
 
+    override fun onAttachedToWindow() {
+        super.onAttachedToWindow()
+        activityContext.addOnDeviceProfileChangeListener(this)
+    }
+
+    override fun onDetachedFromWindow() {
+        super.onDetachedFromWindow()
+        activityContext.removeOnDeviceProfileChangeListener(this)
+    }
+
+    /** Checks the device orientation and updates isLeftRightSplit accordingly. */
+    private fun updateOrientation() {
+        val activity: ActivityContext = ActivityContext.lookupContext(context)
+        isLeftRightSplit = activity.deviceProfile.isLeftRightSplit
+    }
+
+    /** When device profile changes, update orientation */
+    override fun onDeviceProfileChanged(dp: DeviceProfile?) {
+        updateOrientation()
+        invalidate()
+    }
+
     /** Sets up app pair member icons for drawing. */
     private fun applyIcons(contents: ArrayList<WorkspaceItemInfo>) {
         // App pair should always contain 2 members; if not 2, return to avoid a crash loop