Skip to content

Commit

Permalink
Add new activities to simulate change
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozg committed Dec 19, 2024
1 parent 4bd37a8 commit 2233a06
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ private val mainActivityAlias = "com.oguzhanaslann.dynamicicon.MainActivityAlias

android {
namespace = "com.oguzhanaslann.dynamicicon"
compileSdk = 33
compileSdk = 34

defaultConfig {
applicationId = "com.oguzhanaslann.dynamicicon"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
targetSdk = 34
versionCode = 3
versionName = "1.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -74,7 +74,7 @@ android {
}

dependencies {

implementation("androidx.work:work-runtime-ktx:2.9.1")
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.7.2")
Expand All @@ -90,4 +90,4 @@ dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
}
58 changes: 42 additions & 16 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/baseline_30fps_24"
android:label="@string/app_name"
android:roundIcon="@drawable/baseline_30fps_24"
android:supportsRtl="true"
android:theme="@style/Theme.DynamicIcon"
tools:targetApi="31">
android:name=".Application"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/baseline_30fps_24"
android:label="@string/app_name"
android:roundIcon="@drawable/baseline_30fps_24"
android:supportsRtl="true"
android:theme="@style/Theme.DynamicIcon"
tools:targetApi="31">
<activity
android:name="${main_activity}"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.DynamicIcon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity-alias
android:name="${main_activity_alias}"
android:name="${main_activity_alias}"
android:enabled="false"
android:exported="true"
android:icon="@drawable/baseline_60fps_24"
android:label="@string/app_name"
android:roundIcon="@drawable/baseline_60fps_24"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity-alias>

<activity
android:name=".ValhallaActivityIcon30"
android:enabled="false"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.DynamicIcon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ValhallaActivityIcon60"
android:enabled="false"
android:exported="true"
android:icon="@drawable/baseline_60fps_24"
android:label="@string/app_name"
android:roundIcon="@drawable/baseline_60fps_24"
android:targetActivity=".MainActivity">
android:targetActivity=".ValhallaActivityIcon30">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity-alias>
</activity>
</application>
</manifest>
</manifest>
51 changes: 51 additions & 0 deletions app/src/main/java/com/oguzhanaslann/dynamicicon/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.oguzhanaslann.dynamicicon

import android.app.Application
import android.content.ComponentName
import android.content.pm.PackageManager
import android.util.Log

/*
* Created by Thiago Zagui Giacomini on 19/12/2024.
* See thiagozg on GitHub: https://github.com/thiagozg
*/

/// Goal:
// 1. Incluir uma ValhallaActivity no Manifest
// 2. Caso o icone seja 60: manter ele sendo exibido - sem criar 2 icones
// 3. Caso o icone seja 30: manter ele possibilitando a alteração para o icone 60
class Application : Application() {
override fun onCreate() {
super.onCreate()
// TODO: ignorar ativar a Splash Legacy

handleLauncherUpdate()
}

private fun handleLauncherUpdate() {
Log.i("HUE", "handleLauncherUpdate")
val icon60Legacy =
packageManager.getComponentEnabledSetting(ComponentName(packageName, mainActivityAlias))
if (icon60Legacy == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
Log.i("HUE", "vai ativar ValhallaActivityIcon60")
scheduleChangeLauncherActivity(
activitiesEnabled = arrayOf(ValhallaActivityIcon60::class.java.name),
activitiesDisabled = arrayOf(
ValhallaActivityIcon30::class.java.name,
mainActivityAlias,
mainActivity,
),
)
} else {
Log.i("HUE", "vai ativar ValhallaActivityIcon30")
scheduleChangeLauncherActivity(
activitiesEnabled = arrayOf(ValhallaActivityIcon30::class.java.name),
activitiesDisabled = arrayOf(
ValhallaActivityIcon60::class.java.name,
mainActivityAlias,
mainActivity,
),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.oguzhanaslann.dynamicicon

import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import java.util.concurrent.TimeUnit

fun scheduleChangeLauncherActivity(
activitiesEnabled: Array<String>,
activitiesDisabled: Array<String>,
) {
val data = Data.Builder()
.putStringArray("enabled", activitiesEnabled)
.putStringArray("disabled", activitiesDisabled)
.build()

val constraints = Constraints.Builder()
// .setRequiresDeviceIdle(true)
.build()

val workRequest = OneTimeWorkRequest.Builder(ChangeLauncherActivityWorker::class.java)
.setInputData(data)
.setConstraints(constraints)
.setInitialDelay(10, TimeUnit.SECONDS)
.build()

val workManager = WorkManager.getInstance()
workManager.enqueue(workRequest)
}

private fun changeEnabledComponent(
context: Context,
activitiesEnabled: Array<String>,
activitiesDisabled: Array<String>,
) {
val packageManager: PackageManager = context.packageManager
val packageName: String = context.packageName

activitiesEnabled.forEach {
packageManager.setComponentEnabledSetting(
ComponentName(packageName, it),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}

activitiesDisabled.forEach {
packageManager.setComponentEnabledSetting(
ComponentName(packageName, it),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
}

class ChangeLauncherActivityWorker(
private val appContext: Context,
private val workerParams: WorkerParameters,
) : Worker(appContext, workerParams) {

override fun doWork(): Result {
val data = workerParams.inputData
val activitiesEnabled = data.getStringArray("enabled")
val activitiesDisabled = data.getStringArray("disabled")
return try {
changeEnabledComponent(appContext, activitiesEnabled!!, activitiesDisabled!!)
Result.success()
} catch (e: Exception) {
Log.e("HUE", "2doWork exception: $e")
Result.failure()
}
}
}
39 changes: 7 additions & 32 deletions app/src/main/java/com/oguzhanaslann/dynamicicon/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.oguzhanaslann.dynamicicon

import android.content.ComponentName
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -34,45 +32,22 @@ class MainActivity : ComponentActivity() {
) {
Screen(
on30Click = {
changeEnabledComponent(
enabled = mainActivity,
disabled = mainActivityAlias
scheduleChangeLauncherActivity(
activitiesEnabled = arrayOf(mainActivity),
activitiesDisabled = arrayOf(mainActivityAlias)
)
},
on60Click = {
changeEnabledComponent(
enabled = mainActivityAlias,
disabled = mainActivity
scheduleChangeLauncherActivity(
activitiesEnabled = arrayOf(mainActivityAlias),
activitiesDisabled = arrayOf(mainActivity)
)
}
)
}
}
}
}

private fun changeEnabledComponent(
enabled: String,
disabled: String,
) {
packageManager.setComponentEnabledSetting(
ComponentName(
this@MainActivity,
enabled
),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)

packageManager.setComponentEnabledSetting(
ComponentName(
this@MainActivity,
disabled
),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
}

@Composable
Expand Down Expand Up @@ -108,4 +83,4 @@ fun Screen(
@Composable
fun previewScreen() {
Screen()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.oguzhanaslann.dynamicicon

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
import com.oguzhanaslann.dynamicicon.ui.theme.DynamicIconTheme

open class ValhallaActivityIcon30 : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
DynamicIconTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Text(
text = "Valhalla",
modifier = Modifier.fillMaxWidth(),
fontSize = 30.sp,
textAlign = TextAlign.Center,
)
}
}
}
}

override fun onResume() {
super.onResume()
}
}

class ValhallaActivityIcon60 : ValhallaActivityIcon30()

0 comments on commit 2233a06

Please sign in to comment.