Skip to content

Commit

Permalink
Merge pull request #20045 from wordpress-mobile/refactor/remove-image…
Browse files Browse the repository at this point in the history
…-optimization-pop-up-2

Remove image optimization pop-up
  • Loading branch information
Siobhan Bamber authored Jan 26, 2024
2 parents e943e05 + 43f8302 commit a96ae9d
Show file tree
Hide file tree
Showing 16 changed files with 8 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class UserFlagsProviderHelper @Inject constructor(
DeletablePrefKey.RECENTLY_PICKED_SITE_IDS.name,
UndeletablePrefKey.THEME_IMAGE_SIZE_WIDTH.name,
UndeletablePrefKey.BOOKMARKS_SAVED_LOCALLY_DIALOG_SHOWN.name,
UndeletablePrefKey.IMAGE_OPTIMIZE_PROMO_REQUIRED.name,
UndeletablePrefKey.SWIPE_TO_NAVIGATE_NOTIFICATIONS.name,
UndeletablePrefKey.SWIPE_TO_NAVIGATE_READER.name,
UndeletablePrefKey.SHOULD_SHOW_STORIES_INTRO.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,24 +508,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
case RequestCodes.PICTURE_LIBRARY:
case RequestCodes.VIDEO_LIBRARY:
case RequestCodes.AUDIO_LIBRARY:
handlePickerResult(data, resultCode);
break;
case RequestCodes.FILE_LIBRARY:
if (resultCode == Activity.RESULT_OK && data != null) {
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
WPMediaUtils.advertiseImageOptimization(this, () -> handlePickerResult(data, resultCode));
} else {
handlePickerResult(data, resultCode);
}
handlePickerResult(data, resultCode);
}
break;
case RequestCodes.TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
WPMediaUtils.advertiseImageOptimization(this, this::addLastTakenPicture);
} else {
addLastTakenPicture();
}
addLastTakenPicture();
}
break;
case RequestCodes.TAKE_VIDEO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@
import static org.wordpress.android.imageeditor.preview.PreviewImageFragment.PREVIEW_IMAGE_REDUCED_SIZE_FACTOR;
import static org.wordpress.android.ui.history.HistoryDetailContainerFragment.KEY_REVISION;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class EditPostActivity extends LocaleAwareActivity implements
EditorFragmentActivity,
EditorImageSettingsListener,
Expand Down Expand Up @@ -1309,7 +1306,7 @@ public void onPhotoPickerHidden() {
@Override
public void onPhotoPickerMediaChosen(@NonNull final List<? extends Uri> uriList) {
mEditorPhotoPicker.hidePhotoPicker();
mEditorMedia.onPhotoPickerMediaChosen(uriList);
mEditorMedia.addNewMediaItemsToEditorAsync(uriList, false);
}

/*
Expand Down Expand Up @@ -2842,12 +2839,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// handleMediaPickerResult -> addExistingMediaToEditorAndSave
break;
case RequestCodes.PHOTO_PICKER:
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
WPMediaUtils.advertiseImageOptimization(this, () -> handlePhotoPickerResult(data));
} else {
handlePhotoPickerResult(data);
}
break;
case RequestCodes.STOCK_MEDIA_PICKER_SINGLE_SELECT:
handlePhotoPickerResult(data);
break;
Expand All @@ -2861,18 +2852,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
break;
case RequestCodes.MEDIA_LIBRARY:
case RequestCodes.PICTURE_LIBRARY:
mEditorMedia.advertiseImageOptimisationAndAddMedia(WPMediaUtils.retrieveMediaUris(data));
break;
case RequestCodes.TAKE_PHOTO:
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
WPMediaUtils.advertiseImageOptimization(this, this::addLastTakenPicture);
} else {
addLastTakenPicture();
}
break;
case RequestCodes.VIDEO_LIBRARY:
mEditorMedia.addNewMediaItemsToEditorAsync(WPMediaUtils.retrieveMediaUris(data), false);
break;
case RequestCodes.TAKE_PHOTO:
addLastTakenPicture();
break;
case RequestCodes.TAKE_VIDEO:
Uri videoUri = data.getData();
mEditorMedia.addNewMediaToEditorAsync(videoUri, true);
Expand Down Expand Up @@ -3940,10 +3925,6 @@ public void syncPostObjectWithUiAndSaveIt(@Nullable OnPostUpdatedFromUIListener
updateAndSavePostAsync(listener);
}

@Override public void advertiseImageOptimization(@NonNull Function0<Unit> listener) {
WPMediaUtils.advertiseImageOptimization(this, listener::invoke);
}

@Override
public void onMediaModelsCreatedFromOptimizedUris(@NonNull Map<Uri, ? extends MediaModel> oldUriToMediaModels) {
// no op - we're not doing any special handling on MediaModels in EditPostActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import org.wordpress.android.ui.posts.editor.media.EditorMedia.AddMediaToPostUiS
import org.wordpress.android.ui.posts.editor.media.EditorMedia.AddMediaToPostUiState.AddingSingleMedia
import org.wordpress.android.ui.uploads.UploadService
import org.wordpress.android.ui.utils.UiString.UiStringRes
import org.wordpress.android.util.MediaUtilsWrapper
import org.wordpress.android.util.NetworkUtilsWrapper
import org.wordpress.android.util.StringUtils
import org.wordpress.android.util.ToastUtils.Duration
Expand All @@ -52,7 +51,6 @@ class EditorMedia @Inject constructor(
private val updateMediaModelUseCase: UpdateMediaModelUseCase,
private val getMediaModelUseCase: GetMediaModelUseCase,
private val dispatcher: Dispatcher,
private val mediaUtilsWrapper: MediaUtilsWrapper,
private val networkUtilsWrapper: NetworkUtilsWrapper,
private val addLocalMediaToPostUseCase: AddLocalMediaToPostUseCase,
private val addExistingMediaToPostUseCase: AddExistingMediaToPostUseCase,
Expand Down Expand Up @@ -95,20 +93,6 @@ class EditorMedia @Inject constructor(
_uiState.value = AddingMediaIdle
}

// region Adding new media to a post
fun advertiseImageOptimisationAndAddMedia(uriList: List<Uri>) {
if (mediaUtilsWrapper.shouldAdvertiseImageOptimization()) {
editorMediaListener.advertiseImageOptimization {
addNewMediaItemsToEditorAsync(
uriList,
false
)
}
} else {
addNewMediaItemsToEditorAsync(uriList, false)
}
}

fun addNewMediaToEditorAsync(mediaUri: Uri, freshlyTaken: Boolean) {
addNewMediaItemsToEditorAsync(listOf(mediaUri), freshlyTaken)
}
Expand Down Expand Up @@ -145,15 +129,6 @@ class EditorMedia @Inject constructor(
)
}
}

fun onPhotoPickerMediaChosen(uriList: List<Uri>) {
val onlyVideos = uriList.all { mediaUtilsWrapper.isVideo(it.toString()) }
if (onlyVideos) {
addNewMediaItemsToEditorAsync(uriList, false)
} else {
advertiseImageOptimisationAndAddMedia(uriList)
}
}
// endregion

// region Add existing media to a post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.wordpress.android.util.helpers.MediaFile
interface EditorMediaListener {
fun appendMediaFiles(mediaFiles: Map<String, MediaFile>)
fun syncPostObjectWithUiAndSaveIt(listener: OnPostUpdatedFromUIListener? = null)
fun advertiseImageOptimization(listener: () -> Unit)
fun onMediaModelsCreatedFromOptimizedUris(oldUriToMediaFiles: Map<Uri, MediaModel>)
fun getImmutablePost(): PostImmutableModel
fun showVideoDurationLimitWarning(fileName: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ public enum UndeletablePrefKey implements PrefKey {

BOOKMARKS_SAVED_LOCALLY_DIALOG_SHOWN,

// When we need to show the new image optimize promo dialog
IMAGE_OPTIMIZE_PROMO_REQUIRED,

// When we need to show the snackbar indicating how notifications can be navigated through
SWIPE_TO_NAVIGATE_NOTIFICATIONS,

Expand Down Expand Up @@ -603,14 +600,6 @@ public static void setBookmarksSavedLocallyDialogShown() {
setBoolean(UndeletablePrefKey.BOOKMARKS_SAVED_LOCALLY_DIALOG_SHOWN, false);
}

public static boolean isImageOptimizePromoRequired() {
return getBoolean(UndeletablePrefKey.IMAGE_OPTIMIZE_PROMO_REQUIRED, true);
}

public static void setImageOptimizePromoRequired(boolean required) {
setBoolean(UndeletablePrefKey.IMAGE_OPTIMIZE_PROMO_REQUIRED, required);
}

/**
* This method should only be used by specific client classes that need access to the persisted selected site
* instance due to the fact that the in-memory selected site instance might not be yet available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import org.wordpress.android.util.ListUtils
import org.wordpress.android.util.MediaUtils
import org.wordpress.android.util.ToastUtils
import org.wordpress.android.util.ToastUtils.Duration.LONG
import org.wordpress.android.util.WPMediaUtils
import org.wordpress.android.util.WPPermissionUtils
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import org.wordpress.android.util.analytics.AnalyticsUtilsWrapper
Expand Down Expand Up @@ -346,7 +345,7 @@ class StoryComposerActivity : ComposeLoopFrameActivity(),
it.hasExtra(MediaPickerConstants.EXTRA_MEDIA_URIS) -> {
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_URIS)?.let {
val uriList: List<Uri> = convertStringArrayIntoUrisList(it)
storyEditorMedia.onPhotoPickerMediaChosen(uriList)
storyEditorMedia.addNewMediaItemsToEditorAsync(uriList, false)
}
}
it.hasExtra(MediaBrowserActivity.RESULT_IDS) -> {
Expand Down Expand Up @@ -422,7 +421,7 @@ class StoryComposerActivity : ComposeLoopFrameActivity(),
val uriList: List<Uri> = convertStringArrayIntoUrisList(it)

if (uriList.isNotEmpty()) {
storyEditorMedia.onPhotoPickerMediaChosen(uriList)
storyEditorMedia.addNewMediaItemsToEditorAsync(uriList, false)
}
}
}
Expand Down Expand Up @@ -488,10 +487,6 @@ class StoryComposerActivity : ComposeLoopFrameActivity(),
listener?.onPostUpdatedFromUI(null)
}

override fun advertiseImageOptimization(listener: () -> Unit) {
WPMediaUtils.advertiseImageOptimization(this) { listener.invoke() }
}

override fun onMediaModelsCreatedFromOptimizedUris(oldUriToMediaFiles: Map<Uri, MediaModel>) {
// no op - we're not doing any special handling while composing, only when saving in the UploadBridge
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ import org.wordpress.android.ui.stories.media.StoryEditorMedia.AddMediaToStoryPo
import org.wordpress.android.ui.stories.media.StoryEditorMedia.AddMediaToStoryPostUiState.AddingMultipleMediaToStory
import org.wordpress.android.ui.stories.media.StoryEditorMedia.AddMediaToStoryPostUiState.AddingSingleMediaToStory
import org.wordpress.android.ui.utils.UiString.UiStringRes
import org.wordpress.android.util.MediaUtilsWrapper
import org.wordpress.android.viewmodel.Event
import javax.inject.Inject
import javax.inject.Named
import kotlin.coroutines.CoroutineContext

class StoryEditorMedia @Inject constructor(
private val mediaUtilsWrapper: MediaUtilsWrapper,
private val addLocalMediaToPostUseCase: AddLocalMediaToPostUseCase,
private val addExistingMediaToPostUseCase: AddExistingMediaToPostUseCase,
@Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher
Expand All @@ -55,20 +53,6 @@ class StoryEditorMedia @Inject constructor(
_uiState.value = AddingMediaToStoryIdle
}

// region Adding new media to a post
fun advertiseImageOptimisationAndAddMedia(uriList: List<Uri>) {
if (mediaUtilsWrapper.shouldAdvertiseImageOptimization()) {
editorMediaListener.advertiseImageOptimization {
addNewMediaItemsToEditorAsync(
uriList,
false
)
}
} else {
addNewMediaItemsToEditorAsync(uriList, false)
}
}

fun addNewMediaItemsToEditorAsync(uriList: List<Uri>, freshlyTaken: Boolean) {
launch {
_uiState.value = if (uriList.size > 1) {
Expand All @@ -90,15 +74,6 @@ class StoryEditorMedia @Inject constructor(
_uiState.value = AddingMediaToStoryIdle
}
}

fun onPhotoPickerMediaChosen(uriList: List<Uri>) {
val onlyVideos = uriList.all { mediaUtilsWrapper.isVideo(it.toString()) }
if (onlyVideos) {
addNewMediaItemsToEditorAsync(uriList, false)
} else {
advertiseImageOptimisationAndAddMedia(uriList)
}
}
// endregion

fun addExistingMediaToEditorAsync(source: AddExistingMediaSource, mediaIdList: List<Long>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ class StoryMediaSaveUploadBridge @Inject constructor(
listener?.onPostUpdatedFromUI(null)
}

override fun advertiseImageOptimization(listener: () -> Unit) {
// no op
}

override fun onMediaModelsCreatedFromOptimizedUris(oldUriToMediaFiles: Map<Uri, MediaModel>) {
// in order to support Story editing capabilities, we save a serialized version of the Story slides
// after their composedFrameFiles have been processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class MediaUtilsWrapper @Inject constructor(private val appContext: Context) {
fun copyFileToAppStorage(imageUri: Uri, headers: Map<String, String>? = null): Uri? =
MediaUtils.downloadExternalMedia(appContext, imageUri, headers)

fun shouldAdvertiseImageOptimization(): Boolean =
WPMediaUtils.shouldAdvertiseImageOptimization(appContext)

fun getMimeType(uri: Uri): String? = appContext.contentResolver.getType(uri)

fun getVideoThumbnail(videoPath: String, headers: Map<String, String>): String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.ViewConfiguration;
Expand All @@ -19,7 +17,6 @@
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
Expand All @@ -46,7 +43,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class WPMediaUtils {
Expand Down Expand Up @@ -104,75 +100,6 @@ public static boolean isVideoOptimizationEnabled() {
return AppPrefs.isVideoOptimize();
}

/**
* Check if we should advertise image optimization feature for the current site.
* <p>
* The following condition need to be all true:
* 1) Image optimization is ON on the site.
* 2) Didn't already ask to keep or disable the feature.
* 3) The user has granted storage access to the app.
* This is because we don't want to ask so much things to users the first time they try to add a picture to the app.
*
* @param context The context
* @return true if we should advertise the feature, false otherwise.
*/
public static boolean shouldAdvertiseImageOptimization(final Context context) {
boolean isPromoRequired = AppPrefs.isImageOptimizePromoRequired();
if (!isPromoRequired) {
return false;
}

// Check we can access storage before asking for optimizing image
boolean hasStoreAccess = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
|| ContextCompat.checkSelfPermission(context,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (!hasStoreAccess) {
return false;
}

// Check whether image optimization is enabled for the site
return AppPrefs.isImageOptimize();
}

public interface OnAdvertiseImageOptimizationListener {
void done();
}

public static void advertiseImageOptimization(final Context context,
final OnAdvertiseImageOptimizationListener listener) {
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String propertyValue = (which == DialogInterface.BUTTON_POSITIVE) ? "on" : "off";
AnalyticsTracker.track(AnalyticsTracker.Stat.APP_SETTINGS_OPTIMIZE_IMAGES_POPUP_TAPPED,
Collections.singletonMap("option", propertyValue));

if (which == DialogInterface.BUTTON_NEGATIVE && AppPrefs.isImageOptimize()) {
AppPrefs.setImageOptimize(false);
}

listener.done();
}
};

DialogInterface.OnCancelListener onCancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
listener.done();
}
};

AlertDialog.Builder builder = new MaterialAlertDialogBuilder(context);
builder.setTitle(R.string.image_optimization_popup_title);
builder.setMessage(R.string.image_optimization_popup_desc);
builder.setPositiveButton(R.string.leave_on, onClickListener);
builder.setNegativeButton(R.string.turn_off, onClickListener);
builder.setOnCancelListener(onCancelListener);
builder.show();
// Do not ask again
AppPrefs.setImageOptimizePromoRequired(false);
}

/**
* Given a media error returns the error message to display on the UI.
*
Expand Down
Loading

0 comments on commit a96ae9d

Please sign in to comment.