Skip to content

Commit 918a7ca

Browse files
committed
Fixed a connection issue when BaseSyncActivity restored after the system kills it. | #67
1 parent 7189239 commit 918a7ca

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.flowcrypt.email.api.email.model.AttachmentInfo;
2828
import com.flowcrypt.email.api.email.sync.GmailSynsManager;
2929
import com.flowcrypt.email.api.email.sync.SyncListener;
30+
import com.flowcrypt.email.database.dao.source.AccountDao;
31+
import com.flowcrypt.email.database.dao.source.AccountDaoSource;
3032
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource;
3133
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource;
3234
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource;
@@ -101,6 +103,8 @@ public class EmailSyncService extends Service implements SyncListener {
101103
*/
102104
private Account account;
103105

106+
private boolean isServiceStarted;
107+
104108
public EmailSyncService() {
105109
this.replyToMessengers = new HashMap<>();
106110
}
@@ -134,6 +138,7 @@ public void onCreate() {
134138
public int onStartCommand(Intent intent, int flags, int startId) {
135139
Log.d(TAG, "onStartCommand |intent =" + intent + "|flags = " + flags + "|startId = " +
136140
startId);
141+
isServiceStarted = true;
137142
if (intent != null) {
138143
account = intent.getParcelableExtra(EXTRA_KEY_GMAIL_ACCOUNT);
139144
if (account != null) {
@@ -171,6 +176,21 @@ public void onRebind(Intent intent) {
171176
@Override
172177
public IBinder onBind(Intent intent) {
173178
Log.d(TAG, "onBind:" + intent);
179+
180+
if (account == null) {
181+
AccountDao accountDao = new AccountDaoSource().getActiveAccountInformation
182+
(getApplicationContext());
183+
if (accountDao != null) {
184+
account = accountDao.getAccount();
185+
186+
if (account != null && !isServiceStarted) {
187+
EmailSyncService.startEmailSyncService(getContext(), account);
188+
}
189+
}
190+
} else {
191+
gmailSynsManager.beginSync(false);
192+
}
193+
174194
return messenger.getBinder();
175195
}
176196

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/base/BaseActivity.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.support.v4.content.Loader;
1515
import android.support.v7.app.AppCompatActivity;
1616
import android.support.v7.widget.Toolbar;
17+
import android.util.Log;
1718
import android.view.MenuItem;
1819
import android.view.View;
1920

@@ -29,11 +30,16 @@
2930
3031
*/
3132
public abstract class BaseActivity extends AppCompatActivity {
33+
protected static String TAG;
3234

3335
private Snackbar snackbar;
3436
private Toolbar toolbar;
3537
private AppBarLayout appBarLayout;
3638

39+
public BaseActivity() {
40+
TAG = getClass().getSimpleName();
41+
}
42+
3743
/**
3844
* This method can used to change "HomeAsUpEnabled" behavior.
3945
*
@@ -57,10 +63,35 @@ public abstract class BaseActivity extends AppCompatActivity {
5763
@Override
5864
public void onCreate(@Nullable Bundle savedInstanceState) {
5965
super.onCreate(savedInstanceState);
66+
Log.d(TAG, "onCreate");
6067
setContentView(getContentViewResourceId());
6168
initViews();
6269
}
6370

71+
@Override
72+
public void onStart() {
73+
super.onStart();
74+
Log.d(TAG, "onStart");
75+
}
76+
77+
@Override
78+
protected void onResume() {
79+
super.onResume();
80+
Log.d(TAG, "onResume");
81+
}
82+
83+
@Override
84+
protected void onStop() {
85+
super.onStop();
86+
Log.d(TAG, "onStop");
87+
}
88+
89+
@Override
90+
protected void onDestroy() {
91+
super.onDestroy();
92+
Log.d(TAG, "onDestroy");
93+
}
94+
6495
@Override
6596
public boolean onOptionsItemSelected(MenuItem item) {
6697
switch (item.getItemId()) {

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/base/BaseSyncActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
*/
4040

4141
public abstract class BaseSyncActivity extends BaseActivity implements ServiceConnection {
42-
private static final String TAG = BaseSyncActivity.class.getSimpleName();
4342
/**
4443
* Messenger for communicating with the service.
4544
*/
@@ -95,6 +94,7 @@ protected void onDestroy() {
9594

9695
@Override
9796
public void onServiceConnected(ComponentName name, IBinder service) {
97+
Log.d(TAG, "Activity connected to " + EmailSyncService.class.getSimpleName());
9898
syncServiceMessenger = new Messenger(service);
9999
isBound = true;
100100

@@ -103,6 +103,7 @@ public void onServiceConnected(ComponentName name, IBinder service) {
103103

104104
@Override
105105
public void onServiceDisconnected(ComponentName name) {
106+
Log.d(TAG, "Activity disconnected from " + EmailSyncService.class.getSimpleName());
106107
syncServiceMessenger = null;
107108
isBound = false;
108109
}

0 commit comments

Comments
 (0)