Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable image optimization setting by default #19556

1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

23.7
-----
* [**] Enable Optimize Image setting (via Me → App Settings) by default. [https://github.com/wordpress-mobile/WordPress-Android/pull/19556]

23.6
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,25 +499,25 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RequestCodes.PICTURE_LIBRARY:
case RequestCodes.VIDEO_LIBRARY:
case RequestCodes.FILE_LIBRARY:
case RequestCodes.AUDIO_LIBRARY:
handlePickerResult(data, resultCode);
break;
case RequestCodes.FILE_LIBRARY:
if (resultCode == Activity.RESULT_OK && data != null) {
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_URIS));
for (Uri uri : uris) {
getMediaFromDeviceAndTrack(uri, requestCode);
}
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
thomashorta marked this conversation as resolved.
Show resolved Hide resolved
WPMediaUtils.advertiseImageOptimization(this, () -> handlePickerResult(data, resultCode));
} else {
handlePickerResult(data, resultCode);
}
}
break;
case RequestCodes.TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
WPMediaUtils.scanMediaFile(this, mMediaCapturePath);
Uri uri = getOptimizedPictureIfNecessary(Uri.parse(mMediaCapturePath));
mMediaCapturePath = null;
queueFileForUpload(uri, getContentResolver().getType(uri));
trackAddMediaFromDeviceEvents(true, false, uri);
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
WPMediaUtils.advertiseImageOptimization(this, this::addLastTakenPicture);
} else {
addLastTakenPicture();
}
}
break;
case RequestCodes.TAKE_VIDEO:
Expand Down Expand Up @@ -554,6 +554,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

private void addLastTakenPicture() {
WPMediaUtils.scanMediaFile(this, mMediaCapturePath);
Uri uri = getOptimizedPictureIfNecessary(Uri.parse(mMediaCapturePath));
mMediaCapturePath = null;
queueFileForUpload(uri, getContentResolver().getType(uri));
trackAddMediaFromDeviceEvents(true, false, uri);
}

private void handlePickerResult(Intent data, int requestCode) {
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_URIS));
for (Uri uri : uris) {
getMediaFromDeviceAndTrack(uri, requestCode);
}
}
}

private List<Uri> convertStringArrayIntoUrisList(String[] stringArray) {
List<Uri> uris = new ArrayList<>(stringArray.length);
for (String stringUri : stringArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2832,59 +2832,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// handleMediaPickerResult -> addExistingMediaToEditorAndSave
break;
case RequestCodes.PHOTO_PICKER:
case RequestCodes.STOCK_MEDIA_PICKER_SINGLE_SELECT:
// user chose a featured image
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_ID)) {
long mediaId = data.getLongExtra(MediaPickerConstants.EXTRA_MEDIA_ID, 0);
setFeaturedImageId(mediaId, true, false);
} else if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS));
int postId = getImmutablePost().getId();
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
postId
);
for (Uri mediaUri : uris) {
String mimeType = getContentResolver().getType(mediaUri);
EnqueueFeaturedImageResult queueImageResult = mFeaturedImageHelper
.queueFeaturedImageForUpload(
postId, getSite(), mediaUri,
mimeType
);
if (queueImageResult == EnqueueFeaturedImageResult.FILE_NOT_FOUND) {
Toast.makeText(
this,
R.string.file_not_found, Toast.LENGTH_SHORT
).show();
} else if (queueImageResult == EnqueueFeaturedImageResult.INVALID_POST_ID) {
Toast.makeText(
this,
R.string.error_generic, Toast.LENGTH_SHORT
).show();
}
}
if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.refreshViews();
}
} else if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_URIS));
mEditorMedia.addNewMediaItemsToEditorAsync(uris, false);
} else if (data.hasExtra(MediaPickerConstants.EXTRA_SAVED_MEDIA_MODEL_LOCAL_IDS)) {
int[] localIds = data.getIntArrayExtra(MediaPickerConstants.EXTRA_SAVED_MEDIA_MODEL_LOCAL_IDS);
int postId = getImmutablePost().getId();
for (int localId : localIds) {
MediaModel media = mMediaStore.getMediaWithLocalId(localId);
if (media != null) {
mFeaturedImageHelper.queueFeaturedImageForUpload(postId, media);
}
}
if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.refreshViews();
}
if (WPMediaUtils.shouldAdvertiseImageOptimization(this)) {
thomashorta marked this conversation as resolved.
Show resolved Hide resolved
WPMediaUtils.advertiseImageOptimization(this, () -> handlePhotoPickerResult(data));
} else {
handlePhotoPickerResult(data);
}
break;
case RequestCodes.STOCK_MEDIA_PICKER_SINGLE_SELECT:
handlePhotoPickerResult(data);
break;
case RequestCodes.STOCK_MEDIA_PICKER_SINGLE_SELECT_FOR_GUTENBERG_BLOCK:
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_ID)) {
// pass array with single item
Expand Down Expand Up @@ -3000,6 +2956,60 @@ private void addLastTakenPicture() {
}
}

