Add inputs for the pushed message to share activity

This commit is contained in:
tomasvanagas
2020-07-09 22:41:47 +03:00
committed by Jannis Mattheis
parent f85b5312e6
commit dea1e42820
13 changed files with 246 additions and 57 deletions

View File

@@ -47,7 +47,8 @@
android:label="@string/title_activity_settings" />
<activity
android:name=".sharing.ShareActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
android:theme="@style/AppTheme.NoActionBar"
android:label="Push message">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />

View File

@@ -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();
}
}

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/icons"
android:pathData="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
</vector>

View File

@@ -1,5 +1,7 @@
<vector android:height="24dp" android:viewportHeight="20"
android:viewportWidth="20" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
android:viewportWidth="20"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@color/icons"
android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/>

View File

@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/push_title_hint"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/push_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="10" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtTxtPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="@string/push_priority_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLength="3"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
<TextView
android:id="@+id/txtAppListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/appListDescription" />
<Spinner
android:id="@+id/appSpinner"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:textSize="18sp" />
</LinearLayout>
<Button
android:id="@+id/push_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="20dp"
android:text="@string/push_button" />
</LinearLayout>

View File

@@ -15,6 +15,12 @@
</group>
<group android:checkableBehavior="single">
<item
android:orderInCategory="2"
android:icon="@drawable/ic_send"
android:id="@+id/push_message"
android:title="@string/push_message"
/>
<item
android:icon="@drawable/ic_settings"
android:orderInCategory="2"

View File

@@ -36,7 +36,6 @@
<string name="gotify_url">https://push.example.com</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="show_advanced">Show Advanced Options</string>
<string name="disabled_validate_ssl">Disable SSL Validation</string>
<string name="select_ca_certificate">Select CA Certificate</string>
<string name="select_ca_file">Select a Certificate File</string>
@@ -68,4 +67,11 @@
<string name="settings_appearance">Appearance</string>
<string name="setting_theme">Theme</string>
<string name="setting_key_theme">theme</string>
<string name="push_message">Push message</string>
<string name="appListDescription">App:</string>
<string name="priorityDescription">Priority:</string>
<string name="push_button">Push Message</string>
<string name="push_title_hint">Title</string>
<string name="push_content_hint">Content</string>
<string name="push_priority_hint">Priority</string>
</resources>