Skip to content

Commit 2baea37

Browse files
committed
icons loaded
1 parent f705deb commit 2baea37

File tree

15 files changed

+57
-45
lines changed

15 files changed

+57
-45
lines changed
Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.hackthedeveloper.live_icon;
22

33
import android.app.Activity;
4+
import android.content.ComponentName;
45
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.pm.PackageManager;
58
import android.os.Build;
69
import android.util.Log;
710

@@ -37,59 +40,32 @@ void setActivity(Activity activity) {
3740
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
3841

3942
switch (call.method) {
40-
case "initialize":
41-
initialize(call);
43+
case "switchTo":
44+
switchTo(call);
4245
break;
4346
default:
4447
result.notImplemented();
4548
break;
4649
}
4750
}
4851

49-
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
50-
private void initialize(MethodCall call) {
52+
private void switchTo(MethodCall call) {
5153
List<Map<String, String>> args = call.arguments();
52-
final int numberOfIcons = args.size();
53-
for(Map<String,String> arg : args ){
54-
Log.d(TAG, "=====================>initialize: ");
55-
String className = arg.get("className");
56-
File file = new File(className+".java");
57-
try {
58-
Log.d(TAG, "=====================>1st try: ");
59-
file.createNewFile();
60-
} catch (IOException e) {
61-
e.printStackTrace();
62-
}
63-
if (file.exists()) {
64-
// Returning the file name
65-
System.out.println("File name: " + file.getName());
66-
67-
// Returning the path of the file
68-
System.out.println("Absolute path: " + file.getAbsolutePath());
69-
70-
// Displaying whether the file is writable
71-
System.out.println("Writeable: " + file.canWrite());
72-
73-
// Displaying whether the file is readable or not
74-
System.out.println("Readable " + file.canRead());
75-
76-
// Returning the length of the file in bytes
77-
System.out.println("File size in bytes " + file.length());
78-
} else {
79-
System.out.println("The file does not exist.");
80-
}
81-
try {
82-
Log.d(TAG, "=====================>2nd try: ");
83-
FileWriter writeFile = new FileWriter(className+".java");
84-
writeFile.write("package \"com.hackthedeveloper.live_icon\";");
85-
writeFile.write(System.lineSeparator());
86-
writeFile.write("class "+className+"{}");
87-
} catch (IOException e) {
88-
e.printStackTrace();
89-
}
90-
}
91-
}
54+
String iconName = args.get(0).get("iconName");
55+
String className = args.get(0).get("className");
56+
PackageManager pm = activity.getPackageManager();
57+
58+
59+
String packageName = activity.getPackageName();
60+
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
61+
String mainActivityClass = launchIntent.getComponent().getClassName();
62+
63+
ComponentName darkThemeCN = new ComponentName(mainActivityClass, packageName+"."+className);
64+
ComponentName lightThemeCN = new ComponentName(mainActivityClass, packageName+"."+"LightTheme");
9265

66+
pm.setComponentEnabledSetting(lightThemeCN,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
67+
pm.setComponentEnabledSetting(darkThemeCN,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
9368
}
69+
}
9470

9571

example/android/app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,38 @@
3232
<category android:name="android.intent.category.LAUNCHER"/>
3333
</intent-filter>
3434
</activity>
35+
36+
<!-- Live Icon Addition -->
37+
<activity-alias
38+
android:name="DarkTheme"
39+
android:enabled="false"
40+
android:icon="@mipmap/dark_theme"
41+
android:label="DarkThemeLabel"
42+
android:targetActivity=".MainActivity">
43+
<intent-filter>
44+
<action android:name="android.intent.action.MAIN"/>
45+
<category android:name="android.intent.category.LAUNCHER"/>
46+
</intent-filter>
47+
</activity-alias>
48+
49+
<activity-alias
50+
android:name="LightTheme"
51+
android:enabled="false"
52+
android:icon="@mipmap/light_theme"
53+
android:label="LightThemeLabel"
54+
android:targetActivity=".MainActivity">
55+
<intent-filter>
56+
<action android:name="android.intent.action.MAIN"/>
57+
<category android:name="android.intent.category.LAUNCHER"/>
58+
</intent-filter>
59+
</activity-alias>
60+
<!-- Live Icon Addition -->
61+
3562
<!-- Don't delete the meta-data below.
3663
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
3764
<meta-data
3865
android:name="flutterEmbedding"
3966
android:value="2" />
67+
4068
</application>
4169
</manifest>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.hackthedeveloper.live_icon_example;
2+
3+
public class DarkTheme {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.hackthedeveloper.live_icon_example;
2+
3+
public class LightTheme {
4+
}
4.79 KB
Loading
5.87 KB
Loading
2.64 KB
Loading
3.18 KB
Loading
6.38 KB
Loading
7.75 KB
Loading

0 commit comments

Comments
 (0)