Skip to content

Commit 9452ed0

Browse files
committed
refactor : change app call to app get
fix : permission not asked correctly
1 parent 5eeeefb commit 9452ed0

11 files changed

Lines changed: 45 additions & 40 deletions

File tree

floatstat/src/main/java/id/psw/floatstat/App.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ class App : Application() {
116116
rte.printStackTrace()
117117
}
118118
it.iconUri = dData.iconValue.value
119-
ctx.app().shouldUpdate = true
119+
ctx.app.shouldUpdate = true
120120
}
121121
if (dData.iconColor.updated) {
122122
it.iconTint = dData.iconColor.value
123-
ctx.app().shouldUpdate = true
123+
ctx.app.shouldUpdate = true
124124
}
125125
if (dData.textColor.updated) {
126126
it.textTint = dData.textColor.value
127-
ctx.app().shouldUpdate = true
127+
ctx.app.shouldUpdate = true
128128
}
129129
if (dData.textValue.updated) {
130130
it.value = dData.textValue.value
131-
ctx.app().shouldUpdate = true
131+
ctx.app.shouldUpdate = true
132132
}
133133
}
134134
}

floatstat/src/main/java/id/psw/floatstat/Ext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.os.Build
55

66
fun sdkAtLeast(ver: Int) : Boolean = Build.VERSION.SDK_INT >= ver
77

