OSDN Git Service

Reimplement CM Settings Overview Panel in the new Launcher Part 2
authorYvonne Wong <ywong@cyngn.com>
Mon, 30 Nov 2015 23:32:16 +0000 (15:32 -0800)
committerYvonne Wong <ywong@cyngn.com>
Mon, 30 Nov 2015 23:32:16 +0000 (15:32 -0800)
- Enables hiding the search bar from the workspace
- Adds a reload launcher flag to reload only during onResume or newIntent if needed

Change-Id: I0feb279f9003b268b5a129cef40d3665787ee451

src/com/android/launcher3/DeviceProfile.java
src/com/android/launcher3/IconCache.java
src/com/android/launcher3/Launcher.java
src/com/android/launcher3/OverviewSettingsPanel.java
src/com/android/launcher3/SearchDropTargetBar.java
src/com/android/launcher3/Utilities.java
src/com/android/launcher3/allapps/AllAppsContainerView.java
src/com/android/launcher3/list/SettingsPinnedHeaderAdapter.java

index 84b6835..7e9d8f8 100644 (file)
@@ -32,6 +32,7 @@ import android.view.ViewGroup.LayoutParams;
 import android.view.ViewGroup.MarginLayoutParams;
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
+import com.android.launcher3.settings.SettingsProvider;
 
 import com.android.launcher3.allapps.AllAppsContainerView;
 
@@ -96,6 +97,7 @@ public class DeviceProfile {
     public final int allAppsIconTextSizePx;
 
     // QSB
+    public boolean searchBarVisible;
     private int searchBarSpaceWidthPx;
     private int searchBarSpaceHeightPx;
 
@@ -160,6 +162,12 @@ public class DeviceProfile {
         // Calculate the remaining vars
         updateAvailableDimensions(dm, res);
         computeAllAppsButtonSize(context);
+
+        // Search Bar
+        searchBarVisible = isSearchBarEnabled(context);
+        searchBarSpaceWidthPx = Math.min(searchBarSpaceWidthPx, widthPx);
+        searchBarSpaceHeightPx = 2 * edgeMarginPx + (searchBarVisible ?
+                searchBarSpaceHeightPx - getSearchBarTopOffset() : 3 * edgeMarginPx);
     }
 
     /**
@@ -245,9 +253,9 @@ public class DeviceProfile {
     /** Returns the search bar top offset */
     private int getSearchBarTopOffset() {
         if (isTablet && !isVerticalBarLayout()) {
-            return 4 * edgeMarginPx;
+            return searchBarVisible ? 4 * edgeMarginPx : 0;
         } else {
-            return 2 * edgeMarginPx;
+            return searchBarVisible ? 2 * edgeMarginPx : 0;
         }
     }
 
@@ -273,12 +281,13 @@ public class DeviceProfile {
                         (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
                 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
                         availableWidthPx - (edgeMarginPx + gap),
-                        searchBarSpaceHeightPx);
+                        searchBarVisible ? searchBarSpaceHeightPx : edgeMarginPx);
             } else {
                 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
                         getSearchBarTopOffset(),
                         availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
-                        defaultWidgetPadding.right), searchBarSpaceHeightPx);
+                        defaultWidgetPadding.right), searchBarVisible ? searchBarSpaceHeightPx :
+                                edgeMarginPx);
             }
         }
         return bounds;
@@ -382,15 +391,18 @@ public class DeviceProfile {
         return visibleChildren;
     }
 
