Migrate gotify to new client
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
package com.github.gotify;
|
package com.github.gotify;
|
||||||
|
|
||||||
import com.github.gotify.api.Api;
|
import com.github.gotify.api.Api;
|
||||||
|
import com.github.gotify.api.ApiException;
|
||||||
import com.github.gotify.api.Callback;
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.client.ApiClient;
|
|
||||||
import com.github.gotify.client.ApiException;
|
|
||||||
import com.github.gotify.client.api.MessageApi;
|
import com.github.gotify.client.api.MessageApi;
|
||||||
import com.github.gotify.client.model.Message;
|
import com.github.gotify.client.model.Message;
|
||||||
import com.github.gotify.client.model.PagedMessages;
|
import com.github.gotify.client.model.PagedMessages;
|
||||||
@@ -12,26 +11,30 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.github.gotify.api.Callback.call;
|
||||||
|
|
||||||
public class MissedMessageUtil {
|
public class MissedMessageUtil {
|
||||||
static final int NO_MESSAGES = 0;
|
static final int NO_MESSAGES = 0;
|
||||||
|
|
||||||
private final MessageApi api;
|
private final MessageApi api;
|
||||||
|
|
||||||
public MissedMessageUtil(ApiClient client) {
|
public MissedMessageUtil(MessageApi api) {
|
||||||
api = new MessageApi(client);
|
this.api = api;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lastReceivedMessage(Callback.SuccessCallback<Integer> successCallback) {
|
public void lastReceivedMessage(Callback.SuccessCallback<Integer> successCallback) {
|
||||||
Api.<PagedMessages>withLogging((cb) -> api.getMessagesAsync(1, 0, cb))
|
api.getMessages(1, 0)
|
||||||
.handle(
|
.enqueue(
|
||||||
(messages) -> {
|
call(
|
||||||
if (messages.getMessages().size() == 1) {
|
(messages) -> {
|
||||||
successCallback.onSuccess(messages.getMessages().get(0).getId());
|
if (messages.getMessages().size() == 1) {
|
||||||
} else {
|
successCallback.onSuccess(
|
||||||
successCallback.onSuccess(NO_MESSAGES);
|
messages.getMessages().get(0).getId());
|
||||||
}
|
} else {
|
||||||
},
|
successCallback.onSuccess(NO_MESSAGES);
|
||||||
(e) -> {});
|
}
|
||||||
|
},
|
||||||
|
(e) -> {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Message> missingMessages(int till) {
|
public List<Message> missingMessages(int till) {
|
||||||
@@ -40,7 +43,7 @@ public class MissedMessageUtil {
|
|||||||
|
|
||||||
Integer since = null;
|
Integer since = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
PagedMessages pagedMessages = api.getMessages(10, since);
|
PagedMessages pagedMessages = Api.execute(api.getMessages(10, since));
|
||||||
List<Message> messages = pagedMessages.getMessages();
|
List<Message> messages = pagedMessages.getMessages();
|
||||||
List<Message> filtered = filter(messages, till);
|
List<Message> filtered = filter(messages, till);
|
||||||
result.addAll(filtered);
|
result.addAll(filtered);
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import android.graphics.drawable.Drawable;
|
|||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import com.github.gotify.client.ApiClient;
|
|
||||||
import com.github.gotify.client.JSON;
|
import com.github.gotify.client.JSON;
|
||||||
import com.github.gotify.log.Log;
|
import com.github.gotify.log.Log;
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.squareup.picasso.Picasso;
|
import com.squareup.picasso.Picasso;
|
||||||
import com.squareup.picasso.Target;
|
import com.squareup.picasso.Target;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@@ -22,6 +22,8 @@ import okio.Buffer;
|
|||||||
import org.threeten.bp.OffsetDateTime;
|
import org.threeten.bp.OffsetDateTime;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
public static final Gson JSON = new JSON().getGson();
|
||||||
|
|
||||||
public static void showSnackBar(Activity activity, String message) {
|
public static void showSnackBar(Activity activity, String message) {
|
||||||
View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
|
View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
|
||||||
Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT).show();
|
Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT).show();
|
||||||
@@ -51,10 +53,6 @@ public class Utils {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSON json() {
|
|
||||||
return new ApiClient().getJSON();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String readFileFromStream(@NonNull InputStream inputStream) {
|
public static String readFileFromStream(@NonNull InputStream inputStream) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String currentLine;
|
String currentLine;
|
||||||
|
|||||||
@@ -1,4 +1,21 @@
|
|||||||
package com.github.gotify.api;
|
package com.github.gotify.api;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Response;
|
||||||
|
|
||||||
public class Api {
|
public class Api {
|
||||||
|
public static <T> T execute(Call<T> call) throws ApiException {
|
||||||
|
try {
|
||||||
|
Response<T> response = call.execute();
|
||||||
|
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body();
|
||||||
|
} else {
|
||||||
|
throw new ApiException(response);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,44 @@
|
|||||||
package com.github.gotify.api;
|
package com.github.gotify.api;
|
||||||
|
|
||||||
class ApiException {
|
import java.io.IOException;
|
||||||
|
import java.util.Locale;
|
||||||
|
import retrofit2.Response;
|
||||||
|
|
||||||
|
public final class ApiException extends Exception {
|
||||||
|
|
||||||
|
private String body;
|
||||||
|
private int code;
|
||||||
|
|
||||||
|
ApiException(Response<?> response) {
|
||||||
|
super("Api Error", null);
|
||||||
|
try {
|
||||||
|
this.body = response.errorBody() != null ? response.errorBody().string() : "";
|
||||||
|
} catch (IOException e) {
|
||||||
|
this.body = "Error while getting error body :(";
|
||||||
|
}
|
||||||
|
this.code = response.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
ApiException(Throwable cause) {
|
||||||
|
super("Request failed.", cause);
|
||||||
|
this.body = "";
|
||||||
|
this.code = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String body() {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int code() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format(
|
||||||
|
Locale.ENGLISH,
|
||||||
|
"Code(%d) Response: %s",
|
||||||
|
code(),
|
||||||
|
body().substring(0, Math.min(body().length(), 200)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.github.gotify.api;
|
package com.github.gotify.api;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import com.github.gotify.client.ApiException;
|
import com.github.gotify.log.Log;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Response;
|
||||||
|
|
||||||
public class Callback<T> {
|
public class Callback<T> {
|
||||||
private final SuccessCallback<T> onSuccess;
|
private final SuccessCallback<T> onSuccess;
|
||||||
@@ -12,37 +14,39 @@ public class Callback<T> {
|
|||||||
this.onError = onError;
|
this.onError = onError;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onSuccess(T data) {
|
public static <T> retrofit2.Callback<T> callInUI(
|
||||||
this.onSuccess.onSuccess(data);
|
Activity context, SuccessCallback<T> onSuccess, ErrorCallback onError) {
|
||||||
|
return call(
|
||||||
|
(data) -> context.runOnUiThread(() -> onSuccess.onSuccess(data)),
|
||||||
|
(e) -> context.runOnUiThread(() -> onError.onError(e)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void onError(ApiException exception) {
|
public static <T> retrofit2.Callback<T> call() {
|
||||||
this.onError.onError(exception);
|
return call((e) -> {}, (e) -> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Callback<T> call(SuccessCallback<T> onSuccess, ErrorCallback onError) {
|
public static <T> retrofit2.Callback<T> call(
|
||||||
|
SuccessCallback<T> onSuccess, ErrorCallback onError) {
|
||||||
|
return new RetrofitCallback<>(merge(of(onSuccess, onError), errorCallback()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Callback<T> of(SuccessCallback<T> onSuccess, ErrorCallback onError) {
|
||||||
return new Callback<>(onSuccess, onError);
|
return new Callback<>(onSuccess, onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Callback<T> merge(Callback<T> left, Callback<T> right) {
|
private static <T> Callback<T> errorCallback() {
|
||||||
return new Callback<>(
|
return new Callback<>((ignored) -> {}, (error) -> Log.e("Error while api call", error));
|
||||||
(data) -> {
|
|
||||||
left.onSuccess(data);
|
|
||||||
right.onSuccess(data);
|
|
||||||
},
|
|
||||||
(error) -> {
|
|
||||||
left.onError(error);
|
|
||||||
right.onError(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Callback<T> runInUIThread(Activity context, Callback<T> callback) {
|
private static <T> Callback<T> merge(Callback<T> left, Callback<T> right) {
|
||||||
return call(
|
return new Callback<>(
|
||||||
(data) -> {
|
(data) -> {
|
||||||
context.runOnUiThread(() -> callback.onSuccess(data));
|
left.onSuccess.onSuccess(data);
|
||||||
|
right.onSuccess.onSuccess(data);
|
||||||
},
|
},
|
||||||
(e) -> {
|
(error) -> {
|
||||||
context.runOnUiThread(() -> callback.onError(e));
|
left.onError.onError(error);
|
||||||
|
right.onError.onError(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +55,29 @@ public class Callback<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface ErrorCallback {
|
public interface ErrorCallback {
|
||||||
void onError(ApiException e);
|
void onError(ApiException t);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class RetrofitCallback<T> implements retrofit2.Callback<T> {
|
||||||
|
|
||||||
|
private Callback<T> callback;
|
||||||
|
|
||||||
|
private RetrofitCallback(Callback<T> callback) {
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<T> call, Response<T> response) {
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
callback.onSuccess.onSuccess(response.body());
|
||||||
|
} else {
|
||||||
|
callback.onError.onError(new ApiException(response));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<T> call, Throwable t) {
|
||||||
|
callback.onError.onError(new ApiException(t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.github.gotify.api;
|
|||||||
|
|
||||||
import com.github.gotify.SSLSettings;
|
import com.github.gotify.SSLSettings;
|
||||||
import com.github.gotify.Settings;
|
import com.github.gotify.Settings;
|
||||||
import com.github.gotify.Utils;
|
|
||||||
import com.github.gotify.client.ApiClient;
|
import com.github.gotify.client.ApiClient;
|
||||||
import com.github.gotify.client.api.UserApi;
|
import com.github.gotify.client.api.UserApi;
|
||||||
import com.github.gotify.client.api.VersionApi;
|
import com.github.gotify.client.api.VersionApi;
|
||||||
@@ -10,36 +9,41 @@ import com.github.gotify.client.auth.ApiKeyAuth;
|
|||||||
import com.github.gotify.client.auth.HttpBasicAuth;
|
import com.github.gotify.client.auth.HttpBasicAuth;
|
||||||
|
|
||||||
public class ClientFactory {
|
public class ClientFactory {
|
||||||
public static ApiClient unauthorized(String baseUrl, SSLSettings sslSettings) {
|
public static com.github.gotify.client.ApiClient unauthorized(
|
||||||
ApiClient client = new ApiClient();
|
String baseUrl, SSLSettings sslSettings) {
|
||||||
client.setVerifyingSsl(sslSettings.validateSSL);
|
return defaultClient(new String[0], baseUrl, sslSettings);
|
||||||
client.setSslCaCert(Utils.stringToInputStream(sslSettings.cert));
|
|
||||||
client.setBasePath(baseUrl);
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApiClient basicAuth(
|
public static ApiClient basicAuth(
|
||||||
String baseUrl, SSLSettings sslSettings, String username, String password) {
|
String baseUrl, SSLSettings sslSettings, String username, String password) {
|
||||||
ApiClient client = unauthorized(baseUrl, sslSettings);
|
ApiClient client = defaultClient(new String[] {"basicAuth"}, baseUrl, sslSettings);
|
||||||
HttpBasicAuth auth = (HttpBasicAuth) client.getAuthentication("basicAuth");
|
HttpBasicAuth auth = (HttpBasicAuth) client.getApiAuthorizations().get("basicAuth");
|
||||||
auth.setUsername(username);
|
auth.setUsername(username);
|
||||||
auth.setPassword(password);
|
auth.setPassword(password);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApiClient clientToken(String baseUrl, SSLSettings sslSettings, String token) {
|
public static ApiClient clientToken(String baseUrl, SSLSettings sslSettings, String token) {
|
||||||
ApiClient client = unauthorized(baseUrl, sslSettings);
|
ApiClient client = defaultClient(new String[] {"clientTokenHeader"}, baseUrl, sslSettings);
|
||||||
ApiKeyAuth tokenAuth = (ApiKeyAuth) client.getAuthentication("clientTokenHeader");
|
ApiKeyAuth tokenAuth = (ApiKeyAuth) client.getApiAuthorizations().get("clientTokenHeader");
|
||||||
tokenAuth.setApiKey(token);
|
tokenAuth.setApiKey(token);
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VersionApi versionApi(String baseUrl, SSLSettings sslSettings) {
|
public static VersionApi versionApi(String baseUrl, SSLSettings sslSettings) {
|
||||||
return new VersionApi(unauthorized(baseUrl, sslSettings));
|
return unauthorized(baseUrl, sslSettings).createService(VersionApi.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserApi userApiWithToken(Settings settings) {
|
public static UserApi userApiWithToken(Settings settings) {
|
||||||
return new UserApi(clientToken(settings.url(), settings.sslSettings(), settings.token()));
|
return clientToken(settings.url(), settings.sslSettings(), settings.token())
|
||||||
|
.createService(UserApi.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ApiClient defaultClient(
|
||||||
|
String[] authentications, String baseUrl, SSLSettings sslSettings) {
|
||||||
|
ApiClient client = new ApiClient(authentications);
|
||||||
|
CertUtils.applySslSettings(client.getOkBuilder(), sslSettings);
|
||||||
|
client.getAdapterBuilder().baseUrl(baseUrl);
|
||||||
|
return client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,9 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
import com.github.gotify.NotificationSupport;
|
import com.github.gotify.NotificationSupport;
|
||||||
import com.github.gotify.R;
|
import com.github.gotify.R;
|
||||||
import com.github.gotify.Settings;
|
import com.github.gotify.Settings;
|
||||||
import com.github.gotify.api.Api;
|
import com.github.gotify.api.ApiException;
|
||||||
import com.github.gotify.api.Callback;
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.api.ClientFactory;
|
import com.github.gotify.api.ClientFactory;
|
||||||
import com.github.gotify.client.ApiException;
|
|
||||||
import com.github.gotify.client.api.VersionApi;
|
|
||||||
import com.github.gotify.client.model.User;
|
import com.github.gotify.client.model.User;
|
||||||
import com.github.gotify.client.model.VersionInfo;
|
import com.github.gotify.client.model.VersionInfo;
|
||||||
import com.github.gotify.log.Log;
|
import com.github.gotify.log.Log;
|
||||||
@@ -24,6 +22,8 @@ import com.github.gotify.login.LoginActivity;
|
|||||||
import com.github.gotify.messages.MessagesActivity;
|
import com.github.gotify.messages.MessagesActivity;
|
||||||
import com.github.gotify.service.WebSocketService;
|
import com.github.gotify.service.WebSocketService;
|
||||||
|
|
||||||
|
import static com.github.gotify.api.Callback.callInUI;
|
||||||
|
|
||||||
public class InitializationActivity extends AppCompatActivity {
|
public class InitializationActivity extends AppCompatActivity {
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
|
|
||||||
@@ -55,24 +55,25 @@ public class InitializationActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void tryAuthenticate() {
|
private void tryAuthenticate() {
|
||||||
Api.withLogging(ClientFactory.userApiWithToken(settings)::currentUserAsync)
|
ClientFactory.userApiWithToken(settings)
|
||||||
.handleInUIThread(this, this::authenticated, this::failed);
|
.currentUser()
|
||||||
|
.enqueue(callInUI(this, this::authenticated, this::failed));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void failed(ApiException exception) {
|
private void failed(ApiException exception) {
|
||||||
if (exception.getCode() == 0) {
|
if (exception.code() == 0) {
|
||||||
dialog(getString(R.string.not_available, settings.url()));
|
dialog(getString(R.string.not_available, settings.url()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exception.getCode() == 401) {
|
if (exception.code() == 401) {
|
||||||
dialog(getString(R.string.auth_failed));
|
dialog(getString(R.string.auth_failed));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String response = exception.getResponseBody();
|
String response = exception.body();
|
||||||
response = response.substring(0, Math.min(200, response.length()));
|
response = response.substring(0, Math.min(200, response.length()));
|
||||||
dialog(getString(R.string.other_error, settings.url(), exception.getCode(), response));
|
dialog(getString(R.string.other_error, settings.url(), exception.code(), response));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dialog(String message) {
|
private void dialog(String message) {
|
||||||
@@ -116,8 +117,8 @@ public class InitializationActivity extends AppCompatActivity {
|
|||||||
private void requestVersion(
|
private void requestVersion(
|
||||||
final Callback.SuccessCallback<VersionInfo> callback,
|
final Callback.SuccessCallback<VersionInfo> callback,
|
||||||
final Callback.ErrorCallback errorCallback) {
|
final Callback.ErrorCallback errorCallback) {
|
||||||
VersionApi versionApi = ClientFactory.versionApi(settings.url(), settings.sslSettings());
|
ClientFactory.versionApi(settings.url(), settings.sslSettings())
|
||||||
Api.withLogging(versionApi::getVersionAsync)
|
.getVersion()
|
||||||
.handleInUIThread(this, callback, errorCallback);
|
.enqueue(callInUI(this, callback, errorCallback));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,12 +22,11 @@ import com.github.gotify.R;
|
|||||||
import com.github.gotify.SSLSettings;
|
import com.github.gotify.SSLSettings;
|
||||||
import com.github.gotify.Settings;
|
import com.github.gotify.Settings;
|
||||||
import com.github.gotify.Utils;
|
import com.github.gotify.Utils;
|
||||||
import com.github.gotify.api.Api;
|
import com.github.gotify.api.ApiException;
|
||||||
import com.github.gotify.api.Callback;
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.api.CertUtils;
|
import com.github.gotify.api.CertUtils;
|
||||||
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.ApiException;
|
|
||||||
import com.github.gotify.client.api.TokenApi;
|
import com.github.gotify.client.api.TokenApi;
|
||||||
import com.github.gotify.client.api.UserApi;
|
import com.github.gotify.client.api.UserApi;
|
||||||
import com.github.gotify.client.model.Client;
|
import com.github.gotify.client.model.Client;
|
||||||
@@ -36,10 +35,12 @@ import com.github.gotify.init.InitializationActivity;
|
|||||||
import com.github.gotify.log.Log;
|
import com.github.gotify.log.Log;
|
||||||
import com.github.gotify.log.LogsActivity;
|
import com.github.gotify.log.LogsActivity;
|
||||||
import com.github.gotify.log.UncaughtExceptionHandler;
|
import com.github.gotify.log.UncaughtExceptionHandler;
|
||||||
import com.squareup.okhttp.HttpUrl;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.cert.Certificate;
|
import java.security.cert.Certificate;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
|
||||||
|
import static com.github.gotify.api.Callback.callInUI;
|
||||||
|
|
||||||
public class LoginActivity extends AppCompatActivity {
|
public class LoginActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@@ -111,12 +112,9 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
final String fixedUrl = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
|
final String fixedUrl = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
|
||||||
|
|
||||||
Api.withLogging(
|
ClientFactory.versionApi(fixedUrl, tempSSLSettings())
|
||||||
ClientFactory.versionApi(
|
.getVersion()
|
||||||
fixedUrl,
|
.enqueue(callInUI(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl)));
|
||||||
new SSLSettings(!disableSSLValidation, caCertContents))
|
|
||||||
::getVersionAsync)
|
|
||||||
.handleInUIThread(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClick(R.id.open_logs)
|
@OnClick(R.id.open_logs)
|
||||||
@@ -161,7 +159,7 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
FILE_SELECT_CODE);
|
FILE_SELECT_CODE);
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
// case for user not having a file browser installed
|
// case for user not having a file browser installed
|
||||||
Utils.showSnackBar(LoginActivity.this, getString(R.string.please_install_file_browser));
|
Utils.showSnackBar(this, getString(R.string.please_install_file_browser));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,8 +192,7 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
advancedDialog.showRemoveCACertificate(name);
|
advancedDialog.showRemoveCACertificate(name);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Utils.showSnackBar(
|
Utils.showSnackBar(this, getString(R.string.select_ca_failed, e.getMessage()));
|
||||||
LoginActivity.this, getString(R.string.select_ca_failed, e.getMessage()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,13 +231,10 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
loginProgress.setVisibility(View.VISIBLE);
|
loginProgress.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
ApiClient client =
|
ApiClient client =
|
||||||
ClientFactory.basicAuth(
|
ClientFactory.basicAuth(settings.url(), tempSSLSettings(), username, password);
|
||||||
settings.url(),
|
client.createService(UserApi.class)
|
||||||
new SSLSettings(!disableSSLValidation, caCertContents),
|
.currentUser()
|
||||||
username,
|
.enqueue(callInUI(this, (user) -> newClientDialog(client), this::onInvalidLogin));
|
||||||
password);
|
|
||||||
Api.withLogging(new UserApi(client)::currentUserAsync)
|
|
||||||
.handleInUIThread(this, (user) -> newClientDialog(client), this::onInvalidLogin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onInvalidLogin(ApiException e) {
|
private void onInvalidLogin(ApiException e) {
|
||||||
@@ -265,8 +259,9 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
public DialogInterface.OnClickListener doCreateClient(ApiClient client, EditText nameProvider) {
|
public DialogInterface.OnClickListener doCreateClient(ApiClient client, EditText nameProvider) {
|
||||||
return (a, b) -> {
|
return (a, b) -> {
|
||||||
Client newClient = new Client().name(nameProvider.getText().toString());
|
Client newClient = new Client().name(nameProvider.getText().toString());
|
||||||
Api.<Client>withLogging((cb) -> new TokenApi(client).createClientAsync(newClient, cb))
|
client.createService(TokenApi.class)
|
||||||
.handleInUIThread(this, this::onCreatedClient, this::onFailedToCreateClient);
|
.createClient(newClient)
|
||||||
|
.enqueue(callInUI(this, this::onCreatedClient, this::onFailedToCreateClient));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,6 +287,10 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String versionError(String url, ApiException exception) {
|
private String versionError(String url, ApiException exception) {
|
||||||
return getString(R.string.version_failed, url + "/version", exception.getCode());
|
return getString(R.string.version_failed, url + "/version", exception.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
private SSLSettings tempSSLSettings() {
|
||||||
|
return new SSLSettings(!disableSSLValidation, caCertContents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,10 +27,11 @@ import com.github.gotify.MissedMessageUtil;
|
|||||||
import com.github.gotify.R;
|
import com.github.gotify.R;
|
||||||
import com.github.gotify.Settings;
|
import com.github.gotify.Settings;
|
||||||
import com.github.gotify.Utils;
|
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.CertUtils;
|
||||||
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.ApiException;
|
|
||||||
import com.github.gotify.client.api.MessageApi;
|
import com.github.gotify.client.api.MessageApi;
|
||||||
import com.github.gotify.client.api.TokenApi;
|
import com.github.gotify.client.api.TokenApi;
|
||||||
import com.github.gotify.client.model.Application;
|
import com.github.gotify.client.model.Application;
|
||||||
@@ -46,13 +47,13 @@ import com.github.gotify.messages.provider.MessageState;
|
|||||||
import com.github.gotify.messages.provider.MessageWithImage;
|
import com.github.gotify.messages.provider.MessageWithImage;
|
||||||
import com.github.gotify.service.WebSocketService;
|
import com.github.gotify.service.WebSocketService;
|
||||||
import com.google.android.material.navigation.NavigationView;
|
import com.google.android.material.navigation.NavigationView;
|
||||||
import com.squareup.okhttp.HttpUrl;
|
|
||||||
import com.squareup.picasso.OkHttp3Downloader;
|
import com.squareup.picasso.OkHttp3Downloader;
|
||||||
import com.squareup.picasso.Picasso;
|
import com.squareup.picasso.Picasso;
|
||||||
import com.squareup.picasso.Target;
|
import com.squareup.picasso.Target;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
@@ -65,7 +66,7 @@ public class MessagesActivity extends AppCompatActivity
|
|||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String messageJson = intent.getStringExtra("message");
|
String messageJson = intent.getStringExtra("message");
|
||||||
Message message = Utils.json().deserialize(messageJson, Message.class);
|
Message message = Utils.JSON.fromJson(messageJson, Message.class);
|
||||||
new NewSingleMessage().execute(message);
|
new NewSingleMessage().execute(message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -121,7 +122,7 @@ public class MessagesActivity extends AppCompatActivity
|
|||||||
appsHolder.request();
|
appsHolder.request();
|
||||||
initDrawer();
|
initDrawer();
|
||||||
|
|
||||||
messages = new MessageFacade(new MessageApi(client), appsHolder);
|
messages = new MessageFacade(client.createService(MessageApi.class), appsHolder);
|
||||||
|
|
||||||
messagesView.setOnScrollListener(this);
|
messagesView.setOnScrollListener(this);
|
||||||
messagesView.setAdapter(new ListMessageAdapter(this, picasso, emptyList(), this::delete));
|
messagesView.setAdapter(new ListMessageAdapter(this, picasso, emptyList(), this::delete));
|
||||||
@@ -310,7 +311,9 @@ public class MessagesActivity extends AppCompatActivity
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Message> newMessages = new MissedMessageUtil(client).missingMessages(id);
|
List<Message> newMessages =
|
||||||
|
new MissedMessageUtil(client.createService(MessageApi.class))
|
||||||
|
.missingMessages(id);
|
||||||
messages.addMessages(newMessages);
|
messages.addMessages(newMessages);
|
||||||
return !newMessages.isEmpty();
|
return !newMessages.isEmpty();
|
||||||
}
|
}
|
||||||
@@ -423,12 +426,12 @@ public class MessagesActivity extends AppCompatActivity
|
|||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... ignore) {
|
protected Void doInBackground(Void... ignore) {
|
||||||
TokenApi api =
|
TokenApi api =
|
||||||
new TokenApi(
|
ClientFactory.clientToken(
|
||||||
ClientFactory.clientToken(
|
settings.url(), settings.sslSettings(), settings.token())
|
||||||
settings.url(), settings.sslSettings(), settings.token()));
|
.createService(TokenApi.class);
|
||||||
stopService(new Intent(MessagesActivity.this, WebSocketService.class));
|
stopService(new Intent(MessagesActivity.this, WebSocketService.class));
|
||||||
try {
|
try {
|
||||||
List<Client> clients = api.getClients();
|
List<Client> clients = Api.execute(api.getClients());
|
||||||
|
|
||||||
Client currentClient = null;
|
Client currentClient = null;
|
||||||
for (Client client : clients) {
|
for (Client client : clients) {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ package com.github.gotify.messages.provider;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import com.github.gotify.Utils;
|
import com.github.gotify.Utils;
|
||||||
import com.github.gotify.api.Api;
|
import com.github.gotify.api.ApiException;
|
||||||
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.client.ApiClient;
|
import com.github.gotify.client.ApiClient;
|
||||||
import com.github.gotify.client.ApiException;
|
|
||||||
import com.github.gotify.client.api.TokenApi;
|
import com.github.gotify.client.api.TokenApi;
|
||||||
import com.github.gotify.client.model.Application;
|
import com.github.gotify.client.model.Application;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -28,9 +28,9 @@ public class ApplicationHolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void request() {
|
public void request() {
|
||||||
TokenApi tokenApi = new TokenApi(client);
|
client.createService(TokenApi.class)
|
||||||
Api.withLogging(tokenApi::getAppsAsync)
|
.getApps()
|
||||||
.handleInUIThread(activity, this::onReceiveApps, this::onFailedApps);
|
.enqueue(Callback.callInUI(activity, this::onReceiveApps, this::onFailedApps));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onReceiveApps(List<Application> apps) {
|
private void onReceiveApps(List<Application> apps) {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package com.github.gotify.messages.provider;
|
package com.github.gotify.messages.provider;
|
||||||
|
|
||||||
import com.github.gotify.api.Api;
|
import com.github.gotify.api.Api;
|
||||||
import com.github.gotify.client.ApiException;
|
import com.github.gotify.api.ApiException;
|
||||||
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.client.api.MessageApi;
|
import com.github.gotify.client.api.MessageApi;
|
||||||
import com.github.gotify.client.model.Message;
|
import com.github.gotify.client.model.Message;
|
||||||
import com.github.gotify.client.model.PagedMessages;
|
import com.github.gotify.client.model.PagedMessages;
|
||||||
@@ -19,9 +20,9 @@ class MessageRequester {
|
|||||||
try {
|
try {
|
||||||
Log.i("Loading more messages for " + state.appId);
|
Log.i("Loading more messages for " + state.appId);
|
||||||
if (MessageState.ALL_MESSAGES == state.appId) {
|
if (MessageState.ALL_MESSAGES == state.appId) {
|
||||||
return messageApi.getMessages(LIMIT, state.nextSince);
|
return Api.execute(messageApi.getMessages(LIMIT, state.nextSince));
|
||||||
} else {
|
} else {
|
||||||
return messageApi.getAppMessages(state.appId, LIMIT, state.nextSince);
|
return Api.execute(messageApi.getAppMessages(state.appId, LIMIT, state.nextSince));
|
||||||
}
|
}
|
||||||
} catch (ApiException apiException) {
|
} catch (ApiException apiException) {
|
||||||
Log.e("failed requesting messages", apiException);
|
Log.e("failed requesting messages", apiException);
|
||||||
@@ -31,17 +32,16 @@ class MessageRequester {
|
|||||||
|
|
||||||
void asyncRemoveMessage(Message message) {
|
void asyncRemoveMessage(Message message) {
|
||||||
Log.i("Removing message with id " + message.getId());
|
Log.i("Removing message with id " + message.getId());
|
||||||
Api.<Void>withLogging((cb) -> messageApi.deleteMessageAsync(message.getId(), cb))
|
messageApi.deleteMessage(message.getId()).enqueue(Callback.call());
|
||||||
.handle((a) -> {}, (e) -> {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean deleteAll(Integer appId) {
|
boolean deleteAll(Integer appId) {
|
||||||
try {
|
try {
|
||||||
Log.i("Deleting all messages for " + appId);
|
Log.i("Deleting all messages for " + appId);
|
||||||
if (MessageState.ALL_MESSAGES == appId) {
|
if (MessageState.ALL_MESSAGES == appId) {
|
||||||
messageApi.deleteMessages();
|
Api.execute(messageApi.deleteMessages());
|
||||||
} else {
|
} else {
|
||||||
messageApi.deleteAppMessages(appId);
|
Api.execute(messageApi.deleteAppMessages(appId));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (ApiException e) {
|
} catch (ApiException e) {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import com.github.gotify.SSLSettings;
|
|||||||
import com.github.gotify.Utils;
|
import com.github.gotify.Utils;
|
||||||
import com.github.gotify.api.Callback;
|
import com.github.gotify.api.Callback;
|
||||||
import com.github.gotify.api.CertUtils;
|
import com.github.gotify.api.CertUtils;
|
||||||
import com.github.gotify.client.JSON;
|
|
||||||
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 java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -18,7 +17,6 @@ import okhttp3.WebSocketListener;
|
|||||||
|
|
||||||
public class WebSocketConnection {
|
public class WebSocketConnection {
|
||||||
private OkHttpClient client;
|
private OkHttpClient client;
|
||||||
private static final JSON gson = Utils.json();
|
|
||||||
|
|
||||||
private final Handler handler = new Handler();
|
private final Handler handler = new Handler();
|
||||||
private int errorCount = 0;
|
private int errorCount = 0;
|
||||||
@@ -125,7 +123,7 @@ public class WebSocketConnection {
|
|||||||
public void onMessage(WebSocket webSocket, String text) {
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
Log.i("WebSocket: received message " + text);
|
Log.i("WebSocket: received message " + text);
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
Message message = gson.deserialize(text, Message.class);
|
Message message = Utils.JSON.fromJson(text, Message.class);
|
||||||
onMessage.onSuccess(message);
|
onMessage.onSuccess(message);
|
||||||
}
|
}
|
||||||
super.onMessage(webSocket, text);
|
super.onMessage(webSocket, text);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import com.github.gotify.R;
|
|||||||
import com.github.gotify.Settings;
|
import com.github.gotify.Settings;
|
||||||
import com.github.gotify.Utils;
|
import com.github.gotify.Utils;
|
||||||
import com.github.gotify.api.ClientFactory;
|
import com.github.gotify.api.ClientFactory;
|
||||||
|
import com.github.gotify.client.api.MessageApi;
|
||||||
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.log.UncaughtExceptionHandler;
|
import com.github.gotify.log.UncaughtExceptionHandler;
|
||||||
@@ -46,7 +47,8 @@ public class WebSocketService extends Service {
|
|||||||
missingMessageUtil =
|
missingMessageUtil =
|
||||||
new MissedMessageUtil(
|
new MissedMessageUtil(
|
||||||
ClientFactory.clientToken(
|
ClientFactory.clientToken(
|
||||||
settings.url(), settings.sslSettings(), settings.token()));
|
settings.url(), settings.sslSettings(), settings.token())
|
||||||
|
.createService(MessageApi.class));
|
||||||
Log.i("Create " + getClass().getSimpleName());
|
Log.i("Create " + getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +149,7 @@ public class WebSocketService extends Service {
|
|||||||
private void broadcast(Message message) {
|
private void broadcast(Message message) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setAction(NEW_MESSAGE_BROADCAST);
|
intent.setAction(NEW_MESSAGE_BROADCAST);
|
||||||
intent.putExtra("message", Utils.json().serialize(message));
|
intent.putExtra("message", Utils.JSON.toJson(message));
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user