]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/PrepareSaveDialogCoroutine.kt
Migrate the remaining classes to Kotlin. https://redmine.stoutner.com/issues/989
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / PrepareSaveDialogCoroutine.kt
1 /*
2  * Copyright 2020-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser Android is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.coroutines
21
22 import android.content.Context
23
24 import androidx.fragment.app.DialogFragment
25 import androidx.fragment.app.FragmentManager
26
27 import com.stoutner.privacybrowser.R
28 import com.stoutner.privacybrowser.activities.MainWebViewActivity
29 import com.stoutner.privacybrowser.dataclasses.PendingDialogDataClass
30 import com.stoutner.privacybrowser.dialogs.SaveDialog
31 import com.stoutner.privacybrowser.helpers.UrlHelper
32
33 import kotlinx.coroutines.CoroutineScope
34 import kotlinx.coroutines.Dispatchers
35 import kotlinx.coroutines.launch
36 import kotlinx.coroutines.withContext
37
38 import java.lang.Exception
39
40 object PrepareSaveDialogCoroutine {
41     fun prepareSaveDialog(context: Context, supportFragmentManager: FragmentManager, urlString: String, userAgent: String, cookiesEnabled: Boolean) {
42         // Use a coroutine to prepare the save dialog.
43         CoroutineScope(Dispatchers.Main).launch {
44             // Make the network requests on the IO thread.
45             withContext(Dispatchers.IO) {
46                 // Get the file name and size.
47                 val fileNameAndSize = UrlHelper.getNameAndSize(context, urlString, userAgent, cookiesEnabled)
48
49                 // Display the dialog on the main thread.
50                 withContext(Dispatchers.Main) {
51                     // Instantiate the save dialog.
52                     val saveDialogFragment: DialogFragment = SaveDialog.saveUrl(urlString, fileNameAndSize.first, fileNameAndSize.second, userAgent, cookiesEnabled)
53
54                     // Try to show the dialog.  Sometimes the window is not active.
55                     try {
56                         // Show the save dialog.
57                         saveDialogFragment.show(supportFragmentManager, context.getString(R.string.save_dialog))
58                     } catch (exception: Exception) {
59                         // Add the dialog to the pending dialog array list.  It will be displayed in `onStart()`.
60                         MainWebViewActivity.pendingDialogsArrayList.add(PendingDialogDataClass(saveDialogFragment, context.getString(R.string.save_dialog)))
61                     }
62                 }
63             }
64         }
65     }
66 }