diff options
Diffstat (limited to 'src/com/android/wallpaper/widget/LockScreenPreviewer.java')
-rw-r--r-- | src/com/android/wallpaper/widget/LockScreenPreviewer.java | 99 |
1 files changed, 61 insertions, 38 deletions
diff --git a/src/com/android/wallpaper/widget/LockScreenPreviewer.java b/src/com/android/wallpaper/widget/LockScreenPreviewer.java index 9a6f7d4..282a899 100644 --- a/src/com/android/wallpaper/widget/LockScreenPreviewer.java +++ b/src/com/android/wallpaper/widget/LockScreenPreviewer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 The Android Open Source Project + * Copyright (C) 2021 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. @@ -18,9 +18,8 @@ package com.android.wallpaper.widget; import static android.view.View.MeasureSpec.EXACTLY; import static android.view.View.MeasureSpec.makeMeasureSpec; -import android.app.Activity; import android.app.WallpaperColors; -import android.content.res.ColorStateList; +import android.content.Context; import android.content.res.Configuration; import android.graphics.Point; import android.text.format.DateFormat; @@ -28,7 +27,7 @@ import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; +import android.view.WindowManager; import android.widget.TextView; import androidx.annotation.MainThread; @@ -45,41 +44,44 @@ import com.android.wallpaper.util.TimeUtils.TimeTicker; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; -/** A class to load the custom lockscreen view to the preview screen. */ +/** A class to load the new custom lockscreen view to the preview screen. */ public class LockScreenPreviewer implements LifecycleObserver { private static final String DEFAULT_DATE_PATTERN = "EEE, MMM d"; + private static final ExecutorService sExecutorService = Executors.newSingleThreadExecutor(); - private Activity mActivity; - private String mDatePattern; + private final Lifecycle mLifecycle; + private final Context mContext; + private final String mDatePattern; + private final TextView mLockTime; + private final TextView mLockDate; private TimeTicker mTicker; - private ImageView mLockIcon; - private TextView mLockTime; - private TextView mLockDate; - - public LockScreenPreviewer(Lifecycle lifecycle, Activity activity, ViewGroup previewContainer) { - lifecycle.addObserver(this); - mActivity = activity; - View contentView = LayoutInflater.from(mActivity).inflate( + + public LockScreenPreviewer(Lifecycle lifecycle, Context context, ViewGroup previewContainer) { + mLifecycle = lifecycle; + mContext = context; + View contentView = LayoutInflater.from(mContext).inflate( R.layout.lock_screen_preview, /* root= */ null); - mLockIcon = contentView.findViewById(R.id.lock_icon); mLockTime = contentView.findViewById(R.id.lock_time); mLockDate = contentView.findViewById(R.id.lock_date); mDatePattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), DEFAULT_DATE_PATTERN); - Display defaultDisplay = mActivity.getWindowManager().getDefaultDisplay(); + Display defaultDisplay = mContext.getSystemService(WindowManager.class).getDefaultDisplay(); Point screenSize = ScreenSizeCalculator.getInstance().getScreenSize(defaultDisplay); - Configuration config = mActivity.getResources().getConfiguration(); - final boolean directionLTR = config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR; + Configuration config = mContext.getResources().getConfiguration(); + boolean directionLTR = config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR; View rootView = previewContainer.getRootView(); rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View view, int left, int top, int right, int bottom, - int oldLeft, int oldTop, int oldRight, int oldBottom) { + int oldLeft, int oldTop, int oldRight, int oldBottom) { int cardHeight = previewContainer.getMeasuredHeight(); + int cardWidth = previewContainer.getMeasuredWidth(); // Relayout the content view to match full screen size. contentView.measure( @@ -88,7 +90,8 @@ public class LockScreenPreviewer implements LifecycleObserver { contentView.layout(0, 0, screenSize.x, screenSize.y); // Scale the content view from full screen size to the container(card) size. - float scale = (float) cardHeight / screenSize.y; + float scale = cardHeight > 0 ? (float) cardHeight / screenSize.y + : (float) cardWidth / screenSize.x; contentView.setScaleX(scale); contentView.setScaleY(scale); // The pivot point is centered by default, set to (0, 0). @@ -103,21 +106,27 @@ public class LockScreenPreviewer implements LifecycleObserver { rootView.removeOnLayoutChangeListener(this); } }); + mLifecycle.addObserver(this); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) @MainThread public void onResume() { - mTicker = TimeTicker.registerNewReceiver(mActivity, this::updateDateTime); + if (mTicker == null) { + sExecutorService.submit(() -> { + if (mContext != null && mLifecycle.getCurrentState().isAtLeast( + Lifecycle.State.RESUMED)) { + mTicker = TimeTicker.registerNewReceiver(mContext, this::updateDateTime); + } + }); + } updateDateTime(); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) @MainThread public void onPause() { - if (mActivity != null) { - mActivity.unregisterReceiver(mTicker); - } + unregisterReceiver(); } /** @@ -129,32 +138,46 @@ public class LockScreenPreviewer implements LifecycleObserver { public void setColor(@Nullable WallpaperColors colors) { boolean useLightTextColor = colors == null || (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0; - int color = mActivity.getColor(useLightTextColor + int color = mContext.getColor(useLightTextColor ? R.color.text_color_light : R.color.text_color_dark); - int textShadowColor = mActivity.getColor(useLightTextColor + int textShadowColor = mContext.getColor(useLightTextColor ? R.color.smartspace_preview_shadow_color_dark : R.color.smartspace_preview_shadow_color_transparent); - mLockIcon.setImageTintList(ColorStateList.valueOf(color)); mLockDate.setTextColor(color); - mLockTime.setTextColor(color); - mLockDate.setShadowLayer( - mActivity.getResources().getDimension( - R.dimen.smartspace_preview_key_ambient_shadow_blur), - /* dx = */ 0, - /* dy = */ 0, - textShadowColor); - mLockTime.setShadowLayer( - mActivity.getResources().getDimension( + mContext.getResources().getDimension( R.dimen.smartspace_preview_key_ambient_shadow_blur), /* dx = */ 0, /* dy = */ 0, textShadowColor); } + /** Sets visibility for date view. */ + public void setDateViewVisibility(boolean visible) { + mLockDate.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); + } + + public void release() { + mLifecycle.removeObserver(this); + unregisterReceiver(); + } + + private void unregisterReceiver() { + if (mTicker == null) { + return; + } + + sExecutorService.submit(() -> { + if (mContext != null && mTicker != null) { + mContext.unregisterReceiver(mTicker); + mTicker = null; + } + }); + } + private void updateDateTime() { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); - mLockTime.setText(TimeUtils.getFormattedTime(mActivity, calendar)); mLockDate.setText(DateFormat.format(mDatePattern, calendar)); + mLockTime.setText(TimeUtils.getDoubleLineFormattedTime(mLockTime.getContext(), calendar)); } } |