Skip to content

Commit

Permalink
Fixes #204 by disabling editing on self-hosted installation with WP <…
Browse files Browse the repository at this point in the history
… 3.5.2
  • Loading branch information
daniloercoli committed Oct 24, 2013
1 parent 34dfeaf commit 4453557
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/org/wordpress/android/ui/media/MediaEditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,24 @@ public void onClick(View v) {
}
});

disableEditingOnOldVersion();

restoreState(savedInstanceState);

return mScrollView;
}

private void disableEditingOnOldVersion() {

if( MediaUtils.isWordPressVersionWithMediaEditingCapabilities() )
return;

mSaveButton.setEnabled(false);
mTitleView.setEnabled(false);
mCaptionView.setEnabled(false);
mDescriptionView.setEnabled(false);
}

private void restoreState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(BUNDLE_MEDIA_ID)) {
Expand Down Expand Up @@ -325,6 +338,8 @@ private void refreshViews(Cursor cursor) {
mNetworkImageView.setVisibility(View.GONE);
mLocalImageView.setVisibility(View.GONE);
}

disableEditingOnOldVersion();
}

@Override
Expand All @@ -342,6 +357,9 @@ public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_new_media).setVisible(false);
menu.findItem(R.id.menu_search).setVisible(false);
}

if( ! MediaUtils.isWordPressVersionWithMediaEditingCapabilities() )
menu.findItem(R.id.menu_save_media).setVisible(false);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/org/wordpress/android/ui/media/MediaItemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_new_media).setVisible(false);
menu.findItem(R.id.menu_search).setVisible(false);

if (mIsLocal)
if (mIsLocal || ! MediaUtils.isWordPressVersionWithMediaEditingCapabilities() )
menu.findItem(R.id.menu_edit_media).setVisible(false);
}

Expand Down
28 changes: 28 additions & 0 deletions src/org/wordpress/android/ui/media/MediaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.wordpress.passcodelock.AppLockManager;

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.util.Version;

public class MediaUtils {

Expand Down Expand Up @@ -235,4 +237,30 @@ public static Uri getLastRecordedVideoUri(Activity activity) {
return uri;
}

/**
* This is a workaround for WP3.4.2 that deletes the media from the server when editing media properties within the app.
* See: https://github.com/wordpress-mobile/WordPress-Android/issues/204
* @return
*/
public static boolean isWordPressVersionWithMediaEditingCapabilities() {

if( WordPress.currentBlog.isDotcomFlag())
return true;

Version minVersion;
Version currentVersion;
try {
minVersion = new Version("3.5.2");
currentVersion = new Version(WordPress.currentBlog.getWpVersion());

if( currentVersion.compareTo(minVersion) == -1 )
return false;

} catch (IllegalArgumentException e) {
e.printStackTrace();
}

return true;
}

}
49 changes: 49 additions & 0 deletions src/org/wordpress/android/util/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.wordpress.android.util;

//See: http://stackoverflow.com/a/11024200
public class Version implements Comparable<Version> {

private String version;

public final String get() {
return this.version;
}

public Version(String version) {
if(version == null)
throw new IllegalArgumentException("Version can not be null");
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
throw new IllegalArgumentException("Invalid version format");
this.version = version;
}

@Override public int compareTo(Version that) {
if(that == null)
return 1;
String[] thisParts = this.get().split("\\.");
String[] thatParts = that.get().split("\\.");
int length = Math.max(thisParts.length, thatParts.length);
for(int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ?
Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ?
Integer.parseInt(thatParts[i]) : 0;
if(thisPart < thatPart)
return -1;
if(thisPart > thatPart)
return 1;
}
return 0;
}

@Override public boolean equals(Object that) {
if(this == that)
return true;
if(that == null)
return false;
if(this.getClass() != that.getClass())
return false;
return this.compareTo((Version) that) == 0;
}

}

0 comments on commit 4453557

Please sign in to comment.