@Override protected Response doInBackground(Void... params) { // we need this value in onPostExecute, so get it already here mAction = TraktAction.values()[mArgs.getInt(InitBundle.TRAKTACTION)]; // check for network connection if (!AndroidUtils.isNetworkConnected(mContext)) { Response r = new Response(); r.status = TraktStatus.FAILURE; r.error = mContext.getString(R.string.offline); return r; } // check for valid credentials if (!ServiceUtils.isTraktCredentialsValid(mContext)) { // return null so a credentials dialog is displayed // it will call us again with valid credentials return null; } // get an authenticated trakt-java ServiceManager ServiceManager manager = ServiceUtils.getTraktServiceManagerWithAuth(mContext, false); if (manager == null) { // password could not be decrypted Response r = new Response(); r.status = TraktStatus.FAILURE; r.error = mContext.getString(R.string.trakt_generalerror); return r; } // get values used by all actions final int showTvdbId = mArgs.getInt(InitBundle.SHOW_TVDBID); final int season = mArgs.getInt(InitBundle.SEASON); final int episode = mArgs.getInt(InitBundle.EPISODE); // last chance to abort if (isCancelled()) { return null; } try { Response r = null; switch (mAction) { case CHECKIN_EPISODE: { final String message = mArgs.getString(InitBundle.MESSAGE); final CheckinBuilder checkinBuilder = manager.showService().checkin(showTvdbId).season(season).episode(episode); if (!TextUtils.isEmpty(message)) { checkinBuilder.message(message); } r = checkinBuilder.fire(); if (TraktStatus.SUCCESS.equals(r.status)) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); r.message = mContext.getString( R.string.checkin_success_trakt, (r.show != null ? r.show.title + " " : "") + Utils.getEpisodeNumber(prefs, season, episode)); } break; } case CHECKIN_MOVIE: { final String imdbId = mArgs.getString(InitBundle.IMDB_ID); final String message = mArgs.getString(InitBundle.MESSAGE); final MovieService.CheckinBuilder checkinBuilder = manager.movieService().checkin(imdbId); if (!TextUtils.isEmpty(message)) { checkinBuilder.message(message); } r = checkinBuilder.fire(); if (TraktStatus.SUCCESS.equals(r.status)) { r.message = mContext.getString( R.string.checkin_success_trakt, (r.movie != null ? r.movie.title + " " : "")); } break; } case RATE_EPISODE: { final Rating rating = Rating.fromValue(mArgs.getString(InitBundle.RATING)); r = manager .rateService() .episode(showTvdbId) .season(season) .episode(episode) .rating(rating) .fire(); break; } case RATE_SHOW: { final Rating rating = Rating.fromValue(mArgs.getString(InitBundle.RATING)); r = manager.rateService().show(showTvdbId).rating(rating).fire(); break; } case SHOUT: { final String shout = mArgs.getString(InitBundle.MESSAGE); final boolean isSpoiler = mArgs.getBoolean(InitBundle.ISSPOILER); if (episode == 0) { r = manager .commentService() .show(showTvdbId) .comment(shout) .spoiler(isSpoiler) .fire(); } else { r = manager .commentService() .episode(showTvdbId) .season(season) .episode(episode) .comment(shout) .spoiler(isSpoiler) .fire(); } break; } case WATCHLIST_MOVIE: { final int tmdbId = mArgs.getInt(InitBundle.TMDB_ID); manager.movieService().watchlist().movie(tmdbId).fire(); // In case of failure this will just return an exception, so // we need to construct our own response r = new Response(); r.status = TraktStatus.SUCCESS; r.message = mContext.getString(R.string.watchlist_added); break; } case UNWATCHLIST_MOVIE: { final int tmdbId = mArgs.getInt(InitBundle.TMDB_ID); manager.movieService().unwatchlist().movie(tmdbId).fire(); // In case of failure this will just return an exception, so // we need to construct our own response r = new Response(); r.status = TraktStatus.SUCCESS; r.message = mContext.getString(R.string.watchlist_removed); break; } default: break; } return r; } catch (TraktException e) { Utils.trackExceptionAndLog(mContext, TAG, e); Response r = new Response(); r.status = TraktStatus.FAILURE; r.error = mContext.getString(R.string.trakt_generalerror); return r; } catch (ApiException e) { Utils.trackExceptionAndLog(mContext, TAG, e); Response r = new Response(); r.status = TraktStatus.FAILURE; r.error = mContext.getString(R.string.trakt_generalerror); return r; } }
private void onNotify( final SharedPreferences prefs, final Cursor upcomingEpisodes, int count, long latestAirtime) { final Context context = getApplicationContext(); CharSequence tickerText = ""; CharSequence contentTitle = ""; CharSequence contentText = ""; PendingIntent contentIntent = null; // notification sound final String ringtoneUri = NotificationSettings.getNotificationsRingtone(context); // vibration final boolean isVibrating = NotificationSettings.isNotificationVibrating(context); if (count == 1) { // notify in detail about one episode upcomingEpisodes.moveToFirst(); final String showTitle = upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE); final String airs = Utils.formatToTimeAndDay(upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS), this)[ 0]; final String network = upcomingEpisodes.getString(NotificationQuery.NETWORK); tickerText = getString(R.string.upcoming_show, showTitle); contentTitle = showTitle + " " + Utils.getEpisodeNumber( this, upcomingEpisodes.getInt(NotificationQuery.SEASON), upcomingEpisodes.getInt(NotificationQuery.NUMBER)); contentText = getString(R.string.upcoming_show_detailed, airs, network); Intent notificationIntent = new Intent(context, EpisodesActivity.class); notificationIntent.putExtra( EpisodesActivity.InitBundle.EPISODE_TVDBID, upcomingEpisodes.getInt(NotificationQuery._ID)); notificationIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime); contentIntent = PendingIntent.getActivity(context, REQUEST_CODE_SINGLE_EPISODE, notificationIntent, 0); } else if (count > 1) { // notify about multiple episodes tickerText = getString(R.string.upcoming_episodes); contentTitle = getString(R.string.upcoming_episodes_number, count); contentText = getString(R.string.upcoming_display); Intent notificationIntent = new Intent(context, UpcomingRecentActivity.class); notificationIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime); contentIntent = PendingIntent.getActivity(context, REQUEST_CODE_MULTIPLE_EPISODES, notificationIntent, 0); } final NotificationCompat.Builder nb = new NotificationCompat.Builder(context); if (AndroidUtils.isJellyBeanOrHigher()) { // JELLY BEAN and above if (count == 1) { // single episode upcomingEpisodes.moveToFirst(); final String imagePath = upcomingEpisodes.getString(NotificationQuery.POSTER); nb.setLargeIcon(ImageProvider.getInstance(context).getImage(imagePath, true)); final String episodeTitle = upcomingEpisodes.getString(NotificationQuery.TITLE); final String episodeSummary = upcomingEpisodes.getString(NotificationQuery.OVERVIEW); final SpannableStringBuilder bigText = new SpannableStringBuilder(); bigText.append(episodeTitle); bigText.setSpan(new ForegroundColorSpan(Color.WHITE), 0, bigText.length(), 0); bigText.append("\n"); bigText.append(episodeSummary); nb.setStyle( new NotificationCompat.BigTextStyle().bigText(bigText).setSummaryText(contentText)); // Action button to check in Intent checkInActionIntent = new Intent(context, QuickCheckInActivity.class); checkInActionIntent.putExtra( QuickCheckInActivity.InitBundle.EPISODE_TVDBID, upcomingEpisodes.getInt(NotificationQuery._ID)); PendingIntent checkInIntent = PendingIntent.getActivity(context, REQUEST_CODE_ACTION_CHECKIN, checkInActionIntent, 0); nb.addAction(R.drawable.ic_action_checkin, getString(R.string.checkin), checkInIntent); } else { // multiple episodes NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); // display at most the first five final int displayCount = Math.min(count, 5); for (int i = 0; i < displayCount; i++) { if (upcomingEpisodes.moveToPosition(i)) { // add show title, air time and network final SpannableStringBuilder lineText = new SpannableStringBuilder(); lineText.append(upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE)); lineText.setSpan(new ForegroundColorSpan(Color.WHITE), 0, lineText.length(), 0); lineText.append(" "); String airs = Utils.formatToTimeAndDay( upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS), this)[0]; String network = upcomingEpisodes.getString(NotificationQuery.NETWORK); lineText.append(getString(R.string.upcoming_show_detailed, airs, network)); inboxStyle.addLine(lineText); } } // tell if we could not display all episodes if (count > 5) { inboxStyle.setSummaryText(getString(R.string.more, count - 5)); } nb.setStyle(inboxStyle); nb.setContentInfo(String.valueOf(count)); } } else { // ICS and below if (count == 1) { // single episode upcomingEpisodes.moveToFirst(); final String posterPath = upcomingEpisodes.getString(NotificationQuery.POSTER); nb.setLargeIcon(ImageProvider.getInstance(context).getImage(posterPath, true)); } } // If the string is empty, the user chose silent... if (ringtoneUri.length() != 0) { // ...otherwise set the specified ringtone nb.setSound(Uri.parse(ringtoneUri)); } if (isVibrating) { nb.setVibrate(VIBRATION_PATTERN); } nb.setDefaults(Notification.DEFAULT_LIGHTS); nb.setWhen(System.currentTimeMillis()); nb.setAutoCancel(true); nb.setTicker(tickerText); nb.setContentTitle(contentTitle); nb.setContentText(contentText); nb.setContentIntent(contentIntent); nb.setSmallIcon(R.drawable.ic_notification); nb.setPriority(NotificationCompat.PRIORITY_DEFAULT); Intent i = new Intent(this, NotificationService.class); i.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime); PendingIntent deleteIntent = PendingIntent.getService(this, 1, i, 0); nb.setDeleteIntent(deleteIntent); // build the notification Notification notification = nb.build(); // use string resource id, always unique within app final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(R.string.upcoming_show, notification); }