Skip to content

Commit 7678330

Browse files
committed
Jelly: Desktop shortcuts
Load "Desktop site" version of a shortcut at launch time Change-Id: If8335007048a23b103fcaf5d78305d48d9f929b1
1 parent 8bf990f commit 7678330

7 files changed

Lines changed: 93 additions & 24 deletions

File tree

app/src/main/java/org/lineageos/jelly/MainActivity.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
198198
}
199199
private var urlIcon: Bitmap? = null
200200
private var url: String? = null
201+
private var desktopMode = false
201202
private var incognito = false
202203
private var isFullscreenPwa = false
203-
private var desktopMode = false
204204
private var customView: View? = null
205205
private var fullScreenCallback: CustomViewCallback? = null
206206
private lateinit var menuDialog: MenuDialog
@@ -232,20 +232,20 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
232232
true -> intent.getStringExtra(IntentUtils.EXTRA_PAGE_URL)
233233
false -> intent.dataString
234234
}
235+
desktopMode = sharedPreferencesExt.desktopShortcuts.contains(shortcutId)
235236
incognito = intent.getBooleanExtra(IntentUtils.EXTRA_INCOGNITO, false)
236237
isFullscreenPwa = intent.getStringExtra(MANIFEST_DISPLAY)?.let {
237238
ALLOWED_FULLSCREEN_PWA_VALUES.contains(it)
238239
} ?: false
239-
desktopMode = false
240240

241241
// Restore from previous instance
242242
savedInstanceState?.let {
243243
url = url?.takeIf { url ->
244244
url.isNotEmpty()
245245
} ?: it.getString(IntentUtils.EXTRA_URL, null)
246+
desktopMode = it.getBoolean(IntentUtils.EXTRA_DESKTOP_MODE, desktopMode)
246247
incognito = it.getBoolean(IntentUtils.EXTRA_INCOGNITO, incognito)
247248
isFullscreenPwa = it.getBoolean(IntentUtils.EXTRA_FULLSCREEN_PWA, isFullscreenPwa)
248-
desktopMode = it.getBoolean(IntentUtils.EXTRA_DESKTOP_MODE, false)
249249
}
250250

251251
// Make sure prefs are set before loading them
@@ -258,7 +258,7 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
258258

259259
urlBarLayout.isIncognito = incognito
260260

