Wallpaper preview size respects device aspect ratio.

This commit is contained in:
Alan Evans 2021-01-21 14:49:32 -04:00 committed by Greyson Parrelli
parent ce156c3450
commit 93d99287eb
8 changed files with 67 additions and 16 deletions

View file

@ -0,0 +1,21 @@
package org.thoughtcrime.securesms.util;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
public final class DisplayMetricsUtil {
private DisplayMetricsUtil() {
}
public static void forceAspectRatioToScreenByAdjustingHeight(@NonNull DisplayMetrics displayMetrics, @NonNull View view) {
int screenHeight = displayMetrics.heightPixels;
int screenWidth = displayMetrics.widthPixels;
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = params.width * screenHeight / screenWidth;
view.setLayoutParams(params);
}
}

View file

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.wallpaper;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -17,6 +18,7 @@ import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.DisplayMetricsUtil;
import org.thoughtcrime.securesms.util.ThemeUtil;
public class ChatWallpaperFragment extends Fragment {
@ -43,6 +45,8 @@ public class ChatWallpaperFragment extends Fragment {
resetAllWallpaper = view.findViewById(R.id.chat_wallpaper_reset_all_wallpapers);
divider = view.findViewById(R.id.chat_wallpaper_divider);
forceAspectRatioToScreenByAdjustingHeight(chatWallpaperPreview);
viewModel.getCurrentWallpaper().observe(getViewLifecycleOwner(), wallpaper -> {
if (wallpaper.isPresent()) {
wallpaper.get().loadInto(chatWallpaperPreview);
@ -124,4 +128,10 @@ public class ChatWallpaperFragment extends Fragment {
.setCancelable(true)
.show();
}
private void forceAspectRatioToScreenByAdjustingHeight(@NonNull View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
DisplayMetricsUtil.forceAspectRatioToScreenByAdjustingHeight(displayMetrics, view);
}
}

View file

@ -5,6 +5,6 @@ import org.thoughtcrime.securesms.util.MappingAdapter;
class ChatWallpaperPreviewAdapter extends MappingAdapter {
ChatWallpaperPreviewAdapter() {
registerFactory(ChatWallpaperSelectionMappingModel.class, ChatWallpaperViewHolder.createFactory(R.layout.chat_wallpaper_preview_fragment_adapter_item, null));
registerFactory(ChatWallpaperSelectionMappingModel.class, ChatWallpaperViewHolder.createFactory(R.layout.chat_wallpaper_preview_fragment_adapter_item, null, null));
}
}

View file

@ -1,12 +1,15 @@
package org.thoughtcrime.securesms.wallpaper;
import android.util.DisplayMetrics;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.MappingAdapter;
class ChatWallpaperSelectionAdapter extends MappingAdapter {
ChatWallpaperSelectionAdapter(@Nullable ChatWallpaperViewHolder.EventListener eventListener) {
registerFactory(ChatWallpaperSelectionMappingModel.class, ChatWallpaperViewHolder.createFactory(R.layout.chat_wallpaper_selection_fragment_adapter_item, eventListener));
ChatWallpaperSelectionAdapter(@Nullable ChatWallpaperViewHolder.EventListener eventListener, @NonNull DisplayMetrics windowDisplayMetrics) {
registerFactory(ChatWallpaperSelectionMappingModel.class, ChatWallpaperViewHolder.createFactory(R.layout.chat_wallpaper_selection_fragment_adapter_item, eventListener, windowDisplayMetrics));
}
}

View file

@ -4,6 +4,7 @@ import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -16,7 +17,6 @@ import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.flexbox.AlignContent;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
@ -46,11 +46,14 @@ public class ChatWallpaperSelectionFragment extends Fragment {
askForPermissionIfNeededAndLaunchPhotoSelection();
});
DisplayMetrics displayMetrics = new DisplayMetrics();
requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
@SuppressWarnings("CodeBlock2Expr")
ChatWallpaperSelectionAdapter adapter = new ChatWallpaperSelectionAdapter(chatWallpaper -> {
startActivityForResult(ChatWallpaperPreviewActivity.create(requireActivity(), chatWallpaper, viewModel.getRecipientId(), viewModel.getDimInDarkTheme().getValue()), CHOOSE_WALLPAPER);
ActivityTransitionUtil.setSlideInTransition(requireActivity());
});
}, displayMetrics);
flexboxLayoutManager.setJustifyContent(JustifyContent.CENTER);
recyclerView.setLayoutManager(flexboxLayoutManager);

View file

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.wallpaper;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
@ -9,9 +10,9 @@ import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.DisplayMetricsUtil;
import org.thoughtcrime.securesms.util.MappingAdapter;
import org.thoughtcrime.securesms.util.MappingViewHolder;
import org.thoughtcrime.securesms.util.ThemeUtil;
class ChatWallpaperViewHolder extends MappingViewHolder<ChatWallpaperSelectionMappingModel> {
@ -19,11 +20,15 @@ class ChatWallpaperViewHolder extends MappingViewHolder<ChatWallpaperSelectionMa
private final View dimmer;
private final EventListener eventListener;
public ChatWallpaperViewHolder(@NonNull View itemView, @Nullable EventListener eventListener) {
public ChatWallpaperViewHolder(@NonNull View itemView, @Nullable EventListener eventListener, @Nullable DisplayMetrics windowDisplayMetrics) {
super(itemView);
this.preview = itemView.findViewById(R.id.chat_wallpaper_preview);
this.dimmer = itemView.findViewById(R.id.chat_wallpaper_dim);
this.eventListener = eventListener;
if (windowDisplayMetrics != null) {
DisplayMetricsUtil.forceAspectRatioToScreenByAdjustingHeight(windowDisplayMetrics, itemView);
}
}
@Override
@ -41,8 +46,8 @@ class ChatWallpaperViewHolder extends MappingViewHolder<ChatWallpaperSelectionMa
}
}
public static @NonNull MappingAdapter.Factory<ChatWallpaperSelectionMappingModel> createFactory(@LayoutRes int layout, @Nullable EventListener listener) {
return new MappingAdapter.LayoutFactory<>(view -> new ChatWallpaperViewHolder(view, listener), layout);
public static @NonNull MappingAdapter.Factory<ChatWallpaperSelectionMappingModel> createFactory(@LayoutRes int layout, @Nullable EventListener listener, @Nullable DisplayMetrics windowDisplayMetrics) {
return new MappingAdapter.LayoutFactory<>(view -> new ChatWallpaperViewHolder(view, listener, windowDisplayMetrics), layout);
}
public interface EventListener {

View file

@ -13,20 +13,21 @@
<View
android:id="@+id/chat_wallpaper_preview_lightbox"
android:layout_width="0dp"
android:layout_height="320dp"
android:layout_height="0dp"
android:background="@color/signal_background_tertiary"
app:layout_constraintBottom_toBottomOf="@id/chat_wallpaper_preview_background"
app:layout_constraintBottom_toBottomOf="@id/chat_wallpaper_space"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/chat_wallpaper_preview_background" />
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/chat_wallpaper_preview_background"
android:layout_width="156dp"
android:layout_height="288dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@color/signal_background_primary"
android:importantForAccessibility="no"
android:contentDescription="@string/ChatWallpaperFragment__wallpaper_preview_description"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@ -123,6 +124,13 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Space
android:id="@+id/chat_wallpaper_space"
android:layout_width="wrap_content"
android:layout_height="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/chat_wallpaper_preview_bottom_bar" />
<View
android:id="@+id/chat_wallpaper_preview_bottom_bar_input"
android:layout_width="0dp"
@ -227,12 +235,12 @@
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:visibility="gone"
tools:visibility="visible"
android:background="@color/signal_inverse_transparent_15"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/chat_wallpaper_dark_theme_dims_wallpaper" />
app:layout_constraintTop_toBottomOf="@id/chat_wallpaper_dark_theme_dims_wallpaper"
tools:visibility="visible" />
<TextView
android:id="@+id/chat_wallpaper_clear_wallpaper"

View file

@ -2840,6 +2840,7 @@
<string name="ChatWallpaperFragment__contact_name">Contact name</string>
<string name="ChatWallpaperFragment__reset">Reset</string>
<string name="ChatWallpaperFragment__clear">Clear</string>
<string name="ChatWallpaperFragment__wallpaper_preview_description">Wallpaper preview</string>
<!-- ChatWallpaperSelectionFragment -->
<string name="ChatWallpaperSelectionFragment__choose_from_photos">Choose from photos</string>