Skip to content

Instantly share code, notes, and snippets.

interface PlaylistRepository: Repository {
fun addSongToPlaylist(songId: Long, playlistId: Long)
}
@AOrobator
AOrobator / RestoreShortcuts.java
Last active December 5, 2016 00:34
Implementing App Shortcuts - Restoring shortcuts
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager.getDynamicShortcuts().size() == 0) {
// Application restored. Need to re-publish dynamic shortcuts.
if (shortcutManager.getPinnedShortcuts().size() > 0) {
// Pinned shortcuts have been restored. Use
// updateShortcuts(List) to make sure they
// contain up-to-date information.
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 6, 2016 01:22
Implementing App Shortcuts - Tracking usage pt. 2
@TargetApi(N_MR1)
fun trackShortcutUsed(shortcutManager: ShortcutManager, constellation: Constellation) {
shortcutManager.reportShortcutUsed(constellation.name)
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(APP_CONTEXT)
val seenCount = sharedPrefs.getInt(constellation.name, 0)
sharedPrefs
.edit()
.putInt(constellation.name, seenCount + 1)
.apply()
updateShortcuts(shortcutManager)
@AOrobator
AOrobator / ConstellationDetailActivity.kt
Last active December 6, 2016 01:22
Implementing App Shortcuts - Tracking usage pt. 1
shortcutAction {
trackShortcutUsed(it, constellation)
}
@AOrobator
AOrobator / ConstellationDetailActivity.kt
Last active December 18, 2016 23:59
Implementing App Shortcuts - Enabling/Disabling Shortcuts
@TargetApi(N_MR1)
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.enable_shortcut -> shortcutAction {
it.enableShortcuts(listOf(constellation.name))
alertUser(R.string.shortcut_enabled)
}
R.id.disable_shortcut -> shortcutAction {
it.disableShortcuts(listOf(constellation.name))
alertUser(R.string.shortcut_disabled)
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 18, 2016 23:55
Implementing App Shortcuts - Updating your shortcuts
/**
* Sets the new Shortcut List by providing the three most visited
* constellations
* */
@TargetApi(N_MR1)
fun updateShortcuts(shortcutManager: ShortcutManager) {
shortcutManager.dynamicShortcuts =
Constellation
.values()
.sortedWith(compareBy { -getConstellationVisitedCount(it) })
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 6, 2016 01:27
Implementing App Shortcuts - ShortcutActions
inline fun shortcutAction(action: (ShortcutManager) -> Unit): Unit {
if (SDK_INT >= N_MR1) {
val shortcutManager = APP_CONTEXT.getSystemService(ShortcutManager::class.java)
action(shortcutManager)
}
}
@AOrobator
AOrobator / GettingTheShortcutManager.kt
Last active December 3, 2016 18:37
Implementing App Shortcuts - Getting the ShortcutManager
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
@AOrobator
AOrobator / shortcuts.xml
Created December 3, 2016 18:16
Implementing App Shortcuts - shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/random_constellation"
android:shortcutDisabledMessage="@string/shortcut_disabled_message"
android:shortcutId="random_constellation"
android:shortcutLongLabel="@string/shortcut_long_label"
android:shortcutShortLabel="@string/shortcut_short_label">
<!-- Each one of these intents needs an android:action, even if you don't
@AOrobator
AOrobator / AndroidManifest.xml
Last active December 3, 2016 18:15
Implementing Android App Shortcuts - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orobator.konstellations">
<application
android:name=".KonstellationsApplication"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"