Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushing, sharing messages prototype #122

Merged
merged 2 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -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>
12 changes: 11 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
android:name=".settings.SettingsActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/title_activity_settings" />
<activity
tomasvanagas marked this conversation as resolved.
Show resolved Hide resolved
android:name=".sharing.ShareActivity"
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" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

<service android:name=".service.WebSocketService" />

Expand All @@ -55,4 +65,4 @@
</receiver>
</application>

</manifest>
</manifest>
7 changes: 7 additions & 0 deletions app/src/main/java/com/github/gotify/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ public static InputStream stringToInputStream(String str) {
if (str == null) return null;
return new Buffer().writeUtf8(str).inputStream();
}

public static <T> T first(T[] data) {
if (data.length != 1) {
throw new IllegalArgumentException("must be one element");
}
return data[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
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;
Expand All @@ -71,6 +72,7 @@
import java.util.Arrays;
import java.util.List;

import static com.github.gotify.Utils.first;
import static java.util.Collections.emptyList;

public class MessagesActivity extends AppCompatActivity
Expand Down Expand Up @@ -300,6 +302,9 @@ public boolean onNavigationItemSelected(MenuItem item) {
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);
Expand Down Expand Up @@ -695,12 +700,4 @@ private void updateMessagesAndStopLoading(List<MessageWithImage> messageWithImag
adapter.setItems(messageWithImages);
adapter.notifyDataSetChanged();
}

private <T> T first(T[] data) {
if (data.length != 1) {
throw new IllegalArgumentException("must be one element");
}

return data[0];
}
}
153 changes: 153 additions & 0 deletions app/src/main/java/com/github/gotify/sharing/ShareActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
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.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.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.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 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);

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

@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 = 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();
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_send.xml
Original file line number Diff line number Diff line change
@@ -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>
4 changes: 3 additions & 1 deletion app/src/main/res/drawable/ic_settings.xml
Original file line number Diff line number Diff line change
@@ -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"/>
Expand Down
92 changes: 92 additions & 0 deletions app/src/main/res/layout/activity_share.xml
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/messages_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Loading