Skip to content

Commit

Permalink
Replace anko with room in downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
hazuki0x0 committed Feb 20, 2021
1 parent d2c8722 commit bb4b67a
Show file tree
Hide file tree
Showing 25 changed files with 498 additions and 620 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/jp/hazuki/yuzubrowser/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import jp.hazuki.yuzubrowser.adblock.AdBlockModule
import jp.hazuki.yuzubrowser.adblock.AdBlockUiModule
import jp.hazuki.yuzubrowser.bookmark.BookmarkUiModule
import jp.hazuki.yuzubrowser.browser.di.ActivityModule
import jp.hazuki.yuzubrowser.download.DownloadAndroidModule
import jp.hazuki.yuzubrowser.download.DownloadModule
import jp.hazuki.yuzubrowser.history.di.HistoryModule
import jp.hazuki.yuzubrowser.legacy.di.LegacyUiModule
Expand All @@ -44,6 +45,7 @@ import javax.inject.Singleton
WebEncodeModule::class,
UserAgentModule::class,
DownloadModule::class,
DownloadAndroidModule::class,
AdBlockModule::class,
AdBlockUiModule::class,
BookmarkUiModule::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import jp.hazuki.yuzubrowser.core.utility.log.ErrorReport
import jp.hazuki.yuzubrowser.core.utility.log.Logger
import jp.hazuki.yuzubrowser.core.utility.utils.ui
import jp.hazuki.yuzubrowser.download.core.data.DownloadFile
import jp.hazuki.yuzubrowser.download.repository.DownloadsDao
import jp.hazuki.yuzubrowser.download.ui.FastDownloadActivity
import jp.hazuki.yuzubrowser.download.ui.fragment.SaveWebArchiveDialog
import jp.hazuki.yuzubrowser.favicon.FaviconManager
Expand Down Expand Up @@ -223,6 +224,9 @@ class BrowserActivity : BrowserBaseActivity(), BrowserController, FinishAlertDia
@Inject
internal lateinit var okHttp: OkHttpClient

@Inject
internal lateinit var downloadsDao: DownloadsDao

private lateinit var binding: BrowserActivityBinding

@SuppressLint("ClickableViewAccessibility")
Expand Down Expand Up @@ -1425,7 +1429,7 @@ class BrowserActivity : BrowserBaseActivity(), BrowserController, FinishAlertDia
}

override fun onSaveWebViewToFile(root: DocumentFile, file: DownloadFile, webViewNo: Int) {
tabManagerIn[webViewNo].mWebView.saveArchive(root, file)
tabManagerIn[webViewNo].mWebView.saveArchive(downloadsDao, root, file)
}

