summaryrefslogtreecommitdiff
path: root/packages/EasterEgg/src/com/android/egg/quares/QuaresActivity.kt
blob: ce439a9a663cacaf4fc3d29deeba974f90794418 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.egg.quares

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Icon
import android.os.Bundle
import android.text.StaticLayout
import android.text.TextPaint
import android.util.Log
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.Button
import android.widget.CompoundButton
import android.widget.GridLayout

import java.util.Random

import com.android.egg.R

const val TAG = "Quares"

class QuaresActivity : Activity() {
    private var q: Quare = Quare(16, 16, 1)
    private var resId = 0
    private var resName = ""
    private var icon: Icon? = null

    private lateinit var label: Button
    private lateinit var grid: GridLayout

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

        window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE

        actionBar?.hide()

        setContentView(R.layout.activity_quares)

        grid = findViewById(R.id.grid)
        label = findViewById(R.id.label)

        if (savedInstanceState != null) {
            Log.v(TAG, "restoring puzzle from state")
            q = savedInstanceState.getParcelable("q") ?: q
            resId = savedInstanceState.getInt("resId")
            resName = savedInstanceState.getString("resName", "")
            loadPuzzle()
        }

        label.setOnClickListener { newPuzzle() }
    }

    override fun onResume() {
        super.onResume()
        if (resId == 0) {
            // lazy init from onCreate
            newPuzzle()
        }
        checkVictory()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        outState.putParcelable("q", q)
        outState.putInt("resId", resId)
        outState.putString("resName", resName)
    }

    fun newPuzzle() {
        Log.v(TAG, "new puzzle...")

        q.resetUserMarks()
        val oldResId = resId
        resId = android.R.drawable.stat_sys_warning
        try {
            for (tries in 0..3) {
                val ar = resources.obtainTypedArray(R.array.puzzles)
                val newName = ar.getString(Random().nextInt(ar.length()))
                if (newName == null) continue

                Log.v(TAG, "Looking for icon " + newName)

                val pkg = getPackageNameForResourceName(newName)
                val newId = packageManager.getResourcesForApplication(pkg)
                        .getIdentifier(newName, "drawable", pkg)
                if (newId == 0) {
                    Log.v(TAG, "oops, " + newName + " doesn't resolve from pkg " + pkg)
                } else if (newId != oldResId) {
                    // got a good one
                    resId = newId
                    resName = newName
                    break
                }
            }
        } catch (e: RuntimeException) {
            Log.v(TAG, "problem loading puzzle, using fallback", e)
        }
        loadPuzzle()
    }

    fun getPackageNameForResourceName(name: String): String {
        return if (name.contains(":") && !name.startsWith("android:")) {
            name.substring(0, name.indexOf(":"))
        } else {
            packageName
        }
    }

    fun checkVictory() {
        if (q.check()) {
            val dp = resources.displayMetrics.density

            val label: Button = findViewById(R.id.label)
            label.text = resName.replace(Regex("^.*/"), "")
            val drawable = icon?.loadDrawable(this)?.also {
                it.setBounds(0, 0, (32 * dp).toInt(), (32 * dp).toInt())
                it.setTint(label.currentTextColor)
            }
            label.setCompoundDrawables(drawable, null, null, null)

            label.visibility = VISIBLE
        } else {
            label.visibility = GONE
        }
    }

    fun loadPuzzle() {
        Log.v(TAG, "loading " + resName + " at " + q.width + "x" + q.height)

        val dp = resources.displayMetrics.density

        icon = Icon.createWithResource(getPackageNameForResourceName(resName), resId)
        q.load(this, icon!!)

        if (q.isBlank()) {
            // this is a really boring puzzle, let's try again
            resId = 0
            resName = ""
            recreate()
            return
        }

        grid.removeAllViews()
        grid.columnCount = q.width + 1
        grid.rowCount = q.height + 1

        label.visibility = GONE

        val orientation = resources.configuration.orientation

        // clean this up a bit
        val minSide = resources.configuration.smallestScreenWidthDp - 25 // ish
        val size = (minSide / (q.height + 0.5) * dp).toInt()

        val sb = StringBuffer()

        for (j in 0 until grid.rowCount) {
            for (i in 0 until grid.columnCount) {
                val tv: View
                val params = GridLayout.LayoutParams().also {
                    it.width = size
                    it.height = size
                    it.setMargins(1, 1, 1, 1)
                    it.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.TOP) // UGH
                }
                val x = i - 1
                val y = j - 1
                if (i > 0 && j > 0) {
                    if (i == 1 && j > 1) sb.append("\n")
                    sb.append(if (q.getDataAt(x, y) == 0) " " else "X")
                    tv = PixelButton(this)
                    tv.isChecked = q.getUserMark(x, y) != 0
                    tv.setOnClickListener {
                        q.setUserMark(x, y, if (tv.isChecked) 0xFF else 0)
                        val columnCorrect = (grid.getChildAt(i) as? ClueView)?.check(q) ?: false
                        val rowCorrect = (grid.getChildAt(j*(grid.columnCount)) as? ClueView)
                                ?.check(q) ?: false
                        if (columnCorrect && rowCorrect) {
                            checkVictory()
                        } else {
                            label.visibility = GONE
                        }
                    }
                } else if (i == j) { // 0,0
                    tv = View(this)
                    tv.visibility = GONE
                } else {
                    tv = ClueView(this)
                    if (j == 0) {
                        tv.textRotation = 90f
                        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                            params.height /= 2
                            tv.showText = false
                        } else {
                            params.height = (96 * dp).toInt()
                        }
                        if (x >= 0) {
                            tv.setColumn(q, x)
                        }
                    }
                    if (i == 0) {
                        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                            params.width /= 2
                            tv.showText = false
                        } else {
                            params.width = (96 * dp).toInt()
                        }
                        if (y >= 0) {
                            tv.setRow(q, y)
                        }
                    }
                }
                grid.addView(tv, params)
            }
        }

        Log.v(TAG, "icon: \n" + sb)
    }
}

