Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some magic Android OS has no externalFileDir or it will throw an exception, so use internal storage directly,compatible to use MemoryPersistence #423

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

import java.io.File;
Expand Down Expand Up @@ -193,23 +193,37 @@ public void connect(MqttConnectOptions options, String invocationContext, String
try {
if (persistence == null) {
// ask Android where we can put files
File myDir = service.getExternalFilesDir(TAG);

if (myDir == null) {
// No external storage, use internal storage instead.
// some magic Android OS has no externalFileDir or it will throw an exception, so use internal storage directly.
File myDir = null;
// use internal storage instead.
try {
myDir = service.getDir(TAG, Context.MODE_PRIVATE);

if (myDir == null) {
//Shouldn't happen.
resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, "Error! No external and internal storage available");
resultBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, new MqttPersistenceException());
service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
return;
} catch (Exception e) {
//skip
}
if (myDir == null) {
try {
myDir = service.getFilesDir();
if (myDir != null) {
StringBuilder stringBuilder = new StringBuilder(myDir.getAbsolutePath());
stringBuilder.append(File.separator);
stringBuilder.append(TAG);
myDir = new File(stringBuilder.toString());
myDir.mkdirs();
}
} catch (Exception e) {
myDir = null;
}
}

// use that to setup MQTT client persistence storage
persistence = new MqttDefaultFilePersistence(myDir.getAbsolutePath());
if (myDir == null) {
// compatible very few magic Android OS.
persistence = new MemoryPersistence();
} else {
// use that to setup MQTT client persistence storage
persistence = new MqttDefaultFilePersistence(
myDir.getAbsolutePath());
}
}

IMqttActionListener listener = new MqttConnectionListener(resultBundle) {
Expand Down Expand Up @@ -671,7 +685,7 @@ public void onSuccess(IMqttToken asyncActionToken) {

@Override
public void onFailure(IMqttToken asyncActionToken,
Throwable exception) {
Throwable exception) {
// No action
}
});
Expand Down Expand Up @@ -762,7 +776,7 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
* @param activityToken
*/
private void storeSendDetails(final String topic, final MqttMessage msg, final IMqttDeliveryToken messageToken,
final String invocationContext, final String activityToken) {
final String invocationContext, final String activityToken) {
savedTopics.put(messageToken, topic);
savedSentMessages.put(messageToken, msg);
savedActivityTokens.put(messageToken, activityToken);
Expand Down