2018-11-22 14:45:16 +01:00
|
|
|
package de.svenkubiak.jpushover.apis;
|
|
|
|
|
|
|
|
import java.util.NavigableMap;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.TreeMap;
|
2020-07-27 09:22:49 +02:00
|
|
|
import java.util.concurrent.Future;
|
2018-11-22 14:45:16 +01:00
|
|
|
|
|
|
|
import de.svenkubiak.jpushover.enums.Param;
|
2020-03-06 08:35:42 +01:00
|
|
|
import de.svenkubiak.jpushover.enums.Url;
|
2021-06-20 12:01:46 +02:00
|
|
|
import de.svenkubiak.jpushover.exceptions.JPushoverException;
|
2018-11-22 14:45:16 +01:00
|
|
|
import de.svenkubiak.jpushover.http.PushoverRequest;
|
|
|
|
import de.svenkubiak.jpushover.http.PushoverResponse;
|
2020-07-27 09:22:49 +02:00
|
|
|
import de.svenkubiak.jpushover.services.AsyncExecutor;
|
|
|
|
import de.svenkubiak.jpushover.services.AsyncService;
|
2018-11-22 14:45:16 +01:00
|
|
|
import de.svenkubiak.jpushover.utils.Validate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author svenkubiak
|
|
|
|
*
|
|
|
|
*/
|
2020-07-27 09:22:49 +02:00
|
|
|
public class Glance implements API {
|
2020-07-26 19:06:36 +02:00
|
|
|
private NavigableMap<String, String> body = new TreeMap<>();
|
2020-03-05 14:51:53 +01:00
|
|
|
private String proxyHost;
|
2018-11-22 14:45:16 +01:00
|
|
|
private int proxyPort;
|
|
|
|
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance() {
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
/**
|
|
|
|
* Your application's API token
|
|
|
|
* (required)
|
|
|
|
*
|
|
|
|
* @param token The pushover API token
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withToken(String token) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(token, "token can not be null");
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.TOKEN.toString(), token);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
/**
|
|
|
|
* The user/group key (not e-mail address) of your user (or you),
|
|
|
|
* viewable when logged into the @see <a href="https://pushover.net/login">pushover dashboard</a>
|
|
|
|
* (required)
|
|
|
|
*
|
|
|
|
* @param user The username
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withUser(String user) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(user, "user can not be null");
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.USER.toString(), user);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
/**
|
|
|
|
* Your user's device name to send the message directly to that device,
|
|
|
|
* rather than all of the user's devices
|
|
|
|
* (optional)
|
|
|
|
*
|
|
|
|
* @param device The device name
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withDevice(String device) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(device, "device can not be null");
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.DEVICE.toString(), device);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A description of the data being shown, such as "Widgets Sold"
|
|
|
|
*
|
|
|
|
* @param title the title to use
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withTitle(String title) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(title, "title can not be null");
|
|
|
|
Validate.checkArgument(title.length() <= 100, "Title must not exceed a length of 100 characters");
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.TITLE.toString(), title);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main line of data, used on most screens
|
|
|
|
*
|
|
|
|
* @param text the text to use
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withText(String text) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(text, "text can not be null");
|
|
|
|
Validate.checkArgument(text.length() <= 100, "Text must not exceed a length of 100 characters");
|
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.TEXT.toString(), text);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A second line of data
|
|
|
|
*
|
|
|
|
* @param subtext the subtext to use
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withSubtext(String subtext) {
|
2018-11-22 14:45:16 +01:00
|
|
|
Objects.requireNonNull(subtext, "subtext can not be null");
|
2020-03-05 14:51:53 +01:00
|
|
|
Validate.checkArgument(subtext.length() <= 100, "Subtext must not exceed a length of 100 characters");
|
2018-11-22 14:45:16 +01:00
|
|
|
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.SUBTEXT.toString(), subtext);
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shown on smaller screens; useful for simple counts
|
|
|
|
*
|
|
|
|
* @param count the count to use
|
|
|
|
* @return Glance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withCount(int count) {
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.COUNT.toString(), String.valueOf(count));
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shown on some screens as a progress bar/circle
|
|
|
|
*
|
|
|
|
* @param percent the percent to use
|
|
|
|
* @return GLance instance
|
|
|
|
*/
|
2020-07-27 13:35:49 +02:00
|
|
|
public Glance withPercent(int percent) {
|
2020-07-26 19:06:36 +02:00
|
|
|
body.put(Param.PERCENT.toString(), String.valueOf(percent));
|
2018-11-22 14:45:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a glance to pushover
|
|
|
|
*
|
|
|
|
* @return PushoverResponse instance
|
2021-06-20 12:01:46 +02:00
|
|
|
*
|
|
|
|
* @throws JPushoverException
|
2018-11-22 14:45:16 +01:00
|
|
|
*/
|
2020-07-27 09:22:49 +02:00
|
|
|
@Override
|
2021-06-20 12:01:46 +02:00
|
|
|
public PushoverResponse push() throws JPushoverException {
|
2020-07-26 19:06:36 +02:00
|
|
|
Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for a glance");
|
|
|
|
Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for a glance");
|
2018-11-22 14:45:16 +01:00
|
|
|
|
2020-03-06 08:35:42 +01:00
|
|
|
return new PushoverRequest().push(Url.GLANCES.toString(), body, this.proxyHost, this.proxyPort);
|
2018-11-22 14:45:16 +01:00
|
|
|
}
|
2020-07-27 09:22:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a glance to pushover asynchronously
|
|
|
|
*
|
|
|
|
* @return PushoverResponse instance
|
|
|
|
*/
|
|
|
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
2021-06-20 12:01:46 +02:00
|
|
|
public Future<PushoverResponse> pushAsync() {
|
2020-07-27 09:22:49 +02:00
|
|
|
return AsyncService.getInstance().execute(new AsyncExecutor(this));
|
|
|
|
}
|
2020-07-27 13:35:49 +02:00
|
|
|
|
|
|
|
public String getValue(String param) {
|
|
|
|
Objects.requireNonNull(param, "param can not be null");
|
|
|
|
|
|
|
|
return body.get(param);
|
|
|
|
}
|
2018-11-22 14:45:16 +01:00
|
|
|
}
|