-    public void layout(Launcher launcher) {
-        FrameLayout.LayoutParams lp;
-        boolean hasVerticalBarLayout = isVerticalBarLayout();
-        final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
+    public void layoutSearchBar(Launcher launcher, boolean hasVerticalBarLayout) {
+        // Update search bar for live settings
+        searchBarVisible = isSearchBarEnabled(launcher);
 
         // Layout the search bar space
         View searchBar = launcher.getSearchDropTargetBar();
-        lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
+        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
         if (hasVerticalBarLayout) {
+            // If search bar is invisible add some extra padding for the drop targets
+            searchBarSpaceHeightPx = searchBarVisible ? searchBarSpaceHeightPx
+                    : searchBarSpaceHeightPx + 5 * edgeMarginPx;
+
             // Vertical search bar space -- The search bar is fixed in the layout to be on the left
             //                              of the screen regardless of RTL
             lp.gravity = Gravity.LEFT;
@@ -398,7 +410,8 @@ public class DeviceProfile {
 
             LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
             targets.setOrientation(LinearLayout.VERTICAL);
-            FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
+            FrameLayout.LayoutParams targetsLp =
+                    (FrameLayout.LayoutParams) targets.getLayoutParams();
             targetsLp.gravity = Gravity.TOP;
             targetsLp.height = LayoutParams.WRAP_CONTENT;
 
@@ -412,6 +425,23 @@ public class DeviceProfile {
         }
         searchBar.setLayoutParams(lp);
 
+        View qsbBar = launcher.getOrCreateQsbBar();
+        if (qsbBar != null) {
+            qsbBar.setVisibility(searchBarVisible ? View.VISIBLE : View.GONE);
+            LayoutParams vglp = qsbBar.getLayoutParams();
+            vglp.width = LayoutParams.MATCH_PARENT;
+            vglp.height = LayoutParams.MATCH_PARENT;
+            qsbBar.setLayoutParams(vglp);
+        }
+    }
+
+    public void layout(Launcher launcher) {
+        FrameLayout.LayoutParams lp;
+        boolean hasVerticalBarLayout = isVerticalBarLayout();
+        final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
+
+        layoutSearchBar(launcher, hasVerticalBarLayout);
+
         // Layout the workspace
         PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
         lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
@@ -519,4 +549,24 @@ public class DeviceProfile {
                 ? Math.min(widthPx, heightPx)
                 : Math.max(widthPx, heightPx);
     }
+
+    private boolean isSearchBarEnabled(Context context) {
+        boolean searchActivityExists = Utilities.searchActivityExists(context);
+
+        boolean isSearchEnabled = SettingsProvider.getBoolean(context,
+                SettingsProvider.SETTINGS_UI_HOMESCREEN_SEARCH,
+                R.bool.preferences_interface_homescreen_search_default);
+
+        if (searchActivityExists) {
+            return isSearchEnabled;
+        } else {
+            if (isSearchEnabled) {
+                // Disable search bar
+                SettingsProvider.putBoolean(context,
+                        SettingsProvider.SETTINGS_UI_HOMESCREEN_SEARCH, false);
+            }
+
+            return false;
+        }
+    }
 }
index 59ab839..c5eeba6 100644 (file)
@@ -52,8 +52,10 @@ import com.android.launcher3.util.Thunk;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Set;
 import java.util.Stack;
 
@@ -198,6 +200,20 @@ public class IconCache {
     }
 
     /**
+     * Empty out the cache that aren't of the correct grid size
+     */
+    public synchronized void flushInvalidIcons(DeviceProfile deviceProfile) {
+        Iterator<Map.Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
+        while (it.hasNext()) {
+            final CacheEntry e = it.next().getValue();
+            if ((e.icon != null) && (e.icon.getWidth() < deviceProfile.iconSizePx
+                    || e.icon.getHeight() < deviceProfile.iconSizePx)) {
+                it.remove();
+            }
+        }
+    }
+
+    /**
      * Remove any records for the supplied package name from memory.
      */
     private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
index 759ca04..454d776 100644 (file)
@@ -358,6 +358,8 @@ public class Launcher extends Activity
     // the press state and keep this reference to reset the press state when we return to launcher.
     private BubbleTextView mWaitingForResume;
 
+    private boolean mReloadLauncher;
+
     // Preferences
     private boolean mHideIconLabels;
 
@@ -1090,6 +1092,8 @@ public class Launcher extends Activity
         if (mLauncherCallbacks != null) {
             mLauncherCallbacks.onResume();
         }
+
+        reloadLauncherIfNeeded();
     }
 
     @Override
@@ -1695,6 +1699,10 @@ public class Launcher extends Activity
         }
     };
 
