Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy
Transcript of Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy
Garfield Android Studio Plugin
MIROSLAV POPOVIC
Be smart, be lazy
PROBLEM Lot’s of boiler plate code out there
BOILERPLATES
• Send email • Send sms • Mark pin on a map • Dagger • …
Lot’s of lines of code needed for one logical operation
SOLUTION
CODE AUTOMATION Let the machine do the dirty work
SOLUTION
FOR THE BEGINNIG…
How about this…private static void setMarkerOnMap(@DrawableRes int resMarkerIcon, LatLng latLng, GoogleMap map) { MarkerOptions marker = new
MarkerOptions().position(latLng) .icon(BitmapDescriptorFactory .fromResource(resMarkerIcon)); map.addMarker(marker); }
How about this…
public static void sendEmail(String email, String subject, String emailBody, List<Uri> images) throws ActivityNotFoundException { Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email}); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, emailBody); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, (ArrayList<Uri>) images); intent.setType(TEXT_PLAIN); startActivity(intent); }
• Save a template • Auto complete • Export is possible
LIVE TEMPLATES
android.content.Intent view = new Intent(); view.setAction(Intent.ACTION_VIEW); view.setData(android.net.Uri.parse($url$)); startActivity(view);
LIVE TEMPLATES
Can - static code - variables - modification
scripts
LIVE TEMPLATES
Cannot - generate
packages - generate classes - check members
HEAVY CODE AUTOMATION
ANDROID STUDIO PLUGIN - GARFIELD
Dagger, MVP, helper code…
WRITING CODE THAT GENERATES CODE
Garfield Android Studio plugin
1)
2)3)4)
with Garfield
5)
Garfield exported
SETUP
• git clone IntelliJ community edition • run sh getPlugins.sh from the terminal (android studio
plugin included into community project) • create a new Java SDK “IDEA jdk” (and add lib\tools.jar
to the classpath if needed) • make project • create a new Intellij Platform Plugin
[1] git://git.jetbrains.org/idea/community.git
1
CREATING AN ACTION
• Define an Action • Package com.intellij.openapi.actionSystem • Register in plugin.xml
public class NewAction extends AnAction { public void actionPerformed(AnActionEvent e) { Project project = e.getProject(); Module module = e.getData(DataKeys.MODULE); NewForm form = new NewForm(project, module); form.show(); } }
CREATING UI
• Common Swing UI components • You will most probably use a dialog • DialogWrapper used with UI Designer form
@Nullable@Overrideprotected JComponent createCenterPanel() { return rootPanel; }
ANDROID FACET
• Needed to get base project name • IntelliJ Facets: Android, GWT, EJB, Hibernate
public static String getPackageName(Project project, Module module) { AndroidFacet facet = getCurrentFacet(project, module); VirtualFile manifestFile = AndroidRootUtil.getPrimaryManifestFile(facet); Manifest manifest = AndroidUtils.loadDomElement(facet.getModule(), manifestFile, Manifest.class); return manifest.getPackage().getValue(); //get package}
GENERATING CODE ← JAVAPOET
MethodSpec main = MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(void.class) .addParameter(String[].class, "args") .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") .build();
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(main) .build();
public final class HelloWorld { public static void main(String[] args) { System.out.println("Hello, JavaPoet!"); } }
SUMMARY
Copy & Paste Live Templates Android Studio Plugins
Be smart, be lazy
Thank you!
Visit infinum.co or find us on social networks:
infinum.co infinumco infinumco infinum
REFERENCES
http://www.jetbrains.org/pages/viewpage.action?pageId=983225
https://github.com/JetBrains/intellij-community
http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html
Setting up the environment
REFERENCES
https://github.com/JetBrains/intellij-sdk-docs/blob/master/tutorials/action_system/grouping_action.md
http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/creating_an_action.html
https://github.com/winterDroid/android-drawable-importer-intellij-plugin
Action system
REFERENCES
Generating code
https://www.jetbrains.com/idea/help/managing-facets.html
https://www.jetbrains.com/idea/help/facet.html
https://android.googlesource.com/platform/tools/adt/idea/+/1100693bbe6a6bffd566f30969f33b67c483ba7c/android/src/org/jetbrains/android/facet/AndroidFacet.java
https://github.com/square/javapoet