Skip to content

Commit

Permalink
Minor crash fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed Apr 14, 2015
1 parent d808882 commit f375024
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
// handle a crash that occurs where intent comes in as null
if (mState == State.Stopped) {
mHandler.removeCallbacksAndMessages(null);
stopSelf();
}
return START_NOT_STICKY;
Expand Down Expand Up @@ -709,6 +710,7 @@ private void processStopRequest(boolean force) {
giveUpAudioFocus();

// service is no longer necessary. Will be started again if needed.
mHandler.removeCallbacksAndMessages(null);
stopSelf();

// stop async task if it's running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.crashlytics.android.Crashlytics;
import com.quran.labs.androidquran.QuranDataActivity;
import com.quran.labs.androidquran.R;
import com.quran.labs.androidquran.util.QuranUtils;

import android.app.NotificationManager;
import android.app.PendingIntent;
Expand Down Expand Up @@ -79,6 +78,8 @@ public void setFileStatus(int current, int total){
private NotificationManager mNotificationManager;
private LocalBroadcastManager mBroadcastManager;
private int mNotificationColor;
private int mLastProgress;
private int mLastMaximum;

public QuranDownloadNotifier(Context context) {
mAppContext = context.getApplicationContext();
Expand All @@ -87,10 +88,14 @@ public QuranDownloadNotifier(Context context) {
mBroadcastManager = LocalBroadcastManager.getInstance(mAppContext);
mNotificationColor = mAppContext.getResources()
.getColor(R.color.notification_color);
mLastProgress = -1;
mLastMaximum = -1;
}

public void resetNotifications() {
// hide any previous errors, canceled, etc
mLastMaximum = -1;
mLastProgress = -1;
mNotificationManager.cancel(DOWNLOADING_ERROR_NOTIFICATION);
mNotificationManager.cancel(DOWNLOADING_COMPLETE_NOTIFICATION);
mNotificationManager.cancel(DOWNLOADING_PROCESSING_NOTIFICATION);
Expand Down Expand Up @@ -180,6 +185,8 @@ public Intent notifyDownloadSuccessful(NotificationDetails details){
mNotificationManager.cancel(DOWNLOADING_NOTIFICATION);
mNotificationManager.cancel(DOWNLOADING_PROCESSING_NOTIFICATION);
mNotificationManager.cancel(DOWNLOADING_ERROR_NOTIFICATION);
mLastMaximum = -1;
mLastProgress = -1;
showNotification(details.title, successString,
DOWNLOADING_COMPLETE_NOTIFICATION, false);
return broadcastDownloadSuccessful(details);
Expand Down Expand Up @@ -265,36 +272,15 @@ private void showNotification(String titleString,
.setContentText(statusString);

boolean wantProgress = maximum > 0 && maximum >= progress;

/**
* currently running into many system crashes due to the notifications.
* this mostly happens on Kitkat (seldom on any other version). This is
* an attempt to work around the problem with two "experiments" - one is
* to remove progress altogether (if we are on a oneplus) and never show
* the progress bar. the second is to never show indeterminate progress,
* and instead show it as 0% (for all other devices).
*
* current thinking is that the oneplus solution is the "safe solution"
* that should work. if we find that the second solution works, we can
* use that for Kitkat (instead of the less ideal first solution).
*
* note that the crash seems to happen whenever there is a progress bar.
* unfortunately, could not repro this on a oneplus, nor on 4.4 emulators.
*/
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Crashlytics.log(Build.MANUFACTURER + " - max: " + maximum + ", progress: " + progress);
if ("ONEPLUS".equalsIgnoreCase(Build.MANUFACTURER)) {
if (wantProgress && !isIndeterminate) {
builder.setContentText(statusString + " " +
QuranUtils.getLocalizedNumber(mAppContext, progress) + "%");
}
wantProgress = false;
} else if (wantProgress && isIndeterminate) {
progress = 0;
maximum = 100;
isIndeterminate = false;
}
if (mLastProgress == progress && mLastMaximum == maximum) {
// don't keep sending repeat notifications
return;
}
mLastProgress = progress;
mLastMaximum = maximum;

Crashlytics.log(Build.MANUFACTURER + " - max: " + maximum + ", progress: " + progress +
", isIndeterminate: " + isIndeterminate);

if (wantProgress) {
builder.setProgress(maximum, progress, isIndeterminate);
Expand Down
69 changes: 36 additions & 33 deletions app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,30 +570,30 @@ private void setUiVisibilityKitKat(boolean isVisible) {
private void setUiVisibilityListener(){
mViewPager.setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int flags) {
boolean visible =
(flags & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0;
mIsActionBarHidden = !visible;
if (visible){
mAudioStatusBar.updateSelectedItem();
}
@Override
public void onSystemUiVisibilityChange(int flags) {
boolean visible =
(flags & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0;
mIsActionBarHidden = !visible;
if (visible) {
mAudioStatusBar.updateSelectedItem();
}

// animate toolbar
mToolBarArea.animate()
.translationY(visible ? 0 : -mToolBarArea.getHeight())
.setDuration(250)
.start();
// animate toolbar
mToolBarArea.animate()
.translationY(visible ? 0 : -mToolBarArea.getHeight())
.setDuration(250)
.start();

final int margins = mAudioBarParams.bottomMargin + mAudioBarParams.topMargin;
final int margins = mAudioBarParams.bottomMargin + mAudioBarParams.topMargin;

// and statusbar
mAudioStatusBar.animate()
.translationY(visible ? 0 : mAudioStatusBar.getHeight() + margins)
.setDuration(250)
.start();
}
});
// and statusbar
mAudioStatusBar.animate()
.translationY(visible ? 0 : mAudioStatusBar.getHeight() + margins)
.setDuration(250)
.start();
}
});
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Expand Down Expand Up @@ -913,21 +913,24 @@ public boolean onPrepareOptionsMenu(Menu menu) {

MenuItem quran = menu.findItem(R.id.goto_quran);
MenuItem translation = menu.findItem(R.id.goto_translation);
if (!mShowingTranslation) {
quran.setVisible(false);
translation.setVisible(true);
} else {
quran.setVisible(true);
translation.setVisible(false);
if (quran != null && translation != null) {
if (!mShowingTranslation) {
quran.setVisible(false);
translation.setVisible(true);
} else {
quran.setVisible(true);
translation.setVisible(false);
}
}

MenuItem nightMode = menu.findItem(R.id.night_mode);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean isNightMode = prefs.getBoolean(Constants.PREF_NIGHT_MODE, false);
nightMode.setChecked(isNightMode);
nightMode.setIcon(isNightMode ?
R.drawable.ic_night_mode : R.drawable.ic_day_mode);

if (nightMode != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean isNightMode = prefs.getBoolean(Constants.PREF_NIGHT_MODE, false);
nightMode.setChecked(isNightMode);
nightMode.setIcon(isNightMode ?
R.drawable.ic_night_mode : R.drawable.ic_day_mode);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
final MenuItem item = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ else if (ady > mScrollTouchSlop) {
return interceptForDrag || interceptTap;
} catch (ArrayIndexOutOfBoundsException ae) {
return interceptTap;
} catch (NullPointerException npe) {
return interceptTap;
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/searchable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:includeInGlobalSearch="true"
android:searchSuggestAuthority="com.quran.labs.androidquran.data.QuranDataProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
Expand Down

0 comments on commit f375024

Please sign in to comment.