|
| 1 | +package com.duckduckgo.widget |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.os.Bundle |
| 6 | +import androidx.appcompat.app.AppCompatActivity |
| 7 | +import androidx.core.content.pm.ShortcutInfoCompat |
| 8 | +import androidx.core.content.pm.ShortcutManagerCompat |
| 9 | +import androidx.core.graphics.drawable.IconCompat |
| 10 | +import com.duckduckgo.app.browser.BrowserActivity |
| 11 | +import com.duckduckgo.app.browser.R |
| 12 | + |
| 13 | +/** |
| 14 | + * This activity is capable of handling requests to create a shortcut. |
| 15 | + * Exists purely to be able to add the Duck.ai shortcut on the home screen from the widget picker. |
| 16 | + */ |
| 17 | +class DuckAiPinShortcutActivity : AppCompatActivity() { |
| 18 | + |
| 19 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 20 | + super.onCreate(savedInstanceState) |
| 21 | + |
| 22 | + if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) { |
| 23 | + val shortcutInfo = createShortcutInfo(this) |
| 24 | + ShortcutManagerCompat.reportShortcutUsed(this, SHORTCUT_ID) |
| 25 | + setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcutInfo)) |
| 26 | + } else { |
| 27 | + setResult(RESULT_OK) |
| 28 | + } |
| 29 | + |
| 30 | + finish() |
| 31 | + } |
| 32 | + |
| 33 | + private fun createShortcutInfo(context: Context): ShortcutInfoCompat { |
| 34 | + val shortLabel = getString(R.string.duckAiOnlyPinShortcutLabel) |
| 35 | + |
| 36 | + val shortcutIntent = BrowserActivity.intent(context, openDuckChat = true, duckChatSessionActive = true).apply { |
| 37 | + action = Intent.ACTION_VIEW |
| 38 | + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK |
| 39 | + } |
| 40 | + |
| 41 | + return ShortcutInfoCompat.Builder(context, SHORTCUT_ID) |
| 42 | + .setShortLabel(shortLabel) |
| 43 | + .setIcon(IconCompat.createWithResource(context, R.drawable.duckai_64)) |
| 44 | + .setIntent(shortcutIntent) |
| 45 | + .build() |
| 46 | + } |
| 47 | + |
| 48 | + companion object { |
| 49 | + private const val SHORTCUT_ID = "duck_ai_from_widget_picker" |
| 50 | + } |
| 51 | +} |
0 commit comments