private void handlePhotoPickerResult(Intent data) {
// user chose a featured image
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_ID)) {
long mediaId = data.getLongExtra(MediaPickerConstants.EXTRA_MEDIA_ID, 0);
setFeaturedImageId(mediaId, true, false);
} else if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS));
int postId = getImmutablePost().getId();
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
postId
);
for (Uri mediaUri : uris) {
String mimeType = getContentResolver().getType(mediaUri);
EnqueueFeaturedImageResult queueImageResult = mFeaturedImageHelper
.queueFeaturedImageForUpload(
postId, getSite(), mediaUri,
mimeType
);
if (queueImageResult == EnqueueFeaturedImageResult.FILE_NOT_FOUND) {
Toast.makeText(
this,
R.string.file_not_found, Toast.LENGTH_SHORT
).show();
} else if (queueImageResult == EnqueueFeaturedImageResult.INVALID_POST_ID) {
Toast.makeText(
this,
R.string.error_generic, Toast.LENGTH_SHORT
).show();
}
}
if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.refreshViews();
}
} else if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_URIS));
mEditorMedia.addNewMediaItemsToEditorAsync(uris, false);
} else if (data.hasExtra(MediaPickerConstants.EXTRA_SAVED_MEDIA_MODEL_LOCAL_IDS)) {
int[] localIds = data.getIntArrayExtra(MediaPickerConstants.EXTRA_SAVED_MEDIA_MODEL_LOCAL_IDS);
int postId = getImmutablePost().getId();
for (int localId : localIds) {
MediaModel media = mMediaStore.getMediaWithLocalId(localId);
if (media != null) {
mFeaturedImageHelper.queueFeaturedImageForUpload(postId, media);
}
}
if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.refreshViews();
}
}
}

private void handleMediaPickerResult(Intent data) {
// TODO move this to EditorMedia
ArrayList<Long> ids = ListUtils.fromLongArray(data.getLongArrayExtra(MediaBrowserActivity.RESULT_IDS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public static void setPendingDraftsLastNotificationDate(PostModel post, long tim
}

public static boolean isImageOptimize() {
return getBoolean(DeletablePrefKey.IMAGE_OPTIMIZE_ENABLED, false);
return getBoolean(DeletablePrefKey.IMAGE_OPTIMIZE_ENABLED, true);
}

public static void setImageOptimize(boolean optimize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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 @@ -107,8 +108,8 @@ public static boolean isVideoOptimizationEnabled() {
* 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 OFF on the site.
* 2) Didn't already ask to enable the feature.
* 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.
*
Expand All @@ -129,8 +130,8 @@ public static boolean shouldAdvertiseImageOptimization(final Context context) {
return false;
}

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

public interface OnAdvertiseImageOptimizationListener {
Expand All @@ -142,12 +143,12 @@ public static void advertiseImageOptimization(final Context context,
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (AppPrefs.isImageOptimize()) {
// null or image optimization already ON. We should not be here though.
} else {
AppPrefs.setImageOptimize(true);
}
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) {
if (AppPrefs.isImageOptimize()) AppPrefs.setImageOptimize(false);
}

listener.done();
Expand Down
8 changes: 4 additions & 4 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2972,10 +2972,10 @@
<string name="permission_microphone">Microphone</string>

<!-- Image Optimization promo -->
<string name="turn_on">Turn on</string>
<string name="leave_off">Leave off</string>
<string name="image_optimization_promo_title">Turn on image optimization?</string>
<string name="image_optimization_promo_desc">Image optimization shrinks images for quicker uploading.\n\nYou can change this any time in site settings.</string>
<string name="turn_on">Yes, leave on</string>
<string name="leave_off">No, turn off</string>
<string name="image_optimization_promo_title">Keep optimizing images?</string>
<string name="image_optimization_promo_desc">Image optimization shrinks images for faster uploading.\n\This option is enabled by default, but you can change it in the app settings at any time.</string>

<!-- Login -->
<!-- If any of these appear unused, be sure to check the same string in the login library -
Expand Down
1 change: 1 addition & 0 deletions WordPress/src/main/res/xml/app_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@

<org.wordpress.android.ui.prefs.WPSwitchPreference
android:id="@+id/pref_optimize_image"
android:defaultValue="true"
android:key="@string/pref_key_optimize_image"
android:summary="@string/site_settings_optimize_images_summary"
android:title="@string/site_settings_optimize_images" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ public enum Stat {
APP_SETTINGS_VIDEO_OPTIMIZATION_CHANGED,
APP_SETTINGS_MAX_VIDEO_SIZE_CHANGED,
APP_SETTINGS_VIDEO_QUALITY_CHANGED,
APP_SETTINGS_OPTIMIZE_IMAGES_POPUP_TAPPED,
PRIVACY_SETTINGS_OPENED,
PRIVACY_SETTINGS_REPORT_CRASHES_TOGGLED,
SHARING_BUTTONS_EDIT_SHARING_BUTTONS_CHANGED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,8 @@ public static String getEventNameForStat(AnalyticsTracker.Stat stat) {
return "app_settings_max_video_size_changed";
case APP_SETTINGS_VIDEO_QUALITY_CHANGED:
return "app_settings_video_quality_changed";
case APP_SETTINGS_OPTIMIZE_IMAGES_POPUP_TAPPED:
return "app_settings_optimize_images_popup_changed";
case PRIVACY_CHOICES_BANNER_PRESENTED:
return "privacy_choices_banner_presented";
case PRIVACY_CHOICES_BANNER_SETTINGS_BUTTON_TAPPED:
Expand Down
Loading