From 593616d2d94cf2fa937a0d3a89c9e6db661b2b60 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 8 Sep 2019 11:11:02 -0700 Subject: [PATCH] Fix layout transitions on foldable devices --- app/src/main/java/com/limelight/AppView.java | 25 ++++++++++- app/src/main/java/com/limelight/PcView.java | 7 +-- .../com/limelight/grid/AppGridAdapter.java | 43 ++++++++++++++++--- .../limelight/grid/GenericGridAdapter.java | 18 ++++++-- .../com/limelight/grid/PcGridAdapter.java | 22 +++++++++- 5 files changed, 98 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/limelight/AppView.java b/app/src/main/java/com/limelight/AppView.java index 9c4a8564..284a4a62 100644 --- a/app/src/main/java/com/limelight/AppView.java +++ b/app/src/main/java/com/limelight/AppView.java @@ -26,6 +26,7 @@ import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Build; @@ -96,8 +97,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { try { appGridAdapter = new AppGridAdapter(AppView.this, - PreferenceConfiguration.readPreferences(AppView.this).listMode, - PreferenceConfiguration.readPreferences(AppView.this).smallIconMode, + PreferenceConfiguration.readPreferences(AppView.this), computer, localBinder.getUniqueId()); } catch (Exception e) { e.printStackTrace(); @@ -147,6 +147,27 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { } }; + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + + // If appGridAdapter is initialized, let it know about the configuration change. + // If not, it will pick it up when it initializes. + if (appGridAdapter != null) { + // Update the app grid adapter to create grid items with the correct layout + appGridAdapter.updateLayoutWithPreferences(this, PreferenceConfiguration.readPreferences(this)); + + try { + // Reinflate the app grid itself to pick up the layout change + getFragmentManager().beginTransaction() + .replace(R.id.appFragmentContainer, new AdapterFragment()) + .commitAllowingStateLoss(); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } + } + private void startComputerUpdates() { // Don't start polling if we're not bound or in the foreground if (managerBinder == null || !inForeground) { diff --git a/app/src/main/java/com/limelight/PcView.java b/app/src/main/java/com/limelight/PcView.java index c532f879..f5e707e6 100644 --- a/app/src/main/java/com/limelight/PcView.java +++ b/app/src/main/java/com/limelight/PcView.java @@ -126,6 +126,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { // Set default preferences if we've never been run PreferenceManager.setDefaultValues(this, R.xml.preferences, false); + // Set the correct layout for the PC grid + pcGridAdapter.updateLayoutWithPreferences(this, PreferenceConfiguration.readPreferences(this)); + // Setup the list view ImageButton settingsButton = findViewById(R.id.settingsButton); ImageButton addComputerButton = findViewById(R.id.manuallyAddPc); @@ -223,9 +226,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { bindService(new Intent(PcView.this, ComputerManagerService.class), serviceConnection, Service.BIND_AUTO_CREATE); - pcGridAdapter = new PcGridAdapter(this, - PreferenceConfiguration.readPreferences(this).listMode, - PreferenceConfiguration.readPreferences(this).smallIconMode); + pcGridAdapter = new PcGridAdapter(this, PreferenceConfiguration.readPreferences(this)); initializeViews(); } diff --git a/app/src/main/java/com/limelight/grid/AppGridAdapter.java b/app/src/main/java/com/limelight/grid/AppGridAdapter.java index 8add8ebd..5863a192 100644 --- a/app/src/main/java/com/limelight/grid/AppGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/AppGridAdapter.java @@ -1,6 +1,6 @@ package com.limelight.grid; -import android.app.Activity; +import android.content.Context; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; @@ -13,6 +13,7 @@ import com.limelight.grid.assets.DiskAssetLoader; import com.limelight.grid.assets.MemoryAssetLoader; import com.limelight.grid.assets.NetworkAssetLoader; import com.limelight.nvstream.http.ComputerDetails; +import com.limelight.preferences.PreferenceConfiguration; import java.util.Collections; import java.util.Comparator; @@ -23,15 +24,37 @@ public class AppGridAdapter extends GenericGridAdapter { private static final int SMALL_WIDTH_DP = 100; private static final int LARGE_WIDTH_DP = 150; - private final CachedAppAssetLoader loader; + private final ComputerDetails computer; + private final String uniqueId; - public AppGridAdapter(Activity activity, boolean listMode, boolean small, ComputerDetails computer, String uniqueId) { - super(activity, listMode ? R.layout.simple_row : (small ? R.layout.app_grid_item_small : R.layout.app_grid_item)); + private CachedAppAssetLoader loader; - int dpi = activity.getResources().getDisplayMetrics().densityDpi; + public AppGridAdapter(Context context, PreferenceConfiguration prefs, ComputerDetails computer, String uniqueId) { + super(context, getLayoutIdForPreferences(prefs)); + + this.computer = computer; + this.uniqueId = uniqueId; + + updateLayoutWithPreferences(context, prefs); + } + + private static int getLayoutIdForPreferences(PreferenceConfiguration prefs) { + if (prefs.listMode) { + return R.layout.simple_row; + } + else if (prefs.smallIconMode) { + return R.layout.app_grid_item_small; + } + else { + return R.layout.app_grid_item; + } + } + + public void updateLayoutWithPreferences(Context context, PreferenceConfiguration prefs) { + int dpi = context.getResources().getDisplayMetrics().densityDpi; int dp; - if (small) { + if (prefs.smallIconMode) { dp = SMALL_WIDTH_DP; } else { @@ -45,10 +68,18 @@ public class AppGridAdapter extends GenericGridAdapter { } LimeLog.info("Art scaling divisor: " + scalingDivisor); + if (loader != null) { + // Cancel operations on the old loader + cancelQueuedOperations(); + } + this.loader = new CachedAppAssetLoader(computer, scalingDivisor, new NetworkAssetLoader(context, uniqueId), new MemoryAssetLoader(), new DiskAssetLoader(context)); + + // This will trigger the view to reload with the new layout + setLayoutId(getLayoutIdForPreferences(prefs)); } public void cancelQueuedOperations() { diff --git a/app/src/main/java/com/limelight/grid/GenericGridAdapter.java b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java index 981a3883..fa5c1ff6 100644 --- a/app/src/main/java/com/limelight/grid/GenericGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java @@ -10,22 +10,32 @@ import android.widget.ProgressBar; import android.widget.TextView; import com.limelight.R; +import com.limelight.preferences.PreferenceConfiguration; import java.util.ArrayList; public abstract class GenericGridAdapter extends BaseAdapter { protected final Context context; - protected final int layoutId; - protected final ArrayList itemList = new ArrayList<>(); - protected final LayoutInflater inflater; + private int layoutId; + final ArrayList itemList = new ArrayList<>(); + private final LayoutInflater inflater; - public GenericGridAdapter(Context context, int layoutId) { + GenericGridAdapter(Context context, int layoutId) { this.context = context; this.layoutId = layoutId; this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } + void setLayoutId(int layoutId) { + if (layoutId != this.layoutId) { + this.layoutId = layoutId; + + // Force the view to be redrawn with the new layout + notifyDataSetInvalidated(); + } + } + public void clear() { itemList.clear(); } diff --git a/app/src/main/java/com/limelight/grid/PcGridAdapter.java b/app/src/main/java/com/limelight/grid/PcGridAdapter.java index 2d5ad728..04e63239 100644 --- a/app/src/main/java/com/limelight/grid/PcGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/PcGridAdapter.java @@ -10,14 +10,32 @@ import com.limelight.PcView; import com.limelight.R; import com.limelight.nvstream.http.ComputerDetails; import com.limelight.nvstream.http.PairingManager; +import com.limelight.preferences.PreferenceConfiguration; import java.util.Collections; import java.util.Comparator; public class PcGridAdapter extends GenericGridAdapter { - public PcGridAdapter(Context context, boolean listMode, boolean small) { - super(context, listMode ? R.layout.simple_row : (small ? R.layout.pc_grid_item_small : R.layout.pc_grid_item)); + public PcGridAdapter(Context context, PreferenceConfiguration prefs) { + super(context, getLayoutIdForPreferences(prefs)); + } + + private static int getLayoutIdForPreferences(PreferenceConfiguration prefs) { + if (prefs.listMode) { + return R.layout.simple_row; + } + else if (prefs.smallIconMode) { + return R.layout.pc_grid_item_small; + } + else { + return R.layout.pc_grid_item; + } + } + + public void updateLayoutWithPreferences(Context context, PreferenceConfiguration prefs) { + // This will trigger the view to reload with the new layout + setLayoutId(getLayoutIdForPreferences(prefs)); } public void addComputer(PcView.ComputerObject computer) {