|
1 | 1 | package com.hackthedeveloper.live_icon; |
2 | 2 |
|
3 | 3 | import android.app.Activity; |
| 4 | +import android.content.ComponentName; |
4 | 5 | import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.pm.PackageManager; |
5 | 8 | import android.os.Build; |
6 | 9 | import android.util.Log; |
7 | 10 |
|
@@ -37,59 +40,32 @@ void setActivity(Activity activity) { |
37 | 40 | public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { |
38 | 41 |
|
39 | 42 | switch (call.method) { |
40 | | - case "initialize": |
41 | | - initialize(call); |
| 43 | + case "switchTo": |
| 44 | + switchTo(call); |
42 | 45 | break; |
43 | 46 | default: |
44 | 47 | result.notImplemented(); |
45 | 48 | break; |
46 | 49 | } |
47 | 50 | } |
48 | 51 |
|
49 | | - @RequiresApi(api = Build.VERSION_CODES.KITKAT) |
50 | | - private void initialize(MethodCall call) { |
| 52 | + private void switchTo(MethodCall call) { |
51 | 53 | 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"); |
92 | 65 |
|
| 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); |
93 | 68 | } |
| 69 | +} |
94 | 70 |
|
95 | 71 |
|
0 commit comments