Add inputs for the pushed message to share activity
This commit is contained in:
committed by
Jannis Mattheis
parent
f85b5312e6
commit
dea1e42820
@@ -1,14 +1,12 @@
|
||||
package com.github.gotify;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import com.github.gotify.client.JSON;
|
||||
import com.github.gotify.log.Log;
|
||||
@@ -103,11 +101,6 @@ public class Utils {
|
||||
if (data.length != 1) {
|
||||
throw new IllegalArgumentException("must be one element");
|
||||
}
|
||||
|
||||
return data[0];
|
||||
}
|
||||
|
||||
public static void showLongToast(Context context, String message) {
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ import com.github.gotify.messages.provider.MessageWithImage;
|
||||
import com.github.gotify.picasso.PicassoHandler;
|
||||
import com.github.gotify.service.WebSocketService;
|
||||
import com.github.gotify.settings.SettingsActivity;
|
||||
import com.github.gotify.sharing.ShareActivity;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.android.material.snackbar.BaseTransientBottomBar;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
@@ -301,6 +302,9 @@ public class MessagesActivity extends AppCompatActivity
|
||||
startActivity(new Intent(this, LogsActivity.class));
|
||||
} else if (id == R.id.settings) {
|
||||
startActivity(new Intent(this, SettingsActivity.class));
|
||||
} else if (id == R.id.push_message) {
|
||||
Intent intent = new Intent(MessagesActivity.this, ShareActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
drawer.closeDrawer(GravityCompat.START);
|
||||
|
||||
@@ -1,82 +1,141 @@
|
||||
package com.github.gotify.sharing;
|
||||
|
||||
import android.app.Activity;
|
||||
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.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
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.ApiClient;
|
||||
import com.github.gotify.client.api.ApplicationApi;
|
||||
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.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 Activity {
|
||||
private ApiClient client;
|
||||
public class ShareActivity extends AppCompatActivity {
|
||||
private Settings settings;
|
||||
private ApplicationHolder appsHolder;
|
||||
|
||||
@BindView(R.id.title)
|
||||
EditText edtTxtTitle;
|
||||
|
||||
@BindView(R.id.content)
|
||||
EditText edtTxtContent;
|
||||
|
||||
@BindView(R.id.edtTxtPriority)
|
||||
EditText edtTxtPriority;
|
||||
|
||||
@BindView(R.id.appSpinner)
|
||||
Spinner appSpinner;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_share);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
Log.i("Entering " + getClass().getSimpleName());
|
||||
setSupportActionBar(findViewById(R.id.toolbar));
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
actionBar.setDisplayShowCustomEnabled(true);
|
||||
}
|
||||
settings = new Settings(this);
|
||||
handleShareIntent();
|
||||
}
|
||||
|
||||
private void handleShareIntent() {
|
||||
Intent intent = getIntent();
|
||||
String action = intent.getAction();
|
||||
String type = intent.getType();
|
||||
|
||||
if (Intent.ACTION_SEND.equals(action) && type != null) {
|
||||
if ("text/plain".equals(type)) {
|
||||
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
|
||||
if (sharedText != null) {
|
||||
Message message = new Message();
|
||||
message.setMessage(sharedText);
|
||||
message.setTitle("Shared content");
|
||||
message.setPriority((long) 5);
|
||||
|
||||
client =
|
||||
ClientFactory.clientToken(
|
||||
settings.url(), settings.sslSettings(), settings.token());
|
||||
|
||||
new GetApps().execute(message);
|
||||
}
|
||||
if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(type)) {
|
||||
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
|
||||
if (sharedText != null) {
|
||||
edtTxtContent.setText(sharedText);
|
||||
}
|
||||
}
|
||||
|
||||
ApiClient client =
|
||||
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());
|
||||
appsHolder = new ApplicationHolder(this, client);
|
||||
appsHolder.onUpdate(() -> populateSpinner(appsHolder.get()));
|
||||
appsHolder.request();
|
||||
}
|
||||
|
||||
private class GetApps extends AsyncTask<Message, Void, Void> {
|
||||
@Override
|
||||
protected Void doInBackground(Message... messages) {
|
||||
try {
|
||||
ApplicationApi applicationApi = client.createService(ApplicationApi.class);
|
||||
List<Application> apps = Api.execute(applicationApi.getApps());
|
||||
client =
|
||||
ClientFactory.clientToken(
|
||||
settings.url(), settings.sslSettings(), apps.get(0).getToken());
|
||||
new SendSharedContent().execute(first(messages));
|
||||
} catch (ApiException apiException) {
|
||||
Log.e("Failed getting apps", apiException);
|
||||
}
|
||||
|
||||
return null;
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
finish();
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private class SendSharedContent extends AsyncTask<Message, String, String> {
|
||||
@OnClick(R.id.push_button)
|
||||
public void pushMessage(View view) {
|
||||
String titleText = edtTxtTitle.getText().toString();
|
||||
String contentText = edtTxtContent.getText().toString();
|
||||
String priority = edtTxtPriority.getText().toString();
|
||||
int appIndex = 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;
|
||||
}
|
||||
|
||||
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<Application> apps) {
|
||||
List<String> appNameList = new ArrayList<>();
|
||||
for (Application app : apps) {
|
||||
appNameList.add(app.getName());
|
||||
}
|
||||
|
||||
ArrayAdapter<String> adapter =
|
||||
new ArrayAdapter<>(
|
||||
this, android.R.layout.simple_spinner_dropdown_item, appNameList);
|
||||
appSpinner.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private class PushMessage extends AsyncTask<Message, String, String> {
|
||||
private String token;
|
||||
|
||||
public PushMessage(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Message... messages) {
|
||||
List<Application> apps = appsHolder.get();
|
||||
ApiClient pushClient =
|
||||
ClientFactory.clientToken(settings.url(), settings.sslSettings(), token);
|
||||
|
||||
try {
|
||||
MessageApi messageApi = client.createService(MessageApi.class);
|
||||
MessageApi messageApi = pushClient.createService(MessageApi.class);
|
||||
Api.execute(messageApi.createMessage(first(messages)));
|
||||
return "Pushed!";
|
||||
} catch (ApiException apiException) {
|
||||
@@ -87,7 +146,7 @@ public class ShareActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String message) {
|
||||
Utils.showLongToast(ShareActivity.this, message);
|
||||
Toast.makeText(ShareActivity.this, message, Toast.LENGTH_LONG).show();
|
||||
ShareActivity.this.finish();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user