Giter Site home page Giter Site logo

matthiasrobbers / shortbread Goto Github PK

View Code? Open in Web Editor NEW
1.8K 39.0 151.0 452 KB

Android library that creates app shortcuts from annotations

License: Apache License 2.0

Java 100.00%
android android-library annotation-processor app-shortcuts deep-links code-generation kotlin gradle-plugin

shortbread's Introduction

Shortbread

Android library that generates app shortcuts for activities and methods annotated with @Shortcut. No need to touch the manifest, create XML files or use the shortcut manager. Just annotate the code that you want the shortcut to call.

Sample

The four shortcuts above are produced by the following code:

@Shortcut(id = "movies", icon = R.drawable.ic_shortcut_movies, shortLabel = "Movies")
class MoviesActivity : Activity() {

    // ...

    @Shortcut(id = "add_movie", icon = R.drawable.ic_shortcut_add, shortLabel = "Add movie")
    fun addMovie() {
        // could show an AddMovieDialogFragment for example
    }
}
@Shortcut(id = "books", icon = R.drawable.ic_shortcut_books, shortLabel = "Books")
class BooksActivity : Activity() {

    // ...

    @Shortcut(id = "favorite_books", icon = R.drawable.ic_shortcut_favorite, shortLabel = "Favorite books")
    fun showFavoriteBooks() {
        // could show a FavoriteBooksFragment for example
    }
}

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

Kotlin
@Shortcut(
    id = "books",
    icon = R.drawable.ic_shortcut_books,
    shortLabel = "Books",
    shortLabelRes = R.string.shortcut_books_short_label,
    longLabel = "List of books",
    longLabelRes = R.string.shortcut_books_long_label,
    rank = 2, // order in list, relative to other shortcuts
    disabledMessage = "No books are available",
    disabledMessageRes = R.string.shortcut_books_disabled_message,
    enabled = true, // default
    backStack = [MainActivity::class, MainActivity::class],
    activity = MainActivity::class, // the launcher activity to which the shortcut should be attached
    action = "shortcut_books" // intent action to identify the shortcut from the launched activity
)
Java
@Shortcut(
    id = "books",
    icon = R.drawable.ic_shortcut_books,
    shortLabel = "Books",
    shortLabelRes = R.string.shortcut_books_short_label,
    longLabel = "List of books",
    longLabelRes = R.string.shortcut_books_long_label,
    rank = 2, // order in list, relative to other shortcuts
    disabledMessage = "No books are available",
    disabledMessageRes = R.string.shortcut_books_disabled_message,
    enabled = true, // default
    backStack = {MainActivity.class, LibraryActivity.class},
    activity = MainActivity.class, // the launcher activity to which the shortcut should be attached
    action = "shortcut_books" // intent action to identify the shortcut from the launched activity
)

Download

Shortbread is available on mavenCentral().

Kotlin
apply plugin: 'kotlin-kapt'

dependencies {
    implementation 'com.github.matthiasrobbers:shortbread:1.4.0'
    kapt 'com.github.matthiasrobbers:shortbread-compiler:1.4.0'
}
Java
dependencies {
    implementation 'com.github.matthiasrobbers:shortbread:1.4.0'
    annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.4.0'
}

Non-final resource IDs

If you are using resource IDs in @Shortcut attributes auch as shortLabelRes, which is recommended, you may see this warning in Android Studio:

Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them as annotation attributes.

If the annotation is located inside a library, the project won't even compile. To overcome this, add the Shortbread Gradle plugin and apply it to your modules:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.github.matthiasrobbers:shortbread-gradle-plugin:1.4.0'
    }
}
apply plugin: 'com.github.matthiasrobbers.shortbread'

Now make sure you use R2 instead of R inside all @Shortcut annotations. If you are using mipmap as shortcut icon resource type, switch to drawable by simply moving the resources from the mipmap- folders to the corresponding drawable- folders.

@Shortcut(icon = R2.drawable.ic_shortcut_movies, shortLabelRes = R2.string.label_movies)

The plugin uses the Butter Knife Gradle plugin to generate the R2 class with final values. Referencing them does not make the warning disappear (you can suppress it with @SuppressLint("NonConstantResourceId")), but most likely will be the only way to use resource IDs in annotations in the future.

Alternative to Gradle plugin

If for you can't (or don't want to) use the plugin, you can use the additional deprecated string attributes like iconResName, shortLabelResName and so on.

@Shortcut(iconResName = "ic_shortcut_movies", shortLabelResName = "label_movies")

License

Copyright 2017 Matthias Robbers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

shortbread's People

Contributors

matthiasrobbers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shortbread's Issues

Support for Nova Launcher

Nova Launcher allows for pre-7.1 devices to have app shortcuts. However, this library seems to not allow that to work.

not working in Kotlin

in this kotlin code this is not working

class MoviesActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)
    }

    @Shortcut(id = "add_movie1", icon = R.drawable.ic_settings, shortLabel = "Add movie1", rank = 4)
    fun addMovie() {

    }
}

but in java same it's working fine

public class MoviesActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
    }

    @Shortcut(id = "add_movie1", icon = R.drawable.ic_settings, shortLabel = "Add movie1", rank = 4)
    public void addMovie() {

    }
}

Support for dynamic app shortcuts

The library is based on the creation of dynamic app shortcuts but itself does not provide this functionality. The annotated shortcuts are always displayed.

Any ideas how the implement that feature?

Not working on Samsung J2

App Shortcuts does not appear in Samsung J2 while a long press on app icon, while they are appearing on my Moto X Play.

