Skip to content

Commit

Permalink
Add a shortcut when creating ChuckerInterceptor (#588)
Browse files Browse the repository at this point in the history
* Add a shortcut when creating ChuckerInterceptor

* Rename ic resource

* Replace hardcode

* Add other dpi icons

* Modify `createShortcut`

* Add a flag in builder to decide if enable create shortcut

* Add empty func in library-no-op

* Modify comments

* Check shortcut exist before add & catch exception

* Refine code

* Log exception

* Api dump

* Catch specific exceptions
  • Loading branch information
Goooler authored Mar 26, 2021
1 parent c3604b5 commit 664730a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions library-no-op/api/library-no-op.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions library/src/main/java/com/chuckerteam/chucker/api/Chucker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -88,6 +95,7 @@ public class ChuckerInterceptor private constructor(
internal var alwaysReadResponseBody = false
internal var headersToRedact = emptySet<String>()
internal var decoders = emptyList<BodyDecoder>()
internal var createShortcut = true

/**
* Sets the [ChuckerCollector] to customize data retention.
Expand Down Expand Up @@ -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.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@
<string name="chucker_request_not_ready">The request isn\'t ready for sharing or saving</string>
<string name="chucker_request_is_empty">This request is empty</string>
<string name="chucker_response_is_empty">This response is empty</string>
<string name="chucker_shortcut_label">Open chucker</string>
</resources>

0 comments on commit 664730a

Please sign in to comment.