Merge pull request #1978 from nextcloud/hardenNotificationManager

harden notificationManager
This commit is contained in:
Andy Scherzinger 2018-01-18 21:24:16 +01:00 committed by GitHub
commit 12f2be740c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -121,8 +121,7 @@ public class FileDownloader extends Service
super.onCreate(); super.onCreate();
Log_OC.d(TAG, "Creating service"); Log_OC.d(TAG, "Creating service");
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
HandlerThread thread = new HandlerThread("FileDownloaderThread", HandlerThread thread = new HandlerThread("FileDownloaderThread", Process.THREAD_PRIORITY_BACKGROUND);
Process.THREAD_PRIORITY_BACKGROUND);
thread.start(); thread.start();
mServiceLooper = thread.getLooper(); mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper, this); mServiceHandler = new ServiceHandler(mServiceLooper, this);
@ -541,11 +540,16 @@ public class FileDownloader extends Service
showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, download.getAccount()); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, download.getAccount());
showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationBuilder.setContentIntent(PendingIntent.getActivity( mNotificationBuilder.setContentIntent(PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
this, (int) System.currentTimeMillis(), showDetailsIntent, 0 showDetailsIntent, 0));
));
mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotificationBuilder.build());
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
if (mNotificationManager != null) {
mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotificationBuilder.build());
}
} }
@ -561,7 +565,15 @@ public class FileDownloader extends Service
String fileName = filePath.substring(filePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1); String fileName = filePath.substring(filePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1);
String text = String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName); String text = String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName);
mNotificationBuilder.setContentText(text); mNotificationBuilder.setContentText(text);
mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotificationBuilder.build());
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
if (mNotificationManager != null) {
mNotificationManager.notify(R.string.downloader_download_in_progress_ticker,
mNotificationBuilder.build());
}
} }
mLastPercent = percent; mLastPercent = percent;
} }
@ -575,7 +587,13 @@ public class FileDownloader extends Service
*/ */
private void notifyDownloadResult(DownloadFileOperation download, private void notifyDownloadResult(DownloadFileOperation download,
RemoteOperationResult downloadResult) { RemoteOperationResult downloadResult) {
mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker); if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
if (mNotificationManager != null) {
mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
}
if (!downloadResult.isCancelled()) { if (!downloadResult.isCancelled()) {
int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker :
R.string.downloader_download_failed_ticker; R.string.downloader_download_failed_ticker;
@ -604,34 +622,28 @@ public class FileDownloader extends Service
updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND); updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
mNotificationBuilder mNotificationBuilder.setContentIntent(PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
.setContentIntent(PendingIntent.getActivity( updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT));
this, (int) System.currentTimeMillis(), updateAccountCredentials,
PendingIntent.FLAG_ONE_SHOT));
} else { } else {
// TODO put something smart in showDetailsIntent // TODO put something smart in showDetailsIntent
Intent showDetailsIntent = new Intent(); Intent showDetailsIntent = new Intent();
mNotificationBuilder mNotificationBuilder.setContentIntent(PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
.setContentIntent(PendingIntent.getActivity( showDetailsIntent, 0));
this, (int) System.currentTimeMillis(), showDetailsIntent, 0));
} }
mNotificationBuilder.setContentText( mNotificationBuilder.setContentText(ErrorMessageAdapter.getErrorCauseMessage(downloadResult,
ErrorMessageAdapter.getErrorCauseMessage(downloadResult, download, download, getResources()));
getResources())
);
mNotificationManager.notify(tickerId, mNotificationBuilder.build());
// Remove success notification if (mNotificationManager != null) {
if (downloadResult.isSuccess()) { mNotificationManager.notify(tickerId, mNotificationBuilder.build());
// Sleep 2 seconds, so show the notification before remove it
NotificationUtils.cancelWithDelay( // Remove success notification
mNotificationManager, if (downloadResult.isSuccess()) {
R.string.downloader_download_succeeded_ticker, // Sleep 2 seconds, so show the notification before remove it
2000); NotificationUtils.cancelWithDelay(mNotificationManager,
R.string.downloader_download_succeeded_ticker, 2000);
}
} }
} }
} }