diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2019-05-22 23:35:03 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-05-22 23:35:03 +0000 |
commit | a338daa26114554c4bdc3b27d12c5bd62f3db3c2 (patch) | |
tree | 0249c176ceae0a20fe09e0eca650bf079085e029 /src | |
parent | a09cfe23403ae7002961dd2cd02f49b338696a4d (diff) | |
parent | 5686333117ff6e2e4add6b2f32edcf047f241466 (diff) | |
download | android_packages_apps_Trebuchet-a338daa26114554c4bdc3b27d12c5bd62f3db3c2.tar.gz android_packages_apps_Trebuchet-a338daa26114554c4bdc3b27d12c5bd62f3db3c2.tar.bz2 android_packages_apps_Trebuchet-a338daa26114554c4bdc3b27d12c5bd62f3db3c2.zip |
Merge "Adding a utility class to cache views at an activity level" into ub-launcher3-qt-dev
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/launcher3/BaseActivity.java | 7 | ||||
-rw-r--r-- | src/com/android/launcher3/dragndrop/DragView.java | 5 | ||||
-rw-r--r-- | src/com/android/launcher3/folder/FolderIcon.java | 1 | ||||
-rw-r--r-- | src/com/android/launcher3/util/ViewCache.java | 73 | ||||
-rw-r--r-- | src/com/android/launcher3/views/FloatingIconView.java | 44 |
5 files changed, 112 insertions, 18 deletions
diff --git a/src/com/android/launcher3/BaseActivity.java b/src/com/android/launcher3/BaseActivity.java index 7f7224241..424ffde1f 100644 --- a/src/com/android/launcher3/BaseActivity.java +++ b/src/com/android/launcher3/BaseActivity.java @@ -36,6 +36,7 @@ import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate; import com.android.launcher3.uioverrides.UiFactory; import com.android.launcher3.userevent.nano.LauncherLogProto; import com.android.launcher3.util.SystemUiController; +import com.android.launcher3.util.ViewCache; import com.android.launcher3.views.ActivityContext; import java.io.FileDescriptor; @@ -102,6 +103,12 @@ public abstract class BaseActivity extends Activity // animation @InvisibilityFlags private int mForceInvisible; + private final ViewCache mViewCache = new ViewCache(); + + public ViewCache getViewCache() { + return mViewCache; + } + @Override public DeviceProfile getDeviceProfile() { return mDeviceProfile; diff --git a/src/com/android/launcher3/dragndrop/DragView.java b/src/com/android/launcher3/dragndrop/DragView.java index 7af12c57d..9d46cf2ab 100644 --- a/src/com/android/launcher3/dragndrop/DragView.java +++ b/src/com/android/launcher3/dragndrop/DragView.java @@ -44,7 +44,6 @@ import android.view.View; import com.android.launcher3.FastBitmapDrawable; import com.android.launcher3.ItemInfo; import com.android.launcher3.Launcher; -import com.android.launcher3.LauncherModel; import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherState; import com.android.launcher3.LauncherStateManager; @@ -55,6 +54,7 @@ import com.android.launcher3.anim.Interpolators; import com.android.launcher3.icons.LauncherIcons; import com.android.launcher3.util.Themes; import com.android.launcher3.util.Thunk; +import com.android.launcher3.util.UiThreadHelper; import java.util.Arrays; @@ -210,8 +210,7 @@ public class DragView extends View implements LauncherStateManager.StateListener return; } // Load the adaptive icon on a background thread and add the view in ui thread. - final Looper workerLooper = LauncherModel.getWorkerLooper(); - new Handler(workerLooper).postAtFrontOfQueue(new Runnable() { + new Handler(UiThreadHelper.getBackgroundLooper()).postAtFrontOfQueue(new Runnable() { @Override public void run() { Object[] outObj = new Object[1]; diff --git a/src/com/android/launcher3/folder/FolderIcon.java b/src/com/android/launcher3/folder/FolderIcon.java index 0a9bc722d..250169cdb 100644 --- a/src/com/android/launcher3/folder/FolderIcon.java +++ b/src/com/android/launcher3/folder/FolderIcon.java @@ -102,7 +102,6 @@ public class FolderIcon extends FrameLayout implements FolderListener { private List<BubbleTextView> mCurrentPreviewItems = new ArrayList<>(); boolean mAnimating = false; - private Rect mTempBounds = new Rect(); private float mSlop; diff --git a/src/com/android/launcher3/util/ViewCache.java b/src/com/android/launcher3/util/ViewCache.java new file mode 100644 index 000000000..08b874416 --- /dev/null +++ b/src/com/android/launcher3/util/ViewCache.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2019 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.launcher3.util; + +import android.content.Context; +import android.util.SparseArray; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * Utility class to cache views at an activity level + */ +public class ViewCache { + + protected final SparseArray<CacheEntry> mCache = new SparseArray(); + + public void setCacheSize(int layoutId, int size) { + mCache.put(layoutId, new CacheEntry(size)); + } + + public <T extends View> T getView(int layoutId, Context context, ViewGroup parent) { + CacheEntry entry = mCache.get(layoutId); + if (entry == null) { + entry = new CacheEntry(1); + mCache.put(layoutId, entry); + } + + if (entry.mCurrentSize > 0) { + entry.mCurrentSize --; + T result = (T) entry.mViews[entry.mCurrentSize]; + entry.mViews[entry.mCurrentSize] = null; + return result; + } + + return (T) LayoutInflater.from(context).inflate(layoutId, parent, false); + } + + public void recycleView(int layoutId, View view) { + CacheEntry entry = mCache.get(layoutId); + if (entry != null && entry.mCurrentSize < entry.mMaxSize) { + entry.mViews[entry.mCurrentSize] = view; + entry.mCurrentSize++; + } + } + + private static class CacheEntry { + + final int mMaxSize; + final View[] mViews; + + int mCurrentSize; + + public CacheEntry(int maxSize) { + mMaxSize = maxSize; + mViews = new View[maxSize]; + mCurrentSize = 0; + } + } +} diff --git a/src/com/android/launcher3/views/FloatingIconView.java b/src/com/android/launcher3/views/FloatingIconView.java index 3d5877a59..f63bcddf0 100644 --- a/src/com/android/launcher3/views/FloatingIconView.java +++ b/src/com/android/launcher3/views/FloatingIconView.java @@ -27,6 +27,7 @@ import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.annotation.TargetApi; +import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Outline; @@ -40,6 +41,7 @@ import android.os.Build; import android.os.CancellationSignal; import android.os.Handler; import android.os.Looper; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewOutlineProvider; @@ -61,6 +63,7 @@ import com.android.launcher3.graphics.ShiftedBitmapDrawable; import com.android.launcher3.icons.LauncherIcons; import com.android.launcher3.popup.SystemShortcut; import com.android.launcher3.shortcuts.DeepShortcutView; +import com.android.launcher3.util.UiThreadHelper; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; @@ -148,12 +151,20 @@ public class FloatingIconView extends View implements private final SpringAnimation mFgSpringX; private float mFgTransX; - private FloatingIconView(Launcher launcher) { - super(launcher); - mLauncher = launcher; + public FloatingIconView(Context context) { + this(context, null); + } + + public FloatingIconView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public FloatingIconView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + mLauncher = Launcher.getLauncher(context); mBlurSizeOutline = getResources().getDimensionPixelSize( R.dimen.blur_size_medium_outline); - mListenerView = new ListenerView(launcher, null); + mListenerView = new ListenerView(context, attrs); mFgSpringX = new SpringAnimation(this, mFgTransXProperty) .setSpring(new SpringForce() @@ -350,6 +361,7 @@ public class FloatingIconView extends View implements } @WorkerThread + @SuppressWarnings("WrongThread") private void getIcon(View v, ItemInfo info, boolean isOpening, Runnable onIconLoadedRunnable, CancellationSignal loadIconSignal) { final LayoutParams lp = (LayoutParams) getLayoutParams(); @@ -396,7 +408,7 @@ public class FloatingIconView extends View implements && finalDrawable instanceof AdaptiveIconDrawable; int iconOffset = getOffsetForIconBounds(finalDrawable); - new Handler(Looper.getMainLooper()).post(() -> { + mLauncher.getMainExecutor().execute(() -> { if (isAdaptiveIcon) { mIsAdaptiveIcon = true; boolean isFolderIcon = finalDrawable instanceof FolderAdaptiveIcon; @@ -505,6 +517,7 @@ public class FloatingIconView extends View implements } @WorkerThread + @SuppressWarnings("WrongThread") private int getOffsetForIconBounds(Drawable drawable) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || !(drawable instanceof AdaptiveIconDrawable)) { @@ -515,7 +528,7 @@ public class FloatingIconView extends View implements Rect bounds = new Rect(0, 0, lp.width + mBlurSizeOutline, lp.height + mBlurSizeOutline); bounds.inset(mBlurSizeOutline / 2, mBlurSizeOutline / 2); - try (LauncherIcons li = LauncherIcons.obtain(getContext())) { + try (LauncherIcons li = LauncherIcons.obtain(mLauncher)) { Utilities.scaleRectAboutCenter(bounds, li.getNormalizer().getScale(drawable, null)); } @@ -604,11 +617,14 @@ public class FloatingIconView extends View implements * @param isOpening True if this view replaces the icon for app open animation. */ public static FloatingIconView getFloatingIconView(Launcher launcher, View originalView, - boolean hideOriginal, RectF positionOut, boolean isOpening, FloatingIconView recycle) { - if (recycle != null) { - recycle.recycle(); - } - FloatingIconView view = recycle != null ? recycle : new FloatingIconView(launcher); + boolean hideOriginal, RectF positionOut, boolean isOpening) { + final DragLayer dragLayer = launcher.getDragLayer(); + ViewGroup parent = (ViewGroup) dragLayer.getParent(); + + FloatingIconView view = launcher.getViewCache().getView(R.layout.floating_icon_view, + launcher, parent); + view.recycle(); + view.mIsVerticalBarLayout = launcher.getDeviceProfile().isVerticalBarLayout(); view.mOriginalIcon = originalView; @@ -626,16 +642,15 @@ public class FloatingIconView extends View implements originalView.setVisibility(INVISIBLE); }; CancellationSignal loadIconSignal = view.mLoadIconSignal; - new Handler(LauncherModel.getWorkerLooper()).postAtFrontOfQueue(() -> { + new Handler(UiThreadHelper.getBackgroundLooper()).postAtFrontOfQueue(() -> { view.getIcon(originalView, (ItemInfo) originalView.getTag(), isOpening, onIconLoaded, loadIconSignal); }); } // We need to add it to the overlay, but keep it invisible until animation starts.. - final DragLayer dragLayer = launcher.getDragLayer(); view.setVisibility(INVISIBLE); - ((ViewGroup) dragLayer.getParent()).addView(view); + parent.addView(view); dragLayer.addView(view.mListenerView); view.mListenerView.setListener(view::onListenerViewClosed); @@ -714,6 +729,7 @@ public class FloatingIconView extends View implements ((ViewGroup) dragLayer.getParent()).removeView(this); dragLayer.removeView(mListenerView); recycle(); + mLauncher.getViewCache().recycleView(R.layout.floating_icon_view, this); } private void recycle() { |