Skip to content

Commit

Permalink
Restart Android app after icon change
Browse files Browse the repository at this point in the history
  • Loading branch information
remcoanker committed Sep 20, 2022
1 parent 18b34ca commit 7b17c61
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 21 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ Next open your AndroidManifest.xml (placed in `android/src/main/AndroidManifest.

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<!-- This needs to be DEFAULT instead of LAUNCHER -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Expand All @@ -180,11 +181,25 @@ Next open your AndroidManifest.xml (placed in `android/src/main/AndroidManifest.
The `android:targetActivity` must be setted as "MainActivity"
If you have other intent-filter (for example sharing intent), you will have to paste the same value for all your alternative icons (and the default one)
-->
<!-- Default alias -->
<activity-alias
android:name=".default"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:enabled="true"
android:exported="true"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name=".chills"
android:icon="@mipmap/chills"
android:label="Chills"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">

<intent-filter>
Expand All @@ -198,6 +213,7 @@ Next open your AndroidManifest.xml (placed in `android/src/main/AndroidManifest.
android:icon="@mipmap/photos"
android:label="Photos"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">

<intent-filter>
Expand All @@ -211,6 +227,7 @@ Next open your AndroidManifest.xml (placed in `android/src/main/AndroidManifest.
android:icon="@mipmap/teamfortress"
android:label="Teamfortress"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">

<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package io.github.tastelessjolt.flutterdynamicicon;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;

/** FlutterDynamicIconPlugin */
public class FlutterDynamicIconPlugin implements FlutterPlugin {
public class FlutterDynamicIconPlugin implements FlutterPlugin, ActivityAware {
private static final String CHANNEL_NAME = "flutter_dynamic_icon";
private MethodChannel channel;
public static ActivityPluginBinding _activityBinding;

@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
Expand All @@ -22,10 +29,32 @@ public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding) {
}

@Override
public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {
public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
teardownChannel();
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
_activityBinding = binding;
}

@Override
public void onDetachedFromActivity() {
// This is called after the new activity is attached
}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
_activityBinding = binding;
}
@Override
public void onDetachedFromActivityForConfigChanges() {
}

public static Activity getActivity() {
return (_activityBinding != null) ? _activityBinding.getActivity() : null;
}

private void setupChannel(BinaryMessenger messenger, Context context) {
channel = new MethodChannel(messenger, CHANNEL_NAME);
MethodCallHandlerImpl handler = new MethodCallHandlerImpl(context);
Expand All @@ -35,5 +64,5 @@ private void setupChannel(BinaryMessenger messenger, Context context) {
private void teardownChannel() {
channel.setMethodCallHandler(null);
channel = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,7 @@ public static boolean isComponentEnabled(PackageManager pm, String pkgName, Stri
public static List<ComponentName> getComponentNames(Context context, String activityName) {
String packageName = context.getPackageName();
if (activityName == null) {
PackageManager pm = context.getPackageManager();
ArrayList<ComponentName> components = new ArrayList<ComponentName>();
try {
PackageInfo info = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES | PackageManager.GET_DISABLED_COMPONENTS);
ActivityInfo enabled = null;
for(ActivityInfo activityInfo: info.activities) {
Log.d("IconChangerGetComps", activityInfo.name.toString());
if(activityInfo.targetActivity == null) {
components.add(new ComponentName(packageName, activityInfo.name));
}
}
} catch (PackageManager.NameNotFoundException e) {
// the package isn't installed on the device
Log.d(TAG, "the package isn't installed on the device");
}
return components;
activityName = "default";
}

String componentName = String.format("%s.%s", packageName, activityName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.tastelessjolt.flutterdynamicicon;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -74,6 +76,15 @@ public static void enableIcon(Context context, String activityName) {
for(ComponentName toDisable: componentsToDisable) {
pm.setComponentEnabledSetting(toDisable, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

Activity activity = FlutterDynamicIconPlugin.getActivity();
if(activity != null) {
activity.finish();

Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}

}
18 changes: 17 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<!-- Alternative icons configuration -->
<activity-alias
android:name=".default"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:enabled="true"
android:exported="true"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>

<activity-alias
android:name=".chills"
android:icon="@mipmap/chills"
android:label="Chills"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
Expand All @@ -44,6 +58,7 @@
android:icon="@mipmap/photos"
android:label="Photos"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
Expand All @@ -59,6 +74,7 @@
android:icon="@mipmap/teamfortress"
android:label="Teamfortress"
android:enabled="false"
android:exported="true"
android:targetActivity=".MainActivity">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
Expand Down

0 comments on commit 7b17c61

Please sign in to comment.