2021-01-12 18:28:52 +00:00
|
|
|
package org.thoughtcrime.securesms.wallpaper;
|
|
|
|
|
2021-01-20 21:09:36 +00:00
|
|
|
import android.app.AlertDialog;
|
2021-01-12 18:28:52 +00:00
|
|
|
import android.os.Bundle;
|
2021-01-21 18:49:32 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2021-01-12 18:28:52 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2021-01-23 01:59:57 +00:00
|
|
|
import android.widget.TextView;
|
2021-01-12 18:28:52 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2021-01-20 21:09:36 +00:00
|
|
|
import androidx.annotation.StringRes;
|
2021-01-20 02:54:10 +00:00
|
|
|
import androidx.appcompat.widget.SwitchCompat;
|
2021-01-12 18:28:52 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
import androidx.navigation.Navigation;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2021-01-21 18:49:32 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DisplayMetricsUtil;
|
2021-01-20 02:54:10 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ThemeUtil;
|
2021-01-12 18:28:52 +00:00
|
|
|
|
|
|
|
public class ChatWallpaperFragment extends Fragment {
|
2021-01-20 21:09:36 +00:00
|
|
|
|
2021-01-23 01:59:57 +00:00
|
|
|
private boolean isSettingDimFromViewModel;
|
|
|
|
private TextView clearWallpaper;
|
|
|
|
private View resetAllWallpaper;
|
|
|
|
private View divider;
|
2021-01-20 21:09:36 +00:00
|
|
|
|
2021-01-12 18:28:52 +00:00
|
|
|
@Override
|
|
|
|
public @NonNull View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
return inflater.inflate(R.layout.chat_wallpaper_fragment, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
|
ChatWallpaperViewModel viewModel = ViewModelProviders.of(requireActivity()).get(ChatWallpaperViewModel.class);
|
|
|
|
ImageView chatWallpaperPreview = view.findViewById(R.id.chat_wallpaper_preview_background);
|
|
|
|
View setWallpaper = view.findViewById(R.id.chat_wallpaper_set_wallpaper);
|
2021-01-20 02:54:10 +00:00
|
|
|
SwitchCompat dimInNightMode = view.findViewById(R.id.chat_wallpaper_dark_theme_dims_wallpaper);
|
|
|
|
View chatWallpaperDim = view.findViewById(R.id.chat_wallpaper_dim);
|
2021-01-20 21:09:36 +00:00
|
|
|
|
|
|
|
clearWallpaper = view.findViewById(R.id.chat_wallpaper_clear_wallpaper);
|
|
|
|
resetAllWallpaper = view.findViewById(R.id.chat_wallpaper_reset_all_wallpapers);
|
|
|
|
divider = view.findViewById(R.id.chat_wallpaper_divider);
|
2021-01-12 18:28:52 +00:00
|
|
|
|
2021-01-21 18:49:32 +00:00
|
|
|
forceAspectRatioToScreenByAdjustingHeight(chatWallpaperPreview);
|
|
|
|
|
2021-01-12 18:28:52 +00:00
|
|
|
viewModel.getCurrentWallpaper().observe(getViewLifecycleOwner(), wallpaper -> {
|
|
|
|
if (wallpaper.isPresent()) {
|
|
|
|
wallpaper.get().loadInto(chatWallpaperPreview);
|
|
|
|
} else {
|
2021-01-20 02:54:10 +00:00
|
|
|
chatWallpaperPreview.setImageDrawable(null);
|
2021-01-12 18:28:52 +00:00
|
|
|
chatWallpaperPreview.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.signal_background_primary));
|
|
|
|
}
|
2021-01-20 02:54:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
viewModel.getDimInDarkTheme().observe(getViewLifecycleOwner(), shouldDimInNightMode -> {
|
|
|
|
if (shouldDimInNightMode != dimInNightMode.isChecked()) {
|
2021-01-20 21:09:36 +00:00
|
|
|
isSettingDimFromViewModel = true;
|
2021-01-20 02:54:10 +00:00
|
|
|
dimInNightMode.setChecked(shouldDimInNightMode);
|
2021-01-20 21:09:36 +00:00
|
|
|
isSettingDimFromViewModel = false;
|
2021-01-20 02:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chatWallpaperDim.setAlpha(ChatWallpaper.FIXED_DIM_LEVEL_FOR_DARK_THEME);
|
|
|
|
chatWallpaperDim.setVisibility(shouldDimInNightMode && ThemeUtil.isDarkTheme(requireContext()) ? View.VISIBLE : View.GONE);
|
2021-01-12 18:28:52 +00:00
|
|
|
});
|
|
|
|
|
2021-01-20 21:09:36 +00:00
|
|
|
viewModel.getEnableWallpaperControls().observe(getViewLifecycleOwner(), enableWallpaperControls -> {
|
|
|
|
dimInNightMode.setEnabled(enableWallpaperControls);
|
2021-01-23 01:59:57 +00:00
|
|
|
dimInNightMode.setAlpha(enableWallpaperControls ? 1 : 0.5f);
|
|
|
|
clearWallpaper.setEnabled(enableWallpaperControls);
|
|
|
|
clearWallpaper.setAlpha(enableWallpaperControls ? 1 : 0.5f);
|
2021-01-20 21:09:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
chatWallpaperPreview.setOnClickListener(unused -> setWallpaper.performClick());
|
2021-01-12 18:28:52 +00:00
|
|
|
setWallpaper.setOnClickListener(unused -> Navigation.findNavController(view)
|
|
|
|
.navigate(R.id.action_chatWallpaperFragment_to_chatWallpaperSelectionFragment));
|
2021-01-20 02:54:10 +00:00
|
|
|
|
2021-01-20 21:09:36 +00:00
|
|
|
resetAllWallpaper.setVisibility(viewModel.isGlobal() ? View.VISIBLE : View.GONE);
|
|
|
|
|
2021-01-20 02:54:10 +00:00
|
|
|
clearWallpaper.setOnClickListener(unused -> {
|
2021-01-22 13:40:05 +00:00
|
|
|
confirmAction(viewModel.isGlobal() ? R.string.ChatWallpaperFragment__clear_wallpaper_this_will_not
|
|
|
|
: R.string.ChatWallpaperFragment__clear_wallpaper_for_this_chat,
|
2021-01-20 21:09:36 +00:00
|
|
|
R.string.ChatWallpaperFragment__clear,
|
|
|
|
() -> {
|
|
|
|
viewModel.setWallpaper(null);
|
2021-01-22 13:13:12 +00:00
|
|
|
viewModel.setDimInDarkTheme(true);
|
2021-01-20 21:09:36 +00:00
|
|
|
viewModel.saveWallpaperSelection();
|
|
|
|
});
|
2021-01-20 02:54:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
resetAllWallpaper.setOnClickListener(unused -> {
|
2021-01-22 13:42:24 +00:00
|
|
|
confirmAction(R.string.ChatWallpaperFragment__reset_all_wallpapers_including_custom,
|
2021-01-20 21:09:36 +00:00
|
|
|
R.string.ChatWallpaperFragment__reset,
|
|
|
|
() -> {
|
|
|
|
viewModel.setWallpaper(null);
|
2021-01-22 13:13:12 +00:00
|
|
|
viewModel.setDimInDarkTheme(true);
|
2021-01-20 21:09:36 +00:00
|
|
|
viewModel.resetAllWallpaper();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
dimInNightMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
|
|
|
if (!isSettingDimFromViewModel) {
|
|
|
|
viewModel.setDimInDarkTheme(isChecked);
|
|
|
|
}
|
2021-01-20 02:54:10 +00:00
|
|
|
});
|
2021-01-20 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void confirmAction(@StringRes int title, @StringRes int positiveActionLabel, @NonNull Runnable onPositiveAction) {
|
|
|
|
new AlertDialog.Builder(requireContext())
|
|
|
|
.setMessage(title)
|
|
|
|
.setPositiveButton(positiveActionLabel, (dialog, which) -> {
|
|
|
|
onPositiveAction.run();
|
|
|
|
dialog.dismiss();
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, (dialog, which) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
})
|
|
|
|
.setCancelable(true)
|
|
|
|
.show();
|
2021-01-12 18:28:52 +00:00
|
|
|
}
|
2021-01-21 18:49:32 +00:00
|
|
|
|
|
|
|
private void forceAspectRatioToScreenByAdjustingHeight(@NonNull View view) {
|
|
|
|
DisplayMetrics displayMetrics = new DisplayMetrics();
|
|
|
|
requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
|
|
|
DisplayMetricsUtil.forceAspectRatioToScreenByAdjustingHeight(displayMetrics, view);
|
|
|
|
}
|
2021-01-12 18:28:52 +00:00
|
|
|
}
|