From 0e6207b5f32316e33c1029059cc5372b5b7aee2b Mon Sep 17 00:00:00 2001 From: zt515 Date: Mon, 12 Jun 2017 18:46:38 +0800 Subject: [PATCH] UI: SettingsActivity --- app/src/main/AndroidManifest.xml | 12 ++ .../io/neoterm/services/NeoTermService.java | 137 ------------------ .../io/neoterm/services/NeoTermService.kt | 134 +++++++++++++++++ .../ui/settings/GeneralSettingsActivity.kt | 30 ++++ .../ui/settings/PackageSettingsActivity.kt | 30 ++++ .../io/neoterm/ui/settings/SettingActivity.kt | 2 +- .../neoterm/ui/settings/UISettingsActivity.kt | 30 ++++ .../io/neoterm/view/tab/CloseTabProvider.kt | 10 ++ .../main/java/io/neoterm/view/tab/TermTab.kt | 4 - app/src/main/res/menu/tab_switcher.xml | 2 +- app/src/main/res/values/strings.xml | 7 +- app/src/main/res/xml/setting_general.xml | 4 + app/src/main/res/xml/settings_main.xml | 32 ++-- app/src/main/res/xml/settings_package.xml | 4 + app/src/main/res/xml/settings_ui.xml | 4 + 15 files changed, 286 insertions(+), 156 deletions(-) delete mode 100644 app/src/main/java/io/neoterm/services/NeoTermService.java create mode 100644 app/src/main/java/io/neoterm/services/NeoTermService.kt create mode 100644 app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt create mode 100644 app/src/main/java/io/neoterm/ui/settings/PackageSettingsActivity.kt create mode 100644 app/src/main/java/io/neoterm/ui/settings/UISettingsActivity.kt create mode 100644 app/src/main/java/io/neoterm/view/tab/CloseTabProvider.kt create mode 100644 app/src/main/res/xml/setting_general.xml create mode 100644 app/src/main/res/xml/settings_package.xml create mode 100644 app/src/main/res/xml/settings_ui.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 996d7f7..069dc6e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,18 @@ android:name=".ui.settings.SettingActivity" android:theme="@style/Theme.AppCompat" /> + + + + + + diff --git a/app/src/main/java/io/neoterm/services/NeoTermService.java b/app/src/main/java/io/neoterm/services/NeoTermService.java deleted file mode 100644 index 0ce8ebe..0000000 --- a/app/src/main/java/io/neoterm/services/NeoTermService.java +++ /dev/null @@ -1,137 +0,0 @@ -package io.neoterm.services; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.os.Binder; -import android.os.IBinder; -import android.support.annotation.Nullable; -import android.support.v4.content.WakefulBroadcastReceiver; -import android.util.Log; - -import java.util.ArrayList; -import java.util.List; - -import io.neoterm.ui.NeoTermActivity; -import io.neoterm.R; -import io.neoterm.backend.EmulatorDebug; -import io.neoterm.backend.TerminalSession; - -/** - * @author kiva - */ - -public class NeoTermService extends Service { - public class NeoTermBinder extends Binder { - public NeoTermService service = NeoTermService.this; - } - - public static final String ACTION_SERVICE_STOP = "neoterm.action.service.stop"; - - private static final int NOTIFICATION_ID = 52019; - - private final NeoTermBinder neoTermBinder = new NeoTermBinder(); - private final List mTerminalSessions = new ArrayList<>(); - - @Override - public void onCreate() { - super.onCreate(); - startForeground(NOTIFICATION_ID, createNotification()); - } - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return neoTermBinder; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent.getAction(); - if (ACTION_SERVICE_STOP.equals(action)) { - for (int i = 0; i < mTerminalSessions.size(); i++) - mTerminalSessions.get(i).finishIfRunning(); - stopSelf(); - } else if (action != null) { - Log.e(EmulatorDebug.LOG_TAG, "Unknown NeoTermService action: '" + action + "'"); - } - - if ((flags & START_FLAG_REDELIVERY) == 0) { - // Service is started by WBR, not restarted by system, so release the WakeLock from WBR. - WakefulBroadcastReceiver.completeWakefulIntent(intent); - } - - return Service.START_NOT_STICKY; - } - - @Override - public void onDestroy() { - stopForeground(true); - - for (int i = 0; i < mTerminalSessions.size(); i++) - mTerminalSessions.get(i).finishIfRunning(); - mTerminalSessions.clear(); - } - - public List getSessions() { - return mTerminalSessions; - } - - public TerminalSession createTermSession(String executablePath, String[] arguments, String cwd, String[] env, TerminalSession.SessionChangedCallback sessionCallback) { - if (cwd == null) cwd = getFilesDir().getAbsolutePath(); - - boolean isLoginShell = false; - - if (executablePath == null) { - // Fall back to system shell as last resort: - executablePath = "/system/bin/sh"; - isLoginShell = true; - } - - if (arguments == null) { - arguments = new String[]{executablePath}; - } - - int lastSlashIndex = executablePath.lastIndexOf('/'); - String processName = (isLoginShell ? "-" : "") + - (lastSlashIndex == -1 ? executablePath : executablePath.substring(lastSlashIndex + 1)); - - TerminalSession session = new TerminalSession(executablePath, cwd, arguments, env, sessionCallback); - mTerminalSessions.add(session); - updateNotification(); - return session; - } - - public int removeTermSession(TerminalSession sessionToRemove) { - int indexOfRemoved = mTerminalSessions.indexOf(sessionToRemove); - mTerminalSessions.remove(indexOfRemoved); - updateNotification(); - return indexOfRemoved; - } - - private void updateNotification() { - ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, createNotification()); - } - - private Notification createNotification() { - Intent notifyIntent = new Intent(this, NeoTermActivity.class); - notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0); - - int sessionCount = mTerminalSessions.size(); - String contentText = sessionCount + " session" + (sessionCount == 1 ? "" : "s"); - - Notification.Builder builder = new Notification.Builder(this); - builder.setContentTitle(getText(R.string.app_name)); - builder.setContentText(contentText); - builder.setSmallIcon(R.drawable.ic_terminal_running); - builder.setContentIntent(pendingIntent); - builder.setOngoing(true); - builder.setShowWhen(false); - builder.setColor(0xFF000000); - return builder.build(); - } -} diff --git a/app/src/main/java/io/neoterm/services/NeoTermService.kt b/app/src/main/java/io/neoterm/services/NeoTermService.kt new file mode 100644 index 0000000..2138a95 --- /dev/null +++ b/app/src/main/java/io/neoterm/services/NeoTermService.kt @@ -0,0 +1,134 @@ +package io.neoterm.services + +import android.app.Notification +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Binder +import android.os.IBinder +import android.support.v4.content.WakefulBroadcastReceiver +import android.util.Log + +import java.util.ArrayList + +import io.neoterm.ui.NeoTermActivity +import io.neoterm.R +import io.neoterm.backend.EmulatorDebug +import io.neoterm.backend.TerminalSession + +/** + * @author kiva + */ + +class NeoTermService : Service() { + inner class NeoTermBinder : Binder() { + var service = this@NeoTermService + } + + private val neoTermBinder = NeoTermBinder() + private val mTerminalSessions = ArrayList() + + override fun onCreate() { + super.onCreate() + startForeground(NOTIFICATION_ID, createNotification()) + } + + override fun onBind(intent: Intent): IBinder? { + return neoTermBinder + } + + override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { + val action = intent.action + if (ACTION_SERVICE_STOP == action) { + for (i in mTerminalSessions.indices) + mTerminalSessions[i].finishIfRunning() + stopSelf() + } else if (action != null) { + Log.e(EmulatorDebug.LOG_TAG, "Unknown NeoTermService action: '$action'") + } + + if (flags and Service.START_FLAG_REDELIVERY == 0) { + // Service is started by WBR, not restarted by system, so release the WakeLock from WBR. + WakefulBroadcastReceiver.completeWakefulIntent(intent) + } + + return Service.START_NOT_STICKY + } + + override fun onDestroy() { + stopForeground(true) + + for (i in mTerminalSessions.indices) + mTerminalSessions[i].finishIfRunning() + mTerminalSessions.clear() + } + + val sessions: List + get() = mTerminalSessions + + fun createTermSession(executablePath: String?, arguments: Array?, cwd: String?, env: Array?, sessionCallback: TerminalSession.SessionChangedCallback?): TerminalSession { + var executablePath = executablePath + var arguments = arguments + var cwd = cwd + if (cwd == null) cwd = filesDir.absolutePath + + var isLoginShell = false + + if (executablePath == null) { + // Fall back to system shell as last resort: + executablePath = "/system/bin/sh" + isLoginShell = true + } + + if (arguments == null) { + arguments = arrayOf(executablePath) + } + + val lastSlashIndex = executablePath.lastIndexOf('/') + val processName = (if (isLoginShell) "-" else "") + if (lastSlashIndex == -1) executablePath else executablePath.substring(lastSlashIndex + 1) + + val session = TerminalSession(executablePath, cwd, arguments, env, sessionCallback) + mTerminalSessions.add(session) + updateNotification() + return session + } + + fun removeTermSession(sessionToRemove: TerminalSession): Int { + val indexOfRemoved = mTerminalSessions.indexOf(sessionToRemove) + mTerminalSessions.removeAt(indexOfRemoved) + updateNotification() + return indexOfRemoved + } + + private fun updateNotification() { + (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(NOTIFICATION_ID, createNotification()) + } + + private fun createNotification(): Notification { + val notifyIntent = Intent(this, NeoTermActivity::class.java) + notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + val pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0) + + val sessionCount = mTerminalSessions.size + val contentText = sessionCount.toString() + " session" + if (sessionCount == 1) "" else "s" + + val builder = Notification.Builder(this) + builder.setContentTitle(getText(R.string.app_name)) + builder.setContentText(contentText) + builder.setSmallIcon(R.drawable.ic_terminal_running) + builder.setContentIntent(pendingIntent) + builder.setOngoing(true) + builder.setShowWhen(false) + builder.setColor(0xFF000000.toInt()) + return builder.build() + } + + companion object { + + val ACTION_SERVICE_STOP = "neoterm.action.service.stop" + + private val NOTIFICATION_ID = 52019 + } +} diff --git a/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt b/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt new file mode 100644 index 0000000..13f7e0e --- /dev/null +++ b/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt @@ -0,0 +1,30 @@ +package io.neoterm.ui.settings + +import android.os.Bundle +import android.support.v7.app.AppCompatPreferenceActivity +import android.view.MenuItem +import io.neoterm.R + +/** + * @author kiva + */ +class GeneralSettingsActivity : AppCompatPreferenceActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar.title = getString(R.string.general_settings) + supportActionBar.setDisplayHomeAsUpEnabled(true) + addPreferencesFromResource(R.xml.setting_general) + } + + override fun onBuildHeaders(target: MutableList
?) { + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item?.itemId) { + android.R.id.home -> + finish() + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/neoterm/ui/settings/PackageSettingsActivity.kt b/app/src/main/java/io/neoterm/ui/settings/PackageSettingsActivity.kt new file mode 100644 index 0000000..87dccee --- /dev/null +++ b/app/src/main/java/io/neoterm/ui/settings/PackageSettingsActivity.kt @@ -0,0 +1,30 @@ +package io.neoterm.ui.settings + +import android.os.Bundle +import android.support.v7.app.AppCompatPreferenceActivity +import android.view.MenuItem +import io.neoterm.R + +/** + * @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) + } + + override fun onBuildHeaders(target: MutableList
?) { + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item?.itemId) { + android.R.id.home -> + finish() + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/neoterm/ui/settings/SettingActivity.kt b/app/src/main/java/io/neoterm/ui/settings/SettingActivity.kt index 813b32e..f3d36f2 100644 --- a/app/src/main/java/io/neoterm/ui/settings/SettingActivity.kt +++ b/app/src/main/java/io/neoterm/ui/settings/SettingActivity.kt @@ -12,7 +12,7 @@ class SettingActivity : AppCompatPreferenceActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - supportActionBar.title = getString(R.string.menu_settings) + supportActionBar.title = getString(R.string.settings) supportActionBar.setDisplayHomeAsUpEnabled(true) addPreferencesFromResource(R.xml.settings_main) } diff --git a/app/src/main/java/io/neoterm/ui/settings/UISettingsActivity.kt b/app/src/main/java/io/neoterm/ui/settings/UISettingsActivity.kt new file mode 100644 index 0000000..dc30c76 --- /dev/null +++ b/app/src/main/java/io/neoterm/ui/settings/UISettingsActivity.kt @@ -0,0 +1,30 @@ +package io.neoterm.ui.settings + +import android.os.Bundle +import android.support.v7.app.AppCompatPreferenceActivity +import android.view.MenuItem +import io.neoterm.R + +/** + * @author kiva + */ +class UISettingsActivity : AppCompatPreferenceActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar.title = getString(R.string.ui_settings) + supportActionBar.setDisplayHomeAsUpEnabled(true) + addPreferencesFromResource(R.xml.settings_ui) + } + + override fun onBuildHeaders(target: MutableList
?) { + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item?.itemId) { + android.R.id.home -> + finish() + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/neoterm/view/tab/CloseTabProvider.kt b/app/src/main/java/io/neoterm/view/tab/CloseTabProvider.kt new file mode 100644 index 0000000..3ec733c --- /dev/null +++ b/app/src/main/java/io/neoterm/view/tab/CloseTabProvider.kt @@ -0,0 +1,10 @@ +package io.neoterm.view.tab + +import de.mrapp.android.tabswitcher.Tab + +/** + * @author kiva + */ +interface CloseTabProvider { + fun closeTab(tab: Tab) +} \ No newline at end of file diff --git a/app/src/main/java/io/neoterm/view/tab/TermTab.kt b/app/src/main/java/io/neoterm/view/tab/TermTab.kt index f86a3ca..d2ab2c5 100644 --- a/app/src/main/java/io/neoterm/view/tab/TermTab.kt +++ b/app/src/main/java/io/neoterm/view/tab/TermTab.kt @@ -47,7 +47,3 @@ class TermTab(title: CharSequence) : Tab(title) { closeTabProvider?.closeTab(this) } } - -interface CloseTabProvider { - fun closeTab(tab: Tab) -} diff --git a/app/src/main/res/menu/tab_switcher.xml b/app/src/main/res/menu/tab_switcher.xml index 1bb2632..09c79cb 100755 --- a/app/src/main/res/menu/tab_switcher.xml +++ b/app/src/main/res/menu/tab_switcher.xml @@ -29,7 +29,7 @@ License. \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3032a72..b77efaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,11 @@ More Toggle switcher - Settings New session + + About + Settings + General Settings + UI Settings + Package Settings diff --git a/app/src/main/res/xml/setting_general.xml b/app/src/main/res/xml/setting_general.xml new file mode 100644 index 0000000..624ed13 --- /dev/null +++ b/app/src/main/res/xml/setting_general.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/settings_main.xml b/app/src/main/res/xml/settings_main.xml index 64181b9..cc57b2f 100644 --- a/app/src/main/res/xml/settings_main.xml +++ b/app/src/main/res/xml/settings_main.xml @@ -1,20 +1,28 @@ - + + + - + + + - - - + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/settings_package.xml b/app/src/main/res/xml/settings_package.xml new file mode 100644 index 0000000..624ed13 --- /dev/null +++ b/app/src/main/res/xml/settings_package.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/settings_ui.xml b/app/src/main/res/xml/settings_ui.xml new file mode 100644 index 0000000..624ed13 --- /dev/null +++ b/app/src/main/res/xml/settings_ui.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file