Пример #1
0
  public void memosRemind() {
    int tSize, pSize;
    List<Timing> timingList = timingService.getTimingByUserID(sp.getString("user", null));
    List<Periodic> periodicList = periodicService.getPeriodicByUserID(sp.getString("user", null));
    List<Campus> campusList = campusService.getAllCampus();
    if (timingList != null) {
      Collections.sort(
          timingList,
          new Comparator<Timing>() {
            @Override
            public int compare(Timing lhs, Timing rhs) {
              long timingEndtime1 = timService.getSecondsFromDate(lhs.getEnd_time());
              long timingEndtime2 = timService.getSecondsFromDate(rhs.getEnd_time());
              if (timingEndtime1 <= timingEndtime2) {
                return -1;
              } else return 1;
            }
          });
    }

    if (periodicList != null) {
      Collections.sort(
          periodicList,
          new Comparator<Periodic>() {
            @Override
            public int compare(Periodic lhs, Periodic rhs) {
              long periodicEndtime1 = timService.getSecondsFromDate(lhs.getEnd_time());
              long periodicEndtime2 = timService.getSecondsFromDate(rhs.getEnd_time());
              if (periodicEndtime1 <= periodicEndtime2) {
                return -1;
              } else return 1;
            }
          });
    }
    if (timingList != null && periodicList != null) {
      tSize = timingList.size();
      pSize = periodicList.size();
    } else if (timingList != null && periodicList == null) {
      tSize = timingList.size();
      pSize = 0;
    } else if (timingList == null && periodicList != null) {
      tSize = 0;
      pSize = periodicList.size();
    } else {
      tSize = 0;
      pSize = 0;
    }
    if (0 != tSize && 0 != pSize) {
      for (int i = 1; i <= tSize; i++) {
        if (timingList.get(i - 1).getIsfinish() == 0) {
          if (timService.getSecondsFromDate(periodicList.get(0).getEnd_time())
              > timService.getSecondsFromDate(timingList.get(i - 1).getEnd_time())) {
            String content = timingList.get(i - 1).getContent();
            String userId = sp.getString("user", null);
            long endTime = timService.getSecondsFromDate(timingList.get(i - 1).getEnd_time());

            Intent timingAlarm = new Intent(getActivity(), Receiver.class);
            Bundle bund = new Bundle();
            bund.putString("class", "timing");
            bund.putString("user", userId);
            bund.putString("content", content);
            bund.putString("Id", timingList.get(i - 1).getTiming_id());
            if (timingList.get(i - 1).getLocation().isEmpty()) {
              bund.putString("location", null);
            } else {
              bund.putString("location", timingList.get(i - 1).getLocation());
            }
            timingAlarm.putExtras(bund);
            PendingIntent pendingIntent =
                PendingIntent.getBroadcast(
                    getActivity(), 0, timingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager timingManager =
                (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
            timingManager.set(AlarmManager.RTC_WAKEUP, endTime, pendingIntent);
            return;
          } else if (periodicList != null) {
            String content = periodicList.get(0).getContent();
            String userId = sp.getString("user", null);
            long endTime = timService.getSecondsFromDate(periodicList.get(0).getEnd_time());

            Intent timingAlarm = new Intent(getActivity(), Receiver.class);
            Bundle bund = new Bundle();
            bund.putString("class", "periodic");
            bund.putString("user", userId);
            bund.putString("content", content);
            bund.putString("Id", periodicList.get(0).getPeriodic_id());
            bund.putString("location", null);
            timingAlarm.putExtras(bund);
            PendingIntent pendingIntent =
                PendingIntent.getBroadcast(
                    getActivity(), 0, timingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager timingManager =
                (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
            timingManager.set(AlarmManager.RTC_WAKEUP, endTime, pendingIntent);
            return;
          }
        } else {
          Intent timingAlarm = new Intent(getActivity(), Receiver.class);
          PendingIntent pendingIntent =
              PendingIntent.getBroadcast(
                  getActivity(), 0, timingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
          AlarmManager timingManager =
              (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
          timingManager.cancel(pendingIntent);
        }
      }
    } else if (0 != pSize) {
      String content = periodicList.get(0).getContent();
      String userId = sp.getString("user", null);
      long endTime = timService.getSecondsFromDate(periodicList.get(0).getEnd_time());

      Intent timingAlarm = new Intent(getActivity(), Receiver.class);
      Bundle bund = new Bundle();
      bund.putString("user", userId);
      bund.putString("class", "periodic");
      bund.putString("content", content);
      bund.putString("Id", periodicList.get(0).getPeriodic_id());
      timingAlarm.putExtras(bund);
      PendingIntent pendingIntent =
          PendingIntent.getBroadcast(
              getActivity(), 0, timingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
      AlarmManager timingManager =
          (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
      timingManager.set(AlarmManager.RTC_WAKEUP, endTime, pendingIntent);
      return;
    } else if (0 != tSize) {
      for (int i = 0; i < tSize; i++) {
        if (timingList.get(i).getIsfinish() == 0) {
          String content = timingList.get(i).getContent();
          String userId = sp.getString("user", null);
          long endTime = timService.getSecondsFromDate(timingList.get(i).getEnd_time());

          Intent timingAlarm = new Intent(getActivity(), Receiver.class);
          Bundle bund = new Bundle();
          bund.putString("class", "timing");
          bund.putString("user", userId);
          bund.putString("content", content);
          bund.putString("Id", timingList.get(i).getTiming_id());
          if (timingList.get(i).getLocation().isEmpty()) {
            bund.putString("location", null);
          } else {
            bund.putString("location", timingList.get(i).getLocation());
          }
          timingAlarm.putExtras(bund);
          PendingIntent pendingIntent =
              PendingIntent.getBroadcast(
                  getActivity(), 0, timingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
          AlarmManager timingManager =
              (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
          timingManager.set(AlarmManager.RTC_WAKEUP, endTime, pendingIntent);
          return;
        }
      }
    }
  }
Пример #2
0
  private void initAdapert() {
    // TODO Auto-generated method stub
    // 实例化数据库服务
    timingService = new TimingService(memoDb);
    realTimeService = new RealTimeService(memoDb);
    periodicService = new PeriodicService(memoDb);
    // 获取userId
    String userId = sp.getString("user", null);
    // 数据库中获取的List<Timing>

    List<Timing> timingList = timingService.getTimingByUserID(userId);
    List<RealTime> realList = realTimeService.getRealTimeByUserID(userId);
    List<Periodic> perioList = periodicService.getPeriodicByUserID(userId);

    mList = new ArrayList<Map<String, Object>>();

    if (timingList != null) {
      Collections.sort(
          timingList,
          new Comparator<Timing>() {
            @Override
            public int compare(Timing lhs, Timing rhs) {
              String timingStartTime1 = lhs.getStart_time();
              String timingStartTime2 = rhs.getStart_time();
              if (lhs.getPriority() < rhs.getPriority()) {
                return 1;
              } else if (lhs.getPriority() == rhs.getPriority()) {
                if (timingStartTime1.compareTo(timingStartTime2) > 0) {
                  return 1;
                } else {
                  return -1;
                }
              } else {
                return -1;
              }
            }
          });
    }

    if (perioList != null) {
      Collections.sort(
          perioList,
          new Comparator<Periodic>() {

            @Override
            public int compare(Periodic lhs, Periodic rhs) {
              String periodicStartTime1 = lhs.getStart_time();
              String periodicStartTime2 = rhs.getStart_time();
              if (lhs.getPriority() < rhs.getPriority()) {
                return 1;
              } else if (lhs.getPriority() == rhs.getPriority()) {
                if (periodicStartTime1.compareTo(periodicStartTime2) > 0) {
                  return 1;
                } else {
                  return -1;
                }
              } else {
                return -1;
              }
            }
          });
    }

    if (realList != null) {
      Collections.sort(
          realList,
          new Comparator<RealTime>() {

            @Override
            public int compare(RealTime lhs, RealTime rhs) {
              String realStartTime1 = lhs.getStart_time();
              String realStartTime2 = rhs.getStart_time();
              if (lhs.getPriority() < rhs.getPriority()) {
                return 1;
              } else if (lhs.getPriority() == rhs.getPriority()) {
                if (realStartTime1.compareTo(realStartTime2) > 0) {
                  return 1;
                } else {
                  return -1;
                }
              } else {
                return -1;
              }
            }
          });
    }

    if (realList != null) {
      // 实时提醒迭代器
      RealTime tempRealTime;
      Iterator itRealTime = realList.iterator();
      while (itRealTime.hasNext()) {
        tempRealTime = (RealTime) itRealTime.next();
        if (tempRealTime.getIsfinish() == 0) {
          Map<String, Object> mListItem = new HashMap<String, Object>();
          String temp;
          mListItem.put("textListCategory", "实时提醒");
          mListItem.put("ratingBarListItem", (float) tempRealTime.getPriority());
          if (tempRealTime.getContent().length() <= 10) {
            temp = tempRealTime.getContent();
          } else {
            temp = tempRealTime.getContent().substring(0, 10) + "……";
          }
          mListItem.put("textListContent", temp);
          mListItem.put("textIndex", tempRealTime.getReal_id());
          mList.add(mListItem);
        }
      }
    }

    if (timingList != null) {
      Timing tempTiming;
      // 定时提醒迭代器
      Iterator itTiming = timingList.iterator();
      while (itTiming.hasNext()) {
        tempTiming = (Timing) itTiming.next();
        if (tempTiming.getIsfinish() == 0) {
          Map<String, Object> mListItem = new HashMap<String, Object>();
          String temp;
          mListItem.put("textListCategory", "定时提醒");
          mListItem.put("ratingBarListItem", (float) tempTiming.getPriority());
          if (tempTiming.getContent().length() <= 10) {
            temp = tempTiming.getContent();
          } else {
            temp = tempTiming.getContent().substring(0, 10) + "……";
          }
          mListItem.put("textListContent", temp);
          mListItem.put("textIndex", tempTiming.getTiming_id());

          mList.add(mListItem);
        }
      }
    }

    if (perioList != null) {
      Periodic tempPreiodic;
      // 周期性提醒迭代器
      Iterator itPeriodic = perioList.iterator();
      while (itPeriodic.hasNext()) {
        tempPreiodic = (Periodic) itPeriodic.next();
        Map<String, Object> mListItem = new HashMap<String, Object>();
        String temp;
        mListItem.put("textListCategory", "周期性提醒");
        mListItem.put("ratingBarListItem", (float) tempPreiodic.getPriority());
        if (tempPreiodic.getContent().length() <= 10) {
          temp = tempPreiodic.getContent();
        } else {
          temp = tempPreiodic.getContent().substring(0, 10) + "……";
        }
        mListItem.put("textListContent", temp);
        mListItem.put("textIndex", tempPreiodic.getPeriodic_id());
        mList.add(mListItem);
      }
    }
    mAdapter = new SimpleAdapter(getActivity(), mList, R.layout.listview_layout, from, to);

    // 重写Adapter支持RatingBar
    mAdapter.setViewBinder(
        new ViewBinder() {
          @Override
          public boolean setViewValue(View view, Object data, String textRepresentation) {
            // TODO Auto-generated method stub

            if (view.getId() == R.id.textListCategory) {
              category = (String) data;
              if (category.equals("定时提醒")) {
                color = getResources().getColor(R.color.red);
                drawable = getResources().getDrawable(R.drawable.alarm_icon_getup);
              } else if (category.equals("实时提醒")) {
                color = getResources().getColor(R.color.red);
                drawable = getResources().getDrawable(R.drawable.alarm_icon_normal);
              } else if (category.equals("周期性提醒")) {
                color = getResources().getColor(R.color.red);
                drawable = getResources().getDrawable(R.drawable.alarm_icon_monthly);
              }
              TextView textListCategory = (TextView) view;
              textListCategory.setTextColor(color);
              textListCategory.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            }
            if (view.getId() == R.id.ratingBarListItem) {
              Float value = (Float) data;
              float ratingValue = value.floatValue();
              RatingBar ratingBar = (RatingBar) view;
              ratingBar.setRating(ratingValue);
              return true;
            } else return false;
          }
        });
    memosRemind();
  }