Skip to content

Commit

Permalink
Automatically initialize library with app startup (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasRobbers authored Mar 31, 2021
1 parent d693d48 commit 7a264f8
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 176 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
Change Log
==========

Version 1.3.0 *(2021-03-31)*
----------------------------
* New: Shortbread is now initialized automatically using [App Startup](https://developer.android.com/topic/libraries/app-startup)
* Deprecated: `Shortbread.create(context)` - no need to call this anymore as the shortcuts are set automatically during app startup
* Fix: Resource values sometimes were not properly read when the incremental annotation processing was incremental
* Improve: `ActivityLifecycleCallbacks` will not be registered if there are no method shortcuts


Version 1.2.0 *(2021-03-14)*
----------------------------
* New: Support for non-final resource IDs. See [README.md](https://github.com/MatthiasRobbers/shortbread##non-final-resource-ids)
* New: Support for non-final resource IDs. See [README.md](https://github.com/MatthiasRobbers/shortbread#non-final-resource-ids)
for detailed usage instructions.
* Update: `androidx.annotation:annotation` to `1.1.0`
* Update: Android Gradle plugin to `4.1.2`
Expand Down
24 changes: 5 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ class BooksActivity : Activity() {
}
```

To display the shortcuts, call `Shortbread.create(Context context)` as early as possible in the app, for
example in `onCreate` of a custom `Application`.

```kotlin
class App : Application() {

override fun onCreate() {
super.onCreate()

Shortbread.create(this)
}
}
```

Shortcuts can be customized with attributes, just like using the framework API.

<details open>
Expand Down Expand Up @@ -106,8 +92,8 @@ Shortbread is available on `mavenCentral()`.
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'com.github.matthiasrobbers:shortbread:1.2.0'
kapt 'com.github.matthiasrobbers:shortbread-compiler:1.2.0'
implementation 'com.github.matthiasrobbers:shortbread:1.3.0'
kapt 'com.github.matthiasrobbers:shortbread-compiler:1.3.0'
}
```
</details>
Expand All @@ -116,8 +102,8 @@ dependencies {

```groovy
dependencies {
implementation 'com.github.matthiasrobbers:shortbread:1.2.0'
annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.2.0'
implementation 'com.github.matthiasrobbers:shortbread:1.3.0'
annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.3.0'
}
```
</details>
Expand All @@ -137,7 +123,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.github.matthiasrobbers:shortbread-gradle-plugin:1.2.0'
classpath 'com.github.matthiasrobbers:shortbread-gradle-plugin:1.3.0'
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true

GROUP=com.github.matthiasrobbers
VERSION_NAME=1.2.0
VERSION_NAME=1.3.0

POM_URL=https://github.com/matthiasrobbers/shortbread
POM_SCM_URL=https://github.com/matthiasrobbers/shortbread
Expand Down
3 changes: 1 addition & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ buildscript {
repositories {
mavenCentral()
google()
mavenLocal()
}

dependencies {
classpath "com.github.matthiasrobbers:shortbread-gradle-plugin:${VERSION_NAME}"
classpath "com.github.matthiasrobbers:shortbread-gradle-plugin:1.2.0"
}
}

Expand Down
6 changes: 2 additions & 4 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".SampleApplication"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
android:label="@string/app_name">

<activity
android:name="com.example.shortbread.MainActivity">
Expand Down
15 changes: 0 additions & 15 deletions sample/src/main/java/com/example/shortbread/SampleApplication.java

This file was deleted.

49 changes: 29 additions & 20 deletions shortbread-compiler/src/main/java/shortbread/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ class CodeGenerator {
}

void generate() {
TypeSpec shortbread = TypeSpec.classBuilder("ShortbreadGenerated")
TypeSpec.Builder shortbreadBuilder = TypeSpec.classBuilder("ShortbreadGenerated")
.addAnnotation(AnnotationSpec.builder(suppressLint)
.addMember("value", "$S", "NewApi")
.addMember("value", "$S", "ResourceType")
.build())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(createShortcuts())
.addMethod(callMethodShortcut())
.build();
.addMethod(createShortcuts());

MethodSpec callMethodShortcut = callMethodShortcut();
if (callMethodShortcut != null) {
shortbreadBuilder.addMethod(callMethodShortcut);
}

TypeSpec shortbread = shortbreadBuilder.build();

JavaFile javaFile = JavaFile.builder("shortbread", shortbread)
.indent(" ")
Expand Down Expand Up @@ -231,24 +236,28 @@ private MethodSpec callMethodShortcut() {
}
}

final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("callMethodShortcut")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(activity, "activity");

for (final Map.Entry<String, List<String>> annotatedMethodName : classMethodsMap.entrySet()) {
ClassName activityClassName = ClassName.bestGuess(annotatedMethodName.getKey());
List<String> methodNames = annotatedMethodName.getValue();
methodBuilder.beginControlFlow("if (activity instanceof $T)", activityClassName);
for (final String methodName : methodNames) {
methodBuilder.beginControlFlow("if ($S.equals(activity.getIntent().getStringExtra($S)))", methodName, EXTRA_METHOD);
methodBuilder.addStatement("(($T) activity).$L()", activityClassName, methodName);
if (classMethodsMap.isEmpty()) {
return null;
} else {
final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("callMethodShortcut")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(activity, "activity");

for (final Map.Entry<String, List<String>> annotatedMethodName : classMethodsMap.entrySet()) {
ClassName activityClassName = ClassName.bestGuess(annotatedMethodName.getKey());
List<String> methodNames = annotatedMethodName.getValue();
methodBuilder.beginControlFlow("if (activity instanceof $T)", activityClassName);
for (final String methodName : methodNames) {
methodBuilder.beginControlFlow("if ($S.equals(activity.getIntent().getStringExtra($S)))", methodName, EXTRA_METHOD);
methodBuilder.addStatement("(($T) activity).$L()", activityClassName, methodName);
methodBuilder.endControlFlow();
}
methodBuilder.endControlFlow();
}
methodBuilder.endControlFlow();
}

return methodBuilder
.build();
return methodBuilder
.build();
}
}
}
2 changes: 1 addition & 1 deletion shortbread-compiler/src/main/java/shortbread/RScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Copied and stripped from Butter Knife, see
* https://github.com/JakeWharton/butterknife/blob/master/butterknife-compiler/src/main/java/butterknife/compiler/ButterKnifeProcessor.java
*/
public class RScanner extends TreeScanner {
class RScanner extends TreeScanner {

final Map<Integer, Id> resourceIds = new LinkedHashMap<>();

Expand Down
33 changes: 24 additions & 9 deletions shortbread-compiler/src/main/java/shortbread/ShortcutData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,41 @@ class ShortcutData {
ShortcutData(Shortcut shortcut, Map<Integer, Id> resourceIds) {
id = shortcut.id();

if (shortcut.shortLabelRes() != 0 && resourceIds.containsKey(shortcut.shortLabelRes())) {
shortLabelRes = resourceIds.get(shortcut.shortLabelRes());
if (shortcut.shortLabelRes() != 0) {
if (resourceIds.containsKey(shortcut.shortLabelRes())) {
shortLabelRes = resourceIds.get(shortcut.shortLabelRes());
} else {
shortLabelRes = new Id(shortcut.shortLabelRes());
}
}
shortLabelResName = shortcut.shortLabelResName();
shortLabel = shortcut.shortLabel();

if (shortcut.longLabelRes() != 0 && resourceIds.containsKey(shortcut.longLabelRes())) {
longLabelRes = resourceIds.get(shortcut.longLabelRes());
if (shortcut.longLabelRes() != 0) {
if (resourceIds.containsKey(shortcut.longLabelRes())) {
longLabelRes = resourceIds.get(shortcut.longLabelRes());
} else {
longLabelRes = new Id(shortcut.longLabelRes());
}
}
longLabelResName = shortcut.longLabelResName();
longLabel = shortcut.longLabel();

if (shortcut.icon() != 0 && resourceIds.containsKey(shortcut.icon())) {
icon = resourceIds.get(shortcut.icon());
if (shortcut.icon() != 0) {
if (resourceIds.containsKey(shortcut.icon())) {
icon = resourceIds.get(shortcut.icon());
} else {
icon = new Id(shortcut.icon());
}
}
iconResName = shortcut.iconResName();

if (shortcut.disabledMessageRes() != 0
&& resourceIds.containsKey(shortcut.disabledMessageRes())) {
disabledMessageRes = resourceIds.get(shortcut.disabledMessageRes());
if (shortcut.disabledMessageRes() != 0) {
if (resourceIds.containsKey(shortcut.disabledMessageRes())) {
disabledMessageRes = resourceIds.get(shortcut.disabledMessageRes());
} else {
disabledMessageRes = new Id(shortcut.disabledMessageRes());
}
}
disabledMessageResName = shortcut.disabledMessageResName();
disabledMessage = shortcut.disabledMessage();
Expand Down
1 change: 1 addition & 0 deletions shortbread/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ preBuild.dependsOn copyTestResources

dependencies {
api project(':shortbread-annotations')
api 'androidx.startup:startup-runtime:1.0.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.5.1'
testImplementation 'org.mockito:mockito-core:3.8.0'
Expand Down
19 changes: 18 additions & 1 deletion shortbread/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
<manifest package="com.github.matthiasrobbers.shortbread" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.github.matthiasrobbers.shortbread">

<application>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">

<meta-data
android:name="shortbread.ShortbreadInitializer"
android:value="androidx.startup" />
</provider>
</application>
</manifest>
Loading

0 comments on commit 7a264f8

Please sign in to comment.