Globals.kt 3.74 KB
Newer Older
Daniel Wolf's avatar
Daniel Wolf committed
1
2
3
package com.frostnerd.smokescreen

import android.content.Context
4
import android.content.DialogInterface
Daniel Wolf's avatar
Daniel Wolf committed
5
import android.content.pm.PackageManager
Daniel Wolf's avatar
Daniel Wolf committed
6
import android.graphics.Color
Daniel Wolf's avatar
Daniel Wolf committed
7
import android.text.Html
Daniel Wolf's avatar
Daniel Wolf committed
8
9
10
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
11
import android.view.LayoutInflater
Daniel Wolf's avatar
Daniel Wolf committed
12
import android.widget.TextView
Daniel Wolf's avatar
Daniel Wolf committed
13
import androidx.annotation.ColorInt
14
15
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.dialog_privacypolicy.view.*
16
17
18
import okhttp3.internal.toHexString


Daniel Wolf's avatar
Daniel Wolf committed
19
20
/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
Daniel Wolf's avatar
Daniel Wolf committed
21
 *
Daniel Wolf's avatar
Daniel Wolf committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * 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/>.
 *
 * You can contact the developer at daniel.wolf@frostnerd.com.
Daniel Wolf's avatar
Daniel Wolf committed
36
37
 */

38
39
fun showPrivacyPolicyDialog(context: Context) {
    val dialog = AlertDialog.Builder(context, context.getPreferences().theme.dialogStyle)
40
    dialog.setTitle(R.string.about_privacypolicy)
41
42
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_privacypolicy, null, false)
    dialog.setView(view)
43
    view.webView.loadUrl("file:///android_asset/privacy_policy.html")
44
45
46
47
    dialog.setNeutralButton(R.string.all_close, null)
    dialog.show()
}

48
49
fun showInfoTextDialog(context:Context, title:String, text:String,
                       positiveButton:Pair<String, (DialogInterface, Int) -> Unit>? = null,
50
                       negativeButton:Pair<String, (DialogInterface, Int) -> Unit>? = null,
Daniel Wolf's avatar
Daniel Wolf committed
51
52
                       neutralButton:Pair<String, ((DialogInterface, Int) -> Unit)?>? = context.getString(android.R.string.ok) to null): AlertDialog {
    val stringWithLinks = SpannableString(text)
Daniel Wolf's avatar
Daniel Wolf committed
53
54
    Linkify.addLinks(stringWithLinks, Linkify.ALL)

Daniel Wolf's avatar
Daniel Wolf committed
55
56
    val span = Html.fromHtml(stringWithLinks.toString().replace("\n", "<br>"))

Daniel Wolf's avatar
Daniel Wolf committed
57
    val dialogBuilder = AlertDialog.Builder(context, context.getPreferences().theme.dialogStyle)
Daniel Wolf's avatar
Daniel Wolf committed
58
        .setTitle(title)
Daniel Wolf's avatar
Daniel Wolf committed
59
        .setMessage(span)
60
    if(neutralButton != null) dialogBuilder.setNeutralButton(neutralButton.first, neutralButton.second)
61
62
63
64
    if(positiveButton != null) dialogBuilder.setPositiveButton(positiveButton.first, positiveButton.second)
    if(negativeButton != null) dialogBuilder.setNegativeButton(negativeButton.first, negativeButton.second)

    val dialog = dialogBuilder.show()
Daniel Wolf's avatar
Daniel Wolf committed
65
66
    val textView = dialog.findViewById<TextView>(android.R.id.message)
    textView?.movementMethod = LinkMovementMethod.getInstance()
Daniel Wolf's avatar
Daniel Wolf committed
67
    textView?.linksClickable = true
Daniel Wolf's avatar
Daniel Wolf committed
68
69
    textView?.setLinkTextColor(Color.parseColor("#64B5F6"))
    return dialog
70
71
72
73
74
75
76
}

fun isPackageInstalled(context: Context, packageName: String): Boolean {
    val packageManager = context.packageManager
    val intent = packageManager.getLaunchIntentForPackage(packageName) ?: return false
    val list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
    return list.size > 0
77
78
}

79
80
81
82
83
84
85
86
87
88
fun colorToHexString(@ColorInt color:Int):String {
    return String.format("#%06X", 0xFFFFFF and color)
}

@ColorInt
fun opaqueColor(@ColorInt color: Int, opactiy: Int): Int {
    val alpha = opactiy - opactiy % 5
    return Color.parseColor("#" + alpha.toHexString() + colorToHexString(color).replace("#", ""))
}

89
90
interface BackpressFragment {
    fun onBackPressed():Boolean
Daniel Wolf's avatar
Daniel Wolf committed
91
}