Is it possible to make an extension to change app icon

Now Fast has been updated. So we can change icons dynamically:

How to do it?
The easiest way to use Fast.

  1. We add our icons to AndroidManifest using <activity-alias>:
<application>
    <!-- You can use any manifest tag that goes inside the <application> tag -->
    <!-- <service android:name="com.example.MyService"> ... </service> -->
    <activity-alias android:name=".Icon1"
      android:enabled="false"
      android:icon="@drawable/icon1"
      android:targetActivity=".Screen1">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity-alias>
    <activity-alias android:name=".Icon2"
      android:enabled="false"
      android:icon="@drawable/icon2"
      android:targetActivity=".Screen1">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity-alias>
  </application>

Next, we create the code to change the icons:

  @SimpleFunction(description = "")
  public void ChangeIcon(int icon) {
    PackageManager pm = form.$context().getPackageManager();
    String pkg = form.$context().getPackageName();
    ComponentName icon1 = new ComponentName(form.$context(), pkg + ".Icon1");
    ComponentName icon2 = new ComponentName(form.$context(), pkg + ".Icon2");
    ComponentName defaultIcon = new ComponentName(form.$context(), pkg + ".Screen1");
    if(icon == 1) {
      pm.setComponentEnabledSetting(icon1, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(icon2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(defaultIcon, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    } else if(icon == 2) {
      pm.setComponentEnabledSetting(icon1, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(icon2, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(defaultIcon, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    } else if(icon == 3) {
      pm.setComponentEnabledSetting(icon1, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(icon2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
      pm.setComponentEnabledSetting(defaultIcon, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }
  }

We still need to add our icons to the project. To do this, we need to create our own aar library, which we place in the deps folder. It must contain three elements:

  1. A res/drawable folder with our icons.
  2. An AndroidManifest with any package name we choose.
  3. An R.txt file with the names of our icons.

Below is an example library; the txt extension needs to be removed.

my-icons.aar.txt (20.0 KB)

In the fast.yml file we add our library:

dependencies:
- my-icons.aar:aar
9 Likes