Improve name shortening to be smarter (Close #31)

pull/40/head
Fox2Code 2 years ago
parent 584d8b126a
commit d094e297c2

@ -4,6 +4,7 @@ import android.util.Log;
import com.fox2code.mmm.utils.FastException;
import com.fox2code.mmm.utils.Http;
import com.fox2code.mmm.utils.PropUtils;
import org.json.JSONObject;
@ -29,9 +30,8 @@ public class LocalModuleInfo extends ModuleInfo {
this.updateZipUrl = jsonUpdate.getString("zipUrl");
this.updateChangeLog = jsonUpdate.optString("changelog");
if (this.updateZipUrl.isEmpty()) throw FastException.INSTANCE;
if (this.updateVersion == null || this.updateVersion.trim().isEmpty()) {
this.updateVersion = "v" + this.updateVersionCode;
}
this.updateVersion = PropUtils.shortenVersionName(
this.updateVersion.trim(), this.updateVersionCode);
} catch (Exception e) {
this.updateVersion = null;
this.updateVersionCode = Long.MIN_VALUE;

@ -230,9 +230,11 @@ public class PropUtils {
if (!readName || isInvalidValue(moduleInfo.name)) {
moduleInfo.name = makeNameFromId(moduleInfo.id);
}
// We can't accept too long version names for usability reason.
if (!readVersion || moduleInfo.version.length() > 16) {
if (!readVersion) {
moduleInfo.version = "v" + moduleInfo.versionCode;
} else {
moduleInfo.version = shortenVersionName(
moduleInfo.version, moduleInfo.versionCode);
}
if (!readDescription || isInvalidValue(moduleInfo.description)) {
moduleInfo.description = "";
@ -292,4 +294,15 @@ public class PropUtils {
return moduleId.substring(0, 1).toUpperCase(Locale.ROOT) +
moduleId.substring(1).replace('_', ' ');
}
// Make versionName no longer than 16 charters to avoid UI overflow.
public static String shortenVersionName(String versionName, long versionCode) {
if (versionName == null) return "v" + versionCode;
if (versionName.length() <= 16) return versionName;
int i = versionName.lastIndexOf('.');
if (i != -1 && i <= 16 && versionName.indexOf('.') != i
&& versionName.indexOf(' ') == -1)
return versionName.substring(0, i);
return "v" + versionCode;
}
}

Loading…
Cancel
Save