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

View File

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

View File

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

View File

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