Skip to content

Commit

Permalink
Add text zoom option
Browse files Browse the repository at this point in the history
  • Loading branch information
drunlin committed Jan 5, 2019
1 parent a3b0346 commit 944640e
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.github.drunlin.webappbox"
minSdkVersion 16
targetSdkVersion 28
versionCode 3
versionName "1.2"
versionCode 4
versionName "1.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ val PREF_FULL_SCREEN = "full_screen"
val PREF_CLEAR_DATA = "clear_data"
val PREF_CLEAR_CACHE = "clear_cache"
val PREF_LAUNCH_MODE = "launch_mode"
val PREF_TEXT_ZOOM = "text_zoom"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.drunlin.webappbox.data
data class Rule(override var id: Long,
var pattern: URLPattern,
var color: Int,
var textZoom: Int,
var launchMode: LaunchMode,
var orientation: Orientation,
var fullScreen: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.SeekBarPreference
import android.view.View
import com.github.drunlin.webappbox.R
import com.github.drunlin.webappbox.activity.FragmentActivity
Expand Down Expand Up @@ -35,12 +36,23 @@ class PreferencesFragment : PreferenceFragmentCompat(), UserAgentsFragment.OnCha
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setupTextZoomPreference()

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
findPreference(PREF_COLOR).isVisible = false

updateUserAgentSummary()
}

private fun setupTextZoomPreference() {
val preference = findPreference(PREF_TEXT_ZOOM) as SeekBarPreference
preference.summary = getString(R.string.text_zoom_summary, preference.value)
preference.setOnPreferenceChangeListener { _, value ->
preference.summary = getString(R.string.text_zoom_summary, value as Int)
true
}
}

private fun updateUserAgentSummary() {
findPreference(PREF_USER_AGENT).summary = userAgent.name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.databinding.ViewDataBinding
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import com.github.drunlin.webappbox.BR
import com.github.drunlin.webappbox.R
import com.github.drunlin.webappbox.common.BUNDLE_UA
Expand Down Expand Up @@ -62,6 +63,16 @@ class RuleEditorFragment(id: Long?) : EditorFragment<Rule>(id),
editor.onStateChange = { confirmMenu?.isEnabled = it }
editor.isExisted = { v, b -> ruleManager.isExited(v, b) }

textZoomSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) = Unit

override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit

override fun onStopTrackingTouch(seekBar: SeekBar) {
textZoomText.text = getString(R.string.text_zoom_summary, seekBar.progress)
}
})

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
colorItem.setOnClickListener { showDialog(ColorPickerFragment(colorView.color)) }
else
Expand All @@ -74,7 +85,7 @@ class RuleEditorFragment(id: Long?) : EditorFragment<Rule>(id),
}

private fun updateUserAgentSummary() {
(binding as FragmentRuleEditorBinding).userAgentItem!!.summary = userAgent?.name
(binding as FragmentRuleEditorBinding).userAgentItem.summary = userAgent?.name
}

override fun onConfigureView(data: Rule) {
Expand All @@ -97,13 +108,14 @@ class RuleEditorFragment(id: Long?) : EditorFragment<Rule>(id),
val pattern = editor.value
val regex = editor.regex
val color = colorView.color
val tz = textZoomSeekBar.progress
val lm = LaunchMode.valueOf(launchModeSpinner.value)
val so = Orientation.valueOf(orientationSpinner.value)
val ua = userAgent ?: data?.userAgent ?: userAgents.getOrNull(0)
val fs = fullScreenItem.switcher.isChecked
val js = enableJavascriptItem.switcher.isChecked

id?.run { ruleManager.update(this, pattern, regex, color, lm, so, fs, ua, js) }
?: ruleManager.insert(pattern, regex, color, lm, so, fs, ua, js)
id?.run { ruleManager.update(this, pattern, regex, color, tz, lm, so, fs, ua, js) }
?: ruleManager.insert(pattern, regex, color, tz, lm, so, fs, ua, js)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class WebappWindowFragment() : Fragment(), DownloadListener, OnKeyListener, OnGl

updateSystemUi(url)

view.settings.textZoom = rule!!.textZoom
view.settings.userAgentString = rule!!.userAgent.value
view.settings.javaScriptEnabled = rule!!.jsEnabled

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DatabaseManager(private val context: Context) :

companion object {
val DB_NAME = "app.db"
val DB_VERSION = 1
val DB_VERSION = 2

val TABLE_APP = "app"
val TABLE_RULE = "rule"
Expand All @@ -44,6 +44,7 @@ class DatabaseManager(private val context: Context) :
val FULL_SCREEN = "full_screen"
val LAUNCH_MODE = "launch_mode"
val LOCATION = "location"
val TEXT_ZOOM = "text_zoom"
}

@Inject lateinit var userAgentManager: Lazy<UserAgentManager>
Expand Down Expand Up @@ -76,6 +77,7 @@ class DatabaseManager(private val context: Context) :
$PATTERN text not null,
$REGEX integer not null,
$COLOR integer not null,
$TEXT_ZOOM integer not null,
$LAUNCH_MODE text not null,
$ORIENTATION text not null,
$FULL_SCREEN integer not null,
Expand All @@ -89,7 +91,9 @@ class DatabaseManager(private val context: Context) :
sql.split(";\n").filter(String::isNotBlank).map { "$it;" }.forEach { db.execSQL(it) }
}

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) = Unit
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("ALTER TABLE $TABLE_RULE ADD COLUMN $TEXT_ZOOM integer default 100;")
}

