Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Add static shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeontaek committed Sep 9, 2019
1 parent e03495d commit b3391c7
Show file tree
Hide file tree
Showing 46 changed files with 229 additions and 201 deletions.
9 changes: 9 additions & 0 deletions app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
<activity android:name="com.facebook.flipper.android.diagnostics.FlipperDiagnosticActivity"
android:exported="true"/>
<service android:name="com.facebook.flipper.plugins.leakcanary.RecordLeakService" />

<activity
android:name=".HomeActivity">
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts_debug"
tools:node="replace" />
</activity>

</application>

</manifest>
29 changes: 29 additions & 0 deletions app/src/debug/res/xml/shortcuts_debug.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="open_new_tab"
android:enabled="true"
android:icon="@mipmap/ic_static_shortcut_tab"
android:shortcutShortLabel="@string/home_screen_shortcut_open_new_tab"
android:shortcutLongLabel="@string/home_screen_shortcut_open_new_tab">
<intent
android:action="org.mozilla.fenix.OPEN_TAB"
android:targetPackage="org.mozilla.fenix.debug"
android:targetClass="org.mozilla.fenix.IntentReceiverActivity" />
</shortcut>
<shortcut
android:shortcutId="open_new_private_tab"
android:enabled="true"
android:icon="@mipmap/ic_static_shortcut_private_tab"
android:shortcutShortLabel="@string/home_screen_shortcut_open_new_private_tab"
android:shortcutLongLabel="@string/home_screen_shortcut_open_new_private_tab">
<intent
android:action="org.mozilla.fenix.OPEN_PRIVATE_TAB"
android:targetPackage="org.mozilla.fenix.debug"
android:targetClass="org.mozilla.fenix.IntentReceiverActivity" />
</shortcut>
</shortcuts>
41 changes: 4 additions & 37 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,10 @@
<data android:scheme="fenix"
android:host="make_default_browser"/>
</intent-filter>
</activity>

<activity
android:name=".PrivateLaunchActivity"
android:taskAffinity=""
android:label="@string/app_name_private"
android:icon="@mipmap/ic_launcher_private"
android:roundIcon="@mipmap/ic_launcher_private_round"
android:targetActivity=".PrivateLaunchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="fenix"
android:host="home"/>
<data android:scheme="fenix"
android:host="settings"/>
<data android:scheme="fenix"
android:host="turn_on_sync"/>
<data android:scheme="fenix"
android:host="settings_search_engine"/>
<data android:scheme="fenix"
android:host="settings_accessibility"/>
<data android:scheme="fenix"
android:host="settings_delete_browsing_data"/>
<data android:scheme="fenix"
android:host="enable_private_browsing"/>
<data android:scheme="fenix"
android:host="open"/>
<data android:scheme="fenix"
android:host="make_default_browser"/>
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>

<activity
Expand Down Expand Up @@ -161,6 +127,7 @@
android:name="org.mozilla.fenix.alias.IntentReceiverActivity"
android:label="@string/app_name_private"
android:icon="@mipmap/ic_launcher_private"
android:roundIcon="@mipmap/ic_launcher_private_round"
android:targetActivity=".IntentReceiverActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand Down
Binary file modified app/src/main/ic_launcher_private-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 14 additions & 47 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

package org.mozilla.fenix

import android.annotation.TargetApi
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
Expand All @@ -20,9 +15,6 @@ import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PROTECTED
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDestination
import androidx.navigation.fragment.NavHostFragment
Expand All @@ -47,7 +39,10 @@ import org.mozilla.fenix.components.isSentryEnabled
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.SentryBreadcrumbsRecorder
import org.mozilla.fenix.exceptions.ExceptionsFragmentDirections
import org.mozilla.fenix.ext.*
import org.mozilla.fenix.ext.alreadyOnDestination
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.intent.CrashReporterIntentProcessor
import org.mozilla.fenix.home.intent.DeepLinkIntentProcessor
Expand All @@ -64,7 +59,6 @@ import org.mozilla.fenix.share.ShareFragment
import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.utils.Settings
import java.util.*

@SuppressWarnings("TooManyFunctions", "LargeClass")
open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback {
Expand Down Expand Up @@ -176,11 +170,17 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback
}
}

/**
* External sources such as 3rd party links and shortcuts use this function to enter
* private mode directly before the content view is created.
*/
private fun setPrivateMode() {
val startPrivateMode = intent?.toSafeIntent()?.getBooleanExtra(PRIVATE_BROWSING_MODE, false)
if (startPrivateMode == true) {
intent.putExtra(PRIVATE_BROWSING_MODE, false)
Settings.getInstance(this).usePrivateMode = true
intent?.toSafeIntent()?.let {
if (it.hasExtra(PRIVATE_BROWSING_MODE)) {
val startPrivateMode = it.getBooleanExtra(PRIVATE_BROWSING_MODE, false)
Settings.getInstance(this).usePrivateMode = startPrivateMode
intent.removeExtra(PRIVATE_BROWSING_MODE)
}
}
}

Expand Down Expand Up @@ -354,39 +354,6 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback
}
}

