aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
blob: 54a48112152c3422defc5764ec549a7db4b28583 (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
package com.pitchedapps.frost

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.utils.buildIsLollipopAndUp
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.startActivity
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.pitchedapps.frost.activities.LoginActivity
import com.pitchedapps.frost.activities.MainActivity
import com.pitchedapps.frost.activities.SelectorActivity
import com.pitchedapps.frost.dbflow.loadFbCookiesAsync
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.utils.EXTRA_COOKIES
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.launchNewTask
import java.util.*

/**
 * Created by Allan Wang on 2017-05-28.
 */
class StartActivity : KauBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (!buildIsLollipopAndUp) { // not supported
            showInvalidSdkView()
            return
        }

        FbCookie.switchBackUser {
            loadFbCookiesAsync {
                val cookies = ArrayList(it)
                L.i { "Cookies loaded at time ${System.currentTimeMillis()}" }
                L._d { "Cookies: ${cookies.joinToString("\t")}" }
                if (cookies.isNotEmpty()) {
                    if (Prefs.userId != -1L)
                        startActivity<MainActivity>(intentBuilder = {
                            putParcelableArrayListExtra(EXTRA_COOKIES, cookies)
                            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
                        })
                    else
                        launchNewTask<SelectorActivity>(cookies)
                } else
                    launchNewTask<LoginActivity>()
            }
        }

    }

    private fun showInvalidSdkView() {
        setContentView(R.layout.activity_invalid_sdk)
        findViewById<ImageView>(R.id.invalid_icon)
                .setIcon(GoogleMaterial.Icon.gmd_adb, -1, Color.WHITE)

        findViewById<TextView>(R.id.invalid_text).apply {
            try {
                text = String.format(getString(R.string.error_sdk), Build.VERSION.SDK_INT)
            } catch (e: IllegalFormatException) {
                setText(R.string.error_sdk)
            }
        }
    }
}