Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

25
Garfield Android Studio Plugin MIROSLAV POPOVIC Be smart, be lazy

Transcript of Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

Page 1: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

Garfield Android Studio Plugin

MIROSLAV POPOVIC

Be smart, be lazy

Page 2: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

PROBLEM Lot’s of boiler plate code out there

Page 3: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

BOILERPLATES

• Send email • Send sms • Mark pin on a map • Dagger • …

Lot’s of lines of code needed for one logical operation

Page 4: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

SOLUTION

Page 5: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

CODE AUTOMATION Let the machine do the dirty work

Page 6: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

SOLUTION

Page 7: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

FOR THE BEGINNIG…

Page 8: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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); }

Page 9: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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); }

Page 10: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

• Save a template • Auto complete • Export is possible

LIVE TEMPLATES

Page 11: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

android.content.Intent view = new Intent(); view.setAction(Intent.ACTION_VIEW); view.setData(android.net.Uri.parse($url$)); startActivity(view);

LIVE TEMPLATES

Page 12: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

Can - static code - variables - modification

scripts

LIVE TEMPLATES

Cannot - generate

packages - generate classes - check members

Page 13: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

HEAVY CODE AUTOMATION

Page 14: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

ANDROID STUDIO PLUGIN - GARFIELD

Dagger, MVP, helper code…

Page 15: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

WRITING CODE THAT GENERATES CODE

Garfield Android Studio plugin

1)

2)3)4)

with Garfield

5)

Garfield exported

Page 16: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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

Page 17: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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(); } }

Page 18: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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; }

Page 19: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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}

Page 20: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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!"); } }

Page 21: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

SUMMARY

Copy & Paste Live Templates Android Studio Plugins

Be smart, be lazy

Page 22: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

Thank you!

Visit infinum.co or find us on social networks:

infinum.co infinumco infinumco infinum

Page 23: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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

Page 24: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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

Page 25: Infinum Android Talks #15 - Garfield Android Studio Plugin - Be Smart, Be Lazy

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