diff options
Diffstat (limited to 'src/com/android/wallpaper/picker/LivePreviewFragment.java')
-rw-r--r-- | src/com/android/wallpaper/picker/LivePreviewFragment.java | 290 |
1 files changed, 136 insertions, 154 deletions
diff --git a/src/com/android/wallpaper/picker/LivePreviewFragment.java b/src/com/android/wallpaper/picker/LivePreviewFragment.java index 8019eb8..bb64817 100644 --- a/src/com/android/wallpaper/picker/LivePreviewFragment.java +++ b/src/com/android/wallpaper/picker/LivePreviewFragment.java @@ -15,9 +15,13 @@ */ package com.android.wallpaper.picker; +import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; +import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES; + import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY; import static com.android.wallpaper.widget.BottomActionBar.BottomAction.CUSTOMIZE; import static com.android.wallpaper.widget.BottomActionBar.BottomAction.DELETE; +import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT; import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION; import android.annotation.SuppressLint; @@ -27,16 +31,13 @@ import android.app.WallpaperColors; import android.app.WallpaperInfo; import android.app.WallpaperManager; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.graphics.Point; -import android.graphics.Rect; -import android.graphics.RectF; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -51,7 +52,6 @@ import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; -import android.view.animation.AnimationUtils; import android.widget.ImageView; import androidx.annotation.NonNull; @@ -66,20 +66,23 @@ import androidx.slice.widget.SliceView; import com.android.wallpaper.R; import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; +import com.android.wallpaper.util.FullScreenAnimation; +import com.android.wallpaper.util.ResourceUtils; import com.android.wallpaper.util.ScreenSizeCalculator; import com.android.wallpaper.util.SizeCalculator; import com.android.wallpaper.util.WallpaperConnection; import com.android.wallpaper.util.WallpaperSurfaceCallback; import com.android.wallpaper.widget.BottomActionBar; import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback; -import com.android.wallpaper.widget.LiveTileOverlay; +import com.android.wallpaper.widget.BottomActionBar.BottomSheetContent; import com.android.wallpaper.widget.LockScreenPreviewer; import com.android.wallpaper.widget.WallpaperColorsLoader; -import com.android.wallpaper.widget.WallpaperInfoView; - -import com.google.android.material.tabs.TabLayout; import java.util.Locale; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * Fragment which displays the UI for previewing an individual live wallpaper, its attribution @@ -99,34 +102,28 @@ public class LivePreviewFragment extends PreviewFragment implements * @see IWallpaperConnection */ protected WallpaperConnection mWallpaperConnection; - protected WallpaperInfoView mWallpaperInfoView; protected CardView mHomePreviewCard; - protected ImageView mHomePreview; - - private final int[] mLivePreviewLocation = new int[2]; - private final Rect mPreviewLocalRect = new Rect(); - private final Rect mPreviewGlobalRect = new Rect(); + protected SurfaceView mWorkspaceSurface; + protected WallpaperSurfaceCallback mWallpaperSurfaceCallback; + protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; + protected ViewGroup mLockPreviewContainer; + protected LockScreenPreviewer mLockScreenPreviewer; private Intent mDeleteIntent; private Intent mSettingsIntent; - private SliceView mSettingsSliceView; private LiveData<Slice> mSettingsLiveData; - private View mLoadingScrim; private Point mScreenSize; private ViewGroup mPreviewContainer; private TouchForwardingLayout mTouchForwardingLayout; - private SurfaceView mWorkspaceSurface; private SurfaceView mWallpaperSurface; - private ViewGroup mLockPreviewContainer; - private LockScreenPreviewer mLockScreenPreviewer; - private WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; - private WallpaperSurfaceCallback mWallpaperSurfaceCallback; + private Future<Integer> mPlaceholderColorFuture; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); android.app.WallpaperInfo info = mWallpaper.getWallpaperComponent(); + mPlaceholderColorFuture = mWallpaper.computePlaceholderColor(getContext()); String deleteAction = getDeleteAction(info); if (!TextUtils.isEmpty(deleteAction)) { @@ -134,7 +131,6 @@ public class LivePreviewFragment extends PreviewFragment implements mDeleteIntent.setPackage(info.getPackageName()); mDeleteIntent.putExtra(EXTRA_LIVE_WALLPAPER_INFO, info); } - String settingsActivity = getSettingsActivity(info); if (settingsActivity != null) { mSettingsIntent = new Intent(); @@ -165,19 +161,9 @@ public class LivePreviewFragment extends PreviewFragment implements Bundle savedInstanceState) { View view = super.onCreateView(inflater, container, savedInstanceState); - mLoadingScrim = view.findViewById(R.id.loading); - setUpLoadingIndicator(); - Activity activity = requireActivity(); mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize( activity.getWindowManager().getDefaultDisplay()); - - mWallpaperInfoView = (WallpaperInfoView) LayoutInflater.from(getContext()) - .inflate(R.layout.wallpaper_info_view, /* root= */ null); - setUpExploreIntentAndLabel( - () -> mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel, - mExploreIntent, this::onExploreClicked)); - mPreviewContainer = view.findViewById(R.id.live_wallpaper_preview); mTouchForwardingLayout = view.findViewById(R.id.touch_forwarding_layout); // Set aspect ratio on the preview card. @@ -188,41 +174,22 @@ public class LivePreviewFragment extends PreviewFragment implements set.applyTo((ConstraintLayout) mPreviewContainer); mHomePreviewCard = mPreviewContainer.findViewById(R.id.wallpaper_full_preview_card); - mHomePreview = mHomePreviewCard.findViewById(R.id.wallpaper_preview_image); - mTouchForwardingLayout.setTargetView(mHomePreview); - mTouchForwardingLayout.setForwardingEnabled(true); mLockPreviewContainer = mPreviewContainer.findViewById(R.id.lock_screen_preview_container); - mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), activity, + mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), getContext(), mLockPreviewContainer); + mLockScreenPreviewer.setDateViewVisibility(!mFullScreenAnimation.isFullScreen()); + mFullScreenAnimation.setFullScreenStatusListener( + isFullScreen -> mLockScreenPreviewer.setDateViewVisibility(!isFullScreen)); mWallpaperSurface = mHomePreviewCard.findViewById(R.id.wallpaper_surface); + mTouchForwardingLayout.setTargetView(mHomePreviewCard); + mTouchForwardingLayout.setForwardingEnabled(true); mWorkspaceSurface = mHomePreviewCard.findViewById(R.id.workspace_surface); - mWorkspaceSurfaceCallback = new WorkspaceSurfaceHolderCallback( - mWorkspaceSurface, getContext()); + mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface); mWallpaperSurfaceCallback = new WallpaperSurfaceCallback(getContext(), - mHomePreview, mWallpaperSurface); - - TabLayout tabs = inflater.inflate(R.layout.full_preview_tabs, - view.findViewById(R.id.toolbar_tabs_container)) - .findViewById(R.id.full_preview_tabs); - tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { - @Override - public void onTabSelected(TabLayout.Tab tab) { - updateScreenPreview(tab.getPosition() == 0); - } - - @Override - public void onTabUnselected(TabLayout.Tab tab) {} - - @Override - public void onTabReselected(TabLayout.Tab tab) {} - }); + mHomePreviewCard, mWallpaperSurface, mPlaceholderColorFuture, null); - // The TabLayout only contains below tabs, see: full_preview_tabs.xml - // 0. Home tab - // 1. Lock tab - tabs.getTabAt(mViewAsHome ? 0 : 1).select(); - updateScreenPreview(mViewAsHome); + setUpTabs(view.findViewById(R.id.separated_tabs)); view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override @@ -233,6 +200,7 @@ public class LivePreviewFragment extends PreviewFragment implements view.removeOnLayoutChangeListener(this); } }); + return view; } @@ -246,29 +214,29 @@ public class LivePreviewFragment extends PreviewFragment implements private void updateWallpaperSurface() { mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback); + mWallpaperSurface.setZOrderMediaOverlay(true); } - private void updateScreenPreview(boolean isHomeSelected) { + @Override + protected void updateScreenPreview(boolean isHomeSelected) { mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE); + mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE); - } - private void repositionPreview(ImageView previewView) { - previewView.getLocationOnScreen(mLivePreviewLocation); - mPreviewGlobalRect.set(0, 0, previewView.getMeasuredWidth(), - previewView.getMeasuredHeight()); - mPreviewLocalRect.set(mPreviewGlobalRect); - mPreviewGlobalRect.offset(mLivePreviewLocation[0], mLivePreviewLocation[1]); + mFullScreenAnimation.setIsHomeSelected(isHomeSelected); } private void setupCurrentWallpaperPreview() { - mHomePreview.setOnTouchListener((v, ev) -> { + mHomePreviewCard.setOnTouchListener((v, ev) -> { if (mWallpaperConnection != null && mWallpaperConnection.getEngine() != null) { + float scaleRatio = + (float) mTouchForwardingLayout.getWidth() / (float) mScreenSize.x; int action = ev.getActionMasked(); if (action == MotionEvent.ACTION_DOWN) { mBottomActionBar.collapseBottomSheetIfExpanded(); } MotionEvent dup = MotionEvent.obtainNoHistory(ev); + dup.setLocation(ev.getX() / scaleRatio, ev.getY() / scaleRatio); try { mWallpaperConnection.getEngine().dispatchPointer(dup); if (action == MotionEvent.ACTION_UP) { @@ -292,39 +260,52 @@ public class LivePreviewFragment extends PreviewFragment implements @Override public void onDestroyView() { super.onDestroyView(); - if (mSettingsLiveData != null && mSettingsLiveData.hasObservers()) { + if (mSettingsLiveData != null && mSettingsLiveData.hasObservers() + && mSettingsSliceView != null) { mSettingsLiveData.removeObserver(mSettingsSliceView); mSettingsLiveData = null; } - LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay()); if (mWallpaperConnection != null) { mWallpaperConnection.disconnect(); mWallpaperConnection = null; } + if (mLockScreenPreviewer != null) { + mLockScreenPreviewer.release(); + } mWorkspaceSurfaceCallback.cleanUp(); mWorkspaceSurface.getHolder().removeCallback(mWorkspaceSurfaceCallback); mWallpaperSurfaceCallback.cleanUp(); mWallpaperSurface.getHolder().removeCallback(mWallpaperSurfaceCallback); } - private void previewLiveWallpaper(ImageView thumbnailView) { - thumbnailView.post(() -> { + protected void previewLiveWallpaper(ImageView thumbnailView) { + mWallpaperSurface.post(() -> { + Activity activity = getActivity(); + if (activity == null) { + return; + } if (mWallpaperSurfaceCallback.getHomeImageWallpaper() != null) { - mWallpaper.getThumbAsset(requireActivity().getApplicationContext()) - .loadPreviewImage(requireActivity(), + Integer placeholderColor = null; + try { + placeholderColor = mPlaceholderColorFuture.get(50, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + Log.i(TAG, "Couldn't obtain placeholder color", e); + } + mWallpaper.getThumbAsset(activity.getApplicationContext()) + .loadLowResDrawable(activity, mWallpaperSurfaceCallback.getHomeImageWallpaper(), - getResources().getColor(R.color.secondary_color)); + placeholderColor != null + ? placeholderColor + : ResourceUtils.getColorAttr(activity, + android.R.attr.colorBackground), + mPreviewBitmapTransformation); } - LiveTileOverlay.INSTANCE.detach(thumbnailView.getOverlay()); - - setUpLiveWallpaperPreview(mWallpaper, thumbnailView, - new ColorDrawable(getResources().getColor( - R.color.secondary_color, getActivity().getTheme()))); + setUpLiveWallpaperPreview(mWallpaper); }); } - private void setUpLiveWallpaperPreview(com.android.wallpaper.model.WallpaperInfo homeWallpaper, - ImageView previewView, Drawable thumbnail) { + protected void setUpLiveWallpaperPreview( + com.android.wallpaper.model.WallpaperInfo homeWallpaper) { Activity activity = getActivity(); if (activity == null || activity.isFinishing()) { return; @@ -332,21 +313,13 @@ public class LivePreviewFragment extends PreviewFragment implements if (mWallpaperConnection != null) { mWallpaperConnection.disconnect(); } - if (thumbnail != null) { - thumbnail.setBounds(previewView.getLeft(), previewView.getTop(), previewView.getRight(), - previewView.getBottom()); - } if (WallpaperConnection.isPreviewAvailable()) { - repositionPreview(previewView); mWallpaperConnection = new WallpaperConnection( getWallpaperIntent(homeWallpaper.getWallpaperComponent()), activity, /* listener= */ this, - mPreviewGlobalRect); - - LiveTileOverlay.INSTANCE.update(new RectF(mPreviewLocalRect), - ((CardView) previewView.getParent()).getRadius()); + mWallpaperSurface); mWallpaperConnection.setVisibility(true); } else { @@ -355,18 +328,9 @@ public class LivePreviewFragment extends PreviewFragment implements homeWallpaper.getThumbAsset(activity), mLockScreenPreviewer::setColor); } - mLoadingScrim.post(() -> mLoadingScrim.animate() - .alpha(0f) - .setStartDelay(50) - .setDuration(250) - .setInterpolator(AnimationUtils.loadInterpolator(getContext(), - android.R.interpolator.fast_out_linear_in)) - .withEndAction(() -> { - if (mWallpaperConnection != null && !mWallpaperConnection.connect()) { - mWallpaperConnection = null; - LiveTileOverlay.INSTANCE.detach(previewView.getOverlay()); - } - })); + if (mWallpaperConnection != null && !mWallpaperConnection.connect()) { + mWallpaperConnection = null; + } } private void renderWorkspaceSurface() { @@ -377,35 +341,35 @@ public class LivePreviewFragment extends PreviewFragment implements @Override protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { super.onBottomActionBarReady(bottomActionBar); - mBottomActionBar.showActionsOnly(INFORMATION, DELETE, CUSTOMIZE, APPLY); + mBottomActionBar.showActionsOnly(INFORMATION, DELETE, EDIT, CUSTOMIZE, APPLY); mBottomActionBar.setActionClickListener(APPLY, unused -> onSetWallpaperClicked(null)); - mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION); + mBottomActionBar.bindBottomSheetContentWithAction( + new WallpaperInfoContent(getContext()), INFORMATION); + View separatedTabsContainer = getView().findViewById(R.id.separated_tabs_container); // Update target view's accessibility param since it will be blocked by the bottom sheet // when expanded. mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() { @Override public void onBottomSheetCollapsed() { - mPreviewContainer.setImportantForAccessibility( - View.IMPORTANT_FOR_ACCESSIBILITY_YES); + mPreviewContainer.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); + separatedTabsContainer.setImportantForAccessibility( + IMPORTANT_FOR_ACCESSIBILITY_YES); } @Override public void onBottomSheetExpanded() { mPreviewContainer.setImportantForAccessibility( - View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); + IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); + separatedTabsContainer.setImportantForAccessibility( + IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); } }); final Uri uriSettingsSlice = getSettingsSliceUri(mWallpaper.getWallpaperComponent()); if (uriSettingsSlice != null) { - View previewPage = LayoutInflater.from(getContext()) - .inflate(R.layout.preview_customize_settings, null); - mSettingsSliceView = previewPage.findViewById(R.id.settings_slice); - mSettingsSliceView.setMode(SliceView.MODE_LARGE); - mSettingsSliceView.setScrollable(false); mSettingsLiveData = SliceLiveData.fromUri(requireContext(), uriSettingsSlice); - mSettingsLiveData.observeForever(mSettingsSliceView); - mBottomActionBar.attachViewToBottomSheetAndBindAction(previewPage, CUSTOMIZE); + mBottomActionBar.bindBottomSheetContentWithAction( + new PreviewCustomizeSettingsContent(getContext()), CUSTOMIZE); } else { if (mSettingsIntent != null) { mBottomActionBar.setActionClickListener(CUSTOMIZE, listener -> @@ -415,14 +379,19 @@ public class LivePreviewFragment extends PreviewFragment implements } } - final String deleteAction = getDeleteAction(mWallpaper.getWallpaperComponent()); - if (TextUtils.isEmpty(deleteAction)) { + if (TextUtils.isEmpty(getDeleteAction(mWallpaper.getWallpaperComponent()))) { mBottomActionBar.hideActions(DELETE); } else { mBottomActionBar.setActionClickListener(DELETE, listener -> showDeleteConfirmDialog()); } mBottomActionBar.show(); + // Action buttons are disabled when live wallpaper is not loaded. + mBottomActionBar.disableActions(); + // Enable buttons if loaded, or wait for it. + if (isLoaded()) { + mBottomActionBar.enableActions(); + } } @Override @@ -431,40 +400,27 @@ public class LivePreviewFragment extends PreviewFragment implements if (activity == null) { return; } - - mLoadingScrim.post(() -> mLoadingScrim.animate() - .alpha(0f) + mWallpaperSurfaceCallback.getHomeImageWallpaper().animate() + .setStartDelay(250) .setDuration(250) - .setStartDelay(200) - .setInterpolator(AnimationUtils.loadInterpolator(activity, - android.R.interpolator.fast_out_linear_in)) - .withEndAction(() -> { - if (mLoadingProgressBar != null) { - mLoadingProgressBar.hide(); - } - mLoadingScrim.setVisibility(View.GONE); - })); - final Drawable placeholder = mHomePreview.getDrawable() == null - ? new ColorDrawable(getResources().getColor(R.color.secondary_color, - activity.getTheme())) - : mHomePreview.getDrawable(); - LiveTileOverlay.INSTANCE.setForegroundDrawable(placeholder); - LiveTileOverlay.INSTANCE.attach(mHomePreview.getOverlay()); - mHomePreview.animate() - .setStartDelay(0) - .setDuration(150) - .setInterpolator(AnimationUtils.loadInterpolator(activity, - android.R.interpolator.fast_out_linear_in)) - .setUpdateListener(value -> placeholder.setAlpha( - (int) (255 * (1 - value.getAnimatedFraction())))) - .withEndAction(() -> { - LiveTileOverlay.INSTANCE.setForegroundDrawable(null); - }).start(); + .alpha(0f) + .setInterpolator(ALPHA_OUT) + .start(); + + if (mBottomActionBar != null) { + mBottomActionBar.enableActions(); + } } @Override public void onWallpaperColorsChanged(WallpaperColors colors, int displayId) { mLockScreenPreviewer.setColor(colors); + + mFullScreenAnimation.setFullScreenTextColor( + colors == null || (colors.getColorHints() + & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0 + ? FullScreenAnimation.FullScreenTextColor.LIGHT + : FullScreenAnimation.FullScreenTextColor.DARK); } @Override @@ -482,12 +438,7 @@ public class LivePreviewFragment extends PreviewFragment implements @Override protected int getLayoutResId() { - return R.layout.fragment_live_preview_v2; - } - - @Override - protected int getLoadingIndicatorResId() { - return R.id.loading_indicator; + return R.layout.fragment_live_preview; } @Override @@ -532,7 +483,7 @@ public class LivePreviewFragment extends PreviewFragment implements @Override public void onStart() { super.onStart(); - previewLiveWallpaper(mHomePreview); + previewLiveWallpaper(null); } @Override @@ -581,4 +532,35 @@ public class LivePreviewFragment extends PreviewFragment implements private boolean isPackagePreInstalled(ApplicationInfo info) { return info != null && (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; } + + private final class PreviewCustomizeSettingsContent extends BottomSheetContent<View> { + + private PreviewCustomizeSettingsContent(Context context) { + super(context); + } + + @Override + public int getViewId() { + return R.layout.preview_customize_settings; + } + + @Override + public void onViewCreated(View previewPage) { + mSettingsSliceView = previewPage.findViewById(R.id.settings_slice); + mSettingsSliceView.setMode(SliceView.MODE_LARGE); + mSettingsSliceView.setScrollable(false); + if (mSettingsLiveData != null) { + mSettingsLiveData.observeForever(mSettingsSliceView); + } + } + + @Override + public void onRecreateView(View oldPreviewPage) { + super.onRecreateView(oldPreviewPage); + if (mSettingsLiveData != null && mSettingsLiveData.hasObservers() + && mSettingsSliceView != null) { + mSettingsLiveData.removeObserver(mSettingsSliceView); + } + } + } } |