Skip to content
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
39 changes: 9 additions & 30 deletions app/src/main/java/com/github/gotify/messages/MessagesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.github.gotify.Utils;
import com.github.gotify.api.Api;
import com.github.gotify.api.ApiException;
import com.github.gotify.api.CertUtils;
import com.github.gotify.api.ClientFactory;
import com.github.gotify.client.ApiClient;
import com.github.gotify.client.api.ClientApi;
Expand All @@ -60,22 +59,17 @@
import com.github.gotify.messages.provider.MessageFacade;
import com.github.gotify.messages.provider.MessageState;
import com.github.gotify.messages.provider.MessageWithImage;
import com.github.gotify.picasso.PicassoDataRequestHandler;
import com.github.gotify.picasso.PicassoHandler;
import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.SettingsActivity;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import okhttp3.Cache;
import okhttp3.OkHttpClient;

import static java.util.Collections.emptyList;

Expand Down Expand Up @@ -123,9 +117,7 @@ public void onReceive(Context context, Intent intent) {
private boolean isLoadMore = false;
private Integer selectAppIdOnDrawerClose = null;

int PICASSO_CACHE_SIZE = 50 * 1024 * 1024; // 50 MB
private Cache picassoCache;
private Picasso picasso;
private PicassoHandler picassoHandler;

// we need to keep the target references otherwise they get gc'ed before they can be called.
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
Expand All @@ -139,8 +131,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.i("Entering " + getClass().getSimpleName());
settings = new Settings(this);

picassoCache = new Cache(new File(getCacheDir(), "picasso-cache"), PICASSO_CACHE_SIZE);
picasso = makePicasso();
picassoHandler = new PicassoHandler(this, settings);

client =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());
Expand All @@ -157,7 +148,7 @@ protected void onCreate(Bundle savedInstanceState) {
messagesView.getContext(), layoutManager.getOrientation());
ListMessageAdapter adapter =
new ListMessageAdapter(
this, settings, picasso, emptyList(), this::scheduleDeletion);
this, settings, picassoHandler.get(), emptyList(), this::scheduleDeletion);

messagesView.addItemDecoration(dividerItemDecoration);
messagesView.setHasFixedSize(true);
Expand Down Expand Up @@ -200,7 +191,7 @@ public void onDrawerClosed(View drawerView) {

public void onRefreshAll(View view) {
try {
picassoCache.evictAll();
picassoHandler.evict();
} catch (IOException e) {
Log.e("Problem evicting Picasso cache", e);
}
Expand Down Expand Up @@ -234,28 +225,16 @@ protected void onUpdateApps(List<Application> applications) {
item.setCheckable(true);
Target t = Utils.toDrawable(getResources(), item::setIcon);
targetReferences.add(t);
picasso.load(Utils.resolveAbsoluteUrl(settings.url() + "/", app.getImage()))
picassoHandler
.get()
.load(Utils.resolveAbsoluteUrl(settings.url() + "/", app.getImage()))
.error(R.drawable.ic_alarm)
.placeholder(R.drawable.ic_placeholder)
.resize(100, 100)
.into(t);
}
}

private Picasso makePicasso() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cache(picassoCache);

CertUtils.applySslSettings(builder, settings.sslSettings());

OkHttp3Downloader downloader = new OkHttp3Downloader(builder.build());

return new Picasso.Builder(this)
.addRequestHandler(new PicassoDataRequestHandler())
.downloader(downloader)
.build();
}