261-
menuDialog = MenuDialog(this) { option: MenuDialog.Option ->
261+
menuDialog = MenuDialog(this, desktopMode) { option: MenuDialog.Option ->
262262
when (option) {
263263
MenuDialog.Option.BACK -> webView.goBack()
264264
MenuDialog.Option.FORWARD -> webView.goForward()
@@ -316,6 +316,14 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
316316
menuDialog.isDesktopMode = desktopMode
317317
}
318318

319+
MenuDialog.Option.DESKTOP_SHORTCUTS -> Intent(
320+
this,
321+
BackgroundShortcutActivity::class.java
322+
).apply {
323+
putExtra(BackgroundShortcutActivity.DESKTOP_SHORTCUTS, true)
324+
startActivity(this)
325+
}
326+
319327
MenuDialog.Option.BACKGROUND_SHORTCUTS -> backgroundShortcutLauncher.launch(
320328
Intent(
321329
this,
@@ -468,7 +476,7 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
468476
constraintLayout.addView(webView)
469477
setUiMode()
470478
if (webView.initialized) return
471-
webView.init(this, urlBarLayout, incognito)
479+
webView.init(this, urlBarLayout, desktopMode, incognito)
472480
if (url != null || sharedPreferencesExt.homePageAutoload) {
473481
webView.loadUrl(url ?: sharedPreferencesExt.homePage)
474482
}

app/src/main/java/org/lineageos/jelly/shortcut/BackgroundShortcutActivity.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.lineageos.jelly.shortcut
77

88
import android.content.ComponentName
9-
import android.content.Context
109
import android.content.Intent
1110
import android.content.ServiceConnection
1211
import android.os.Build
@@ -23,6 +22,7 @@ import androidx.lifecycle.lifecycleScope
2322
import androidx.lifecycle.repeatOnLifecycle
2423
import androidx.recyclerview.widget.LinearLayoutManager
2524
import androidx.recyclerview.widget.RecyclerView
25+
import kotlinx.coroutines.Dispatchers
2626
import kotlinx.coroutines.launch
2727
import org.lineageos.jelly.R
2828
import org.lineageos.jelly.utils.IntentUtils
@@ -47,6 +47,7 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
4747
private lateinit var selected: MutableSet<String>
4848
private lateinit var backgroundShortcuts: List<BackgroundShortcut>
4949

50+
private var desktopShortcuts: Boolean = false
5051
private var shortcutId: String? = null
5152
private var backgroundShortcutService: BackgroundShortcutService? = null
5253
private var serviceConnection = object : ServiceConnection {
@@ -70,8 +71,19 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
7071
setDisplayShowHomeEnabled(true)
7172
}
7273

74+
desktopShortcuts = intent.getBooleanExtra(DESKTOP_SHORTCUTS, false)
7375
shortcutId = intent.getStringExtra(IntentUtils.EXTRA_SHORTCUT_ID)
7476

77+
title = when (desktopShortcuts) {
78+
true -> {
79+
lifecycleScope.launch(Dispatchers.IO) {
80+
refresh()
81+
}
82+
getString(R.string.desktop_shortcuts_title)
83+
}
84+
false -> getString(R.string.background_shortcuts_title)
85+
}
86+
7587
adapter.getSelected = { id -> selected.contains(id) }
7688
adapter.onLayoutClick = { id ->
7789
val contains = selected.contains(id)
@@ -111,13 +123,17 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
111123

112124
override fun onStart() {
113125
super.onStart()
114-
val intent = Intent(this, BackgroundShortcutService::class.java)
115-
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
126+
if (!desktopShortcuts) {
127+
val intent = Intent(this, BackgroundShortcutService::class.java)
128+
bindService(intent, serviceConnection, BIND_AUTO_CREATE)
129+
}
116130
}
117131

118132
override fun onStop() {
119133
super.onStop()
120-
if (backgroundShortcutService != null) unbindService(serviceConnection)
134+
if (!desktopShortcuts && backgroundShortcutService != null) {
135+
unbindService(serviceConnection)
136+
}
121137
}
122138

123139
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -142,10 +158,8 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
142158
}
143159

144160
private fun refresh() {
145-
backgroundShortcutService?.let { service ->
146-
val running = service.getRunning()
147-
model.next(running)
148-
}
161+
val running = backgroundShortcutService?.getRunning() ?: setOf()
162+
model.next(running)
149163
}
150164

151165
private fun save() {
@@ -154,10 +168,14 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
154168
finish()
155169
}
156170

157-
private fun getSavedSelected(): Set<String> = sharedPreferencesExt.backgroundShortcuts
171+
private fun getSavedSelected(): Set<String> = when (desktopShortcuts) {
172+
true -> sharedPreferencesExt.desktopShortcuts
173+
false -> sharedPreferencesExt.backgroundShortcuts
174+
}
158175

159-
private fun setSaveSelected(selected: Set<String>) {
160-
sharedPreferencesExt.backgroundShortcuts = selected
176+
private fun setSaveSelected(selected: Set<String>) = when (desktopShortcuts) {
177+
true -> sharedPreferencesExt.desktopShortcuts = selected
178+
false -> sharedPreferencesExt.backgroundShortcuts = selected
161179
}
162180

163181
private fun getValidSelected(selected: Set<String>): Set<String> {
@@ -166,4 +184,8 @@ class BackgroundShortcutActivity : AppCompatActivity(R.layout.activity_backgroun
166184
selected.forEach { if (list.contains(it)) validSelected.add(it) }
167185
return validSelected.toSet()
168186
}
187+
188+
companion object {
189+
const val DESKTOP_SHORTCUTS = "desktop_shortcuts"
190+
}
169191
}

app/src/main/java/org/lineageos/jelly/ui/MenuDialog.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.lineageos.jelly.R
1717

1818
class MenuDialog(
1919
context: Context,
20+
desktopMode: Boolean,
2021
private val onClickListener: (option: Option) -> Unit
2122
) {
2223
private val layoutInflater = LayoutInflater.from(context)
@@ -45,6 +46,9 @@ class MenuDialog(
4546
private val findInPageButton by lazy { view.findViewById<LinearLayout>(R.id.findInPageButton) }
4647
private val desktopViewSwitch by lazy { view.findViewById<MaterialSwitch>(R.id.desktopViewSwitch) }
4748
private val printButton by lazy { view.findViewById<LinearLayout>(R.id.printButton) }
49+
private val desktopShortcutsButton by lazy {
50+
view.findViewById<LinearLayout>(R.id.desktopShortcutsButton)
51+
}
4852
private val backgroundShortcutsButton by lazy {
4953
view.findViewById<LinearLayout>(R.id.backgroundShortcutsButton)
5054
}
@@ -81,6 +85,7 @@ class MenuDialog(
8185
FIND_IN_PAGE,
8286
DESKTOP_VIEW,
8387
PRINT,
88+
DESKTOP_SHORTCUTS,
8489
BACKGROUND_SHORTCUTS,
8590
SETTINGS,
8691
}
@@ -101,8 +106,12 @@ class MenuDialog(
101106

102107
addToHomeScreenButton.setOnClickListener { triggerOption(Option.ADD_TO_HOME_SCREEN) }
103108
findInPageButton.setOnClickListener { triggerOption(Option.FIND_IN_PAGE) }
104-
desktopViewSwitch.setOnCheckedChangeListener { _, _ -> triggerOption(Option.DESKTOP_VIEW) }
109+
desktopViewSwitch.apply {
110+
isChecked = desktopMode
111+
setOnCheckedChangeListener { _, _ -> triggerOption(Option.DESKTOP_VIEW) }
112+
}
105113
printButton.setOnClickListener { triggerOption(Option.PRINT) }
114+
desktopShortcutsButton.setOnClickListener { triggerOption(Option.DESKTOP_SHORTCUTS) }
106115
backgroundShortcutsButton.setOnClickListener { triggerOption(Option.BACKGROUND_SHORTCUTS) }
107116
settingsButton.setOnClickListener { triggerOption(Option.SETTINGS) }
108117
}

app/src/main/java/org/lineageos/jelly/utils/SharedPreferencesExt.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class SharedPreferencesExt(context: Context) {
2727
}
2828
}
2929

30+
var desktopShortcuts: Set<String>
31+
get() = sharedPreferences.getStringSet(DESKTOP_SHORTCUTS_KEY, setOf<String>())!!
32+
set(value) = sharedPreferences.edit { putStringSet(DESKTOP_SHORTCUTS_KEY, value) }
33+
3034
var backgroundShortcuts: Set<String>
3135
get() = sharedPreferences.getStringSet(BACKGROUND_SHORTCUTS_KEY, setOf<String>())!!
3236
set(value) = sharedPreferences.edit { putStringSet(BACKGROUND_SHORTCUTS_KEY, value) }
@@ -80,6 +84,8 @@ class SharedPreferencesExt(context: Context) {
8084
get() = sharedPreferences.getBoolean(WEB_DEBUGGING_ENABLED_KEY, WEB_DEBUGGING_ENABLED_DEFAULT)
8185

8286
companion object {
87+
private const val DESKTOP_SHORTCUTS_KEY = "desktop_shortcuts"
88+
8389
private const val BACKGROUND_SHORTCUTS_KEY = "background_shortcuts"
8490

8591
private const val PROTECTED_MEDIA_WHITELIST_KEY = "protected_media_whitelist"

app/src/main/java/org/lineageos/jelly/webview/WebViewExt.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ class WebViewExt @JvmOverloads constructor(
152152
}
153153

154154
fun init(
155-
activity: WebViewExtActivity, urlBarLayout: UrlBarLayout, incognito: Boolean
155+
activity: WebViewExtActivity,
156+
urlBarLayout: UrlBarLayout,
157+
desktopMode: Boolean,
158+
incognito: Boolean,
156159
) {
157160
if (initialized) return
158161
this.activity = activity
@@ -170,6 +173,7 @@ class WebViewExt @JvmOverloads constructor(
170173
urlBarLayout.onClearSearchCallback = { clearMatches() }
171174
urlBarLayout.onSearchPositionChangeCallback = { findNext(it) }
172175
setup(urlBarLayout)
176+
setWebViewDesktopMode(desktopMode)
173177
initialized = true
174178
}
175179

@@ -192,15 +196,20 @@ class WebViewExt @JvmOverloads constructor(
192196

193197
var isDesktopMode: Boolean
194198
get() = desktopMode
195-
set(desktopMode) {
196-
this.desktopMode = desktopMode
197-
val settings = settings
198-
settings.userAgentString = if (desktopMode) desktopUserAgent else mobileUserAgent
199-
settings.useWideViewPort = desktopMode
200-
settings.loadWithOverviewMode = desktopMode
199+
set(value) {
200+
setWebViewDesktopMode(value)
201201
reload()
202202
}
203203

204+
private fun setWebViewDesktopMode(value: Boolean) {
205+
desktopMode = value
206+
settings.apply {
207+
userAgentString = if (value) desktopUserAgent else mobileUserAgent
208+
useWideViewPort = value
209+
loadWithOverviewMode = value
210+
}
211+
}
212+
204213
companion object {
205214
private const val TAG = "WebViewExt"
206215
private const val DESKTOP_DEVICE = "X11; Linux x86_64"

app/src/main/res/layout/menu_dialog.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@
184184

185185
<com.google.android.material.divider.MaterialDivider style="@style/Theme.Jelly.MenuDivider" />
186186

187+
<LinearLayout
188+
android:id="@+id/desktopShortcutsButton"
189+
style="@style/Theme.Jelly.MenuItem">
190+
191+
<TextView
192+
style="@style/Theme.Jelly.MenuItemTitle"
193+
android:text="@string/menu_desktop_shortcuts"
194+
app:drawableStartCompat="@drawable/ic_desktop" />
195+
</LinearLayout>
196+
187197
<LinearLayout
188198
android:id="@+id/backgroundShortcutsButton"
189199
style="@style/Theme.Jelly.MenuItem">

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<string name="menu_downloads">Downloads</string>
4242
<!-- Menu action: add shortcut of the current page to the launcher -->
4343
<string name="menu_shortcut">Add shortcut</string>
44+
<!-- Menu action: manage desktop shortcuts -->
45+
<string name="menu_desktop_shortcuts">Desktop shortcuts</string>
4446
<!-- Menu action: manage background shortcuts -->
4547
<string name="menu_background_shortcuts">Background shortcuts</string>
4648
<!-- Menu action: show settings -->
@@ -69,6 +71,9 @@
6971
<!-- Download dialog: button that starts the download -->
7072
<string name="download_positive">Download</string>
7173

74+
<!-- Desktop shortcuts -->
75+
<string name="desktop_shortcuts_title">Desktop shortcuts</string>
76+
7277
<!-- Background shortcuts -->
7378
<string name="background_shortcuts_title">Background shortcuts</string>
7479
<string name="background_shortcuts_save_action">Save</string>

0 commit comments

Comments
 (0)