-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature|optimize|build] Added GIF support on the reading page; optim…
…ize image download code on the reading page; update dependencies
- Loading branch information
Showing
21 changed files
with
654 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import kotlin.math.max | ||
|
||
/** | ||
* Boyer–Moore string-search algorithm | ||
* | ||
* Returns the index within this string of the first occurrence of the | ||
* specified substring. If it is not a substring, return -1. | ||
* | ||
* There is no Galil because it only generates one match. | ||
* | ||
* @param needle The target string to search | ||
* @return The start index of the substring | ||
*/ | ||
fun <T> Array<T>.indexOf(needle: Array<T>): Int { | ||
if (needle.isEmpty()) { | ||
return 0 | ||
} | ||
val charTable = makeCharTable(needle) | ||
val offsetTable = makeOffsetTable(needle) | ||
var i = needle.size - 1 | ||
var j: Int | ||
while (i < this.size) { | ||
j = needle.size - 1 | ||
while (needle[j] == this[i]) { | ||
if (j == 0) return i | ||
i-- | ||
j-- | ||
} | ||
// i += needle.length - j; // For naive method | ||
i += max(offsetTable[needle.size - 1 - j], charTable[this[i]]!!) | ||
} | ||
return -1 | ||
} | ||
|
||
/** | ||
* Makes the jump table based on the mismatched character information. | ||
* (bad-character rule) | ||
*/ | ||
private fun <T> makeCharTable(needle: Array<T>): HashMap<T, Int> { | ||
val table = hashMapOf<T, Int>() | ||
for (i in needle) { | ||
table[i] = needle.size | ||
} | ||
for (i in needle.indices) { | ||
table[needle[i]] = needle.size - 1 - i | ||
} | ||
return table | ||
} | ||
|
||
/** | ||
* Makes the jump table based on the scan offset which mismatch occurs. | ||
* (good suffix rule) | ||
*/ | ||
private fun <T> makeOffsetTable(needle: Array<T>): IntArray { | ||
val table = IntArray(needle.size) | ||
var lastPrefixPosition = needle.size | ||
for (i in needle.size downTo 1) { | ||
if (isPrefix(needle, i)) { | ||
lastPrefixPosition = i | ||
} | ||
table[needle.size - i] = lastPrefixPosition - i + needle.size | ||
} | ||
for (i in 0 until needle.size - 1) { | ||
val slen = suffixLength(needle, i) | ||
table[slen] = needle.size - 1 - i + slen | ||
} | ||
return table | ||
} | ||
|
||
/** | ||
* Is needle[p:end] a prefix of needle? | ||
*/ | ||
private fun <T> isPrefix(needle: Array<T>, p: Int): Boolean { | ||
var i = p | ||
var j = 0 | ||
while (i < needle.size) { | ||
if (needle[i] != needle[j]) { | ||
return false | ||
} | ||
i++ | ||
j++ | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Returns the maximum length of the substring ends at p and is a suffix. | ||
* (good-suffix rule) | ||
*/ | ||
private fun <T> suffixLength(needle: Array<T>, p: Int): Int { | ||
var len = 0 | ||
var i = p | ||
var j = needle.size - 1 | ||
while (i >= 0 && needle[i] == needle[j]) { | ||
len += 1 | ||
--i | ||
--j | ||
} | ||
return len | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,28 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.drawable.BitmapDrawable | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import android.os.Build | ||
import com.skyd.anivu.config.Const | ||
import com.skyd.anivu.ext.content.saveToGallery | ||
import kotlin.random.Random | ||
|
||
fun BitmapDrawable.saveToGallery( | ||
context: Context, | ||
filename: String = System.currentTimeMillis().toString() + "_" + Random.nextInt(), | ||
): Boolean { | ||
val contentResolver = context.contentResolver | ||
|
||
val values = ContentValues().apply { | ||
put(MediaStore.Images.Media.DISPLAY_NAME, "$filename.png") | ||
put(MediaStore.Images.Media.MIME_TYPE, "image/png") | ||
put( | ||
MediaStore.Images.Media.RELATIVE_PATH, | ||
Environment.DIRECTORY_PICTURES + "/" + context.getAppName() | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
context.contentResolver.saveToGallery( | ||
fileNameWithExt = "$filename.png", | ||
mimetype = "image/png", | ||
output = { outputStream -> | ||
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, outputStream) == true | ||
} | ||
) | ||
} else { | ||
Const.ANIVU_PICTURES_DIR.outputStream().use { outputStream -> | ||
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, outputStream) == true | ||
} | ||
} | ||
|
||
val uri = contentResolver | ||
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) ?: return false | ||
contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
val out = bitmap?.compress(Bitmap.CompressFormat.PNG, 100, outputStream) | ||
return out == true | ||
} ?: return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/skyd/anivu/ext/content/ContentResolverExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.skyd.anivu.ext.content | ||
|
||
import android.content.ContentResolver | ||
import android.content.ContentValues | ||
import android.provider.MediaStore | ||
import java.io.OutputStream | ||
|
||
fun ContentResolver.saveToGallery( | ||
fileNameWithExt: String, | ||
mimetype: String? = null, | ||
output: (OutputStream) -> Boolean, | ||
): Boolean { | ||
val contentValues = ContentValues().gallery(fileNameWithExt, mimetype) | ||
val uri = insert( | ||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | ||
contentValues | ||
) ?: return false | ||
return openOutputStream(uri)?.use(output) ?: false | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/skyd/anivu/ext/content/ContentValuesExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.skyd.anivu.ext.content | ||
|
||
import android.content.ContentValues | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
|
||
fun ContentValues.gallery( | ||
fileNameWithExt: String, | ||
mimetype: String? = null, | ||
) = apply { | ||
put(MediaStore.Images.Media.DISPLAY_NAME, fileNameWithExt) | ||
if (mimetype != null) { | ||
put(MediaStore.Images.Media.MIME_TYPE, mimetype) | ||
} | ||
put( | ||
MediaStore.Images.Media.RELATIVE_PATH, | ||
Environment.DIRECTORY_PICTURES + "/AniVu" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.