diff --git a/app/src/main/java/com/github/gotify/sharing/ShareActivity.java b/app/src/main/java/com/github/gotify/sharing/ShareActivity.java deleted file mode 100644 index e49fbb2..0000000 --- a/app/src/main/java/com/github/gotify/sharing/ShareActivity.java +++ /dev/null @@ -1,167 +0,0 @@ -package com.github.gotify.sharing; - -import android.content.Intent; -import android.os.AsyncTask; -import android.os.Bundle; -import android.view.MenuItem; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.Spinner; -import android.widget.Toast; -import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; -import com.github.gotify.R; -import com.github.gotify.Settings; -import com.github.gotify.api.Api; -import com.github.gotify.api.ApiException; -import com.github.gotify.api.ClientFactory; -import com.github.gotify.client.ApiClient; -import com.github.gotify.client.api.MessageApi; -import com.github.gotify.client.model.Application; -import com.github.gotify.client.model.Message; -import com.github.gotify.databinding.ActivityShareBinding; -import com.github.gotify.log.Log; -import com.github.gotify.messages.provider.ApplicationHolder; -import java.util.ArrayList; -import java.util.List; - -import static com.github.gotify.Utils.first; - -public class ShareActivity extends AppCompatActivity { - private ActivityShareBinding binding; - private Settings settings; - private ApplicationHolder appsHolder; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - binding = ActivityShareBinding.inflate(getLayoutInflater()); - setContentView(binding.getRoot()); - - Log.i("Entering " + getClass().getSimpleName()); - setSupportActionBar(binding.appBarDrawer.toolbar); - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setDisplayShowCustomEnabled(true); - } - settings = new Settings(this); - - Intent intent = getIntent(); - String type = intent.getType(); - if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(type)) { - String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); - if (sharedText != null) { - binding.content.setText(sharedText); - } - } - - if (!settings.tokenExists()) { - Toast.makeText(getApplicationContext(), R.string.not_loggedin_share, Toast.LENGTH_SHORT) - .show(); - finish(); - return; - } - - ApiClient client = - ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token()); - appsHolder = new ApplicationHolder(this, client); - appsHolder.onUpdate( - () -> { - List apps = appsHolder.get(); - populateSpinner(apps); - - boolean appsAvailable = !apps.isEmpty(); - binding.pushButton.setEnabled(appsAvailable); - binding.missingAppsContainer.setVisibility( - appsAvailable ? View.GONE : View.VISIBLE); - }); - appsHolder.onUpdateFailed(() -> binding.pushButton.setEnabled(false)); - appsHolder.request(); - } - - @Override - protected void onPostCreate(@Nullable Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - binding.pushButton.setOnClickListener(ignored -> pushMessage()); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - } - return super.onOptionsItemSelected(item); - } - - public void pushMessage() { - String titleText = binding.title.getText().toString(); - String contentText = binding.content.getText().toString(); - String priority = binding.edtTxtPriority.getText().toString(); - int appIndex = binding.appSpinner.getSelectedItemPosition(); - - if (contentText.isEmpty()) { - Toast.makeText(this, "Content should not be empty.", Toast.LENGTH_LONG).show(); - return; - } else if (priority.isEmpty()) { - Toast.makeText(this, "Priority should be number.", Toast.LENGTH_LONG).show(); - return; - } else if (appIndex == Spinner.INVALID_POSITION) { - // For safety, e.g. loading the apps needs too much time (maybe a timeout) and - // the user tries to push without an app selected. - Toast.makeText(this, "An app must be selected.", Toast.LENGTH_LONG).show(); - return; - } - - Message message = new Message(); - if (!titleText.isEmpty()) { - message.setTitle(titleText); - } - message.setMessage(contentText); - message.setPriority(Long.parseLong(priority)); - new PushMessage(appsHolder.get().get(appIndex).getToken()).execute(message); - } - - private void populateSpinner(List apps) { - List appNameList = new ArrayList<>(); - for (Application app : apps) { - appNameList.add(app.getName()); - } - - ArrayAdapter adapter = - new ArrayAdapter<>( - this, android.R.layout.simple_spinner_dropdown_item, appNameList); - binding.appSpinner.setAdapter(adapter); - } - - private class PushMessage extends AsyncTask { - private String token; - - public PushMessage(String token) { - this.token = token; - } - - @Override - protected String doInBackground(Message... messages) { - List apps = appsHolder.get(); - ApiClient pushClient = - ClientFactory.clientToken(settings.url(), settings.sslSettings(), token); - - try { - MessageApi messageApi = pushClient.createService(MessageApi.class); - Api.execute(messageApi.createMessage(first(messages))); - return "Pushed!"; - } catch (ApiException apiException) { - Log.e("Failed sending message", apiException); - return "Oops! Something went wrong..."; - } - } - - @Override - protected void onPostExecute(String message) { - Toast.makeText(ShareActivity.this, message, Toast.LENGTH_LONG).show(); - ShareActivity.this.finish(); - } - } -} diff --git a/app/src/main/java/com/github/gotify/sharing/ShareActivity.kt b/app/src/main/java/com/github/gotify/sharing/ShareActivity.kt new file mode 100644 index 0000000..712d82c --- /dev/null +++ b/app/src/main/java/com/github/gotify/sharing/ShareActivity.kt @@ -0,0 +1,161 @@ +package com.github.gotify.sharing + +import android.content.Intent +import android.os.AsyncTask +import android.os.Bundle +import android.view.MenuItem +import android.view.View +import android.widget.ArrayAdapter +import android.widget.Spinner +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.github.gotify.R +import com.github.gotify.Settings +import com.github.gotify.Utils +import com.github.gotify.api.Api +import com.github.gotify.api.ApiException +import com.github.gotify.api.ClientFactory +import com.github.gotify.client.api.MessageApi +import com.github.gotify.client.model.Application +import com.github.gotify.client.model.Message +import com.github.gotify.databinding.ActivityShareBinding +import com.github.gotify.log.Log +import com.github.gotify.messages.provider.ApplicationHolder + +class ShareActivity : AppCompatActivity() { + private lateinit var binding: ActivityShareBinding + private lateinit var settings: Settings + private lateinit var appsHolder: ApplicationHolder + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityShareBinding.inflate(layoutInflater) + setContentView(binding.root) + + Log.i("Entering ${javaClass.simpleName}") + setSupportActionBar(binding.appBarDrawer.toolbar) + val actionBar = supportActionBar + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true) + actionBar.setDisplayShowCustomEnabled(true) + } + settings = Settings(this) + + val intent = intent + val type = intent.type + if (Intent.ACTION_SEND == intent.action && "text/plain" == type) { + val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT) + if (sharedText != null) { + binding.content.setText(sharedText) + } + } + + if (!settings.tokenExists()) { + Toast.makeText(applicationContext, R.string.not_loggedin_share, Toast.LENGTH_SHORT) + .show() + finish() + return + } + + val client = ClientFactory.clientToken( + settings.url(), + settings.sslSettings(), + settings.token() + ) + appsHolder = ApplicationHolder(this, client) + appsHolder.onUpdate { + val apps = appsHolder.get() + populateSpinner(apps) + + val appsAvailable = apps.isNotEmpty() + binding.pushButton.isEnabled = appsAvailable + binding.missingAppsContainer.visibility = if (appsAvailable) View.GONE else View.VISIBLE + } + appsHolder.onUpdateFailed { binding.pushButton.isEnabled = false } + appsHolder.request() + } + + override fun onPostCreate(savedInstanceState: Bundle?) { + super.onPostCreate(savedInstanceState) + binding.pushButton.setOnClickListener { pushMessage() } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == R.id.home) { + finish() + return true + } + return false + } + + fun pushMessage() { + val titleText = binding.title.text.toString() + val contentText = binding.content.text.toString() + val priority = binding.edtTxtPriority.text.toString() + val appIndex = binding.appSpinner.selectedItemPosition + + if (contentText.isEmpty()) { + Toast.makeText(this, "Content should not be empty.", Toast.LENGTH_LONG).show() + return + } else if (priority.isEmpty()) { + Toast.makeText(this, "Priority should be number.", Toast.LENGTH_LONG).show() + return + } else if (appIndex == Spinner.INVALID_POSITION) { + // For safety, e.g. loading the apps needs too much time (maybe a timeout) and + // the user tries to push without an app selected. + Toast.makeText(this, "An app must be selected.", Toast.LENGTH_LONG).show() + return + } + + val message = Message() + if (titleText.isNotEmpty()) { + message.title = titleText + } + message.message = contentText + message.priority = priority.toLong() + + PushMessage({ + val pushClient = ClientFactory.clientToken( + settings.url(), + settings.sslSettings(), + appsHolder.get()[appIndex].token + ) + try { + val messageApi = pushClient.createService(MessageApi::class.java) + Api.execute(messageApi.createMessage(it)) + "Pushed!" + } catch (apiException: ApiException) { + Log.e("Failed sending message", apiException) + "Oops! Something went wrong..." + } + }, { + Toast.makeText(this, it, Toast.LENGTH_LONG).show() + finish() + }).execute(message) + } + + private fun populateSpinner(apps: List) { + val appNameList = mutableListOf() + apps.forEach { app -> + appNameList.add(app.name) + } + + val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, appNameList) + binding.appSpinner.adapter = adapter + } + + class PushMessage( + private val backgroundAction: (message: Message?) -> String, + private val postAction: (message: String) -> Unit + ) : AsyncTask() { + @Deprecated("Deprecated in Java") + override fun doInBackground(vararg messages: Message?): String { + return backgroundAction(Utils.first(messages)) + } + + @Deprecated("Deprecated in Java") + override fun onPostExecute(message: String) { + postAction(message) + } + } +}