-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically import Android plugins (#3788)
- Loading branch information
Showing
3 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
android/capacitor/src/main/java/com/getcapacitor/PluginManager.java
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,56 @@ | ||
package com.getcapacitor; | ||
|
||
import android.content.res.AssetManager; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class PluginManager { | ||
|
||
private final AssetManager assetManager; | ||
|
||
public PluginManager(AssetManager assetManager) { | ||
this.assetManager = assetManager; | ||
} | ||
|
||
public List<Class<? extends Plugin>> loadPluginClasses() throws PluginLoadException { | ||
JSONArray pluginsJSON = parsePluginsJSON(); | ||
ArrayList<Class<? extends Plugin>> pluginList = new ArrayList<>(); | ||
|
||
try { | ||
for (int i = 0, size = pluginsJSON.length(); i < size; i++) { | ||
JSONObject pluginJSON = pluginsJSON.getJSONObject(i); | ||
String classPath = pluginJSON.getString("classpath"); | ||
Class<?> c = Class.forName(classPath); | ||
pluginList.add(c.asSubclass(Plugin.class)); | ||
} | ||
} catch (JSONException e) { | ||
throw new PluginLoadException("Could not parse capacitor.plugins.json as JSON"); | ||
} catch (ClassNotFoundException e) { | ||
throw new PluginLoadException("Could not find class by class path: " + e.getMessage()); | ||
} | ||
|
||
return pluginList; | ||
} | ||
|
||
private JSONArray parsePluginsJSON() throws PluginLoadException { | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(assetManager.open("capacitor.plugins.json")))) { | ||
StringBuilder builder = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
builder.append(line); | ||
} | ||
String jsonString = builder.toString(); | ||
return new JSONArray(jsonString); | ||
} catch (IOException e) { | ||
throw new PluginLoadException("Could not load capacitor.plugins.json"); | ||
} catch (JSONException e) { | ||
throw new PluginLoadException("Could not parse capacitor.plugins.json as JSON"); | ||
} | ||
} | ||
} |
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