+    /**
+     * Initializes the device profile based off of the launcher app state and screen orientation
+     * @param app The launcher app state
+     */
     public void initializeDeviceProfile(LauncherAppState app) {
         // Load configuration-specific DeviceProfile
         mDeviceProfile = getResources().getConfiguration().orientation
@@ -1702,16 +1710,45 @@ public class Launcher extends Activity
                 app.getInvariantDeviceProfile().landscapeProfile
                 : app.getInvariantDeviceProfile().portraitProfile;
 
-        mModel = app.setLauncher(this);
-        mIconCache = app.getIconCache();
-
         mHideIconLabels = SettingsProvider.getBoolean(this,
                 SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
                 R.bool.preferences_interface_homescreen_hide_icon_labels_default);
+
+        mModel = app.setLauncher(this);
+        mIconCache = app.getIconCache();
+        mIconCache.flushInvalidIcons(mDeviceProfile);
+    }
+
+    /**
+     * Sets the reload launcher flag to true, which will reload the launcher at the next appropriate
+     * time.
+     */
+    public void setReloadLauncher() {
+        mReloadLauncher = true;
     }
 
-    public void reloadLauncher()
+    /**
+     * If the reload launcher flag is set to true, the launcher will be reloaded.
+     * @return Whether the launcher was actually reloaded.
+     */
+    public boolean reloadLauncherIfNeeded() {
+        if (mReloadLauncher) {
+            reloadLauncher(mWorkspace.getCurrentPage());
+            mReloadLauncher = false;
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Reloads the launcher by re-initializing the device profile and layout
+     * @param page
+     */
+    public void reloadLauncher(int page)
     {
+        mSearchDropTargetBar.setupQsb(this);
+
         // Re-initialize device profile
         LauncherAppState app = LauncherAppState.getInstance();
         app.initInvariantDeviceProfile();
@@ -1721,7 +1758,7 @@ public class Launcher extends Activity
 
         // Reload
         mModel.resetLoadedState(true, true);
-        mModel.startLoader(mWorkspace.getRestorePage(), LauncherModel.LOADER_FLAG_NONE);
+        mModel.startLoader(page, LauncherModel.LOADER_FLAG_NONE);
         mWorkspace.updateCustomContentVisibility();
 
         mAppsView.reset();
@@ -1959,6 +1996,8 @@ public class Launcher extends Activity
         }
         super.onNewIntent(intent);
 
+        reloadLauncherIfNeeded();
+
         // Close the menu
         Folder openFolder = mWorkspace.getOpenFolder();
         boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
index b4ba06a..f608b26 100644 (file)
@@ -42,8 +42,9 @@ public class OverviewSettingsPanel {
                 res.getString(R.string.icon_labels)};
 
         String[] valuesApp = new String[] {
-                res.getString(R.string.larger_icons_text),
-                res.getString(R.string.protected_app_settings)};
+                res.getString(R.string.larger_icons_text)/*,
+                TODO: Uncomment
+                res.getString(R.string.protected_app_settings)*/};
 
         mSettingsAdapter = new SettingsPinnedHeaderAdapter(mLauncher);
         mSettingsAdapter.setHeaders(headers);
index 772a334..182d741 100644 (file)
@@ -64,6 +64,7 @@ public class SearchDropTargetBar extends FrameLayout implements DragController.D
     private static final AccelerateInterpolator sAccelerateInterpolator =
             new AccelerateInterpolator();
 
+    private Launcher mLauncher;
     private State mState = State.SEARCH_BAR;
     @Thunk View mQSB;
     @Thunk View mDropTargetBar;
@@ -84,6 +85,8 @@ public class SearchDropTargetBar extends FrameLayout implements DragController.D
     }
 
     public void setup(Launcher launcher, DragController dragController) {
+        mLauncher = launcher;
+
         dragController.addDragListener(this);
         dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
 
@@ -134,24 +137,39 @@ public class SearchDropTargetBar extends FrameLayout implements DragController.D
         });
     }
 