class PixelButton(context: Context) : CompoundButton(context) {
    init {
        setBackgroundResource(R.drawable.pixel_bg)
        isClickable = true
        isEnabled = true
    }
}

class ClueView(context: Context) : View(context) {
    var row: Int = -1
    var column: Int = -1
    var textRotation: Float = 0f
    var text: CharSequence = ""
    var showText = true
    val paint: TextPaint
    val incorrectColor: Int
    val correctColor: Int

    init {
        setBackgroundColor(0)
        paint = TextPaint().also {
            it.textSize = 14f * context.resources.displayMetrics.density
            it.color = context.getColor(R.color.q_clue_text)
            it.typeface = Typeface.DEFAULT_BOLD
            it.textAlign = Paint.Align.CENTER
        }
        incorrectColor = context.getColor(R.color.q_clue_bg)
        correctColor = context.getColor(R.color.q_clue_bg_correct)
    }

    fun setRow(q: Quare, row: Int): Boolean {
        this.row = row
        this.column = -1
        this.textRotation = 0f
        text = q.getRowClue(row).joinToString("-")
        return check(q)
    }
    fun setColumn(q: Quare, column: Int): Boolean {
        this.column = column
        this.row = -1
        this.textRotation = 90f
        text = q.getColumnClue(column).joinToString("-")
        return check(q)
    }
    fun check(q: Quare): Boolean {
        val correct = q.check(column, row)
        setBackgroundColor(if (correct) correctColor else incorrectColor)
        return correct
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (!showText) return
        canvas?.let {
            val x = canvas.width / 2f
            val y = canvas.height / 2f
            var textWidth = canvas.width
            if (textRotation != 0f) {
                canvas.rotate(textRotation, x, y)
                textWidth = canvas.height
            }
            val textLayout = StaticLayout.Builder.obtain(
                    text, 0, text.length, paint, textWidth).build()
            canvas.translate(x, y - textLayout.height / 2)
            textLayout.draw(canvas)
        }
    }
}