Ejemplo n.º 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));
   }
 }
Ejemplo n.º 2
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();
    }
  }