Skip to content

Commit

Permalink
Fixed a bunch of bugs from firebase crash reports (please in the futu…
Browse files Browse the repository at this point in the history
…re also report it on github! it's horribly painful trying to figure out specifically what you did to crash the app) Fixed issue #5. Fixed  issue #3?
  • Loading branch information
ezhes committed May 30, 2017
1 parent 1f74b5c commit 7a07f42
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 56 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ android {
applicationId "eu.aero2x.andromedab"
minSdkVersion 19
targetSdkVersion 25
versionCode 4
versionName "1.2.0.2"
versionCode 5
versionName "1.2.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "MIN_SERVER_VERSION", '"1.2.0"'
buildConfigField "String", "MIN_SERVER_VERSION", '"1.2.1"'
}
debug{
buildConfigField "String", "MIN_SERVER_VERSION", '"1.2.0"'
buildConfigField "String", "MIN_SERVER_VERSION", '"1.2.1"'
}

}
Expand Down
65 changes: 21 additions & 44 deletions app/src/main/java/eu/aero2x/andromedab/ContactSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void onNewIntent(Intent intent) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_select);

mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
//Setup our update from github
new AppUpdater(this)
Expand Down Expand Up @@ -131,8 +132,20 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(DialogInterface dialog, int whichButton) {
String givenEndPoint = apiIPEndPoint.getText().toString().trim();
String givenKey = apiProtectionKey.getText().toString().trim();
int givenAPIPort = Integer.valueOf(apiPort.getText().toString().trim());
int givenSocketPort = Integer.valueOf(socketPort.getText().toString().trim());

String apiPortText = apiPort.getText().toString().trim();
if (apiPortText.equals("")) {
//No API port given, default
apiPortText = "8735";
}
String socketPortText = socketPort.getText().toString().trim();
if (socketPortText.equals("")) {
//No socket port given, default
socketPortText = "8736";
}

int givenAPIPort = Integer.valueOf(apiPortText);
int givenSocketPort = Integer.valueOf(socketPortText);

SharedPreferences.Editor editor = getSharedPreferences("CONFIG", MODE_PRIVATE).edit();
editor.putString("apiIPEndpoint",givenEndPoint);
Expand Down Expand Up @@ -258,56 +271,29 @@ public void onResponse(String response) {
//Log the server version
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, versionObject.getString("version"));
mFirebaseAnalytics.logEvent("server_version", bundle);
mFirebaseAnalytics.logEvent("server_version_" + versionObject.getString("version"), bundle);

Version serverVersion = new Version(versionObject.getString("version"));


//Check if our server version is below the app's required
if (serverVersion.compareTo(new Version(BuildConfig.MIN_SERVER_VERSION)) < 0) {
AlertDialog alertDialog = new AlertDialog.Builder(ContactSelect.this).create();
alertDialog.setTitle("Server version too old");
alertDialog.setMessage("Your server is running version " + serverVersion.get() + " but the BUILDCONFIG for the application demands that you be running at least " + BuildConfig.MIN_SERVER_VERSION + "\n\nYou can continue to use the application however behavior is entirely undocumented.");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
UITools.showAlertDialogSafe(ContactSelect.this,R.id.activity_contact_select,"Server version too old","Your server is running version " + serverVersion.get() + " but the BUILDCONFIG for the application demands that you be running at least " + BuildConfig.MIN_SERVER_VERSION + "\n\nYou can continue to use the application however behavior is entirely undocumented.");
}
//We are online!
UITools.showSnackBar(findViewById(android.R.id.content), "Successfully connected!", Snackbar.LENGTH_LONG);
//Since we can see the server, setup our contacts
setupConversations();
}catch (JSONException e) {
AlertDialog alertDialog = new AlertDialog.Builder(ContactSelect.this).create();
alertDialog.setTitle("Server version too old");
alertDialog.setMessage("The server should have responded with a version number JSON at /isUp. You can continue to use the app but it is highly recommended that you update the server ASAP;\nServer said:" + response +"\n" + e.toString());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
UITools.showAlertDialogSafe(ContactSelect.this,R.id.activity_contact_select,"Server version too old","The server should have responded with a version number JSON at /isUp. You can continue to use the app but it is highly recommended that you update the server ASAP;\nServer said:" + response +"\n" + e.toString());
//Log the server version
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, "<1.1.1");
mFirebaseAnalytics.logEvent("server_version", bundle);
//Since we can see the server, setup our contacts
setupConversations();
}catch (IllegalArgumentException e) {
AlertDialog alertDialog = new AlertDialog.Builder(ContactSelect.this).create();
alertDialog.setTitle("Server version invalid");
alertDialog.setMessage("The server should have responded with a version number JSON at /isUp but we got:" + response +"\n" + e.toString());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
UITools.showAlertDialogSafe(ContactSelect.this,R.id.activity_contact_select,"Server version invalid","The server should have responded with a version number JSON at /isUp but we got:" + response +"\n" + e.toString());
//Log the server version
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, response);
Expand All @@ -322,16 +308,8 @@ public void onErrorResponse(VolleyError error) {
String err = (error.toString() == null) ? "Generic network error" : error.toString();
error.printStackTrace();

AlertDialog alertDialog = new AlertDialog.Builder(ContactSelect.this).create();
alertDialog.setTitle("Couldn't connect to endpoint");
alertDialog.setMessage("The server didn't respond."+"\n" + err.toString());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
UITools.showAlertDialogSafe(ContactSelect.this,R.id.activity_contact_select,"Couldn't connect to endpoint","The server didn't respond."+"\n" + err.toString());

}
});
}
Expand Down Expand Up @@ -454,7 +432,6 @@ public String getDialogName() {
return conversation.getString("display_name");
}catch (JSONException e) {
FirebaseCrash.log("Nameless chat error");
FirebaseCrash.report(e);
return "Nameless chat error";
}
}
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/eu/aero2x/andromedab/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ public void onBackPressed() {
@Override
public void onResume() {
super.onResume();

//Get our messages here. This is a very good place to reload messages since we get called at the end of onCreate
//but we also get called when we come back from sleep or backgrounding so we ought to refresh anyways
//messagesListAdapter.clear();
//setupMessages();

if (socketClient != null && socketClient.socketThread.isCancelled() == false) {
socketClient.socketThread.cancel(false);
}
Expand Down Expand Up @@ -254,17 +260,16 @@ public void run() {
}

private void setupMessages() {

//Now let's nab our messages. Setup data storage. We have to set this up ASAP because if we wait someone (will and has) tried to send messages before anything else loaded
messageDataStore = new ArrayList<>();
RemoteMessagesInterface.getMessagesForConversation(Integer.valueOf(hash), this, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//We have our json messages, now we just need to parse it
try {
JSONArray messageBundle = new JSONArray(response);


//Now let's nab our messages. Setup data storage
messageDataStore = new ArrayList<>();
//Stuff our messages
for (int i = 0; i != messageBundle.length(); i++) {
messageDataStore.addAll(parseMessageBundle(messageBundle.getJSONObject(i))); //Build our message list
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public void onBind(Message message) {
if (message.isSent()) {
if (message.isDelivered()) {
if (message.isRead()) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
try {
Date parsed = sdf.parse(message.getTimeRead());
//This date bundle is in cocoa time so we need to convert it
long epochTimeRead = (Integer.valueOf(message.getTimeRead())+ 978307200L)*1000;

//We are read
messageStatus.setText("Read at " + new SimpleDateFormat("h:mm a").format(parsed));
} catch (ParseException e) {
messageStatus.setText("Read at " + new SimpleDateFormat("h:mm a").format(new Date(epochTimeRead)));
} catch (NumberFormatException e) {
e.printStackTrace();
}

Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/eu/aero2x/andromedab/UITools.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package eu.aero2x.andromedab;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.view.View;

Expand Down Expand Up @@ -38,4 +43,35 @@ public static void showSnackBar(View view,String message,int duration) {
final Snackbar snackBar = Snackbar.make(view, message, duration);
snackBar.show();
}

/**
* Showing an alert dialog with info from onCreate is needed however this can sometimes create a race condition which crashes the app if we get a response from the server before the activity is ready
* https://stackoverflow.com/a/4713487/1166266
* @param context The activity context
* @param title The title for the alert
* @param message The alert message
*/
public static void showAlertDialogSafe(final Context context, final int viewToWaitForID, final String title, final String message) {
//Supposedly you can create an activity reference from context
final Activity activity = (Activity) context;
View targetView = activity.findViewById(viewToWaitForID);
//Check if we have a valid target and the activity isn't being destroyed under us.
if (targetView != null && activity.isFinishing() == false) {

targetView.post(new Runnable() {
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
}
}
}

0 comments on commit 7a07f42

Please sign in to comment.