fun showShortcut() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
val icon = IconCompat.createWithResource(this, R.mipmap.ic_launcher_private)
val shortcut = ShortcutInfoCompat.Builder(this, UUID.randomUUID().toString())
.setShortLabel(getString(R.string.app_name_private))
.setLongLabel(getString(R.string.app_name_private))
.setIcon(icon)
.setIntent(Intent(this, HomeActivity::class.java).apply {
action = Intent.ACTION_VIEW
//flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra(HomeActivity.PRIVATE_BROWSING_MODE, true)
putExtra(HomeActivity.OPEN_TO_SEARCH, StartSearchIntentProcessor.PRIVATE_BROWSING_LAUNCHER)
})
.build()
val intentTest = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val intentSender = PendingIntent.getActivity(this, 0, intentTest, PendingIntent.FLAG_UPDATE_CURRENT).intentSender
updateShortcut26(this, shortcut)
ShortcutManagerCompat.requestPinShortcut(this, shortcut, intentSender)
}
}

@TargetApi(26) // Add this to make lint happy
private fun updateShortcut26(context: Context, shortcut: ShortcutInfoCompat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
val list = ArrayList<ShortcutInfo>()
list.add(shortcut.toShortcutInfo())
shortcutManager.updateShortcuts(list)
}
}
}

companion object {
const val OPEN_TO_BROWSER = "open_to_browser"
const val OPEN_TO_BROWSER_AND_LOAD = "open_to_browser_and_load"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.mozilla.fenix.customtabs.AuthCustomTabActivity
import org.mozilla.fenix.customtabs.CustomTabActivity
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.home.intent.StartSearchIntentProcessor
import org.mozilla.fenix.utils.Settings

class IntentReceiverActivity : Activity() {
Expand Down Expand Up @@ -82,6 +83,14 @@ class IntentReceiverActivity : Activity() {
// from a session that the user already "erased".
intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY == 0
}
intent.action == ACTION_OPEN_TAB || intent.action == ACTION_OPEN_PRIVATE_TAB -> {
intent.setClassName(applicationContext, HomeActivity::class.java.name)
val startPrivateMode = (intent.action == ACTION_OPEN_PRIVATE_TAB)
intent.putExtra(HomeActivity.PRIVATE_BROWSING_MODE, startPrivateMode)
intent.putExtra(HomeActivity.OPEN_TO_SEARCH, StartSearchIntentProcessor.PRIVATE_BROWSING_LAUNCHER)
intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
true
}
else -> {
intent.setClassName(applicationContext, HomeActivity::class.java.name)
false
Expand Down Expand Up @@ -128,5 +137,7 @@ class IntentReceiverActivity : Activity() {
const val SPEECH_PROCESSING = "speech_processing"
const val PREVIOUS_INTENT = "previous_intent"
const val PRIVATE_MODE = "private"
const val ACTION_OPEN_TAB = "org.mozilla.fenix.OPEN_TAB"
const val ACTION_OPEN_PRIVATE_TAB = "org.mozilla.fenix.OPEN_PRIVATE_TAB"
}
}
26 changes: 0 additions & 26 deletions app/src/main/java/org/mozilla/fenix/PrivateLaunchActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.mozilla.fenix.components

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.home.intent.StartSearchIntentProcessor
import java.util.UUID

/**
* Handles the creation of pinned shortcuts.
*/
class PrivateShortcutCreateManager {

companion object {
fun createPrivateShortcut(context: Context) {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
val icon = IconCompat.createWithResource(context, R.mipmap.ic_launcher_private_round)
val shortcut = ShortcutInfoCompat.Builder(context, UUID.randomUUID().toString())
.setShortLabel(context.getString(R.string.app_name_private))
.setLongLabel(context.getString(R.string.app_name_private))
.setIcon(icon)
.setIntent(Intent(context, HomeActivity::class.java).apply {
action = Intent.ACTION_VIEW
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra(HomeActivity.PRIVATE_BROWSING_MODE, true)
putExtra(HomeActivity.OPEN_TO_SEARCH, StartSearchIntentProcessor.PRIVATE_BROWSING_SHORTCUT)
})
.build()
val homeScreenIntent = Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val intentSender = PendingIntent
.getActivity(context, 0, homeScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
.intentSender
ShortcutManagerCompat.requestPinShortcut(context, shortcut, intentSender)
}
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import androidx.transition.TransitionInflater
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.pbm_shortcut_popup.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -63,6 +62,7 @@ import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.collections.CreateCollectionViewModel
import org.mozilla.fenix.collections.SaveCollectionStep
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.PrivateShortcutCreateManager
import org.mozilla.fenix.components.TabCollectionStorage
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.nav
Expand Down Expand Up @@ -297,7 +297,8 @@ class HomeFragment : Fragment(), AccountObserver {

requireComponents.backgroundServices.accountManager.register(this, owner = this)

if (Settings.getInstance(context!!).showPrivateModeContextualFeatureRecommender && browsingModeManager.mode.isPrivate) {
if (Settings.getInstance(context!!).showPrivateModeContextualFeatureRecommender &&
browsingModeManager.mode.isPrivate) {
recommendPrivateBrowsingShortcut()
}
}
Expand Down Expand Up @@ -563,7 +564,7 @@ class HomeFragment : Fragment(), AccountObserver {
)
layout.findViewById<Button>(R.id.cfr_pos_button).apply {
setOnClickListener {
(activity as HomeActivity).showShortcut()
PrivateShortcutCreateManager.createPrivateShortcut(context)
trackingOnboarding.dismiss()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ class StartSearchIntentProcessor(
companion object {
const val SEARCH_WIDGET = "search_widget"
const val PRIVATE_BROWSING_LAUNCHER = "private_browsing_launcher"
const val PRIVATE_BROWSING_SHORTCUT = "private_browsing_shortcut"
}
}
Loading

0 comments on commit b3391c7

Please sign in to comment.