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

17
.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>android</name>
<comment>Project android created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>

View File

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

View File

@@ -1,14 +1,12 @@
package com.github.gotify; package com.github.gotify;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.view.View; import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.github.gotify.client.JSON; import com.github.gotify.client.JSON;
import com.github.gotify.log.Log; import com.github.gotify.log.Log;
@@ -103,11 +101,6 @@ public class Utils {
if (data.length != 1) { if (data.length != 1) {
throw new IllegalArgumentException("must be one element"); throw new IllegalArgumentException("must be one element");
} }
return data[0]; 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.picasso.PicassoHandler;
import com.github.gotify.service.WebSocketService; import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.SettingsActivity; import com.github.gotify.settings.SettingsActivity;
import com.github.gotify.sharing.ShareActivity;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
@@ -301,6 +302,9 @@ public class MessagesActivity extends AppCompatActivity
startActivity(new Intent(this, LogsActivity.class)); startActivity(new Intent(this, LogsActivity.class));
} else if (id == R.id.settings) { } else if (id == R.id.settings) {
startActivity(new Intent(this, SettingsActivity.class)); 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); drawer.closeDrawer(GravityCompat.START);

View File

@@ -1,82 +1,141 @@
package com.github.gotify.sharing; package com.github.gotify.sharing;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; 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.Settings;
import com.github.gotify.Utils;
import com.github.gotify.api.Api; import com.github.gotify.api.Api;
import com.github.gotify.api.ApiException; import com.github.gotify.api.ApiException;
import com.github.gotify.api.ClientFactory; import com.github.gotify.api.ClientFactory;
import com.github.gotify.client.ApiClient; 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.api.MessageApi;
import com.github.gotify.client.model.Application; import com.github.gotify.client.model.Application;
import com.github.gotify.client.model.Message; import com.github.gotify.client.model.Message;
import com.github.gotify.log.Log; import com.github.gotify.log.Log;
import com.github.gotify.messages.provider.ApplicationHolder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.github.gotify.Utils.first; import static com.github.gotify.Utils.first;
public class ShareActivity extends Activity { public class ShareActivity extends AppCompatActivity {
private ApiClient client;
private Settings settings; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
settings = new Settings(this); setContentView(R.layout.activity_share);
handleShareIntent(); 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);
private void handleShareIntent() {
Intent intent = getIntent(); Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType(); String type = intent.getType();
if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(type)) {
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) { if (sharedText != null) {
Message message = new Message(); edtTxtContent.setText(sharedText);
message.setMessage(sharedText);
message.setTitle("Shared content");
message.setPriority((long) 5);
client =
ClientFactory.clientToken(
settings.url(), settings.sslSettings(), settings.token());
new GetApps().execute(message);
}
}
} }
} }
private class GetApps extends AsyncTask<Message, Void, Void> { ApiClient client =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());
appsHolder = new ApplicationHolder(this, client);
appsHolder.onUpdate(() -> populateSpinner(appsHolder.get()));
appsHolder.request();
}
@Override @Override
protected Void doInBackground(Message... messages) { public boolean onOptionsItemSelected(MenuItem item) {
try { if (item.getItemId() == android.R.id.home) {
ApplicationApi applicationApi = client.createService(ApplicationApi.class); finish();
List<Application> apps = Api.execute(applicationApi.getApps()); }
client = return super.onOptionsItemSelected(item);
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; @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;
} }
private class SendSharedContent extends AsyncTask<Message, String, String> {
@Override @Override
protected String doInBackground(Message... messages) { protected String doInBackground(Message... messages) {
List<Application> apps = appsHolder.get();
ApiClient pushClient =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), token);
try { try {
MessageApi messageApi = client.createService(MessageApi.class); MessageApi messageApi = pushClient.createService(MessageApi.class);
Api.execute(messageApi.createMessage(first(messages))); Api.execute(messageApi.createMessage(first(messages)));
return "Pushed!"; return "Pushed!";
} catch (ApiException apiException) { } catch (ApiException apiException) {
@@ -87,7 +146,7 @@ public class ShareActivity extends Activity {
@Override @Override
protected void onPostExecute(String message) { protected void onPostExecute(String message) {
Utils.showLongToast(ShareActivity.this, message); Toast.makeText(ShareActivity.this, message, Toast.LENGTH_LONG).show();
ShareActivity.this.finish(); 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" <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 <path
android:fillColor="@color/icons" 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"/> 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>
<group android:checkableBehavior="single"> <group android:checkableBehavior="single">
<item
android:orderInCategory="2"
android:icon="@drawable/ic_send"
android:id="@+id/push_message"
android:title="@string/push_message"
/>
<item <item
android:icon="@drawable/ic_settings" android:icon="@drawable/ic_settings"
android:orderInCategory="2" android:orderInCategory="2"

View File

@@ -36,7 +36,6 @@
<string name="gotify_url">https://push.example.com</string> <string name="gotify_url">https://push.example.com</string>
<string name="username">Username</string> <string name="username">Username</string>
<string name="password">Password</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="disabled_validate_ssl">Disable SSL Validation</string>
<string name="select_ca_certificate">Select CA Certificate</string> <string name="select_ca_certificate">Select CA Certificate</string>
<string name="select_ca_file">Select a Certificate File</string> <string name="select_ca_file">Select a Certificate File</string>
@@ -68,4 +67,11 @@
<string name="settings_appearance">Appearance</string> <string name="settings_appearance">Appearance</string>
<string name="setting_theme">Theme</string> <string name="setting_theme">Theme</string>
<string name="setting_key_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> </resources>

View File

@@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.android.tools.build:gradle:4.0.0'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View File

@@ -10,7 +10,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
} }
} }

View File

@@ -1,6 +1,6 @@
#Fri May 08 13:09:14 CEST 2020 #Fri Jul 10 21:53:51 EEST 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-rc-4-all.zip