private fun query(table: String, columns: Array<String>, selection: String?,
selectionArgs: Array<String>?): Cursor {
Expand Down Expand Up @@ -204,6 +208,7 @@ class DatabaseManager(private val context: Context) :
values.put(PATTERN, pattern.pattern)
values.put(REGEX, pattern.regex)
values.put(COLOR, color)
values.put(TEXT_ZOOM, textZoom)
values.put(LAUNCH_MODE, launchMode.name)
values.put(ORIENTATION, orientation.name)
values.put(FULL_SCREEN, fullScreen)
Expand All @@ -228,15 +233,15 @@ class DatabaseManager(private val context: Context) :

fun getRules(id: Long): MutableList<Rule> {
val list = LinkedList<Rule>()
val columns = arrayOf(ID, PATTERN, REGEX, COLOR, LAUNCH_MODE, ORIENTATION, FULL_SCREEN,
val columns = arrayOf(ID, PATTERN, REGEX, COLOR, TEXT_ZOOM, LAUNCH_MODE, ORIENTATION, FULL_SCREEN,
USER_AGENT, JS_ENABLE)
val cursor = query(TABLE_RULE, columns, "$APP_ID=$id", ORDER)
if (cursor.moveToFirst())
do
list.add(Rule(cursor.getLong(0), URLPattern(cursor.getString(1), cursor.getBoolean(2)),
cursor.getInt(3), LaunchMode.valueOf(cursor.getString(4)),
Orientation.valueOf(cursor.getString(5)), cursor.getBoolean(6),
userAgentManager.get().getUserAgent(cursor.getLong(7)), cursor.getBoolean(8)))
cursor.getInt(3), cursor.getInt(4), LaunchMode.valueOf(cursor.getString(5)),
Orientation.valueOf(cursor.getString(6)), cursor.getBoolean(7),
userAgentManager.get().getUserAgent(cursor.getLong(8)), cursor.getBoolean(9)))
while (cursor.moveToNext())
cursor.close()
return list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PreferenceModel : Injectable, SharedPreferences.OnSharedPreferenceChangeLi
val orientation = Orientation.valueOf(
preferences.getString(PREF_ORIENTATION, Orientation.NORMAL.name))
_defaultRule = Rule(-1, URLPattern(), preferences.getInt(PREF_COLOR, Color.BLACK),
preferences.getInt(PREF_TEXT_ZOOM, 100),
launchMode, orientation, preferences.getBoolean(PREF_FULL_SCREEN, false),
userAgent, preferences.getBoolean(PREF_ENABLE_JS, true))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ open class RuleManager : DataManager<Rule>() {
onMove.invoke { it(from, to) }
}

fun update(id: Long, pattern: String, regex: Boolean, color: Int, launchMode: LaunchMode,
fun update(id: Long, pattern: String, regex: Boolean, color: Int, textZoom: Int, launchMode: LaunchMode,
orientation: Orientation, fullScreen: Boolean, userAgent: UserAgent?, enableJS: Boolean) {
val (index, rule) = rules.findIndexedValue { it.id == id }
rule.pattern.pattern = pattern
rule.pattern.regex = regex
rule.color = color
rule.textZoom = textZoom
rule.launchMode = launchMode
rule.orientation = orientation
rule.fullScreen = fullScreen
Expand All @@ -41,10 +42,10 @@ open class RuleManager : DataManager<Rule>() {
update(index, rule)
}

fun insert(pattern: String, regex: Boolean, color: Int, launchMode: LaunchMode,
fun insert(pattern: String, regex: Boolean, color: Int, textZoom: Int, launchMode: LaunchMode,
orientation: Orientation, fullScreen: Boolean,
userAgent: UserAgent?, enableJS: Boolean) {
val rule = Rule(generateId(), URLPattern(pattern, regex), color, launchMode, orientation,
val rule = Rule(generateId(), URLPattern(pattern, regex), color, textZoom, launchMode, orientation,
fullScreen, userAgent ?: userAgentManager.defaultUserAgent, enableJS)
insert(rule)
}
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/fragment_rule_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@
android:layout_gravity="right|center_vertical"
app:color="@{rule != null ? rule.color : @android:color/black}"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/ListPreferredItem">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_zoom"
style="@style/ListItemTitle"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/textZoomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/text_zoom_summary(rule != null ? rule.textZoom : 100)}"
tools:text="100%"
style="@style/ListItemSummary"/>
<SeekBar
android:id="@+id/textZoomSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="200"
android:progress="@{rule != null ? rule.textZoom : 100}"/>
</LinearLayout>
</LinearLayout>
<com.github.drunlin.webappbox.widget.AppSpinner
android:id="@+id/launchModeSpinner"
android:layout_width="match_parent"
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/raw/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ INSERT INTO "user_agent" VALUES(1,'Android Phone','Mozilla/5.0 (Linux; Android 7
INSERT INTO "user_agent" VALUES(2,'Android Tablet','Mozilla/5.0 (Linux; Android 7.0; Nexus 9 Build/NPD90G; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.90 Safari/537.36 Sleipnir/3.5.4');
INSERT INTO "user_agent" VALUES(3,'iPhone','Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A5341a Safari/602.1');
INSERT INTO "user_agent" VALUES(4,'iPad','Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.86 Mobile/14A5335b Safari/601.1.46');
INSERT INTO "rule" VALUES(1,1,'.+',1,-16777216,'SINGLE_TOP','LANDSCAPE',1,0,1,1);
INSERT INTO "rule" VALUES(2,2,'https://(www\.)?regex101\.com/.*',1,-12627531,'CLEAR_TOP','NORMAL',0,0,1,2);
INSERT INTO "rule" VALUES(3,2,'.+',1,-16777216,'STANDARD','NORMAL',0,0,1,2);
INSERT INTO "rule" VALUES(1,1,'.+',1,-16777216,100,'SINGLE_TOP','LANDSCAPE',1,0,1,1);
INSERT INTO "rule" VALUES(2,2,'https://(www\.)?regex101\.com/.*',1,-12627531,100,'CLEAR_TOP','NORMAL',0,0,1,2);
INSERT INTO "rule" VALUES(3,2,'.+',1,-16777216,100,'STANDARD','NORMAL',0,0,1,2);
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@
<item>全局唯一</item>
</string-array>
<string name="app_name">网络应用盒子</string>
<string name="text_zoom">文字大小</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<string name="ok">OK</string>
<string name="add">Add</string>
<string name="version">Version</string>
<string name="version_code" translatable="false">1.2</string>
<string name="version_code" translatable="false">1.3</string>
<string name="github" translatable="false">Github</string>
<string name="project_homepage" translatable="false">https://github.com/drunlin/webappbox</string>
<string name="developer">Developer</string>
Expand Down Expand Up @@ -106,4 +106,6 @@
<string name="pattern_summary">Links that are not matched are opened in other apps</string>
<string name="rule_summary">Set special rules for pages</string>
<string name="preview">Preview</string>
<string name="text_zoom">Text zoom</string>
<string name="text_zoom_summary" translatable="false">%d%%</string>
</resources>
13 changes: 10 additions & 3 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidElementNotAllowed -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:title="@string/default_rule">
<com.thebluealliance.spectrum.SpectrumPreferenceCompat
android:key="status_bar_color"
android:title="@string/status_bar_color"
android:defaultValue="@android:color/black"/>
<android.support.v7.preference.SeekBarPreference
android:key="text_zoom"
android:title="@string/text_zoom"
tools:summary="100%"
app:showSeekBarValue="false"
android:max="200"
android:defaultValue="100"/>
<ListPreference
android:key="launch_mode"
android:title="@string/launch_mode"
Expand Down

0 comments on commit 944640e

Please sign in to comment.