override fun startActivity(intent: Intent, @RequestCause cause: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package jp.hazuki.yuzubrowser.legacy.settings.data;

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;

import com.squareup.moshi.Moshi;
Expand All @@ -31,8 +29,6 @@
import jp.hazuki.yuzubrowser.core.utility.extensions.ContextExtensionsKt;
import jp.hazuki.yuzubrowser.core.utility.log.Logger;
import jp.hazuki.yuzubrowser.core.utility.storage.DocumentFileKt;
import jp.hazuki.yuzubrowser.download.compatible.DownloadInfoDatabase;
import jp.hazuki.yuzubrowser.download.service.DownloadDatabase;
import jp.hazuki.yuzubrowser.legacy.R;
import jp.hazuki.yuzubrowser.legacy.action.ActionList;
import jp.hazuki.yuzubrowser.legacy.action.SingleAction;
Expand All @@ -48,7 +44,6 @@
import jp.hazuki.yuzubrowser.legacy.toolbar.ToolbarManager;
import jp.hazuki.yuzubrowser.legacy.useragent.UserAgentList;
import jp.hazuki.yuzubrowser.legacy.useragent.UserAgentUpdaterKt;
import jp.hazuki.yuzubrowser.legacy.utils.converter.PatternUrlConverter;
import jp.hazuki.yuzubrowser.legacy.webencode.WebTextEncode;
import jp.hazuki.yuzubrowser.legacy.webencode.WebTextEncodeList;
import jp.hazuki.yuzubrowser.search.model.provider.SearchSuggestProviders;
Expand Down Expand Up @@ -229,60 +224,6 @@ public static void settingInitialValue(Context context, SharedPreferences shared
if (lastLaunch >= 0 && (lastLaunch < 410010 || prefVersionCode < PREF_VERSION)) {
//version up code
if (lastLaunch < 410010) {

if (lastLaunch < 300000) {
PatternUrlConverter converter = new PatternUrlConverter();
converter.convert(context);
}

if (lastLaunch <= 300100 && "NORMAL>".equals(AppPrefs.layout_algorithm.get())) {
AppPrefs.layout_algorithm.set("NORMAL");
}

if (lastLaunch <= 300103 && "SINGLE_COLUMN>".equals(AppPrefs.layout_algorithm.get())) {
AppPrefs.layout_algorithm.set("SINGLE_COLUMN");
}

if (lastLaunch < 300200) {
SearchUrlManager urlManager = new SearchUrlManager(context, moshi);
String[] urls = context.getResources().getStringArray(R.array.default_search_url);
String[] titles = context.getResources().getStringArray(R.array.default_search_url_name);
int[] colors = context.getResources().getIntArray(R.array.default_search_url_color);
SearchSuggestProviders providers = new SearchSuggestProviders(urlManager.load());

for (int i = 0; i < urls.length; i++) {
providers.add(new SearchUrl(titles[i], urls[i], colors[i]));
}

if (!"http://www.google.com/m?q=%s".equals(AppPrefs.search_url.get())) {
providers.add(new SearchUrl("Custom", AppPrefs.search_url.get(), 0));
providers.setSelectedId(providers.get(providers.getSize() - 1).getId());
}

urlManager.save(providers.toSettings());
AppPrefs.search_url.set(providers.get(providers.getSelectedIndex()).getUrl());

AppPrefs.fullscreen_hide_mode.set(shared_preference.getBoolean("fullscreen_hide_nav", false) ? 2 : 0);

shared_preference.edit().remove("fullscreen_hide_nav").apply();
}

if (lastLaunch < 300303 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.deleteNotificationChannel("jp.hazuki.yuzubrowser.channel.dl.notify");
}

if (lastLaunch < 400100) {
DownloadInfoDatabase from = new DownloadInfoDatabase(context);
DownloadDatabase to = DownloadDatabase.Companion.getInstance(context);

to.convert(from.getConvertData());

from.deleteDatabase(context);
AppPrefs.download_folder.set("file://" + AppPrefs.download_folder.get());
}

AdBlockInitSupportKt.initAbpFilter(context, abpDatabase);
AdBlockInitSupportKt.disableYuzuList(abpDatabase);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import jp.hazuki.yuzubrowser.download.core.data.DownloadFile
import jp.hazuki.yuzubrowser.download.core.data.DownloadFileInfo
import jp.hazuki.yuzubrowser.download.core.data.MetaData
import jp.hazuki.yuzubrowser.download.createFileOpenIntent
import jp.hazuki.yuzubrowser.download.service.DownloadDatabase
import jp.hazuki.yuzubrowser.download.repository.DownloadsDao
import jp.hazuki.yuzubrowser.legacy.R
import jp.hazuki.yuzubrowser.ui.widget.toast
import jp.hazuki.yuzubrowser.webview.CustomWebView
Expand All @@ -44,19 +44,19 @@ import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException

fun CustomWebView.saveArchive(root: DocumentFile, file: DownloadFile) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val outFile = root.uri.getWritableFileOrNull()
fun CustomWebView.saveArchive(downloadsDao: DownloadsDao, root: DocumentFile, file: DownloadFile) {
ui {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val outFile = root.uri.getWritableFileOrNull()

if (outFile != null && outFile.exists()) {
val downloadedFile = File(outFile, file.name!!)
saveWebArchiveMethod(downloadedFile.toString())
onDownload(webView.context, root, file, DocumentFile.fromFile(downloadedFile), true, downloadedFile.length())
return
if (outFile != null && outFile.exists()) {
val downloadedFile = File(outFile, file.name!!)
saveWebArchiveMethod(downloadedFile.toString())
onDownload(webView.context, downloadsDao, root, file, DocumentFile.fromFile(downloadedFile), true, downloadedFile.length())
return@ui
}
}
}

ui {
val context = webView.context
val tmpFile = File(context.cacheDir, "page.tmp")
saveWebArchiveMethod(tmpFile.absolutePath)
Expand All @@ -77,7 +77,7 @@ fun CustomWebView.saveArchive(root: DocumentFile, file: DownloadFile) {
val name = file.name!!

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && root.uri.scheme == "file") {
context.copyArchive(tmpFile, name, file)
context.copyArchive(downloadsDao, tmpFile, name, file)
return@ui
}
val saveTo = root.createFile(MIME_TYPE_MHTML, name)
Expand All @@ -97,12 +97,12 @@ fun CustomWebView.saveArchive(root: DocumentFile, file: DownloadFile) {

success = saveTo.exists()

onDownload(context, root, file, saveTo, success, size)
onDownload(context, downloadsDao, root, file, saveTo, success, size)
}
}
}

private fun Context.copyArchive(tmpFile: File, name: String, file: DownloadFile) {
private suspend fun Context.copyArchive(downloadsDao: DownloadsDao, tmpFile: File, name: String, file: DownloadFile) {
val values = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME, name)
put(MediaStore.Downloads.MIME_TYPE, getMimeType(name))
Expand All @@ -118,50 +118,70 @@ private fun Context.copyArchive(tmpFile: File, name: String, file: DownloadFile)
return
}

try {
contentResolver.openOutputStream(uri)?.use { os ->
tmpFile.inputStream().use {
it.copyTo(os)
values.apply {
clear()
put(MediaStore.Downloads.IS_PENDING, 0)
val result = withContext(Dispatchers.IO) {
try {
contentResolver.openOutputStream(uri)?.use { os ->
tmpFile.inputStream().use {
it.copyTo(os)
values.apply {
clear()
put(MediaStore.Downloads.IS_PENDING, 0)
}
val dFile = DocumentFile.fromSingleUri(this@copyArchive, uri)!!
val size = dFile.length()
val info = DownloadFileInfo(uri, file, MetaData(name, MIME_TYPE_MHTML, size, false))
info.state = DownloadFileInfo.STATE_DOWNLOADED
downloadsDao.insert(info)
contentResolver.update(uri, values, null, null)
return@withContext true
}
val dFile = DocumentFile.fromSingleUri(this, uri)!!
val size = dFile.length()
val info = DownloadFileInfo(dFile, file, MetaData(name, MIME_TYPE_MHTML, size, false))
info.state = DownloadFileInfo.STATE_DOWNLOADED
DownloadDatabase.getInstance(this).insert(info)
contentResolver.update(uri, values, null, null)
return
}
} catch (e: IOException) {
ErrorReport.printAndWriteLog(e)
} finally {
tmpFile.delete()
}
} catch (e: IOException) {
ErrorReport.printAndWriteLog(e)
} finally {
tmpFile.delete()
return@withContext false
}

contentResolver.delete(uri, null, null)
if (!result) {
contentResolver.delete(uri, null, null)
}
}

private fun onDownload(context: Context, root: DocumentFile, file: DownloadFile, downloadedFile: DocumentFile, success: Boolean, size: Long) {
private suspend fun onDownload(
context: Context,
dao: DownloadsDao,
root: DocumentFile,
file: DownloadFile,
downloadedFile: DocumentFile,
success: Boolean,
size: Long
) {
val name = file.name!!

val info = DownloadFileInfo(root, file, MetaData(name, MIME_TYPE_MHTML, size, false))
info.state = if (success) DownloadFileInfo.STATE_DOWNLOADED else DownloadFileInfo.STATE_UNKNOWN_ERROR
DownloadDatabase.getInstance(context).insert(info)
val info = withContext(Dispatchers.IO) {
DownloadFileInfo(root.uri, file, MetaData(name, MIME_TYPE_MHTML, size, false)).also {
it.state = if (success) {
DownloadFileInfo.STATE_DOWNLOADED
} else {
DownloadFileInfo.STATE_UNKNOWN_ERROR
}
it.id = dao.insert(it)
}
}

if (success) {
context.toast(context.getString(R.string.saved_file) + name)

val notify = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_DOWNLOAD_NOTIFY)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(name)
.setContentText(context.getText(R.string.download_success))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(PendingIntent.getActivity(context.applicationContext, 0, info.createFileOpenIntent(context, downloadedFile), 0))
.build()
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(name)
.setContentText(context.getText(R.string.download_success))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(PendingIntent.getActivity(context.applicationContext, 0, info.createFileOpenIntent(context, downloadedFile), 0))
.build()

val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(info.id.toInt(), notify)
Expand Down
Loading

0 comments on commit bb4b67a

Please sign in to comment.