Is there a method call back when a shortcut is received by an activity?

Hey!

Is there/there should be a method that gets a call back declared in the activity in case one wants to further handle intent from the shortcut, so you can post process that from the call back method.

Would be really cool!

For example:

public onShortcutCalled(String shortcutId){
if(shortcutId == "profile"){
//Open profile
}
if(shortcutId == "cats"){
//Open cats
}
}

API support

Which is the lowest Android API it supports?

Do not special work

hi
i use shortbread in my project step by step but not change any thing and i when touch my app icon open my app like before!
my phone for test is sony xperia z1 android 5.1

Support for Kotlin

As of now, the library doesn't work properly in Kotlin. It gives the following error

An annotation argument should be compile-time constant. 

This is the code that i am using.

@Shortcut(id = "mastered", icon =  R.drawable.favorites, shortLabelRes = R.string.mastered_category)

The above error comes both for drawable and string resources.

Use strings for labels

Hi,

For icons, I can use project's like R.drawable.ic_shortcut_movies, but labels are hardcoded strings. How can I use project's strings like R.string.title?
Thanks for the lib!

Error:(8, 26) error: cannot find symbol class ShortcutInfo

package shortbread;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Icon;
import com.devzapps.irctcquicktatkalbooking.Activity.TicketViewer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressLint({
"NewApi",
"ResourceType"
})
public final class ShortbreadGenerated {
public static List<List> createShortcuts(Context context) {
List enabledShortcuts = new ArrayList<>();
List disabledShortcuts = new ArrayList<>();
enabledShortcuts.add(new ShortcutInfo.Builder(context, "ticket_list")
.setShortLabel("My Tickets")
.setIcon(Icon.createWithResource(context, 2130837744))
.setIntents(TaskStackBuilder.create(context)
.addParentStack(TicketViewer.class)
.addNextIntent(new Intent(context, TicketViewer.class)
.setAction(Intent.ACTION_VIEW))
.getIntents())
.setRank(0)
.build());
return Arrays.asList(enabledShortcuts, disabledShortcuts);
}

public static void callMethodShortcut(Activity activity) {
}

}

Error on ShortBreadGenerated.java

Support for Java 8

Seeing this warning

Warning:Supported source version 'RELEASE_7' from annotation processor 'shortbread.ShortcutProcessor' less than -source '1.8'

Guessing its gotta do with the Java version the lib is written to

Crashing on onCreate() Application

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference
at shortbread.Shortbread.setShortcuts(Shortbread.java:66)
at shortbread.Shortbread.create(Shortbread.java:48)
at com.popstack.mvoter2015.MVoterApp.onCreate(MVoterApp.java:22)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
at android.app.ActivityThread.-wrap2(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method)

Getting java.lang.ClassNotFoundException while trying to init the library

I'm getting this exception when I call Shortbread.create(this); in my Application class.
Am I missing something?

OS: 7.1
Device: Google Pixel

java.lang.ClassNotFoundException: shortbread.ShortbreadGenerated
W/System.err:     at java.lang.Class.classForName(Native Method)
W/System.err:     at java.lang.Class.forName(Class.java:400)
W/System.err:     at java.lang.Class.forName(Class.java:326)
W/System.err:     at shortbread.Shortbread.create(Shortbread.java:37)
Caused by: java.lang.ClassNotFoundException: Didn't find class "shortbread.ShortbreadGenerated"

Error:(9, 36) error: cannot find symbol class Activity

package shortbread;

import android.annotation.SuppressLint;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Icon;
import com.abc.TestApp.Activity;
import com.abc.TestApp.Activity.ActivityTwo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressLint({
"NewApi",
"ResourceType"
})
public final class ShortbreadGenerated {
public static List<List> createShortcuts(Context context) {
List enabledShortcuts = new ArrayList<>();
List disabledShortcuts = new ArrayList<>();
enabledShortcuts.add(new ShortcutInfo.Builder(context, "test")
.setShortLabel(context.getString(2131230812))
.setIcon(Icon.createWithResource(context, 2130837607))
.setIntents(TaskStackBuilder.create(context)
.addParentStack(ActivityTwo.class)
.addNextIntent(new Intent(Intent.ACTION_VIEW).setClass(context, Activity.ActivityOne.class))
.addNextIntent(new Intent(Intent.ACTION_VIEW).setClass(context, Activity.ActivityThree.class))
.addNextIntent(new Intent(context, ActivityTwo.class)
.setAction(Intent.ACTION_VIEW))
.getIntents())
.setRank(2)
.build());
return Arrays.asList(enabledShortcuts, disabledShortcuts);
}

public static void callMethodShortcut(android.app.Activity activity) {
}

}

error in ShortbreadGenerated.java
Error:(9, 36) error: cannot find symbol class Activity
error is in this line "import com.abc.TestApp.Activity;"

Code is

@shortcut(id = "test",
icon = R.drawable.ic_test,
shortLabelRes = R.string.shortcut_test,
rank = 2,
backStack = {ActivityOne.class, ActivityThree.class}
)

If I remove backstack, then its working fine. But, with backstack or activity its giving the error

One more thing I just want to mention, that I have different packages for different activities.

change direction and font

Hi there, is it possible to change direction on shortcut items to RTL? can i change the typeface of titles?
thanks in advanced

Ability to remove and add shortcuts [Enhancement]

Hey there, cool lib!

Not sure if this is the same as Dynamic shortcuts, but essentially adding and removing shortcuts from the application.

Most apps are very dynamic and change views/layouts based on conditions..this would be really helpful

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.