PackageManager: Better source selector

FontManager: Update default font
This commit is contained in:
zt515
2017-07-02 21:27:57 +08:00
parent 8df8b714bf
commit cd0de9b40a
12 changed files with 128 additions and 100 deletions

View File

@ -39,9 +39,6 @@
<activity
android:name=".ui.settings.UISettingsActivity"
android:theme="@style/Theme.AppCompat" />
<activity
android:name=".ui.settings.PackageSettingsActivity"
android:theme="@style/Theme.AppCompat" />
<service
android:name=".services.NeoTermService"

Binary file not shown.

View File

@ -1,6 +1,7 @@
package io.neoterm.customize
import android.annotation.SuppressLint
import io.neoterm.BuildConfig
/**
* @author kiva
@ -17,11 +18,16 @@ object NeoTermPath {
const val SOURCE_FILE = "$USR_PATH/etc/apt/sources.list"
const val PACKAGE_LIST_DIR = "$USR_PATH/var/lib/apt/lists"
const val DEFAULT_SOURCE = "https://mirrors.geekpie.org/neoterm"
const val SERVER_BASE_URL = DEFAULT_SOURCE
const val SERVER_BOOT_URL = "$SERVER_BASE_URL/boot"
private const val RELEASE_SOURCE = "https://mirrors.geekpie.org/neoterm"
private const val DEBUG_SOURCE = "http://192.243.117.135"
const val DEBUG_SOURCE = "http://192.243.117.135"
const val DEBUG_SERVER = DEBUG_SOURCE
const val DEBUG_SERVER_BOOT_URL = "$DEBUG_SERVER/boot"
val DEFAULT_SOURCE: String
val SERVER_BASE_URL: String
val SERVER_BOOT_URL: String
init {
DEFAULT_SOURCE = if (BuildConfig.DEBUG) DEBUG_SOURCE else RELEASE_SOURCE
SERVER_BASE_URL = DEFAULT_SOURCE
SERVER_BOOT_URL = "$SERVER_BASE_URL/boot"
}
}

View File

@ -7,13 +7,17 @@ import android.graphics.Typeface
* @author kiva
*/
object FontManager {
private var DEFAULT_FONT: Typeface? = null
private lateinit var DEFAULT_FONT: Typeface
fun init(context: Context) {
DEFAULT_FONT = Typeface.createFromAsset(context.assets, "font.ttf")
}
fun getDefaultFont(): Typeface {
return DEFAULT_FONT!!
return DEFAULT_FONT
}
fun getCurrentFont(): Typeface {
return DEFAULT_FONT
}
}

View File

