Rewrite 'init' to Kotlin
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
package com.github.gotify.init;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import com.github.gotify.Settings;
|
||||
import com.github.gotify.service.WebSocketService;
|
||||
|
||||
public class BootCompletedReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Settings settings = new Settings(context);
|
||||
|
||||
if (!settings.tokenExists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(new Intent(context, WebSocketService.class));
|
||||
} else {
|
||||
context.startService(new Intent(context, WebSocketService.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.github.gotify.init
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import com.github.gotify.Settings
|
||||
import com.github.gotify.service.WebSocketService
|
||||
|
||||
class BootCompletedReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val settings = Settings(context)
|
||||
|
||||
if (!settings.tokenExists()) {
|
||||
return
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(Intent(context, WebSocketService::class.java))
|
||||
} else {
|
||||
context.startService(Intent(context, WebSocketService::class.java))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package com.github.gotify.init;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import com.github.gotify.NotificationSupport;
|
||||
import com.github.gotify.R;
|
||||
import com.github.gotify.Settings;
|
||||
import com.github.gotify.api.ApiException;
|
||||
import com.github.gotify.api.Callback;
|
||||
import com.github.gotify.api.ClientFactory;
|
||||
import com.github.gotify.client.model.User;
|
||||
import com.github.gotify.client.model.VersionInfo;
|
||||
import com.github.gotify.log.Log;
|
||||
import com.github.gotify.log.UncaughtExceptionHandler;
|
||||
import com.github.gotify.login.LoginActivity;
|
||||
import com.github.gotify.messages.MessagesActivity;
|
||||
import com.github.gotify.service.WebSocketService;
|
||||
import com.github.gotify.settings.ThemeHelper;
|
||||
|
||||
import static com.github.gotify.api.Callback.callInUI;
|
||||
|
||||
public class InitializationActivity extends AppCompatActivity {
|
||||
private Settings settings;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Log.init(this);
|
||||
String theme =
|
||||
PreferenceManager.getDefaultSharedPreferences(this)
|
||||
.getString(
|
||||
getString(R.string.setting_key_theme),
|
||||
getString(R.string.theme_default));
|
||||
ThemeHelper.setTheme(this, theme);
|
||||
|
||||
setContentView(R.layout.splash);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationSupport.createChannels(
|
||||
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE));
|
||||
}
|
||||
|
||||
UncaughtExceptionHandler.registerCurrentThread();
|
||||
settings = new Settings(this);
|
||||
Log.i("Entering " + getClass().getSimpleName());
|
||||
|
||||
if (settings.tokenExists()) {
|
||||
tryAuthenticate();
|
||||
} else {
|
||||
showLogin();
|
||||
}
|
||||
}
|
||||
|
||||
private void showLogin() {
|
||||
startActivity(new Intent(this, LoginActivity.class));
|
||||
finish();
|
||||
}
|
||||
|
||||
private void tryAuthenticate() {
|
||||
ClientFactory.userApiWithToken(settings)
|
||||
.currentUser()
|
||||
.enqueue(callInUI(this, this::authenticated, this::failed));
|
||||
}
|
||||
|
||||
private void failed(ApiException exception) {
|
||||
if (exception.code() == 0) {
|
||||
dialog(getString(R.string.not_available, settings.url()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (exception.code() == 401) {
|
||||
dialog(getString(R.string.auth_failed));
|
||||
return;
|
||||
}
|
||||
|
||||
String response = exception.body();
|
||||
response = response.substring(0, Math.min(200, response.length()));
|
||||
dialog(getString(R.string.other_error, settings.url(), exception.code(), response));
|
||||
}
|
||||
|
||||
private void dialog(String message) {
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.oops)
|
||||
.setMessage(message)
|
||||
.setPositiveButton(R.string.retry, (a, b) -> tryAuthenticate())
|
||||
.setNegativeButton(R.string.logout, (a, b) -> showLogin())
|
||||
.show();
|
||||
}
|
||||
|
||||
private void authenticated(User user) {
|
||||
Log.i("Authenticated as " + user.getName());
|
||||
|
||||
settings.user(user.getName(), user.isAdmin());
|
||||
requestVersion(
|
||||
() -> {
|
||||
startActivity(new Intent(this, MessagesActivity.class));
|
||||
finish();
|
||||
});
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(new Intent(this, WebSocketService.class));
|
||||
} else {
|
||||
startService(new Intent(this, WebSocketService.class));
|
||||
}
|
||||
}
|
||||
|
||||
private void requestVersion(Runnable runnable) {
|
||||
requestVersion(
|
||||
(version) -> {
|
||||
Log.i("Server version: " + version.getVersion() + "@" + version.getBuildDate());
|
||||
settings.serverVersion(version.getVersion());
|
||||
runnable.run();
|
||||
},
|
||||
(e) -> {
|
||||
runnable.run();
|
||||
});
|
||||
}
|
||||
|
||||
private void requestVersion(
|
||||
final Callback.SuccessCallback<VersionInfo> callback,
|
||||
final Callback.ErrorCallback errorCallback) {
|
||||
ClientFactory.versionApi(settings.url(), settings.sslSettings())
|
||||
.getVersion()
|
||||
.enqueue(callInUI(this, callback, errorCallback));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.github.gotify.init
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.gotify.NotificationSupport
|
||||
import com.github.gotify.R
|
||||
import com.github.gotify.Settings
|
||||
import com.github.gotify.api.ApiException
|
||||
import com.github.gotify.api.Callback
|
||||
import com.github.gotify.api.Callback.SuccessCallback
|
||||
import com.github.gotify.api.ClientFactory
|
||||
import com.github.gotify.client.model.User
|
||||
import com.github.gotify.client.model.VersionInfo
|
||||
import com.github.gotify.log.Log
|
||||
import com.github.gotify.log.UncaughtExceptionHandler
|
||||
import com.github.gotify.login.LoginActivity
|
||||
import com.github.gotify.messages.MessagesActivity
|
||||
import com.github.gotify.service.WebSocketService
|
||||
import com.github.gotify.settings.ThemeHelper
|
||||
|
||||
class InitializationActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var settings: Settings
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
Log.init(this)
|
||||
val theme = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))
|
||||
ThemeHelper.setTheme(this, theme)
|
||||
|
||||
setContentView(R.layout.splash)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationSupport.createChannels(
|
||||
this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
)
|
||||
}
|
||||
UncaughtExceptionHandler.registerCurrentThread()
|
||||
settings = Settings(this)
|
||||
Log.i("Entering ${javaClass.simpleName}")
|
||||
|
||||
if (settings.tokenExists()) {
|
||||
tryAuthenticate()
|
||||
} else {
|
||||
showLogin()
|
||||
}
|
||||
}
|
||||
|
||||
private fun showLogin() {
|
||||
startActivity(Intent(this, LoginActivity::class.java))
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun tryAuthenticate() {
|
||||
ClientFactory.userApiWithToken(settings)
|
||||
.currentUser()
|
||||
.enqueue(Callback.callInUI(this, { authenticated(it) }) { apiException ->
|
||||
failed(apiException)
|
||||
})
|
||||
}
|
||||
|
||||
private fun failed(exception: ApiException) {
|
||||
when (exception.code()) {
|
||||
0 -> {
|
||||
dialog(getString(R.string.not_available, settings.url()))
|
||||
return
|
||||
}
|
||||
401 -> {
|
||||
dialog(getString(R.string.auth_failed))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var response = exception.body()
|
||||
response = response.substring(0, 200.coerceAtMost(response.length))
|
||||
dialog(getString(R.string.other_error, settings.url(), exception.code(), response))
|
||||
}
|
||||
|
||||
private fun dialog(message: String) {
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle(R.string.oops)
|
||||
.setMessage(message)
|
||||
.setPositiveButton(R.string.retry) { _, _ -> tryAuthenticate() }
|
||||
.setNegativeButton(R.string.logout) { _, _ -> showLogin() }
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun authenticated(user: User) {
|
||||
Log.i("Authenticated as ${user.name}")
|
||||
|
||||
settings.user(user.name, user.isAdmin)
|
||||
requestVersion {
|
||||
startActivity(Intent(this, MessagesActivity::class.java))
|
||||
finish()
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(Intent(this, WebSocketService::class.java))
|
||||
} else {
|
||||
startService(Intent(this, WebSocketService::class.java))
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestVersion(runnable: Runnable) {
|
||||
requestVersion({ version: VersionInfo ->
|
||||
Log.i("Server version: ${version.version}@${version.buildDate}")
|
||||
settings.serverVersion(version.version)
|
||||
runnable.run()
|
||||
}) { runnable.run() }
|
||||
}
|
||||
|
||||
private fun requestVersion(
|
||||
callback: SuccessCallback<VersionInfo>,
|
||||
errorCallback: Callback.ErrorCallback
|
||||
) {
|
||||
ClientFactory.versionApi(settings.url(), settings.sslSettings())
|
||||
.version
|
||||
.enqueue(Callback.callInUI(this, callback, errorCallback))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user