Пример #1
0
 @Override
 public void onRequestPermissionsResult(
     int requestCode, String[] permissions, int[] grantResults) {
   Log.d(
       "RALMN",
       String.valueOf(requestCode)
           + " - "
           + Arrays.toString(permissions)
           + " "
           + Arrays.toString(grantResults));
   if (Arrays.asList(permissions).contains(Manifest.permission.READ_CALENDAR)
       && checkSelfPermission(Manifest.permission.READ_CALENDAR)
           == PackageManager.PERMISSION_GRANTED) {
     Log.v("WAKEMEUP", "Perms Read caleander allowed");
     SharedPreferences sharedPreferences = getSharedPreferences("fr.ralmn.wakemeup", MODE_PRIVATE);
     if (!sharedPreferences.contains("calendars")) {
       HashSet<String> calendarsId = new HashSet<>();
       List<AndroidCalendar> androidCalendars = CalendarHelper.getCalendars(this);
       for (AndroidCalendar androidCalendar : androidCalendars) {
         calendarsId.add(androidCalendar.getId() + "");
       }
       sharedPreferences.edit().putStringSet("calendars", calendarsId).apply();
     }
     ListView alarmList = (ListView) findViewById(R.id.alarmsListView);
     List<Alarm> alarms = CalendarHelper.calculateWeekAlarms(this); // Alarm.getAlarms(this);
     Collections.sort(alarms);
     CalendarHelper.calculateNextAlarm(this);
     alarmList.setAdapter(new AlarmArrayAdapter(this, R.layout.alarm_list_item, alarms));
   }
 }
Пример #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_list);

    ListView alarmList = (ListView) findViewById(R.id.alarmsListView);
    List<Alarm> alarms = new ArrayList<>();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        && checkSelfPermission(Manifest.permission.READ_CALENDAR)
            == PackageManager.PERMISSION_DENIED) {
      if (!shouldShowRequestPermissionRationale(Manifest.permission.READ_CALENDAR)) {
        requestPermissions(new String[] {Manifest.permission.READ_CALENDAR}, FETCH_PERMS);
      } else {
        Log.i("WAKEMEUP", "Rationale");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.perms_rational_calendar_title);
        builder.setMessage(R.string.perms_rational_calendar_message);
        builder.setPositiveButton(
            R.string.perms_rational_retry,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                requestPermissions(new String[] {Manifest.permission.READ_CALENDAR}, FETCH_PERMS);
              }
            });
        builder.setNegativeButton(
            R.string.perms_rational_exit,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                quit();
              }
            });
        builder.show().show();
        return;
      }
    }

    initSharedPreference();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
        || checkSelfPermission(Manifest.permission.READ_CALENDAR)
            == PackageManager.PERMISSION_GRANTED) {
      alarms = CalendarHelper.calculateWeekAlarms(this); // Alarm.getAlarms(this);
      Collections.sort(alarms);
      CalendarHelper.calculateNextAlarm(this);
    }

    alarmList.setAdapter(new AlarmArrayAdapter(this, R.layout.alarm_list_item, alarms));

    //        Calendar calendar = Calendar.getInstance();
    //        calendar.add(Calendar.SECOND, 10);
    //        Alarm a = new Alarm(calendar, "Test");
    //        a.defineAlarm(this);

  }
Пример #3
0
  @Override
  protected void onResume() {
    super.onResume();
    ListView alarmList = (ListView) findViewById(R.id.alarmsListView);

    List<Alarm> alarms = Alarm.getAlarms(this);
    Collections.sort(alarms);

    alarmList.setAdapter(new AlarmArrayAdapter(this, R.layout.alarm_list_item, alarms));
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
        || checkSelfPermission(Manifest.permission.READ_CALENDAR)
            == PackageManager.PERMISSION_GRANTED) CalendarHelper.calculateNextAlarm(this);
  }
Пример #4
0
  private void initSharedPreference() {

    SharedPreferences sharedPreferences = getSharedPreferences("fr.ralmn.wakemeup", MODE_PRIVATE);
    SharedPreferences defaultSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
        || checkSelfPermission(Manifest.permission.READ_CALENDAR)
            == PackageManager.PERMISSION_GRANTED) {
      if (!sharedPreferences.contains("calendars")) {
        HashSet<String> calendarsId = new HashSet<>();
        List<AndroidCalendar> androidCalendars = CalendarHelper.getCalendars(this);
        for (AndroidCalendar androidCalendar : androidCalendars) {
          calendarsId.add(androidCalendar.getId() + "");
        }
        sharedPreferences.edit().putStringSet("calendars", calendarsId).apply();
      }
    }

    if (!sharedPreferences.contains("alarmsBefore")) {
      HashSet<String> alarmsBefore = new HashSet<>();
      alarmsBefore.add("2:00");
      alarmsBefore.add("1:55");
      alarmsBefore.add("1:52");
      sharedPreferences.edit().putStringSet("alarmsBefore", alarmsBefore).apply();
    }

    if (!defaultSharedPref.contains("default_vibrate")) {
      defaultSharedPref.edit().putBoolean("default_vibrate", true).apply();
    }
    if (!defaultSharedPref.contains("default_ringtone")) {
      defaultSharedPref
          .edit()
          .putString(
              "default_ringtone",
              RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString())
          .apply();
    }
  }