@ -79,7 +79,6 @@ public final class BaseFileInstaller {
public void run() {
try {
double progressFloat = ((double) totalReadBytesFinal) / ((double) totalBytesFinal) * 100.0;
Log.e("NeoTerm-Installer", "total: " + totalBytesFinal + ", read: " + totalReadBytesFinal + ", " + progressFloat);
progress.setProgress((int) progressFloat);
} catch (RuntimeException ignore) {
// activity dismissed
@ -191,7 +190,7 @@ public final class BaseFileInstaller {
private static URL determineZipUrl() throws MalformedURLException {
String archName = determineArchName();
String baseUrl = BuildConfig.DEBUG ? NeoTermPath.DEBUG_SERVER_BOOT_URL : NeoTermPath.SERVER_BOOT_URL;
String baseUrl = NeoTermPath.INSTANCE.getSERVER_BOOT_URL();
return new URL(baseUrl + "/" + archName + ".zip");
}

View File

@ -3,6 +3,7 @@ package io.neoterm.ui.pm
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.view.MenuItemCompat
@ -18,6 +19,7 @@ import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import com.github.wrdlbrnft.sortedlistadapter.SortedListAdapter
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
import io.neoterm.R
@ -90,20 +92,28 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
val sourceFiles = ArrayList<File>()
val sourceUrl = NeoPreference.loadString(R.string.key_package_source, NeoTermPath.DEFAULT_SOURCE)
val packageFilePrefix = detectSourceFilePrefix(sourceUrl)
File(NeoTermPath.PACKAGE_LIST_DIR).listFiles().filterTo(sourceFiles) { it.name.startsWith(packageFilePrefix) }
if (packageFilePrefix.isNotEmpty()) {
File(NeoTermPath.PACKAGE_LIST_DIR)
.listFiles()
.filterTo(sourceFiles) { it.name.startsWith(packageFilePrefix) }
}
return sourceFiles
}
private fun detectSourceFilePrefix(sourceUrl: String): String {
val url = URL(sourceUrl)
val builder = StringBuilder()
builder.append(url.host)
builder.append("_")
if (url.path.isNotEmpty()) {
builder.append(url.path.substring(1)) // Skip '/'
try {
val url = URL(sourceUrl)
val builder = StringBuilder()
builder.append(url.host)
if (url.path != null && url.path.isNotEmpty()) {
builder.append("_")
builder.append(url.path.substring(1)) // Skip '/'
}
builder.append("_dists_stable_main_binary-")
return builder.toString()
} catch (e: Exception) {
return ""
}
builder.append("_dists_stable_main_binary-")
return builder.toString()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@ -118,29 +128,72 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
when (item?.itemId) {
android.R.id.home -> finish()
R.id.action_source -> changeSource()
R.id.action_update_and_refresh -> executeAptUpdate()
R.id.action_refresh -> refreshPackageList()
}
return super.onOptionsItemSelected(item)
}
private fun changeSource() {
val sourceList = resources.getStringArray(R.array.pref_package_source_values)
val currentSource = NeoPreference.loadString(R.string.key_package_source, NeoTermPath.DEFAULT_SOURCE)
var checkedItem = sourceList.indexOf(currentSource)
if (checkedItem == -1) {
checkedItem = sourceList.size - 1
}
@SuppressLint("ShowToast")
var toast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
var selectedIndex = 0
AlertDialog.Builder(this)
.setTitle(R.string.pref_package_source)
.setSingleChoiceItems(R.array.pref_package_source_entries, checkedItem, { dialog, which ->
if (which == sourceList.size - 1) {
changeSourceToUserInput()
dialog.dismiss()
} else {
selectedIndex = which
toast.cancel()
toast = Toast.makeText(this@PackageManagerActivity, sourceList[which], Toast.LENGTH_SHORT)
toast.show()
}
})
.setPositiveButton(android.R.string.yes, { _, _ ->
if (selectedIndex != sourceList.size - 1) {
changeSourceInternal(sourceList[selectedIndex])
}
})
.setNegativeButton(android.R.string.no, null)
.show()
}
private fun changeSourceToUserInput() {
val editText = EditText(this)
editText.setText(NeoPreference.loadString(R.string.key_package_source, NeoTermPath.DEFAULT_SOURCE))
val currentSource = NeoPreference.loadString(R.string.key_package_source, NeoTermPath.DEFAULT_SOURCE)
editText.setText(currentSource)
editText.requestFocus()
editText.setSelection(0, currentSource.length)
AlertDialog.Builder(this)
.setTitle(R.string.pref_package_source)
.setView(editText)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, { _, _ ->
val source = editText.text.toString()
NeoPreference.store(R.string.key_package_source, source)
val sourceFile = File(NeoTermPath.SOURCE_FILE)
FileUtils.writeFile(sourceFile, generateSourceFile(source).toByteArray())
postChangeSource()
changeSourceInternal(source)
})
.show()
}
private fun postChangeSource() {
private fun changeSourceInternal(source: String) {
NeoPreference.store(R.string.key_package_source, source)
val sourceFile = File(NeoTermPath.SOURCE_FILE)
FileUtils.writeFile(sourceFile, generateSourceFile(source).toByteArray())
executeAptUpdate()
}
private fun executeAptUpdate() {
val dialog = TerminalDialog(this@PackageManagerActivity, DialogInterface.OnCancelListener {
refreshPackageList()
})
@ -171,6 +224,10 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
adapter.edit()
.replaceAll(models)
.commit()
if (models.isEmpty()) {
Toast.makeText(this@PackageManagerActivity, R.string.package_list_empty, Toast.LENGTH_SHORT).show()
changeSource()
}
}
}.start()
}

View File

@ -1,62 +0,0 @@
package io.neoterm.ui.settings
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatPreferenceActivity
import android.view.MenuItem
import io.neoterm.R
import io.neoterm.customize.NeoTermPath
import io.neoterm.preference.NeoPreference
import io.neoterm.utils.FileUtils
import java.io.File
/**
* @author kiva
*/
class PackageSettingsActivity : AppCompatPreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar.title = getString(R.string.package_settings)
supportActionBar.setDisplayHomeAsUpEnabled(true)
addPreferencesFromResource(R.xml.settings_package)
val preference = findPreference(getString(R.string.key_package_source))
preference.summary = NeoPreference.loadString(R.string.key_package_source, NeoTermPath.DEFAULT_SOURCE)
preference.setOnPreferenceChangeListener { preference, newValue ->
val newSource = newValue as String
preference.summary = newSource
if (newSource.isNotEmpty()) {
val sourceFile = File(NeoTermPath.SOURCE_FILE)
FileUtils.writeFile(sourceFile, generateSourceFile(newSource).toByteArray())
AlertDialog.Builder(this@PackageSettingsActivity)
.setMessage(R.string.source_changed)
.setPositiveButton(android.R.string.yes, null)
.show()
}
return@setOnPreferenceChangeListener true
}
}
private fun generateSourceFile(source: String): String {
return StringBuilder().append("# Generated by NeoTerm-Preference\n")
.append("deb ")
.append(source)
.append(" stable main")
.append("\n")
.toString()
}
override fun onBuildHeaders(target: MutableList<Header>?) {
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
android.R.id.home ->
finish()
}
return super.onOptionsItemSelected(item)
}
}

