Skip to content

Commit

Permalink
Stop re-initing when coming back to the app
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Jun 8, 2014
1 parent c9e814d commit 923d867
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
20 changes: 17 additions & 3 deletions app/src/main/java/im/tox/antox/activities/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package im.tox.antox.activities;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
Expand All @@ -9,6 +10,7 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand All @@ -32,8 +34,6 @@
import im.tox.antox.tox.ToxDoService;
import im.tox.antox.tox.ToxSingleton;
import im.tox.antox.utils.Constants;
import im.tox.antox.utils.DHTNodeDetails;
import im.tox.antox.utils.DhtNode;
import im.tox.antox.utils.Tuple;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -123,7 +123,8 @@ protected void onCreate(Bundle savedInstanceState) {
}

/* If the tox service isn't already running, start it */
if (!toxSingleton.toxStarted) {
if (!toxSingleton.isRunning) {
//new InitToxAsync(getApplicationContext()).execute();
toxSingleton.initTox(getApplicationContext());
}

Expand Down Expand Up @@ -302,4 +303,17 @@ public void copyToxID(View view) {
.getSystemService(context.CLIPBOARD_SERVICE);
clipboard.setText(sharedPreferences.getString("tox_id", ""));
}

private class InitToxAsync extends AsyncTask<Void, Void, Void> {

private Context ctx;

public InitToxAsync(Context ctx) { this.ctx = ctx; }

@Override
protected Void doInBackground(Void... params) {
toxSingleton.initTox(ctx);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public AntoxOnConnectionStatusCallback(Context ctx) {

@Override
public void execute(AntoxFriend friend, boolean online) {
Log.d(TAG, "Connection Status Callback");
AntoxDB db = new AntoxDB(ctx);
db.updateUserOnline(friend.getId(), online);
Intent update = new Intent(Constants.BROADCAST_ACTION);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/im/tox/antox/tox/ToxDoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void run() {
/* Praise the sun */
try {
toxSingleton.jTox.doTox();
toxSingleton.isRunning = true;
} catch (ToxException e) {
Log.d(TAG, e.getError().toString());
e.printStackTrace();
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/im/tox/antox/tox/ToxSingleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import im.tox.antox.R;
import im.tox.antox.callbacks.AntoxOnActionCallback;
import im.tox.antox.callbacks.AntoxOnConnectionStatusCallback;
import im.tox.antox.callbacks.AntoxOnFriendRequestCallback;
Expand Down Expand Up @@ -56,7 +54,6 @@ public class ToxSingleton {
public JTox jTox;
private AntoxFriendList antoxFriendList;
public CallbackHandler callbackHandler;
public boolean toxStarted = false;
public NotificationManager mNotificationManager;
public ToxDataFile dataFile;
public File qrFile;
Expand All @@ -71,6 +68,7 @@ public class ToxSingleton {
public rx.Observable activeKeyAndIsFriendSubject;
public Observable friendListAndRequestsSubject;
public Observable chatActiveAndKey;
public boolean isRunning = false;

public AntoxFriend getAntoxFriend(String key) {
return antoxFriendList.getById(key);
Expand Down Expand Up @@ -346,8 +344,6 @@ public void initTox(Context ctx) {
Intent startToxIntent = new Intent(ctx, ToxDoService.class);
startToxIntent.setAction(Constants.START_TOX);
ctx.startService(startToxIntent);

toxStarted = true;
}

public static ToxSingleton getInstance() {
Expand Down

0 comments on commit 923d867

Please sign in to comment.