aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt
blob: bce3aa486e8daedf52ed00065b94385086718cc2 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * 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.activities

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import ca.allanwang.kau.kpref.activity.CoreAttributeContract
import ca.allanwang.kau.kpref.activity.KPrefActivity
import ca.allanwang.kau.kpref.activity.KPrefAdapterBuilder
import ca.allanwang.kau.ui.views.RippleCanvas
import ca.allanwang.kau.utils.finishSlideOut
import ca.allanwang.kau.utils.setMenuIcons
import ca.allanwang.kau.utils.startActivityForResult
import ca.allanwang.kau.utils.startLink
import ca.allanwang.kau.utils.tint
import ca.allanwang.kau.utils.withSceneTransitionAnimation
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.db.NotificationDao
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.settings.getAppearancePrefs
import com.pitchedapps.frost.settings.getBehaviourPrefs
import com.pitchedapps.frost.settings.getDebugPrefs
import com.pitchedapps.frost.settings.getExperimentalPrefs
import com.pitchedapps.frost.settings.getFeedPrefs
import com.pitchedapps.frost.settings.getNotificationPrefs
import com.pitchedapps.frost.settings.getSecurityPrefs
import com.pitchedapps.frost.settings.sendDebug
import com.pitchedapps.frost.utils.ActivityThemer
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.REQUEST_REFRESH
import com.pitchedapps.frost.utils.REQUEST_RESTART
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.frostChangelog
import com.pitchedapps.frost.utils.frostNavigationBar
import com.pitchedapps.frost.utils.launchNewTask
import com.pitchedapps.frost.utils.loadAssets
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Created by Allan Wang on 2017-06-06.
 */
@AndroidEntryPoint
class SettingsActivity : KPrefActivity() {

    @Inject
    lateinit var fbCookie: FbCookie

    @Inject
    lateinit var prefs: Prefs

    @Inject
    lateinit var themeProvider: ThemeProvider

    @Inject
    lateinit var notifDao: NotificationDao

    @Inject
    lateinit var activityThemer: ActivityThemer

    private var resultFlag = Activity.RESULT_CANCELED

    companion object {
        private const val REQUEST_RINGTONE = 0b10111 shl 5
        const val REQUEST_NOTIFICATION_RINGTONE = REQUEST_RINGTONE or 1
        const val REQUEST_MESSAGE_RINGTONE = REQUEST_RINGTONE or 2
        const val ACTIVITY_REQUEST_TABS = 29
        const val ACTIVITY_REQUEST_DEBUG = 53
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (fetchRingtone(requestCode, resultCode, data)) return
        when (requestCode) {
            ACTIVITY_REQUEST_TABS -> {
                if (resultCode == Activity.RESULT_OK)
                    shouldRestartMain()
                return
            }
            ACTIVITY_REQUEST_DEBUG -> {
                val url = data?.extras?.getString(DebugActivity.RESULT_URL)
                if (resultCode == Activity.RESULT_OK && url?.isNotBlank() == true)
                    sendDebug(url, data.getStringExtra(DebugActivity.RESULT_BODY))
                return
            }
        }
        reloadList()
    }

    /**
     * Fetch ringtone and save uri
     * Returns [true] if consumed, [false] otherwise
     */
    private fun fetchRingtone(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
        if (requestCode and REQUEST_RINGTONE != REQUEST_RINGTONE || resultCode != Activity.RESULT_OK) return false
        val uri = data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
        val uriString: String = uri?.toString() ?: ""
        if (uri != null) {
            try {
                grantUriPermission(
                    "com.android.systemui",
                    uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION
                )
            } catch (e: Exception) {
                L.e(e) { "grantUriPermission" }
            }
        }
        when (requestCode) {
            REQUEST_NOTIFICATION_RINGTONE -> {
                prefs.notificationRingtone = uriString
                reloadByTitle(R.string.notification_ringtone)
            }
            REQUEST_MESSAGE_RINGTONE -> {
                prefs.messageRingtone = uriString
                reloadByTitle(R.string.message_ringtone)
            }
        }
        return true
    }

