forked from thiagozg/DynamicIcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new activities to simulate change
- Loading branch information
Showing
8 changed files
with
241 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
app/src/main/java/com/oguzhanaslann/dynamicicon/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
) | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
app/src/main/java/com/oguzhanaslann/dynamicicon/ChangeLauncherActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/oguzhanaslann/dynamicicon/ValhallaActivityIcon30.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |