1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

convert some more classes to kotlin

This commit is contained in:
Austin Huang 2021-05-22 12:35:38 -04:00
parent 1a7d5082ba
commit 974a846185
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
3 changed files with 100 additions and 216 deletions

View File

@ -1,47 +1,52 @@
package awais.instagrabber.utils; package awais.instagrabber.utils
import android.util.Base64; import android.util.Base64
import java.security.GeneralSecurityException
import java.security.InvalidAlgorithmParameterException
import java.security.InvalidKeyException
import java.security.NoSuchAlgorithmException
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import androidx.annotation.NonNull; object PasswordUtils {
private const val cipherAlgo = "AES"
import java.security.GeneralSecurityException; private const val cipherTran = "AES/CBC/PKCS5Padding"
import java.security.InvalidAlgorithmParameterException; @JvmStatic
import java.security.InvalidKeyException; @Throws(Exception::class)
import java.security.NoSuchAlgorithmException; fun dec(encrypted: String?, keyValue: ByteArray?): ByteArray {
return try {
import javax.crypto.BadPaddingException; val cipher = Cipher.getInstance(cipherTran)
import javax.crypto.Cipher; val secretKey = SecretKeySpec(keyValue, cipherAlgo)
import javax.crypto.IllegalBlockSizeException; cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16)))
import javax.crypto.NoSuchPaddingException; cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT or Base64.NO_PADDING or Base64.NO_WRAP))
import javax.crypto.spec.IvParameterSpec; } catch (e: NoSuchAlgorithmException) {
import javax.crypto.spec.SecretKeySpec; throw IncorrectPasswordException(e)
} catch (e: NoSuchPaddingException) {
public final class PasswordUtils { throw IncorrectPasswordException(e)
private static final String cipherAlgo = "AES"; } catch (e: InvalidAlgorithmParameterException) {
private static final String cipherTran = "AES/CBC/PKCS5Padding"; throw IncorrectPasswordException(e)
} catch (e: InvalidKeyException) {
public static byte[] dec(final String encrypted, final byte[] keyValue) throws Exception { throw IncorrectPasswordException(e)
try { } catch (e: BadPaddingException) {
final Cipher cipher = Cipher.getInstance(cipherTran); throw IncorrectPasswordException(e)
final SecretKeySpec secretKey = new SecretKeySpec(keyValue, cipherAlgo); } catch (e: IllegalBlockSizeException) {
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[16])); throw IncorrectPasswordException(e)
return cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT | Base64.NO_PADDING | Base64.NO_WRAP));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
throw new IncorrectPasswordException(e);
} }
} }
public static byte[] enc(@NonNull final String str, final byte[] keyValue) throws Exception { @JvmStatic
final Cipher cipher = Cipher.getInstance(cipherTran); @Throws(Exception::class)
final SecretKeySpec secretKey = new SecretKeySpec(keyValue, cipherAlgo); fun enc(str: String, keyValue: ByteArray?): ByteArray {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(new byte[16])); val cipher = Cipher.getInstance(cipherTran)
final byte[] bytes = cipher.doFinal(str.getBytes()); val secretKey = SecretKeySpec(keyValue, cipherAlgo)
return Base64.encode(bytes, Base64.DEFAULT | Base64.NO_PADDING | Base64.NO_WRAP); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16)))
val bytes = cipher.doFinal(str.toByteArray())
return Base64.encode(bytes, Base64.DEFAULT or Base64.NO_PADDING or Base64.NO_WRAP)
} }
public static class IncorrectPasswordException extends Exception { class IncorrectPasswordException(e: GeneralSecurityException?) : Exception(e)
public IncorrectPasswordException(final GeneralSecurityException e) {
super(e);
}
}
} }

View File

