// this method is meant to be called just by AlarmReceiver class! public static void registerNextReminder(int currentVocabularyId, boolean doesTriggersNext) { Vocabulary current = Vocabularies.select(currentVocabularyId); if (current == null) { // exception occurred. return; } current.setLearned(true); Vocabularies.update(current); if (!doesTriggersNext) { return; } ReminderSettings settings = ReminderManager.getReminderSettings(); Vocabulary next = Vocabularies.next(currentVocabularyId); if (next == null) { settings.setStatus(ReminderSettings.Status.FINISHED); settings.setReminder(null); ReminderManager.applyReminderSettings(settings); return; } Reminder reminder = new Reminder(next.getId(), null, next.getVocabulary(), next.getVocabEnglishDef(), true); int todaySentWords = getTodaySentWords(); if (todaySentWords < settings.getWordsPerDay()) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MINUTE, settings.getIntervals()); reminder.setTime(calendar.getTime()); settings.setReminder(reminder); addAlarm(reminder); applyReminderSettings(settings); } else { settings.setReminder(reminder); applyReminderSettings(settings); start(false); } }
public static void start(boolean isResume) { ReminderSettings settings = getReminderSettings(); if (settings == null) { return; } if (settings.getStatus() != ReminderSettings.Status.RUNNING) { return; } Vocabulary vocabulary = Vocabularies.select(settings.getReminder().getVocabularyId()); if (vocabulary == null) { return; } Calendar calendar = Calendar.getInstance(); int today = calendar.get(Calendar.DAY_OF_WEEK); int[] timeParts = getTimeParts(settings.getStartTime()); int nextDayOffset = 0; long elapsedMinutes = getElapsedMinutes(); // returns minutes passed from midnight till now int startTime = timeParts[0] * 60 + timeParts[1]; Reminder nextReminder = settings.getReminder(); Date now = new Date(); if (nextReminder.getTime() == null) { isResume = false; } else if (nextReminder.getTime().after(now)) { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd", Locale.getDefault()); isResume = formatter.format(nextReminder.getTime()).equals(formatter.format(now)); } // in case user manually resumes reminder if (isResume && settings.getWeekdays()[today - 1] && elapsedMinutes >= startTime) { Reminder lastReminder = getLastReminder(); Vocabulary lastVocabulary = null; if (lastReminder != null) { lastVocabulary = Vocabularies.next(lastReminder.getVocabularyId()); } // if there is no reminder at all or current vocabulary isn't in another group int todaySentWords = getTodaySentWords(); if (lastVocabulary == null || todaySentWords < settings.getWordsPerDay()) { Vocabulary pivotal = lastVocabulary; if (pivotal == null) { pivotal = Vocabularies.next(0); } if (pivotal == null) { return; } ArrayList<Vocabulary> queuedVocabularies = Vocabularies.select( " Where " + Vocabularies.Id + " >= ?", new String[] {String.valueOf(pivotal.getId())}, " Limit " + String.valueOf(settings.getWordsPerDay() - todaySentWords)); for (int j = todaySentWords; j < settings.getWordsPerDay(); j++) { Vocabulary current = queuedVocabularies.get(j - todaySentWords); calendar.add(Calendar.SECOND, 1); Date time = calendar.getTime(); if (elapsedMinutes < startTime + (j * settings.getIntervals())) { time = getTime(nextDayOffset, startTime + j * settings.getIntervals()); } Reminder reminder = new Reminder( current.getId(), time, current.getVocabulary(), current.getVocabEnglishDef(), false); // we'll meet this condition for sure! if (j == settings.getWordsPerDay() - 1 || elapsedMinutes <= startTime + (j * settings.getIntervals())) { reminder.setTriggerNext(true); settings.setReminder(reminder); addAlarm(reminder); ReminderManager.applyReminderSettings(settings); return; } else { settings.setReminder(reminder); addAlarm(reminder); ReminderManager.applyReminderSettings(settings); } } } } if (!settings.getWeekdays()[today - 1] || elapsedMinutes > startTime || getTodaySentWords() >= settings.getWordsPerDay()) { for (int j = 1; j <= 7; j++) { if (settings.getWeekdays()[(today - 1 + j) % 7]) { nextDayOffset = j; break; } } } Date alarmTime = getTime(nextDayOffset, startTime); settings.getReminder().setTime(alarmTime); ReminderManager.applyReminderSettings(settings); addAlarm( new Reminder( vocabulary.getId(), alarmTime, vocabulary.getVocabulary(), vocabulary.getVocabEnglishDef(), true)); }
@Override protected void onCreated() { setContentView(R.layout.activity_vocabulary_details); layoutRoot = (LinearLayout) findViewById(R.id.layoutRoot); txtVocabulary = (TextView) findViewById(R.id.txtVocabulary); chkBookmark = (CheckBox) findViewById(R.id.chkBookmark); tabCategories = (PagerSlidingTabStrip) findViewById(R.id.tabDescriptions); pagerCategories = (ViewPager) findViewById(R.id.pagerDescItems); int vocabularyId = getIntent().getIntExtra(VOCAB_ID, 0); vocabulary = Vocabularies.select(vocabularyId); assert vocabulary != null; txtVocabulary.setText(vocabulary.getVocabulary()); chkBookmark.setChecked(vocabulary.getBookmarked()); chkBookmark.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { boolean bookmarked = chkBookmark.isChecked(); vocabulary.setBookmarked(bookmarked); if (Vocabularies.update(vocabulary) > 0) { if (bookmarked) { MySnackbar.show(layoutRoot, "Vocabulary added to bookmarks", Snackbar.LENGTH_SHORT); } else { MySnackbar.show( layoutRoot, "Vocabulary removed from bookmarks", Snackbar.LENGTH_SHORT); } } } }); sentences = Sentences.getVocabSentences(vocabularyId); notes = Notes.getNotes(vocabularyId); ArrayList<String> tabTitles = new ArrayList<>(); tabTitles.add("Definition"); tabTitles.add("Sentences"); if (notes.size() > 0) { tabTitles.add("Notes"); tabCount = 3; } descriptionPagerAdapter = new DescriptionPagerAdapter(getSupportFragmentManager(), tabTitles, vocabularyId); pagerCategories.setAdapter(descriptionPagerAdapter); // Bind the tabCategories to the ViewPager tabCategories.setViewPager(pagerCategories); pagerCategories.addOnPageChangeListener( new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { changeTabTitleColors(position); } @Override public void onPageScrollStateChanged(int state) {} }); changeTabsFont(); changeTabTitleColors(0); }