This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alarm): add time alarm for inventory
Signed-off-by: Rafa Hernandez <rhernandez@teclib.com>
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
app/src/main/java/org/flyve/mdm/agent/utils/TimeAlarm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (C) 2017 Teclib' | ||
* | ||
* This file is part of Flyve MDM Inventory Agent Android. | ||
* | ||
* Flyve MDM Inventory Agent Android is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Android is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 3 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* Flyve MDM Inventory Agent Android is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* ------------------------------------------------------------------------------ | ||
* @author Rafael Hernandez - rafaelje | ||
* @copyright Copyright (c) 2017 Flyve MDM | ||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html | ||
* @link https://github.com/flyve-mdm/android-inventory-agent/ | ||
* @link http://www.glpi-project.org/ | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
package org.flyve.mdm.agent.utils; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
|
||
public class TimeAlarm extends BroadcastReceiver { | ||
|
||
/** | ||
* Schedules the alarm | ||
* @param context | ||
*/ | ||
public void setAlarm(Context context) { | ||
|
||
FlyveLog.d("Set Alarm"); | ||
|
||
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); | ||
Intent i = new Intent(context, TimeAlarm.class); | ||
i.setAction("org.flyve.inventory.agent.ALARM"); | ||
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); | ||
|
||
SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(context); | ||
String timeInventory = customSharedPreference.getString("timeInventory", "Week"); | ||
|
||
int time = 60 * 1000; | ||
|
||
if (timeInventory.equals("Day")) { | ||
time = 24 * 60 * 60 * 1000; | ||
FlyveLog.d("Alarm Daily"); | ||
} else if(timeInventory.equals("Week")) { | ||
time = 7 * 24 * 60 * 60 * 1000; | ||
FlyveLog.d("Alarm Weekly"); | ||
} else if(timeInventory.equals("Month")) { | ||
time = 30 * 24 * 60 * 60 * 1000; | ||
FlyveLog.d("Alarm Monthly"); | ||
} | ||
|
||
try { | ||
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), time, pi); | ||
} catch (NullPointerException ex) { | ||
FlyveLog.e(ex.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* If the success XML is created, it sends the inventory | ||
* @param context in which the receiver is running | ||
* @param intent being received | ||
*/ | ||
@Override | ||
public void onReceive(final Context context, Intent intent) { | ||
FlyveLog.d("Launch inventory from alarm"); | ||
|
||
|
||
|
||
} | ||
|
||
/** | ||
* Removes the alarm with a matching argument | ||
* @param context | ||
*/ | ||
public void cancelAlarm(Context context) { | ||
Intent intent = new Intent(context, TimeAlarm.class); | ||
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
alarmManager.cancel(sender); | ||
} | ||
} |