Skip to content

Commit

Permalink
Fix crash reported on Google Play
Browse files Browse the repository at this point in the history
Log:

```
ava.lang.RuntimeException: Unable to resume activity {com.nononsenseapps.notepad/com.nononsenseapps.notepad.ActivityMain_}: java.lang.IllegalArgumentException: account must not be null
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalArgumentException: account must not be null
at android.content.ContentResolver.isSyncActive(ContentResolver.java:2195)
at com.nononsenseapps.helpers.SyncStatusMonitor.startMonitoring(SyncStatusMonitor.java:44)
at com.nononsenseapps.notepad.ActivityMain.onResume(ActivityMain.java:487)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
at android.app.Activity.performResume(Activity.java:6019)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
... 11 more
```

Signed-off-by: Jonas Kalderstam <[email protected]>
  • Loading branch information
spacecowboy committed Nov 27, 2014
1 parent 6feaba8 commit 0ff7421
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 133 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ repositories {
// Version number
def versionMajor = 5 // Major UI overhauls
def versionMinor = 5 // Some new functionality
def versionPatch = 3 // Bug fixes
def versionBuild = 1 // Bump for dogfood builds, public betas, etc.
def versionPatch = 4 // Bug fixes
def versionBuild = 0 // Bump for dogfood builds, public betas, etc.

// Version name from git
def getVersionName = { ->
Expand Down
203 changes: 100 additions & 103 deletions app/src/main/java/com/nononsenseapps/helpers/SyncStatusMonitor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nononsenseapps.helpers;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.BroadcastReceiver;
Expand All @@ -17,118 +18,114 @@
import com.nononsenseapps.notepad.sync.SyncAdapter;

public class SyncStatusMonitor extends BroadcastReceiver {
public static interface OnSyncStartStopListener {
/**
* This is always called on the activity's UI thread.
*/
public void onSyncStartStop(final boolean ongoing);
}
private static final String TAG = "SyncStatusMonitor";
Activity activity;

Activity activity;
/**
* Call this in the activity's onResume
*/
public void startMonitoring(final Activity activity) {
this.activity = activity;

/**
* Call this in the activity's onResume
*/
public void startMonitoring(final Activity activity) {
this.activity = activity;
activity.registerReceiver(this, new IntentFilter(
SyncAdapter.SYNC_FINISHED));
activity.registerReceiver(this, new IntentFilter(
SyncAdapter.SYNC_STARTED));

activity.registerReceiver(this, new IntentFilter(
SyncAdapter.SYNC_FINISHED));
activity.registerReceiver(this, new IntentFilter(
SyncAdapter.SYNC_STARTED));

String accountName = PreferenceManager.getDefaultSharedPreferences(
activity).getString(SyncPrefs.KEY_ACCOUNT, "");
// Sync state might have changed, make sure we're spinning when
// we should
if (accountName != null
&& !accountName.isEmpty()
&& ContentResolver.isSyncActive(SyncPrefs.getAccount(
AccountManager.get(activity), accountName),
MyContentProvider.AUTHORITY)) {
try {
((OnSyncStartStopListener) activity).onSyncStartStop(true);
}
catch (Exception e) {
}
}
else {
try {
((OnSyncStartStopListener) activity).onSyncStartStop(false);
}
catch (Exception e) {
}
}
}
final String accountName = PreferenceManager.getDefaultSharedPreferences(
activity).getString(SyncPrefs.KEY_ACCOUNT, "");
Account account = null;
if (accountName != null && !accountName.isEmpty()) {
account = SyncPrefs.getAccount(AccountManager.get(activity), accountName);
}
// Sync state might have changed, make sure we're spinning when
// we should
try {
if (account != null
&& ContentResolver.isSyncActive(account,
MyContentProvider.AUTHORITY)) {
((OnSyncStartStopListener) activity).onSyncStartStop(true);
} else {
((OnSyncStartStopListener) activity).onSyncStartStop(false);
}
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
}

/**
* Call this in the activity's onPause
*/
public void stopMonitoring() {
try {
activity.unregisterReceiver(this);
}
catch (Exception e) {

}
try {
((OnSyncStartStopListener) activity).onSyncStartStop(false);
}
catch (Exception e) {
}
}
/**
* Call this in the activity's onPause
*/
public void stopMonitoring() {
try {
activity.unregisterReceiver(this);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
try {
((OnSyncStartStopListener) activity).onSyncStartStop(false);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
}

@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(SyncAdapter.SYNC_STARTED)) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
((OnSyncStartStopListener) activity)
.onSyncStartStop(true);
}
catch (Exception e) {
}
}
});
}
else { //if (intent.getAction().equals(SyncAdapter.SYNC_FINISHED)) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
((OnSyncStartStopListener) activity)
.onSyncStartStop(false);
}
catch (Exception e) {
}
}
});
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(SyncAdapter.SYNC_STARTED)) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
((OnSyncStartStopListener) activity)
.onSyncStartStop(true);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
}
});
} else { //if (intent.getAction().equals(SyncAdapter.SYNC_FINISHED)) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
((OnSyncStartStopListener) activity)
.onSyncStartStop(false);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
}
});
Bundle b = intent.getExtras();
if (b == null) {
b = new Bundle();
}
tellUser(context, b.getInt(SyncAdapter.SYNC_RESULT,
SyncAdapter.SUCCESS));
}
}
tellUser(context, b.getInt(SyncAdapter.SYNC_RESULT,
SyncAdapter.SUCCESS));
}
}

