diff --git a/library-no-op/api/library-no-op.api b/library-no-op/api/library-no-op.api index 97007b4a2..5804b3a1a 100644 --- a/library-no-op/api/library-no-op.api +++ b/library-no-op/api/library-no-op.api @@ -32,6 +32,7 @@ public final class com/chuckerteam/chucker/api/ChuckerInterceptor$Builder { public final fun alwaysReadResponseBody (Z)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun build ()Lcom/chuckerteam/chucker/api/ChuckerInterceptor; public final fun collector (Lcom/chuckerteam/chucker/api/ChuckerCollector;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; + public final fun createShortcut (Z)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun maxContentLength (J)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun redactHeaders (Ljava/lang/Iterable;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun redactHeaders ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; diff --git a/library-no-op/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt b/library-no-op/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt index 1396d5b71..6108de225 100644 --- a/library-no-op/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt +++ b/library-no-op/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt @@ -4,7 +4,6 @@ import android.content.Context import okhttp3.Interceptor import okhttp3.Response import java.io.IOException -import kotlin.jvm.Throws /** * No-op implementation. @@ -44,6 +43,8 @@ public class ChuckerInterceptor private constructor( public fun addBodyDecoder(decoder: Any): Builder = this + public fun createShortcut(enable: Boolean): Builder = this + public fun build(): ChuckerInterceptor = ChuckerInterceptor(this) } } diff --git a/library/api/library.api b/library/api/library.api index bdf6b1401..5ba5ec7f3 100644 --- a/library/api/library.api +++ b/library/api/library.api @@ -32,6 +32,7 @@ public final class com/chuckerteam/chucker/api/ChuckerInterceptor$Builder { public final fun alwaysReadResponseBody (Z)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun build ()Lcom/chuckerteam/chucker/api/ChuckerInterceptor; public final fun collector (Lcom/chuckerteam/chucker/api/ChuckerCollector;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; + public final fun createShortcut (Z)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun maxContentLength (J)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun redactHeaders (Ljava/lang/Iterable;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; public final fun redactHeaders ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder; diff --git a/library/src/main/java/com/chuckerteam/chucker/api/Chucker.kt b/library/src/main/java/com/chuckerteam/chucker/api/Chucker.kt index bae02c84b..20a589b32 100644 --- a/library/src/main/java/com/chuckerteam/chucker/api/Chucker.kt +++ b/library/src/main/java/com/chuckerteam/chucker/api/Chucker.kt @@ -2,16 +2,25 @@ package com.chuckerteam.chucker.api import android.content.Context import android.content.Intent +import android.content.pm.ShortcutInfo +import android.content.pm.ShortcutManager +import android.graphics.drawable.Icon +import android.os.Build import android.util.Log +import com.chuckerteam.chucker.R import com.chuckerteam.chucker.internal.support.Logger import com.chuckerteam.chucker.internal.support.NotificationHelper import com.chuckerteam.chucker.internal.ui.MainActivity +import java.lang.IllegalArgumentException +import java.lang.IllegalStateException /** * Chucker methods and utilities to interact with the library. */ public object Chucker { + private const val SHORTCUT_ID = "chuckerShortcutId" + /** * Check if this instance is the operation one or no-op. * @return `true` if this is the operation instance. @@ -30,6 +39,35 @@ public object Chucker { .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } + /** + * Create a shortcut to launch Chucker UI. + * @param context An Android [Context]. + */ + internal fun createShortcut(context: Context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + context.getSystemService(ShortcutManager::class.java)?.let { sm -> + sm.dynamicShortcuts.forEach { + if (it.id == SHORTCUT_ID) return@let + } + val shortcut = ShortcutInfo.Builder(context, SHORTCUT_ID) + .setShortLabel(context.getString(R.string.chucker_shortcut_label)) + .setLongLabel(context.getString(R.string.chucker_shortcut_label)) + .setIcon( + Icon.createWithResource(context, R.mipmap.chucker_ic_launcher_round) + ) + .setIntent(getLaunchIntent(context).setAction(Intent.ACTION_VIEW)) + .build() + try { + sm.addDynamicShortcuts(listOf(shortcut)) + } catch (e: IllegalArgumentException) { + Logger.warn("ShortcutManager addDynamicShortcuts failed ", e) + } catch (e: IllegalStateException) { + Logger.warn("ShortcutManager addDynamicShortcuts failed ", e) + } + } + } + } + /** * Dismisses all previous Chucker notifications. */ diff --git a/library/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt b/library/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt index 6974d489a..e3eba99bf 100755 --- a/library/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt +++ b/library/src/main/java/com/chuckerteam/chucker/api/ChuckerInterceptor.kt @@ -7,6 +7,7 @@ import com.chuckerteam.chucker.internal.support.CacheDirectoryProvider import com.chuckerteam.chucker.internal.support.PlainTextDecoder import com.chuckerteam.chucker.internal.support.RequestProcessor import com.chuckerteam.chucker.internal.support.ResponseProcessor +import com.chuckerteam.chucker.internal.ui.MainActivity import okhttp3.Interceptor import okhttp3.Response import java.io.IOException @@ -53,6 +54,12 @@ public class ChuckerInterceptor private constructor( decoders, ) + init { + if (builder.createShortcut) { + Chucker.createShortcut(builder.context) + } + } + /** Adds [headerName] into [headersToRedact] */ public fun redactHeader(vararg headerName: String) { headersToRedact.addAll(headerName) @@ -88,6 +95,7 @@ public class ChuckerInterceptor private constructor( internal var alwaysReadResponseBody = false internal var headersToRedact = emptySet() internal var decoders = emptyList() + internal var createShortcut = true /** * Sets the [ChuckerCollector] to customize data retention. @@ -140,6 +148,14 @@ public class ChuckerInterceptor private constructor( this.decoders += decoder } + /** + * If set to `true`, [ChuckerInterceptor] will create a shortcut for your app + * which can make you easily to access chucker's [MainActivity] + */ + public fun createShortcut(enable: Boolean): Builder = apply { + this.createShortcut = enable + } + /** * Sets provider of a directory where Chucker will save temporary responses * before processing them. diff --git a/library/src/main/res/mipmap-hdpi/chucker_ic_launcher_round.png b/library/src/main/res/mipmap-hdpi/chucker_ic_launcher_round.png new file mode 100644 index 000000000..6c2d234fe Binary files /dev/null and b/library/src/main/res/mipmap-hdpi/chucker_ic_launcher_round.png differ diff --git a/library/src/main/res/mipmap-xhdpi/chucker_ic_launcher_round.png b/library/src/main/res/mipmap-xhdpi/chucker_ic_launcher_round.png new file mode 100644 index 000000000..60099ccf3 Binary files /dev/null and b/library/src/main/res/mipmap-xhdpi/chucker_ic_launcher_round.png differ diff --git a/library/src/main/res/mipmap-xxhdpi/chucker_ic_launcher_round.png b/library/src/main/res/mipmap-xxhdpi/chucker_ic_launcher_round.png new file mode 100644 index 000000000..4dfb35b98 Binary files /dev/null and b/library/src/main/res/mipmap-xxhdpi/chucker_ic_launcher_round.png differ diff --git a/library/src/main/res/mipmap-xxxhdpi/chucker_ic_launcher_round.png b/library/src/main/res/mipmap-xxxhdpi/chucker_ic_launcher_round.png new file mode 100644 index 000000000..ffc5e9d49 Binary files /dev/null and b/library/src/main/res/mipmap-xxxhdpi/chucker_ic_launcher_round.png differ diff --git a/library/src/main/res/values/strings.xml b/library/src/main/res/values/strings.xml index b73f051e0..989870425 100644 --- a/library/src/main/res/values/strings.xml +++ b/library/src/main/res/values/strings.xml @@ -57,4 +57,5 @@ The request isn\'t ready for sharing or saving This request is empty This response is empty + Open chucker