    override fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit = {
        textColor = { themeProvider.textColor }
        accentColor = { themeProvider.accentColor }
    }

    override fun onCreateKPrefs(savedInstanceState: Bundle?): KPrefAdapterBuilder.() -> Unit = {
        subItems(R.string.appearance, getAppearancePrefs()) {
            descRes = R.string.appearance_desc
            iicon = GoogleMaterial.Icon.gmd_palette
        }

        subItems(R.string.behaviour, getBehaviourPrefs()) {
            descRes = R.string.behaviour_desc
            iicon = GoogleMaterial.Icon.gmd_trending_up
        }

        subItems(R.string.newsfeed, getFeedPrefs()) {
            descRes = R.string.newsfeed_desc
            iicon = CommunityMaterial.Icon3.cmd_newspaper
        }

        subItems(R.string.notifications, getNotificationPrefs()) {
            descRes = R.string.notifications_desc
            iicon = GoogleMaterial.Icon.gmd_notifications
        }

        subItems(R.string.security, getSecurityPrefs()) {
            descRes = R.string.security_desc
            iicon = GoogleMaterial.Icon.gmd_lock
        }

//        subItems(R.string.network, getNetworkPrefs()) {
//            descRes = R.string.network_desc
//            iicon = GoogleMaterial.Icon.gmd_network_cell
//        }

        // todo add donation?

        plainText(R.string.about_frost) {
            descRes = R.string.about_frost_desc
            iicon = GoogleMaterial.Icon.gmd_info
            onClick = {
                startActivityForResult<AboutActivity>(
                    9,
                    bundleBuilder = {
                        withSceneTransitionAnimation(this@SettingsActivity)
                    }
                )
            }
        }

        plainText(R.string.help_translate) {
            descRes = R.string.help_translate_desc
            iicon = GoogleMaterial.Icon.gmd_translate
            onClick = { startLink(R.string.translation_url) }
        }

        plainText(R.string.replay_intro) {
            iicon = GoogleMaterial.Icon.gmd_replay
            onClick = { launchNewTask<IntroActivity>(cookies(), true) }
        }

        subItems(R.string.experimental, getExperimentalPrefs()) {
            descRes = R.string.experimental_desc
            iicon = CommunityMaterial.Icon2.cmd_flask_outline
        }

        subItems(R.string.debug_frost, getDebugPrefs()) {
            descRes = R.string.debug_frost_desc
            iicon = CommunityMaterial.Icon.cmd_android_debug_bridge
            visible = { prefs.debugSettings }
        }
    }

    fun setFrostResult(flag: Int) {
        resultFlag = resultFlag or flag
    }

    fun shouldRestartMain() {
        setFrostResult(REQUEST_RESTART)
    }

    fun shouldRefreshMain() {
        setFrostResult(REQUEST_REFRESH)
    }

    @SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        activityThemer.setFrostTheme(forceTransparent = true)
        super.onCreate(savedInstanceState)
        animate = prefs.animate
        themeExterior(false)
    }

    fun themeExterior(animate: Boolean = true) {
        if (animate) bgCanvas.fade(themeProvider.bgColor)
        else bgCanvas.set(themeProvider.bgColor)
        if (animate) toolbarCanvas.ripple(
            themeProvider.headerColor,
            RippleCanvas.MIDDLE,
            RippleCanvas.END
        )
        else toolbarCanvas.set(themeProvider.headerColor)
        frostNavigationBar(prefs, themeProvider)
    }

    override fun onBackPressed() {
        if (!super.backPress()) {
            setResult(resultFlag)
            launch(NonCancellable) {
                loadAssets(themeProvider)
                finishSlideOut()
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_settings, menu)
        toolbar.tint(themeProvider.iconColor)
        setMenuIcons(
            menu, themeProvider.iconColor,
            R.id.action_github to CommunityMaterial.Icon2.cmd_github,
            R.id.action_changelog to GoogleMaterial.Icon.gmd_info
        )
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_github -> startLink(R.string.github_url)
            R.id.action_changelog -> frostChangelog()
            else -> return super.onOptionsItemSelected(item)
        }
        return true
    }
}