View File

@ -12,6 +12,7 @@ import de.mrapp.android.tabswitcher.Tab
import de.mrapp.android.tabswitcher.TabSwitcher
import de.mrapp.android.tabswitcher.TabSwitcherDecorator
import io.neoterm.R
import io.neoterm.customize.font.FontManager
import io.neoterm.preference.NeoPreference
import io.neoterm.ui.NeoTermActivity
import io.neoterm.view.ExtraKeysView
@ -69,7 +70,7 @@ class TermTabDecorator(val context: NeoTermActivity) : TabSwitcherDecorator() {
return
}
view.textSize = NeoPreference.loadInt(NeoPreference.KEY_FONT_SIZE, 30)
view.setTypeface(Typeface.MONOSPACE)
view.setTypeface(FontManager.getCurrentFont())
context.fullScreenToggleButton.setStatus(NeoPreference.loadBoolean(R.string.key_ui_fullscreen, false))
if (tab is TermTab) {

View File

@ -9,6 +9,16 @@
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="@+id/action_refresh"
android:title="@string/menu_refresh_list"
app:showAsAction="never" />
<item
android:id="@+id/action_update_and_refresh"
android:title="@string/menu_update"
app:showAsAction="never" />
<item
android:id="@+id/action_source"
android:title="@string/pref_package_source"

View File

@ -43,6 +43,9 @@
<string name="retry">重试</string>
<string name="source_changed">APT 源已更改,你可能需要执行 apt update 来更新</string>
<string name="package_details">软件包: %s\n版本: %s\n依赖: %s\n占用空间: %s\n描述: %s\n主页: %s</string>
<string name="package_list_empty">软件包列表为空,请检查你的软件源</string>
<string name="menu_refresh_list">刷新</string>
<string name="menu_update">更新 &amp; 刷新</string>
<string-array name="pref_general_program_selection_entries">
<item>只使用 NeoTerm</item>
@ -51,4 +54,10 @@
</string-array>
<string name="done">完成</string>
<string name="install">安装</string>
<string-array name="pref_package_source_entries">
<item>默认源 (稳定,推荐)</item>
<item>调试源 (可能不稳定)</item>
<item>输入…</item>
</string-array>
</resources>

View File

@ -49,6 +49,9 @@
<string name="done">Done</string>
<string name="install">Install</string>
<string name="package_details">Package: %s\nVersion: %s\nDepends: %s\nInstalled Size: %s\nDescription: %s\nHome Page: %s</string>
<string name="package_list_empty">Package list is empty, please check out your source.</string>
<string name="menu_refresh_list">Refresh</string>
<string name="menu_update">Update &amp; Refresh</string>
<string-array name="pref_ui_color_scheme_entries" translatable="false">
<item>Default</item>
@ -73,4 +76,16 @@
<item>System First</item>
</string-array>
<string-array name="pref_package_source_entries">
<item>Default Source (stable, recommended)</item>
<item>Debug Source (maybe unstable)</item>
<item>Input…</item>
</string-array>
<string-array name="pref_package_source_values" translatable="false">
<item>https://mirrors.geekpie.org/neoterm</item>
<item>http://192.243.117.135</item>
<item>User-Input</item>
</string-array>
</resources>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:defaultValue="https://mirrors.geekpie.org/neoterm"
android:key="@string/key_package_source"
android:title="@string/pref_package_source" />
</PreferenceScreen>