+    public void setupQsb(Launcher launcher) {
+        mLauncher = launcher;
+        mQSB = launcher.getOrCreateQsbBar();
+    }
+
     public void setQsbSearchBar(View qsb) {
+        float alpha = 1f;
+        int visibility = View.VISIBLE;
+        if (mQSB != null) {
+            alpha = mQSB.getAlpha();
+            visibility = mQSB.getVisibility();
+        }
+
         mQSB = qsb;
         if (mQSB != null) {
-            // Update the search ber animation
+            mQSB.setAlpha(alpha);
+            mQSB.setVisibility(visibility);
+
+            // Update the search bar animation
             mQSBSearchBarAnimator = new LauncherViewPropertyAnimator(mQSB);
             mQSBSearchBarAnimator.setInterpolator(sAccelerateInterpolator);
             mQSBSearchBarAnimator.addListener(new AnimatorListenerAdapter() {
                 @Override
                 public void onAnimationStart(Animator animation) {
                     // Ensure that the view is visible for the animation
-                    if (mQSB != null) {
+                    if (mQSB != null && isSearchBarVisible()) {
                         mQSB.setVisibility(View.VISIBLE);
                     }
                 }
 
                 @Override
                 public void onAnimationEnd(Animator animation) {
-                    if (mQSB != null) {
+                    if (mQSB != null && isSearchBarVisible()) {
                         AlphaUpdateListener.updateVisibility(mQSB, mAccessibilityEnabled);
                     }
                 }
@@ -173,9 +191,10 @@ public class SearchDropTargetBar extends FrameLayout implements DragController.D
             AccessibilityManager am = (AccessibilityManager)
                     getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
             mAccessibilityEnabled = am.isEnabled();
-
-            animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
-                    duration);
+            if (mLauncher.getDeviceProfile().searchBarVisible) {
+                animateViewAlpha(mQSBSearchBarAnimator, mQSB, newState.getSearchBarAlpha(),
+                        duration);
+            }
             animateViewAlpha(mDropTargetBarAnimator, mDropTargetBar, newState.getDropTargetBarAlpha(),
                     duration);
         }
@@ -245,8 +264,15 @@ public class SearchDropTargetBar extends FrameLayout implements DragController.D
         }
     }
 
+    public boolean isSearchBarVisible() {
+        if (mLauncher != null) {
+            return mLauncher.getDeviceProfile().searchBarVisible;
+        }
+        return true;
+    }
+
     public void enableAccessibleDrag(boolean enable) {
-        if (mQSB != null) {
+        if (mQSB != null && isSearchBarVisible()) {
             mQSB.setVisibility(enable ? View.GONE : View.VISIBLE);
         }
         mInfoDropTarget.enableAccessibleDrag(enable);
index adedd33..9e55875 100644 (file)
@@ -710,4 +710,12 @@ public final class Utilities {
     public static String createDbSelectionQuery(String columnName, Iterable<?> values) {
         return String.format(Locale.ENGLISH, "%s IN (%s)", columnName, TextUtils.join(", ", values));
     }
+
+    public static boolean searchActivityExists(Context context) {
+        final SearchManager searchManager =
+                (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
+        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
+
+        return globalSearchActivity != null;
+    }
 }
index 76f47c5..0a94be7 100644 (file)
@@ -229,6 +229,9 @@ public class AllAppsContainerView extends BaseContainerView implements DragSourc
         updateScrubber();
     }
 
+    /**
+     * Resets the existing apps in the list
+     */
     public void reset() {
         List<AppInfo> apps = mApps.getApps();
         updateApps(apps);
index aa2f451..1dcc91d 100644 (file)
@@ -15,9 +15,11 @@ import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.TextView;
+import android.widget.Toast;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.OverviewSettingsPanel;
 import com.android.launcher3.R;
+import com.android.launcher3.Utilities;
 import com.android.launcher3.settings.SettingsProvider;
 
 public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
@@ -87,18 +89,20 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
         Resources res = mLauncher.getResources();
 
 
-        boolean current = false;
-        String state = "";
+        boolean current;
+        String state;
 
         switch (partition) {
             case OverviewSettingsPanel.HOME_SETTINGS_POSITION:
                 switch (position) {
-                    /*case 0:
-                        current = mLauncher.isSearchBarEnabled();
+                    case 0:
+                        current = SettingsProvider.getBoolean(mContext,
+                                SettingsProvider.SETTINGS_UI_HOMESCREEN_SEARCH,
+                                R.bool.preferences_interface_homescreen_search_default);
                         state = current ? res.getString(R.string.setting_state_on)
                                 : res.getString(R.string.setting_state_off);
                         ((TextView) v.findViewById(R.id.item_state)).setText(state);
-                        break;*/
+                        break;
                     case 1:
                         current = SettingsProvider.getBoolean(mContext,
                                 SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
@@ -206,21 +210,21 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
             switch (partition) {
                 case OverviewSettingsPanel.HOME_SETTINGS_POSITION:
                     switch (position) {
-                        /*case 0:
+                        case 0:
                             updateSearchBarVisibility(v);
-                            mLauncher.setUpdateDynamicGrid(false);
-                            break;*/
+                            mLauncher.setReloadLauncher();
+                            break;
                         case 1:
                             onIconLabelsBooleanChanged(v,
                                     SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
                                     R.bool.preferences_interface_homescreen_hide_icon_labels_default);
-                            mLauncher.reloadLauncher();
+                            mLauncher.setReloadLauncher();
                             break;
                         case 2:
                             onSettingsBooleanChanged(v,
                                     SettingsProvider.SETTINGS_UI_HOMESCREEN_SCROLLING_WALLPAPER_SCROLL,
                                     R.bool.preferences_interface_homescreen_scrolling_wallpaper_scroll_default);
-                            mLauncher.reloadLauncher();
+                            mLauncher.setReloadLauncher();
                             break;
                         /*case 3:
                             mLauncher.onClickDynamicGridSizeButton();
@@ -233,7 +237,7 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
                             onIconLabelsBooleanChanged(v,
                                     SettingsProvider.SETTINGS_UI_DRAWER_HIDE_ICON_LABELS,
                                     R.bool.preferences_interface_drawer_hide_icon_labels_default);
-                            mLauncher.reloadLauncher();
+                            mLauncher.setReloadLauncher();
                             break;
                     }
                     break;
@@ -243,7 +247,7 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
                             onSettingsBooleanChanged(v,
                                     SettingsProvider.SETTINGS_UI_GENERAL_ICONS_LARGE,
                                     R.bool.preferences_interface_general_icons_large_default);
-                            mLauncher.reloadLauncher();
+                            mLauncher.setReloadLauncher();
                             break;
                         /*case 1:
                             Intent intent = new Intent();
@@ -259,7 +263,7 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
         }
     };
 
-    /*private void updateSearchBarVisibility(View v) {
+    private void updateSearchBarVisibility(View v) {
         boolean isSearchEnabled = SettingsProvider.getBoolean(mContext,
                 SettingsProvider.SETTINGS_UI_HOMESCREEN_SEARCH,
                 R.bool.preferences_interface_homescreen_search_default);
@@ -275,7 +279,7 @@ public class SettingsPinnedHeaderAdapter extends PinnedHeaderListAdapter {
         onSettingsBooleanChanged(v,
                 SettingsProvider.SETTINGS_UI_HOMESCREEN_SEARCH,
                 R.bool.preferences_interface_homescreen_search_default);
-    }*/
+    }
 
     private void onSettingsBooleanChanged(View v, String key, int res) {
         boolean current = SettingsProvider.getBoolean(