private void initDrawer() {
setSupportActionBar(toolbar);
navigationView.setItemIconTintList(null);
Expand Down Expand Up @@ -370,7 +349,7 @@ protected void onPause() {
@Override
protected void onDestroy() {
super.onDestroy();
picasso.shutdown();
picassoHandler.get().shutdown();
}

private void scheduleDeletion(int position, Message message, boolean listAnimation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.github.gotify.client.model.Application;
import com.github.gotify.client.model.Message;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class MessageImageCombiner {
public class MessageImageCombiner {

List<MessageWithImage> combine(List<Message> messages, List<Application> applications) {
Map<Integer, String> appIdToImage = appIdToImage(applications);
Expand All @@ -26,8 +26,8 @@ List<MessageWithImage> combine(List<Message> messages, List<Application> applica
return result;
}

private Map<Integer, String> appIdToImage(List<Application> applications) {
Map<Integer, String> map = new HashMap<>();
public static Map<Integer, String> appIdToImage(List<Application> applications) {
Map<Integer, String> map = new ConcurrentHashMap<>();
for (Application app : applications) {
map.put(app.getId(), app.getImage());
}
Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/com/github/gotify/picasso/PicassoHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.gotify.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.github.gotify.R;
import com.github.gotify.Settings;
import com.github.gotify.Utils;
import com.github.gotify.api.Callback;
import com.github.gotify.api.CertUtils;
import com.github.gotify.api.ClientFactory;
import com.github.gotify.client.api.ApplicationApi;
import com.github.gotify.log.Log;
import com.github.gotify.messages.provider.MessageImageCombiner;
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import okhttp3.Cache;
import okhttp3.OkHttpClient;

public class PicassoHandler {

private static final int PICASSO_CACHE_SIZE = 50 * 1024 * 1024; // 50 MB
private static final String PICASSO_CACHE_SUBFOLDER = "picasso-cache";

private Context context;
private Settings settings;

private Cache picassoCache;

private Picasso picasso;
private Map<Integer, String> appIdToAppImage = new ConcurrentHashMap<>();

public PicassoHandler(Context context, Settings settings) {
this.context = context;
this.settings = settings;

picassoCache =
new Cache(
new File(context.getCacheDir(), PICASSO_CACHE_SUBFOLDER),
PICASSO_CACHE_SIZE);
picasso = makePicasso();
}

private Picasso makePicasso() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cache(picassoCache);
CertUtils.applySslSettings(builder, settings.sslSettings());
OkHttp3Downloader downloader = new OkHttp3Downloader(builder.build());
return new Picasso.Builder(context).downloader(downloader).build();
}

public Bitmap getIcon(Integer appId) {
if (appId == -1) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.gotify);
}

try {
return picasso.load(
Utils.resolveAbsoluteUrl(
settings.url() + "/", appIdToAppImage.get(appId)))
.get();
} catch (IOException e) {
Log.e("Could not load image for notification", e);
}
return BitmapFactory.decodeResource(context.getResources(), R.drawable.gotify);
}

public void updateAppIds() {
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token())
.createService(ApplicationApi.class)
.getApps()
.enqueue(
Callback.call(
(apps) -> {
appIdToAppImage.clear();
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps));
},
(t) -> {
appIdToAppImage.clear();
}));
}

public Picasso get() {
return picasso;
}

public void evict() throws IOException {
picassoCache.evictAll();
}
}
30 changes: 24 additions & 6 deletions app/src/main/java/com/github/gotify/service/WebSocketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import com.github.gotify.Settings;
import com.github.gotify.Utils;
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.Message;
import com.github.gotify.log.Log;
import com.github.gotify.log.UncaughtExceptionHandler;
import com.github.gotify.messages.Extras;
import com.github.gotify.messages.MessagesActivity;
import com.github.gotify.picasso.PicassoHandler;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -46,16 +48,17 @@ public class WebSocketService extends Service {
private AtomicInteger lastReceivedMessage = new AtomicInteger(NOT_LOADED);
private MissedMessageUtil missingMessageUtil;

private PicassoHandler picassoHandler;

@Override
public void onCreate() {
super.onCreate();
settings = new Settings(this);
missingMessageUtil =
new MissedMessageUtil(
ClientFactory.clientToken(
settings.url(), settings.sslSettings(), settings.token())
.createService(MessageApi.class));
ApiClient client =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());
missingMessageUtil = new MissedMessageUtil(client.createService(MessageApi.class));
Log.i("Create " + getClass().getSimpleName());
picassoHandler = new PicassoHandler(this, settings);
}

@Override
Expand Down Expand Up @@ -115,6 +118,8 @@ private void startPushService() {
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
ReconnectListener receiver = new ReconnectListener(this::doReconnect);
registerReceiver(receiver, intentFilter);

picassoHandler.updateAppIds();
}

private void onDisconnect() {
Expand Down Expand Up @@ -176,13 +181,15 @@ private void onMessage(Message message) {
if (lastReceivedMessage.get() < message.getId()) {
lastReceivedMessage.set(message.getId());
}

broadcast(message);
showNotification(
message.getId(),
message.getTitle(),
message.getMessage(),
message.getPriority(),
message.getExtras());
message.getExtras(),
message.getAppid());
}

private void broadcast(Message message) {
Expand Down Expand Up @@ -224,6 +231,16 @@ private void foreground(String message) {

private void showNotification(
int id, String title, String message, long priority, Map<String, Object> extras) {
showNotification(id, title, message, priority, extras, -1);
}

private void showNotification(
int id,
String title,
String message,
long priority,
Map<String, Object> extras,
Integer appid) {

Intent intent;

Expand Down Expand Up @@ -263,6 +280,7 @@ private void showNotification(
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_gotify)
.setLargeIcon(picassoHandler.getIcon(appid))
.setTicker(getString(R.string.app_name) + " - " + title)
.setGroup(NotificationSupport.Group.MESSAGES)
.setContentTitle(title)
Expand Down