aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt
blob: b2bb1d117731dfb63b93ac0fb269df3e0bc43883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.settings

import android.content.Context
import ca.allanwang.kau.kpref.activity.KPrefAdapterBuilder
import ca.allanwang.kau.utils.launchMain
import ca.allanwang.kau.utils.materialDialog
import ca.allanwang.kau.utils.startActivityForResult
import ca.allanwang.kau.utils.string
import ca.allanwang.kau.utils.toast
import ca.allanwang.kau.utils.withMainContext
import com.afollestad.materialdialogs.callbacks.onDismiss
import com.afollestad.materialdialogs.list.listItems
import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.DebugActivity
import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.activities.SettingsActivity.Companion.ACTIVITY_REQUEST_DEBUG
import com.pitchedapps.frost.debugger.OfflineWebsite
import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.parsers.FrostParser
import com.pitchedapps.frost.facebook.parsers.MessageParser
import com.pitchedapps.frost.facebook.parsers.NotifParser
import com.pitchedapps.frost.facebook.parsers.SearchParser
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.frostUriFromFile
import com.pitchedapps.frost.utils.sendFrostEmail
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import java.io.File

/**
 * Created by Allan Wang on 2017-06-30.
 *
 * A sub pref section that is enabled through a hidden preference
 * Each category will load a page, extract the contents, remove private info, and create a report
 */
fun SettingsActivity.getDebugPrefs(): KPrefAdapterBuilder.() -> Unit = {

    plainText(R.string.disclaimer) {
        descRes = R.string.debug_disclaimer_info
    }

    plainText(R.string.debug_web) {
        descRes = R.string.debug_web_desc
        onClick =
            { this@getDebugPrefs.startActivityForResult<DebugActivity>(ACTIVITY_REQUEST_DEBUG) }
    }

    plainText(R.string.debug_parsers) {
        descRes = R.string.debug_parsers_desc
        onClick = {

            val parsers = arrayOf(NotifParser, MessageParser, SearchParser)

            materialDialog {
                listItems(items = parsers.map { string(it.nameRes) }) { dialog, position, _ ->
                    dialog.dismiss()
                    val parser = parsers[position]
                    var attempt: Job? = null
                    val loading = materialDialog {
                        message(parser.nameRes)
                        // TODO change dialog? No more progress view
                        negativeButton(R.string.kau_cancel) {
                            attempt?.cancel()
                            it.dismiss()
                        }
                        cancelOnTouchOutside(false)
                    }

                    attempt = launch(Dispatchers.IO) {
                        try {
                            val data = parser.parse(fbCookie.webCookie)
                            withMainContext {
                                loading.dismiss()
                                createEmail(parser, data?.data, prefs)
                            }
                        } catch (e: Exception) {
                            createEmail(parser, "Error: ${e.message}", prefs)
                        }
                    }
                }
            }
        }
    }
}

private fun Context.createEmail(parser: FrostParser<*>, content: Any?, prefs: Prefs) =
    sendFrostEmail(
        "${string(R.string.debug_report)}: ${parser::class.java.simpleName}",
        prefs = prefs
    ) {
        addItem("Url", parser.url)
        addItem("Contents", "$content")
    }

private const val ZIP_NAME = "debug"

fun SettingsActivity.sendDebug(url: String, html: String?) {

    val downloader = OfflineWebsite(
        url,
        cookie = fbCookie.webCookie ?: "",
        baseUrl = FB_URL_BASE,
        html = html,
        baseDir = DebugActivity.baseDir(this)
    )

    val job = Job()

    val md = materialDialog {
        title(R.string.parsing_data)
        // TODO remove dialog? No progress ui
        negativeButton(R.string.kau_cancel) { it.dismiss() }
        cancelOnTouchOutside(false)
        onDismiss { job.cancel() }
    }

    val progressChannel = Channel<Int>(10)

    launchMain {
        for (p in progressChannel) {
//            md.setProgress(p)
        }
    }

    launchMain {
        val success = downloader.loadAndZip(ZIP_NAME) {
            progressChannel.offer(it)
        }
        md.dismiss()
        progressChannel.close()
        if (success) {
            val zipUri = frostUriFromFile(
                File(downloader.baseDir, "$ZIP_NAME.zip")
            )
            L.i { "Sending debug zip with uri $zipUri" }
            sendFrostEmail(R.string.debug_report_email_title, prefs = prefs) {
                addItem("Url", url)
                addAttachment(zipUri)
                extras = {
                    type = "application/zip"
                }
            }
        } else {
            toast(R.string.error_generic)
        }
    }
}