Skip to content

Commit

Permalink
refactor quick toggle settings so that it can work in onyx boox
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Mar 18, 2021
1 parent 3c34a4b commit 66be7a9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 69 deletions.
49 changes: 25 additions & 24 deletions app/src/main/java/de/baumann/browser/activity/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class BrowserActivity : AppCompatActivity(), BrowserController, View.OnClickList

private lateinit var bookmarkDB: BookmarkList

private var isVerticalRead = false

// Classes
private inner class VideoCompletionListener : OnCompletionListener, MediaPlayer.OnErrorListener {
override fun onError(mp: MediaPlayer, what: Int, extra: Int): Boolean {
Expand Down Expand Up @@ -214,7 +212,7 @@ class BrowserActivity : AppCompatActivity(), BrowserController, View.OnClickList
private fun initTouchArea() {
updateTouchAreaType()
binding.omniboxTouch.setOnLongClickListener {
TouchAreaDialog(BrowserActivity@this).show()
TouchAreaDialog(BrowserActivity@ this).show()
true
}
sp.registerOnSharedPreferenceChangeListener(touchAreaChangeListener)
Expand Down Expand Up @@ -257,27 +255,27 @@ class BrowserActivity : AppCompatActivity(), BrowserController, View.OnClickList
}

when(TouchAreaType.values()[sp.getInt("sp_touch_area_type", 0)]) {
TouchAreaType.BottomLeftRight -> {
touchAreaPageUp = findViewById(R.id.touch_area_bottom_left)
touchAreaPageDown = findViewById(R.id.touch_area_bottom_right)
}
TouchAreaType.Left -> {
touchAreaPageUp = findViewById(R.id.touch_area_left_1)
touchAreaPageDown = findViewById(R.id.touch_area_left_2)
}
TouchAreaType.Right -> {
touchAreaPageUp = findViewById(R.id.touch_area_right_1)
touchAreaPageDown = findViewById(R.id.touch_area_right_2)
}
TouchAreaType.BottomLeftRight -> {
touchAreaPageUp = findViewById(R.id.touch_area_bottom_left)
touchAreaPageDown = findViewById(R.id.touch_area_bottom_right)
}
TouchAreaType.Left -> {
touchAreaPageUp = findViewById(R.id.touch_area_left_1)
touchAreaPageDown = findViewById(R.id.touch_area_left_2)
}
TouchAreaType.Right -> {
touchAreaPageUp = findViewById(R.id.touch_area_right_1)
touchAreaPageDown = findViewById(R.id.touch_area_right_2)
}
}

val isTouchEnabled = sp.getBoolean("sp_enable_touch", false)
with (touchAreaPageUp) {
with(touchAreaPageUp) {
if (isTouchEnabled) visibility = VISIBLE
setOnClickListener { ninjaWebView.pageUpWithNoAnimation() }
setOnLongClickListener { ninjaWebView.jumpToTop(); true }
}
with (touchAreaPageDown) {
with(touchAreaPageDown) {
if (isTouchEnabled) visibility = VISIBLE
setOnClickListener { ninjaWebView.pageDownWithNoAnimation() }
setOnLongClickListener { ninjaWebView.jumpToBottom(); true }
Expand Down Expand Up @@ -570,8 +568,8 @@ public override fun onActivityResult(requestCode: Int, resultCode: Int, data: In
ninjaWebView.pageDownWithNoAnimation()
}
R.id.omnibox_vertical_read -> {
isVerticalRead = !isVerticalRead
if (isVerticalRead) {
setVerticalRead(!getVerticalRead())
if (getVerticalRead()) {
ninjaWebView.applyVerticalRead()
} else {
ninjaWebView.applyHorizontalRead()
Expand Down Expand Up @@ -604,7 +602,7 @@ public override fun onActivityResult(requestCode: Int, resultCode: Int, data: In
ninjaWebView.stopLoading()
}
R.id.omnibox_bar_setting -> {
val intent = Intent( this, Settings_UIActivity::class.java)
val intent = Intent(this, Settings_UIActivity::class.java)
.putExtra(Constants.ARG_LAUNCH_TOOLBAR_SETTING, true)
startActivity(intent)
overridePendingTransition(0, 0);
Expand Down Expand Up @@ -822,8 +820,7 @@ public override fun onActivityResult(requestCode: Int, resultCode: Int, data: In
reorderToolbarIcons()
}

private val toolbarChangeListener = SharedPreferences.OnSharedPreferenceChangeListener {
_, key -> if (key.equals("sp_toolbar_icons")) reorderToolbarIcons()
private val toolbarChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> if (key.equals("sp_toolbar_icons")) reorderToolbarIcons()
}

private val toolbarActionViews: List<View> by lazy {
Expand Down Expand Up @@ -1191,8 +1188,7 @@ public override fun onActivityResult(requestCode: Int, resultCode: Int, data: In
}

private fun showFastToggleDialog() {
bottomSheetDialog = FastToggleDialog( this, ninjaWebView.title ?: "", ninjaWebView.url ?: "")
{
FastToggleDialog(this, ninjaWebView.title ?: "", ninjaWebView.url ?: "") {
if (ninjaWebView != null) {
ninjaWebView.initPreferences()
ninjaWebView.reload()
Expand Down Expand Up @@ -1871,6 +1867,11 @@ public override fun onActivityResult(requestCode: Int, resultCode: Int, data: In
mActionMode = null
}

private fun getVerticalRead(): Boolean = sp.getBoolean("sp_vertical_read", false)

private fun setVerticalRead(isEnabled: Boolean) =
sp.edit { putBoolean("sp_vertical_read", isEnabled) }


private fun hideKeyboard() {
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/de/baumann/browser/view/NinjaWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,10 @@ public boolean dispatchKeyEvent(KeyEvent event) {
}

public void applyVerticalRead() {
setVerticalRead(true);
injectJavascript(this, verticalLayoutCss.getBytes());
}

public void applyHorizontalRead() {
setVerticalRead(false);
injectJavascript(this, horizontalLayoutCss.getBytes());
}

Expand Down Expand Up @@ -438,10 +436,6 @@ private void injectJavascript(WebView view, byte[] bytes) {
}
}

private void setVerticalRead(boolean isEnabled) {
sp.edit().putBoolean("sp_vertical_read", isEnabled).apply();
}

private boolean getVerticalRead() {
return sp.getBoolean("sp_vertical_read", false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,25 @@ import de.baumann.browser.browser.Javascript
import de.baumann.browser.unit.HelperUnit

class FastToggleDialog(
context: Context,
private val context: Context,
private val title: String,
private val url: String,
private val okAction: () -> Unit,
) : BottomSheetDialog(context, R.style.BottomSheetDialog) {
) {
private val sp: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) }
private lateinit var dialog: AlertDialog
private lateinit var view: View


fun show() {
view = View.inflate(context, R.layout.dialog_toggle, null)
val builder = AlertDialog.Builder(context, R.style.TouchAreaDialog).apply { setView(view) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(View.inflate(context, R.layout.dialog_toggle, null))
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
initViews()
dialog = builder.create().apply {
window?.setGravity(Gravity.BOTTOM)
}
dialog.show()
}

private fun initViews() {
Expand All @@ -46,21 +53,21 @@ class FastToggleDialog(
}

private fun initTitle() {
val dialogTitle = findViewById<TextView>(R.id.dialog_title) ?: return
val dialogTitle = view.findViewById<TextView>(R.id.dialog_title) ?: return
dialogTitle.text = title
}

private fun initToggles() {
val toggleHistory = findViewById<ImageButton>(R.id.toggle_history) ?: return
val toggleHistoryView = findViewById<View>(R.id.toggle_historyView) ?: return
val toggleLocation = findViewById<ImageButton>(R.id.toggle_location) ?: return
val toggleLocationView = findViewById<View>(R.id.toggle_locationView) ?: return
val toggleImages = findViewById<ImageButton>(R.id.toggle_images) ?: return
val toggleImagesView = findViewById<View>(R.id.toggle_imagesView) ?: return
val toggleMediaContinue = findViewById<ImageButton>(R.id.toggle_media_continue) ?: return
val toggleMediaContinueView = findViewById<View>(R.id.toggle_media_continue_view) ?: return
val toggleDesktop = findViewById<ImageButton>(R.id.toggle_desktop) ?: return
val toggleDesktopView = findViewById<View>(R.id.toggle_desktopView) ?: return
val toggleHistory = view.findViewById<ImageButton>(R.id.toggle_history) ?: return
val toggleHistoryView = view.findViewById<View>(R.id.toggle_historyView) ?: return
val toggleLocation = view.findViewById<ImageButton>(R.id.toggle_location) ?: return
val toggleLocationView = view.findViewById<View>(R.id.toggle_locationView) ?: return
val toggleImages = view.findViewById<ImageButton>(R.id.toggle_images) ?: return
val toggleImagesView = view.findViewById<View>(R.id.toggle_imagesView) ?: return
val toggleMediaContinue = view.findViewById<ImageButton>(R.id.toggle_media_continue) ?: return
val toggleMediaContinueView = view.findViewById<View>(R.id.toggle_media_continue_view) ?: return
val toggleDesktop = view.findViewById<ImageButton>(R.id.toggle_desktop) ?: return
val toggleDesktopView = view.findViewById<View>(R.id.toggle_desktopView) ?: return

updateViewVisibility(toggleHistoryView, sp.getBoolean("saveHistory", false))
updateViewVisibility(toggleLocationView, R.string.sp_location)
Expand Down Expand Up @@ -95,9 +102,9 @@ class FastToggleDialog(
val cookieHosts = Cookie(context)
val adBlock = AdBlock(context)

val btnJavaScriptWhiteList = findViewById<ImageButton>(R.id.imageButton_js) ?: return
val btnAbWhiteList = findViewById<ImageButton>(R.id.imageButton_ab) ?: return
val btnCookieWhiteList = findViewById<ImageButton>(R.id.imageButton_cookie) ?: return
val btnJavaScriptWhiteList = view.findViewById<ImageButton>(R.id.imageButton_js) ?: return
val btnAbWhiteList = view.findViewById<ImageButton>(R.id.imageButton_ab) ?: return
val btnCookieWhiteList = view.findViewById<ImageButton>(R.id.imageButton_cookie) ?: return

setImgButtonResource(btnJavaScriptWhiteList, javaHosts.isWhite(url))
setImgButtonResource(btnCookieWhiteList, cookieHosts.isWhite(url))
Expand Down Expand Up @@ -130,9 +137,9 @@ class FastToggleDialog(
}

private fun initSwitches() {
val switchJavascript = findViewById<CheckBox>(R.id.switch_js) ?: return
val switchAdBlock = findViewById<CheckBox>(R.id.switch_adBlock) ?: return
val switchCookie = findViewById<CheckBox>(R.id.switch_cookie) ?: return
val switchJavascript = view.findViewById<CheckBox>(R.id.switch_js) ?: return
val switchAdBlock = view.findViewById<CheckBox>(R.id.switch_adBlock) ?: return
val switchCookie = view.findViewById<CheckBox>(R.id.switch_cookie) ?: return
switchJavascript.isChecked = sp.getBoolean(getString(R.string.sp_javascript), true)
switchAdBlock.isChecked = sp.getBoolean(getString(R.string.sp_ad_block), true)
switchCookie.isChecked = sp.getBoolean(getString(R.string.sp_cookies), true)
Expand All @@ -143,11 +150,11 @@ class FastToggleDialog(
}

private fun initOkCancelBar() {
findViewById<Button>(R.id.action_ok)?.setOnClickListener {
dismiss()
view.findViewById<Button>(R.id.action_ok)?.setOnClickListener {
dialog.dismiss()
okAction.invoke()
}
findViewById<Button>(R.id.action_cancel)?.setOnClickListener { dismiss() }
view.findViewById<Button>(R.id.action_cancel)?.setOnClickListener { dialog.dismiss() }
}

private fun getString(resId: Int): String = context.getString(resId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import de.baumann.browser.preference.TouchAreaType
class TouchAreaDialog(private val context: Context) {
private val sp: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) }

private var builder: AlertDialog.Builder = AlertDialog.Builder(context, R.style.TouchAreaDialog)
private lateinit var dialog: AlertDialog

fun show() {
val view = View.inflate(context, R.layout.dialog_touch_area, null)
builder.setView(view)
val builder = AlertDialog.Builder(context, R.style.TouchAreaDialog).apply { setView(view) }

initViews(view)
dialog = builder.create().apply {
window?.setGravity(Gravity.BOTTOM)
Expand Down
17 changes: 6 additions & 11 deletions app/src/main/res/layout/dialog_toggle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white"
android:background="@drawable/dialog_border_bg"
android:gravity="center"
tools:ignore="ContentDescription" >

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black"/>

<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:includeFontPadding="false"
android:padding="8dp"
Expand All @@ -32,10 +28,9 @@
android:layout_height="1dp"
android:background="?android:attr/colorButtonNormal" />

<ScrollView android:layout_width="match_parent"
<ScrollView android:layout_width="350dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/white">
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
android:orientation="vertical"
Expand Down

0 comments on commit 66be7a9

Please sign in to comment.