private void tellUser(Context context, int result) {
int text = R.string.sync_failed;
switch (result) {
case SyncAdapter.ERROR:
text = R.string.sync_failed;
break;
case SyncAdapter.LOGIN_FAIL:
text = R.string.sync_login_failed;
break;
case SyncAdapter.SUCCESS:
default:
return;
}

private void tellUser(Context context, int result) {
int text = R.string.sync_failed;
switch (result) {
case SyncAdapter.ERROR:
text = R.string.sync_failed;
break;
case SyncAdapter.LOGIN_FAIL:
text = R.string.sync_login_failed;
break;
case SyncAdapter.SUCCESS:
default:
return;
}
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast.show();
}

Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast.show();
}
public static interface OnSyncStartStopListener {
/**
* This is always called on the activity's UI thread.
*/
public void onSyncStartStop(final boolean ongoing);
}
}
62 changes: 34 additions & 28 deletions app/src/main/java/com/nononsenseapps/notepad/prefs/SyncPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,22 @@ public static void setSyncInterval(Context activity,
KEY_BACKGROUND_SYNC, false);

if (accountName != null && !accountName.isEmpty()) {
if (!backgroundSync) {
// Disable periodic syncing
ContentResolver.removePeriodicSync(
getAccount(AccountManager.get(activity), accountName),
MyContentProvider.AUTHORITY, new Bundle());
} else {
// Convert from minutes to seconds
long pollFrequency = 3600;
// Set periodic syncing
ContentResolver.addPeriodicSync(
getAccount(AccountManager.get(activity), accountName),
MyContentProvider.AUTHORITY, new Bundle(),
pollFrequency);
Account account = getAccount(AccountManager.get(activity), accountName);
if (account != null) {
if (!backgroundSync) {
// Disable periodic syncing
ContentResolver.removePeriodicSync(
account,
MyContentProvider.AUTHORITY, new Bundle());
} else {
// Convert from minutes to seconds
long pollFrequency = 3600;
// Set periodic syncing
ContentResolver.addPeriodicSync(
account,
MyContentProvider.AUTHORITY, new Bundle(),
pollFrequency);
}
}
}
}
Expand Down Expand Up @@ -351,21 +354,24 @@ private void toggleSync(SharedPreferences sharedPreferences) {
String accountName = sharedPreferences.getString(KEY_ACCOUNT, "");

if (accountName != null && !accountName.isEmpty()) {
if (enabled) {
// set syncable
ContentResolver.setSyncAutomatically(
getAccount(AccountManager.get(activity), accountName),
MyContentProvider.AUTHORITY, true);
ContentResolver.setIsSyncable(
getAccount(AccountManager.get(activity), accountName),
MyContentProvider.AUTHORITY, 1);
// Also set sync frequency
setSyncInterval(activity, sharedPreferences);
} else {
// set unsyncable
// ContentResolver.setIsSyncable(
// getAccount(AccountManager.get(activity), accountName),
// MyContentProvider.AUTHORITY, 0);
Account account = getAccount(AccountManager.get(activity), accountName);
if (account != null) {
if (enabled) {
// set syncable
ContentResolver.setSyncAutomatically(
account,
MyContentProvider.AUTHORITY, true);
ContentResolver.setIsSyncable(
account,
MyContentProvider.AUTHORITY, 1);
// Also set sync frequency
setSyncInterval(activity, sharedPreferences);
} else {
// set unsyncable
// ContentResolver.setIsSyncable(
// getAccount(AccountManager.get(activity), accountName),
// MyContentProvider.AUTHORITY, 0);
}
}
} else if (enabled) {
showAccountDialog();
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@
android:autoLink="web|email|map"
android:text="http://goo.gl/Ocjjl" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nVersion 5.5.4"
android:fontFamily="sans-serif-condensed"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/changelog_5_5_4" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/changelog_strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="changelog_5_5_4">
\nFixed possible crash when Google account is removed from device.</string>
<string name="changelog_5_5_3">
\nFixed bug when multiple items were moved/deleted, syncing with SD-card could recreate them.
\nFixed bug where renaming a list or moving tasks would not update the list in side navigation bar.
Expand Down

0 comments on commit 0ff7421

Please sign in to comment.