Commit 22a7124b authored by Habib Kazemi's avatar Habib Kazemi Committed by Geoffrey Métais

Refactor Strings.kt

I removed the object Strings and convert other functions
to extension functions.
Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent 50dda20b
......@@ -255,7 +255,7 @@ public class ExternalMonitor extends BroadcastReceiver implements LifecycleObser
if (Util.isArrayEmpty(devices))
return false;
for (String dev : devices)
if (device.startsWith(Strings.INSTANCE.removeFileProtocole(dev)))
if (device.startsWith(Strings.removeFileProtocole(dev)))
return true;
return false;
}
......
......@@ -162,7 +162,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
private fun addDeviceIfNeeded(path: String) {
for (devicePath in medialibrary.devices) {
if (path.startsWith(Strings.removeFileProtocole(devicePath))) {
if (path.startsWith(devicePath.removeFileProtocole())) {
exitCommand()
return
}
......@@ -275,7 +275,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
sb.setLength(0)
when {
parsing > 0 -> sb.append(getString(R.string.ml_parse_media)).append(' ').append(parsing).append("%")
currentDiscovery != null -> sb.append(getString(R.string.ml_discovering)).append(' ').append(Uri.decode(Strings.removeFileProtocole(currentDiscovery)))
currentDiscovery != null -> sb.append(getString(R.string.ml_discovering)).append(' ').append(Uri.decode(currentDiscovery?.removeFileProtocole()))
else -> sb.append(getString(R.string.ml_parse_media))
}
val progressText = sb.toString()
......
......@@ -57,7 +57,7 @@ public class VLCApplication extends Application {
public final static String ACTION_MEDIALIBRARY_READY = "VLC/VLCApplication";
private static volatile VLCApplication instance;
public final static String SLEEP_INTENT = Strings.INSTANCE.buildPkgString("SleepIntent");
public final static String SLEEP_INTENT = Strings.buildPkgString("SleepIntent");
public static Calendar sPlayerSleepTime = null;
......
......@@ -250,7 +250,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
return null;
if (((MediaWrapper)mItem).getType() == MediaWrapper.TYPE_VIDEO)
checkSubtitles(itemFile);
mBinding.setSizeValueText(Strings.INSTANCE.readableFileSize(itemFile.length()));
mBinding.setSizeValueText(Strings.readableFileSize(itemFile.length()));
return null;
}
......
......@@ -191,7 +191,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
public override fun getSubTitle(): String? {
if (isRootDirectory) return null
var mrl = Strings.removeFileProtocole(mrl)
var mrl = mrl?.removeFileProtocole() ?: ""
if (!TextUtils.isEmpty(mrl)) {
if (this is FileBrowserFragment && mrl.startsWith(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY))
mrl = getString(R.string.internal_memory) + mrl.substring(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY.length)
......
......@@ -79,7 +79,7 @@ public class FileBrowserFragment extends BaseBrowserFragment {
else {
String title;
if (getCurrentMedia() != null) {
if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, Strings.INSTANCE.removeFileProtocole(getMrl())))
if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, Strings.removeFileProtocole(getMrl())))
title = getString(R.string.internal_memory);
else
title = this instanceof FilePickerFragment ? getCurrentMedia().getUri().toString() : getCurrentMedia().getTitle();
......
......@@ -40,7 +40,7 @@ import org.videolan.vlc.R
import org.videolan.vlc.repository.DirectoryRepository
import org.videolan.vlc.util.AndroidDevices
import org.videolan.vlc.util.FileUtils
import org.videolan.vlc.util.Strings
import org.videolan.vlc.util.removeFileProtocole
import org.videolan.vlc.viewmodels.browser.BrowserModel
import org.videolan.vlc.viewmodels.browser.TYPE_PICKER
......@@ -98,7 +98,7 @@ class FilePickerFragment : FileBrowserFragment() {
fun browseUp() {
when {
isRootDirectory -> requireActivity().finish()
TextUtils.equals(Strings.removeFileProtocole(mrl), AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY) -> {
TextUtils.equals(mrl?.removeFileProtocole(), AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY) -> {
mrl = null
isRootDirectory = true
viewModel.fetch()
......@@ -112,7 +112,7 @@ class FilePickerFragment : FileBrowserFragment() {
override fun defineIsRoot() = mrl?.run {
if (startsWith("file")) {
val path = Strings.removeFileProtocole(this@run)
val path = removeFileProtocole()
val rootDirectories = runBlocking(Dispatchers.IO) { DirectoryRepository.getInstance(requireContext()).getMediaDirectories() }
for (directory in rootDirectories) if (path.startsWith(directory)) return false
return true
......
......@@ -278,7 +278,7 @@ public class AdvOptionsDialog extends DismissDialogFragment implements View.OnCl
UiTools.getResourceFromAttribute(mActivity, R.attr.ic_speed_normal_style),
0, 0);
} else {
mPlaybackSpeed.setText(Strings.INSTANCE.formatRateString(mService.getRate()));
mPlaybackSpeed.setText(Strings.formatRateString(mService.getRate()));
mPlaybackSpeed.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_speed_on, 0, 0);
}
}
......
......@@ -165,7 +165,7 @@ public class PlaybackSpeedDialog extends DismissDialogFragment implements Playba
private void updateInterface() {
float rate = mService.getRate();
mSpeedValue.setText(Strings.INSTANCE.formatRateString(rate));
mSpeedValue.setText(Strings.formatRateString(rate));
if (rate != 1.0f) {
mPlaybackSpeedIcon.setImageResource(R.drawable.ic_speed_reset);
mSpeedValue.setTextColor(getResources().getColor(R.color.orange500));
......
......@@ -51,7 +51,7 @@ public class BitmapCache {
final long cacheSize = Runtime.getRuntime().maxMemory() / 5;
if (BuildConfig.DEBUG)
Log.i(TAG, "LRUCache size set to " + Strings.INSTANCE.readableSize(cacheSize));
Log.i(TAG, "LRUCache size set to " + Strings.readableSize(cacheSize));
mMemCache = new LruCache<String, Bitmap>((int) cacheSize) {
......
......@@ -88,7 +88,7 @@ public class MediaInfoAdapter extends RecyclerView.Adapter<MediaInfoAdapter.View
private void appendCommon(StringBuilder textBuilder, Resources res, Media.Track track) {
if (track.bitrate != 0)
textBuilder.append(res.getString(R.string.track_bitrate_info, Strings.INSTANCE.readableSize(track.bitrate)));
textBuilder.append(res.getString(R.string.track_bitrate_info, Strings.readableSize(track.bitrate)));
textBuilder.append(res.getString(R.string.track_codec_info, track.codec));
if (track.language != null && !track.language.equalsIgnoreCase("und"))
textBuilder.append(res.getString(R.string.track_language_info, track.language));
......
......@@ -149,7 +149,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
private final static String TAG = "VLC/VideoPlayerActivity";
private final static String ACTION_RESULT = Strings.INSTANCE.buildPkgString("player.result");
private final static String ACTION_RESULT = Strings.buildPkgString("player.result");
private final static String EXTRA_POSITION = "extra_position";
private final static String EXTRA_DURATION = "extra_duration";
private final static String EXTRA_URI = "extra_uri";
......
......@@ -128,12 +128,12 @@ public class AndroidDevices {
final String type = tokens.hasMoreTokens() ? tokens.nextToken() : null;
// skip if already in list or if type/mountpoint is blacklisted
if (list.contains(mountpoint) || typeBL.contains(type) || Strings.INSTANCE.startsWith(mountBL, mountpoint))
if (list.contains(mountpoint) || typeBL.contains(type) || Strings.startsWith(mountBL, mountpoint))
continue;
// check that device is in whitelist, and either type or mountpoint is in a whitelist
if (Strings.INSTANCE.startsWith(deviceWL, device) && (typeWL.contains(type) || Strings.INSTANCE.startsWith(mountWL, mountpoint))) {
final int position = Strings.INSTANCE.containsName(list, FileUtils.getFileNameFromPath(mountpoint));
if (Strings.startsWith(deviceWL, device) && (typeWL.contains(type) || Strings.startsWith(mountWL, mountpoint))) {
final int position = Strings.containsName(list, FileUtils.getFileNameFromPath(mountpoint));
if (position > -1)
list.remove(position);
list.add(mountpoint);
......
......@@ -42,7 +42,7 @@ const val ACTIVITY_RESULT_OPEN = 2
const val ACTIVITY_RESULT_SECONDARY = 3
// PlaybackService
@JvmField val ACTION_REMOTE_GENERIC = Strings.buildPkgString("remote.")!!
@JvmField val ACTION_REMOTE_GENERIC = "remote.".buildPkgString()!!
@JvmField val EXTRA_SEARCH_BUNDLE = "${ACTION_REMOTE_GENERIC}extra_search_bundle"
@JvmField val ACTION_PLAY_FROM_SEARCH = "${ACTION_REMOTE_GENERIC}play_from_search"
@JvmField val ACTION_REMOTE_SWITCH_VIDEO = "${ACTION_REMOTE_GENERIC}SwitchToVideo"
......@@ -73,9 +73,9 @@ const val ACTION_RESUME_SCAN = "action_resume_scan"
const val ACTION_PAUSE_SCAN = "action_pause_scan"
// VideoPlayerActivity
@JvmField val PLAY_FROM_VIDEOGRID = Strings.buildPkgString("gui.video.PLAY_FROM_VIDEOGRID")
@JvmField val PLAY_FROM_SERVICE = Strings.buildPkgString("gui.video.PLAY_FROM_SERVICE")
@JvmField val EXIT_PLAYER = Strings.buildPkgString("gui.video.EXIT_PLAYER")
@JvmField val PLAY_FROM_VIDEOGRID = "gui.video.PLAY_FROM_VIDEOGRID".buildPkgString()
@JvmField val PLAY_FROM_SERVICE = "gui.video.PLAY_FROM_SERVICE".buildPkgString()
@JvmField val EXIT_PLAYER = "gui.video.EXIT_PLAYER".buildPkgString()
const val PLAY_EXTRA_ITEM_LOCATION = "item_location"
const val PLAY_EXTRA_SUBTITLES_LOCATION = "subtitles_location"
const val PLAY_EXTRA_ITEM_TITLE = "title"
......
......@@ -222,7 +222,7 @@ public class FileUtils {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static boolean deleteFile (String path){
boolean deleted = false;
path = Uri.decode(Strings.INSTANCE.removeFileProtocole(path));
path = Uri.decode(Strings.removeFileProtocole(path));
//Delete from Android Medialib, for consistency with device MTP storing and other apps listing content:// media
final ContentResolver cr = VLCApplication.getAppContext().getContentResolver();
try {
......
......@@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*/
@file:JvmName("Strings")
package org.videolan.vlc.util
import org.videolan.vlc.BuildConfig
......@@ -25,47 +26,42 @@ import java.text.DecimalFormat
private const val TAG = "VLC/UiTools/Strings"
object Strings {
fun stripTrailingSlash(s: String): String {
return if (s.endsWith("/") && s.length > 1) s.substring(0, s.length - 1) else s
}
//TODO: Remove this after convert the dependent code to kotlin
fun startsWith(array: Array<String>, text: String) = array.any { text.startsWith(it)}
//TODO: Remove this after convert the dependent code to kotlin
fun containsName(list: List<String>, text: String) = list.indexOfLast { it.endsWith(text) }
fun stripTrailingSlash(s: String): String {
return if (s.endsWith("/") && s.length > 1) s.substring(0, s.length - 1) else s
}
/**
* Get the formatted current playback speed in the form of 1.00x
*/
fun formatRateString(rate: Float) = String.format(java.util.Locale.US, "%.2fx", rate)
//TODO: Remove this after convert the dependent code to kotlin
fun startsWith(array: Array<String>, text: String) = array.any { text.startsWith(it)}
fun readableFileSize(size: Long): String {
if (size <= 0) return "0"
val units = arrayOf("B", "KiB", "MiB", "GiB", "TiB")
val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
//TODO: Remove this after convert the dependent code to kotlin
fun containsName(list: List<String>, text: String) = list.indexOfLast { it.endsWith(text) }
fun readableSize(size: Long): String {
if (size <= 0) return "0"
val units = arrayOf("B", "KB", "MB", "GB", "TB")
val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1000.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1000.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
/**
* Get the formatted current playback speed in the form of 1.00x
*/
fun Float.formatRateString() = String.format(java.util.Locale.US, "%.2fx", this)
fun removeFileProtocole(path: String?): String {
if (path == null) return ""
return if (path.startsWith("file://"))
path.substring(7)
else
path
}
fun Long.readableFileSize(): String {
val size: Long = this
if (size <= 0) return "0"
val units = arrayOf("B", "KiB", "MiB", "GiB", "TiB")
val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
fun buildPkgString(string: String) = "${BuildConfig.APPLICATION_ID}.$string"
fun Long.readableSize(): String {
val size: Long = this
if (size <= 0) return "0"
val units = arrayOf("B", "KB", "MB", "GB", "TB")
val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1000.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1000.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
//TODO: Remove this after convert the dependent code to kotlin
fun stringArrayContains(array: Array<String>, string: String) = array.contains(string)
fun String.removeFileProtocole(): String {
return if (this.startsWith("file://"))
this.substring(7)
else
this
}
fun String.buildPkgString() = "${BuildConfig.APPLICATION_ID}.$this"
......@@ -45,11 +45,11 @@ import java.util.Locale;
abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
public static final String TAG = "VLC/VLCAppWidgetProvider";
public static final String ACTION_REMOTE_BACKWARD = Strings.INSTANCE.buildPkgString("remote.Backward");
public static final String ACTION_REMOTE_PLAYPAUSE = Strings.INSTANCE.buildPkgString("remote.PlayPause");
public static final String ACTION_REMOTE_STOP = Strings.INSTANCE.buildPkgString("remote.Stop");
public static final String ACTION_REMOTE_FORWARD = Strings.INSTANCE.buildPkgString("remote.Forward");
public static final String ACTION_WIDGET_PREFIX = Strings.INSTANCE.buildPkgString("widget.");
public static final String ACTION_REMOTE_BACKWARD = Strings.buildPkgString("remote.Backward");
public static final String ACTION_REMOTE_PLAYPAUSE = Strings.buildPkgString("remote.PlayPause");
public static final String ACTION_REMOTE_STOP = Strings.buildPkgString("remote.Stop");
public static final String ACTION_REMOTE_FORWARD = Strings.buildPkgString("remote.Forward");
public static final String ACTION_WIDGET_PREFIX = Strings.buildPkgString("widget.");
public static final String ACTION_WIDGET_INIT = ACTION_WIDGET_PREFIX+"INIT";
public static final String ACTION_WIDGET_UPDATE = ACTION_WIDGET_PREFIX+"UPDATE";
public static final String ACTION_WIDGET_UPDATE_COVER = ACTION_WIDGET_PREFIX+"UPDATE_COVER";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment