Add share activity
This commit is contained in:
committed by
Jannis Mattheis
parent
85c255e462
commit
f85b5312e6
@@ -45,6 +45,15 @@
|
||||
android:name=".settings.SettingsActivity"
|
||||
android:theme="@style/AppTheme.NoActionBar"
|
||||
android:label="@string/title_activity_settings" />
|
||||
<activity
|
||||
android:name=".sharing.ShareActivity"
|
||||
android:theme="@android:style/Theme.Translucent.NoTitleBar">
|
||||
<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" />
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
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;
|
||||
@@ -96,4 +98,16 @@ public class Utils {
|
||||
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];
|
||||
}
|
||||
|
||||
public static void showLongToast(Context context, String message) {
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ import java.util.ArrayList;
|
||||
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
|
||||
@@ -695,12 +696,4 @@ public class MessagesActivity extends AppCompatActivity
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.github.gotify.sharing;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
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 java.util.List;
|
||||
|
||||
import static com.github.gotify.Utils.first;
|
||||
|
||||
public class ShareActivity extends Activity {
|
||||
private ApiClient client;
|
||||
private Settings settings;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private class SendSharedContent extends AsyncTask<Message, String, String> {
|
||||
@Override
|
||||
protected String doInBackground(Message... messages) {
|
||||
try {
|
||||
MessageApi messageApi = client.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) {
|
||||
Utils.showLongToast(ShareActivity.this, message);
|
||||
ShareActivity.this.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user