@ -1,122 +1,75 @@
package awais.instagrabber.utils; package awais.instagrabber.utils
import android.content.Context; import android.content.Context
import android.text.SpannableString; import android.text.format.DateFormat
import android.text.format.DateFormat; import android.text.format.DateUtils
import android.text.format.DateUtils; import android.util.Patterns
import android.text.style.URLSpan; import java.util.*
import android.util.Patterns;
import androidx.annotation.NonNull; object TextUtils {
@JvmStatic
import java.util.ArrayList; fun isEmpty(charSequence: CharSequence?): Boolean {
import java.util.Arrays; if (charSequence == null || charSequence.length < 1) return true
import java.util.Collections; if (charSequence is String) {
import java.util.Date; var str = charSequence
import java.util.List; if ("" == str || "null" == str || str.isEmpty()) return true
import java.util.Locale; str = str.trim { it <= ' ' }
import java.util.regex.Matcher; return "" == str || "null" == str || str.isEmpty()
import java.util.stream.Collectors;
public final class TextUtils {
// extracted from String class
public static int indexOfChar(@NonNull final CharSequence sequence, final int ch, final int startIndex) {
final int max = sequence.length();
if (startIndex < max) {
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
for (int i = startIndex; i < max; i++) if (sequence.charAt(i) == ch) return i;
} else if (Character.isValidCodePoint(ch)) {
final char hi = (char) ((ch >>> 10) + (Character.MIN_HIGH_SURROGATE - (Character.MIN_SUPPLEMENTARY_CODE_POINT >>> 10)));
final char lo = (char) ((ch & 0x3ff) + Character.MIN_LOW_SURROGATE);
for (int i = startIndex; i < max; i++)
if (sequence.charAt(i) == hi && sequence.charAt(i + 1) == lo) return i;
} }
} return "null".contentEquals(charSequence) || "".contentEquals(charSequence)
return -1;
} }
public static CharSequence getSpannableUrl(final String url) { @JvmStatic
if (isEmpty(url)) return url; @JvmOverloads
final int httpIndex = url.indexOf("http:"); fun millisToTimeString(millis: Long, includeHoursAlways: Boolean = false): String {
final int httpsIndex = url.indexOf("https:"); val sec = (millis / 1000).toInt() % 60
if (httpIndex == -1 && httpsIndex == -1) return url; var min = (millis / (1000 * 60)).toInt()
final int length = url.length();
final int startIndex = httpIndex != -1 ? httpIndex : httpsIndex;
final int spaceIndex = url.indexOf(' ', startIndex + 1);
final int endIndex = (spaceIndex != -1 ? spaceIndex : length);
final String extractUrl = url.substring(startIndex, Math.min(length, endIndex));
final SpannableString spannableString = new SpannableString(url);
spannableString.setSpan(new URLSpan(extractUrl), startIndex, endIndex, 0);
return spannableString;
}
public static boolean isEmpty(final CharSequence charSequence) {
if (charSequence == null || charSequence.length() < 1) return true;
if (charSequence instanceof String) {
String str = (String) charSequence;
if ("".equals(str) || "null".equals(str) || str.isEmpty()) return true;
str = str.trim();
return "".equals(str) || "null".equals(str) || str.isEmpty();
}
return "null".contentEquals(charSequence) || "".contentEquals(charSequence);
}
public static String millisToTimeString(final long millis) {
return millisToTimeString(millis, false);
}
public static String millisToTimeString(final long millis, final boolean includeHoursAlways) {
final int sec = (int) (millis / 1000) % 60;
int min = (int) (millis / (1000 * 60));
if (min >= 60) { if (min >= 60) {
min = (int) ((millis / (1000 * 60)) % 60); min = (millis / (1000 * 60) % 60).toInt()
final int hr = (int) ((millis / (1000 * 60 * 60)) % 24); val hr = (millis / (1000 * 60 * 60) % 24).toInt()
return String.format(Locale.ENGLISH, "%02d:%02d:%02d", hr, min, sec); return String.format(Locale.ENGLISH, "%02d:%02d:%02d", hr, min, sec)
} }
if (includeHoursAlways) { return if (includeHoursAlways) {
return String.format(Locale.ENGLISH, "%02d:%02d:%02d", 0, min, sec); String.format(Locale.ENGLISH, "%02d:%02d:%02d", 0, min, sec)
} } else String.format(Locale.ENGLISH, "%02d:%02d", min, sec)
return String.format(Locale.ENGLISH, "%02d:%02d", min, sec);
} }
public static String getRelativeDateTimeString(final Context context, final long from) { @JvmStatic
final Date now = new Date(); fun getRelativeDateTimeString(context: Context?, from: Long): String {
final Date then = new Date(from); val now = Date()
int days = daysBetween(from, now.getTime()); val then = Date(from)
if (days == 0) { val days = daysBetween(from, now.time)
return DateFormat.getTimeFormat(context).format(then); return if (days == 0) {
} DateFormat.getTimeFormat(context).format(then)
return DateFormat.getDateFormat(context).format(then); } else DateFormat.getDateFormat(context).format(then)
} }
private static int daysBetween(long d1, long d2) { private fun daysBetween(d1: Long, d2: Long): Int {
return (int) ((d2 - d1) / DateUtils.DAY_IN_MILLIS); return ((d2 - d1) / DateUtils.DAY_IN_MILLIS).toInt()
} }
public static List<String> extractUrls(final String text) { @JvmStatic
if (isEmpty(text)) return Collections.emptyList(); fun extractUrls(text: String?): List<String> {
final Matcher matcher = Patterns.WEB_URL.matcher(text); if (isEmpty(text)) return emptyList()
final List<String> urls = new ArrayList<>(); val matcher = Patterns.WEB_URL.matcher(text)
val urls: MutableList<String> = ArrayList()
while (matcher.find()) { while (matcher.find()) {
urls.add(matcher.group()); urls.add(matcher.group())
} }
return urls; return urls
} }
// https://github.com/notslang/instagram-id-to-url-segment // https://github.com/notslang/instagram-id-to-url-segment
public static long shortcodeToId(final String shortcode) { @JvmStatic
long result = 0L; fun shortcodeToId(shortcode: String): Long {
for (int i = 0; i < shortcode.length() && i < 11; i++){ var result = 0L
final char c = shortcode.charAt(i); var i = 0
final int k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(c); while (i < shortcode.length && i < 11) {
result = result * 64 + k; val c = shortcode[i]
val k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(c)
result = result * 64 + k
i++
} }
return result; return result
} }
} }

