Adds notification clearing and makes the client a little more stable.

This commit is contained in:
Vavassor 2017-05-19 20:00:57 -04:00
parent 388ecfcf2e
commit 73a5144741
4 changed files with 28 additions and 20 deletions

View File

@ -6,18 +6,18 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" /> <!--For notifications-->
<uses-permission android:name="android.permission.WAKE_LOCK" /> <!--Required by Eclipse Paho-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--Required by Eclipse Paho-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--Required by Eclipse Paho-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".TuskyApplication"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".TuskyApplication">
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"

View File

@ -152,7 +152,7 @@ public class BaseActivity extends AppCompatActivity {
}
protected void createTuskyAPI() {
pushNotificationClient = new PushNotificationClient(this,
pushNotificationClient = new PushNotificationClient(getApplicationContext(),
getString(R.string.tusky_api_url));
}

View File

@ -15,7 +15,6 @@
package com.keylesspalace.tusky;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
@ -215,7 +214,7 @@ public class MainActivity extends BaseActivity implements SFragment.OnUserRemove
.putString("current", "[]")
.apply();
pushNotificationClient.clearNotifications();
pushNotificationClient.clearNotifications(this);
/* After editing a profile, the profile header in the navigation drawer needs to be
* refreshed */

View File

@ -1,5 +1,6 @@
package com.keylesspalace.tusky.util;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
@ -38,6 +39,8 @@ import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import static android.content.Context.NOTIFICATION_SERVICE;
public class PushNotificationClient {
private static final String TAG = "PushNotificationClient";
private static final String TOPIC = "tusky/notification";
@ -51,20 +54,24 @@ public class PushNotificationClient {
private MqttAndroidClient mqttAndroidClient;
private MastodonAPI mastodonApi;
private boolean connected;
private ArrayDeque<QueuedAction> queuedActions;
private boolean subscribed;
public PushNotificationClient(final @NonNull Context context, @NonNull String serverUri) {
public PushNotificationClient(final @NonNull Context applicationContext,
@NonNull String serverUri) {
queuedActions = new ArrayDeque<>();
// Create the MQTT client.
String clientId = MqttClient.generateClientId();
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
mqttAndroidClient = new MqttAndroidClient(applicationContext, serverUri, clientId);
mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
if (reconnect) {
flushQueuedActions();
if (subscribed) {
subscribeToTopic();
}
}
}
@ -75,7 +82,7 @@ public class PushNotificationClient {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
onMessageReceived(context, new String(message.getPayload()));
onMessageReceived(applicationContext, new String(message.getPayload()));
}
@Override
@ -108,7 +115,6 @@ public class PushNotificationClient {
bufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(bufferOptions);
onConnectionSuccess();
connected = true;
flushQueuedActions();
}
@ -125,7 +131,8 @@ public class PushNotificationClient {
}
private void flushQueuedActions() {
for (QueuedAction action : queuedActions) {
while (!queuedActions.isEmpty()) {
QueuedAction action = queuedActions.pop();
switch (action) {
case SUBSCRIBE: subscribeToTopic(); break;
case UNSUBSCRIBE: unsubscribeToTopic(); break;
@ -136,7 +143,7 @@ public class PushNotificationClient {
/** Disconnect from the MQTT broker. */
public void disconnect() {
if (!connected) {
if (!mqttAndroidClient.isConnected()) {
queuedActions.add(QueuedAction.DISCONNECT);
return;
}
@ -154,7 +161,7 @@ public class PushNotificationClient {
/** Subscribe to the push notification topic. */
public void subscribeToTopic() {
if (!connected) {
if (!mqttAndroidClient.isConnected()) {
queuedActions.add(QueuedAction.SUBSCRIBE);
return;
}
@ -162,6 +169,7 @@ public class PushNotificationClient {
mqttAndroidClient.subscribe(TOPIC, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
subscribed = true;
onConnectionSuccess();
}
@ -179,12 +187,13 @@ public class PushNotificationClient {
/** Unsubscribe from the push notification topic. */
public void unsubscribeToTopic() {
if (!connected) {
if (!mqttAndroidClient.isConnected()) {
queuedActions.add(QueuedAction.UNSUBSCRIBE);
return;
}
try {
mqttAndroidClient.unsubscribe(TOPIC);
subscribed = false;
} catch (MqttException e) {
Log.e(TAG, "An exception occurred while unsubscribing." + e.getMessage());
onConnectionFailure();
@ -259,7 +268,7 @@ public class PushNotificationClient {
mastodonApi = retrofit.create(MastodonAPI.class);
}
public void clearNotifications() {
// TODO: make it happen
public void clearNotifications(Context context) {
((NotificationManager) (context.getSystemService(NOTIFICATION_SERVICE))).cancel(NOTIFY_ID);
}
}