8-
fun Context.app() : App {
8+
val Context.app : App get(){
99
return if(this is App) this
1010
else this.applicationContext as App
1111
}

floatstat/src/main/java/id/psw/floatstat/FloatWindowService.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class FloatWindowService : Service() {
9090
vWinMan = winMan
9191

9292
mainView.setOnClickListener {
93-
val app = app()
9493
if(app.hideOnTap){
9594
vMainView?.visibility = View.GONE
9695
}else{
@@ -115,7 +114,6 @@ class FloatWindowService : Service() {
115114
viewParam.x = if(viewParam.x < 0) -w else w
116115

117116
if((currentTouch - mainView.initialTouch).length() < slop){ // Is Tap
118-
val app = app()
119117
if(app.hideOnTap){
120118
vMainView?.visibility = View.GONE
121119
}else{
@@ -184,8 +182,8 @@ class FloatWindowService : Service() {
184182

185183
override fun onCreate() {
186184
super.onCreate()
187-
app().isFloatServiceRunning = true
188-
app().refreshPluginList()
185+
app.isFloatServiceRunning = true
186+
app.refreshPluginList()
189187
createNotification()
190188
createView()
191189
}
@@ -194,14 +192,14 @@ class FloatWindowService : Service() {
194192
when(intent?.action){
195193
ACTION_CLOSE -> {
196194
stopSelf()
197-
if(sdkAtLeast(Build.VERSION_CODES.N)) SettingTileService.update(app())
198-
app().clearPlugins()
195+
if(sdkAtLeast(Build.VERSION_CODES.N)) SettingTileService.update(app)
196+
app.clearPlugins()
199197
}
200198
ACTION_EDIT -> {
201199
openEditWindow()
202200
}
203201
ACTION_VISIBILITY -> {
204-
if(app().hideOnTap){
202+
if(app.hideOnTap){
205203
vMainView?.visibility = View.VISIBLE
206204
}else{
207205
vMainView?.visibility = when(vMainView?.visibility){
@@ -210,10 +208,10 @@ class FloatWindowService : Service() {
210208
else -> View.VISIBLE
211209
}
212210
}
213-
app().isFloatWindowVisible= vMainView?.visibility == View.VISIBLE
211+
app.isFloatWindowVisible= vMainView?.visibility == View.VISIBLE
214212
}
215213
}
216-
if(sdkAtLeast(Build.VERSION_CODES.N)) SettingTileService.update(app())
214+
if(sdkAtLeast(Build.VERSION_CODES.N)) SettingTileService.update(app)
217215
return super.onStartCommand(intent, flags, startId)
218216
}
219217

@@ -231,7 +229,6 @@ class FloatWindowService : Service() {
231229
isDark.select(AlertDialog.THEME_DEVICE_DEFAULT_DARK, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
232230
}
233231

234-
val app = app()
235232
applicationContext.setTheme(ic)
236233
val dlgBuilder = AlertDialog.Builder(applicationContext, ic)
237234
var selectorRView : RecyclerView? = null
@@ -271,10 +268,10 @@ class FloatWindowService : Service() {
271268

272269
setOnMenuItemClickListener {
273270
when(it.itemId){
274-
R.id.plugin_selector_donate -> { app().openDonateUri() }
271+
R.id.plugin_selector_donate -> { app.openDonateUri() }
275272
R.id.plugin_selector_refresh -> {
276273
editWindow?.cancel()
277-
app().refreshPluginList()
274+
app.refreshPluginList()
278275
Toast.makeText(app, context.getString(R.string.plugin_selector_updating_warning), Toast.LENGTH_LONG).show()
279276
Timer("UpdateWait", false).schedule(1000){
280277
wHandler.post {
@@ -328,7 +325,6 @@ class FloatWindowService : Service() {
328325
}
329326

330327
private fun saveOrderAndActivation(dataList: ArrayList<PluginSelectorItem>, selectedDefault: Int) {
331-
val app = app()
332328
app.activePlugins.clear()
333329
dataList.filter { it.isActive }.forEach {
334330
app.activePlugins.add(App.PluginId(it.pkgName, it.id))
@@ -354,7 +350,7 @@ class FloatWindowService : Service() {
354350

355351
override fun onDestroy() {
356352
super.onDestroy()
357-
app().isFloatServiceRunning = false
353+
app.isFloatServiceRunning = false
358354
Toast.makeText(applicationContext, "Closing View", Toast.LENGTH_SHORT).show()
359355
if(vMainView != null){
360356
vWinMan?.removeView(vMainView)

floatstat/src/main/java/id/psw/floatstat/InternalIconProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class InternalIconProvider : PluginIconProvider() {
1717
private lateinit var cacheDir : File
1818

1919
override fun onCreate(): Boolean {
20-
val app = context!!.app()
20+
val app = context!!.app
2121

2222
val cDir = app.externalCacheDir ?: app.cacheDir
2323
cacheDir = File(cDir, "icon_caches")

floatstat/src/main/java/id/psw/floatstat/InternalStatProviderService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class InternalStatProviderService : Service() {
5353
val density = resources.displayMetrics.density
5454
val iconSize = (64 * density).toInt()
5555

56-
val atlas =
57-
resources.getDrawable(R.drawable.sheet_icons).toBitmap(iconSize * 4, iconSize * 2, Bitmap.Config.ALPHA_8)
56+
@Suppress("DEPRECATION") val atlas =
57+
resources.getDrawable(R.drawable.sheet_icons)
58+
.toBitmap(iconSize * 4, iconSize * 2, Bitmap.Config.ALPHA_8)
5859
val uvRect = Rect()
5960
val vpRect = Rect()
6061
arrayOf(
@@ -69,7 +70,7 @@ class InternalStatProviderService : Service() {
6970
uvRect.set(x, y, x + iconSize, y + iconSize)
7071
vpRect.set(0,0,iconSize,iconSize)
7172
drawCnv.drawBitmap(atlas, uvRect, vpRect, null)
72-
iconUris[id] = app().iconProvider.registerIcon(id, ctBmp)
73+
iconUris[id] = app.iconProvider.registerIcon(id, ctBmp)
7374
}
7475
}
7576
}

floatstat/src/main/java/id/psw/floatstat/MainActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package id.psw.floatstat
22

33
import android.annotation.SuppressLint
44
import android.app.Activity
5+
import android.app.NotificationManager
56
import android.app.StatusBarManager
67
import android.content.ComponentName
78
import android.content.Context
@@ -33,7 +34,7 @@ class MainActivity : Activity() {
3334
@SuppressLint("WrongConstant") // Android 13+, to ask registration for new system bar tile
3435
private fun askAddTile(){
3536
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
36-
if(app().pref.getBoolean(App.PK_TILE_ADDED, true)){
37+
if(app.pref.getBoolean(App.PK_TILE_ADDED, true)){
3738
val sbm = getSystemService(Context.STATUS_BAR_SERVICE) as StatusBarManager
3839
sbm.requestAddTileService(
3940
ComponentName(this, SettingTileService::class.java),
@@ -42,7 +43,7 @@ class MainActivity : Activity() {
4243
mainExecutor,
4344
){
4445
if(it == StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ADDED || it == StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ALREADY_ADDED){
45-
app().pref.edit().putBoolean(App.PK_TILE_ADDED, true).apply()
46+
app.pref.edit().putBoolean(App.PK_TILE_ADDED, true).apply()
4647
}
4748
}
4849
}
@@ -59,13 +60,15 @@ class MainActivity : Activity() {
5960
}
6061

6162
private fun askPermission() {
62-
if(sdkAtLeast(Build.VERSION_CODES.M)){
63+
if(sdkAtLeast(Build.VERSION_CODES.M) && !Settings.canDrawOverlays(this)){
6364
val i = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
6465
Toast.makeText(applicationContext, getString(R.string.allow_overlay_please), Toast.LENGTH_LONG).show()
6566
startActivityForResult(i, SYSTEM_ALERT_PERMISSION)
6667
}
6768

68-
if(sdkAtLeast(Build.VERSION_CODES.TIRAMISU)){
69+
val notifyMan = (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
70+
71+
if(sdkAtLeast(Build.VERSION_CODES.TIRAMISU) && !notifyMan.areNotificationsEnabled()){
6972
val i = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
7073
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
7174
.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
@@ -91,6 +94,11 @@ class MainActivity : Activity() {
9194
if(sdkAtLeast(Build.VERSION_CODES.M)){
9295
permitted = Settings.canDrawOverlays(this)
9396
}
97+
98+
if(sdkAtLeast(Build.VERSION_CODES.TIRAMISU)){
99+
val notifyMan = (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
100+
permitted = permitted && notifyMan.areNotificationsEnabled()
101+
}
94102
return permitted
95103
}
96104
}

floatstat/src/main/java/id/psw/floatstat/SettingTileService.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class SettingTileService : TileService() {
6060
val pending = isChangePending
6161
t.icon = _cachedIcon
6262

63-
if(app().isFloatServiceRunning){
64-
val isVisible =app().isFloatWindowVisible
63+
if(app.isFloatServiceRunning){
64+
val isVisible =app.isFloatWindowVisible
6565
t.state = isVisible.select(Tile.STATE_ACTIVE, Tile.STATE_INACTIVE)
6666
setDescription(
6767
pending.select(getString(R.string.tile_visibility_pending), isVisible.select(getString(
@@ -80,10 +80,10 @@ class SettingTileService : TileService() {
8080

8181
override fun onClick() {
8282
unlockAndRun {
83-
if(app().isFloatServiceRunning){
84-
startService(Intent(FloatWindowService.ACTION_VISIBILITY).setClass(app(), FloatWindowService::class.java))
83+
if(app.isFloatServiceRunning){
84+
startService(Intent(FloatWindowService.ACTION_VISIBILITY).setClass(app, FloatWindowService::class.java))
8585
}else{
86-
FloatWindowService.startServiceS(app())
86+
FloatWindowService.startServiceS(app)
8787
}
8888
isChangePending = true
8989
internalUpdateTile()

floatstat/src/main/java/id/psw/floatstat/StatusView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class StatusView @JvmOverloads constructor(
3636
var isDragged = false
3737
val initialPosition = Point()
3838
val initialTouch = PointF()
39-
private val app = context.app()
39+
private val app = context.app
4040

4141
private fun si(i: Int) : Int = (resources.displayMetrics.density * i).toInt()
4242
private fun sf(i: Float) : Int = (resources.displayMetrics.density * i).toInt()
@@ -146,7 +146,7 @@ class StatusView @JvmOverloads constructor(
146146
LINE_HEIGHT = lineSz
147147
val icLeft = PADDING * 1.0f
148148
val txLeft = (PADDING * 2.0f) + lineSz
149-
val app = context.app()
149+
val app = context.app
150150
app.activePlugins.forEachIndexed { i, aPlug ->
151151
app.pluginList.firstOrNull {
152152
plug -> plug.name.className == aPlug.pkg

floatstat/src/main/java/id/psw/floatstat/TileLongClickActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import kotlin.concurrent.timer
1111
class TileLongClickActivity : Activity() {
1212
override fun onCreate(savedInstanceState: Bundle?) {
1313
super.onCreate(savedInstanceState)
14-
if(!app().isFloatServiceRunning){
14+
if(!app.isFloatServiceRunning){
1515
startActivity(Intent(this, MainActivity::class.java))
16-
Toast.makeText(app(), "Starting service", Toast.LENGTH_SHORT).show()
16+
Toast.makeText(app, "Starting service", Toast.LENGTH_SHORT).show()
1717
Handler(Looper.getMainLooper()).postDelayed ({
1818
startEditor()
1919
}, 3000)

floatstat/src/main/java/id/psw/floatstat/providers/InternalStatProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class InternalStatProvider(val ctx: InternalStatProviderService) : IFloatStatDat
3838

3939
if(!keepRunning) return
4040

41-
val update = ctx.app().pref.getLong(PK_UPDATE_RATE, 100L).coerceAtLeast(UPDATE_RATE_LOWEST)
41+
val update = ctx.app.pref.getLong(PK_UPDATE_RATE, 100L).coerceAtLeast(UPDATE_RATE_LOWEST)
4242
mainTimer.schedule( timerTask { doUpdate() }, update)
4343
}
4444

0 commit comments

Comments
 (0)