View File

@ -1,74 +0,0 @@
package awais.instagrabber.utils;
import androidx.annotation.NonNull;
import java.io.CharArrayWriter;
import java.util.BitSet;
// same as java.net.URLEncoder
public final class UrlEncoder {
private static final BitSet dontNeedEncoding = new BitSet(256);
private static final int caseDiff = ('a' - 'A');
static {
int i;
for (i = 'a'; i <= 'z'; i++) dontNeedEncoding.set(i);
for (i = 'A'; i <= 'Z'; i++) dontNeedEncoding.set(i);
for (i = '0'; i <= '9'; i++) dontNeedEncoding.set(i);
dontNeedEncoding.set(' ');
dontNeedEncoding.set('-');
dontNeedEncoding.set('_');
dontNeedEncoding.set('.');
dontNeedEncoding.set('*');
}
@NonNull
public static String encodeUrl(@NonNull final String s) {
final StringBuilder out = new StringBuilder(s.length());
final CharArrayWriter charArrayWriter = new CharArrayWriter();
boolean needToChange = false;
for (int i = 0; i < s.length(); ) {
int c = s.charAt(i);
if (dontNeedEncoding.get(c)) {
if (c == ' ') {
c = '+';
needToChange = true;
}
out.append((char) c);
i++;
} else {
do {
charArrayWriter.write(c);
if (c >= 0xD800 && c <= 0xDBFF && i + 1 < s.length()) {
final int d = s.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
charArrayWriter.write(d);
i++;
}
}
i++;
} while (i < s.length() && !dontNeedEncoding.get(c = s.charAt(i)));
charArrayWriter.flush();
final byte[] ba = charArrayWriter.toString().getBytes();
for (final byte b : ba) {
out.append('%');
char ch = Character.forDigit((b >> 4) & 0xF, 16);
if (Character.isLetter(ch)) ch -= caseDiff;
out.append(ch);
ch = Character.forDigit(b & 0xF, 16);
if (Character.isLetter(ch)) ch -= caseDiff;
out.append(ch);
}
charArrayWriter.reset();
needToChange = true;
}
}
return (needToChange ? out.toString() : s);
}
}