Skip to content

Instantly share code, notes, and snippets.

@cliffgr
Created May 18, 2017 11:06
Show Gist options
  • Save cliffgr/e1ad9d3993e1a03029d9effc5dd94910 to your computer and use it in GitHub Desktop.
Save cliffgr/e1ad9d3993e1a03029d9effc5dd94910 to your computer and use it in GitHub Desktop.
package com.velti.ka.sultana.reciever;
/**
* Created by kantoniou on 27/3/2017.
* android-AppUsageStatistics-master
*/
import android.app.AlarmManager;
import android.app.AppOpsManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PersistableBundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import com.velti.ka.sultana.MainActivity;
import com.velti.ka.sultana.app.App;
import com.velti.ka.sultana.constant.Constants;
import com.velti.ka.sultana.pref.Prefs;
import com.velti.ka.sultana.service.LollipopService;
import com.velti.ka.sultana.service.PreLollipopService;
import java.util.Calendar;
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD = 15000;
private static final String TAG = "PollReceiver";
private AlarmManager alarmManager;
@Override
public void onReceive(Context context, Intent i) {
// if ("android.intent.action.BOOT_COMPLETED".equals(i.getAction())) {
long progressStart = Prefs.with(App.getAppContext()).readLong("progressStart", 0L);
long progressEnd = Prefs.with(App.getAppContext()).readLong("progressEnd", 0L);
if (progressStart > 0L && progressEnd > 0L) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
PollReceiver.scheduleAlarms(context);
} else {
if (i.getAction().equals(Constants.startService)) {
Log.e(TAG, Constants.startService);
} else if (i.getAction().equals(Constants.stopService)) {
Log.e(TAG, Constants.stopService);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, PreLollipopService.class);
intent.setData((Uri.parse(Constants.POLLING_INTENT)));
intent.setAction(Constants.POLLING_ACTION);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 12345, intent, 0);
alarmManager.cancel(pIntent);
} else if (i.getAction().equals(Constants.makeRunService)) {
Log.e(TAG, Constants.makeRunService);
} else {
Log.e(TAG, "ELSE");
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, PreLollipopService.class);
intent.setData((Uri.parse(Constants.POLLING_INTENT)));
intent.setAction(Constants.POLLING_ACTION);
intent.putExtra("progress", System.currentTimeMillis());
intent.putExtra("progressEnd", progressEnd);
PendingIntent pIntent = PendingIntent.getService(context, 12345, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pIntent);
alarmManager.setRepeating(AlarmManager.RTC, progressStart, PERIOD, pIntent);
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
public static void scheduleAlarms(Context ctxt) {
if (hasPermission(ctxt)) {
PersistableBundle bundle = new PersistableBundle();
bundle.putLong("start", System.currentTimeMillis());
ComponentName serviceName = new ComponentName(ctxt, LollipopService.class);
JobInfo jobInfo = new JobInfo.Builder(1, serviceName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(PERIOD)
.setExtras(bundle)
.build();
JobScheduler scheduler = (JobScheduler) ctxt.getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS)
Log.d("YEAAAAA", "Job scheduled successfully!");
}
}
private static boolean hasPermission(Context ctxt) {
AppOpsManager appOps = (AppOpsManager) ctxt.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), ctxt.getPackageName());
return mode == AppOpsManager.MODE_ALLOWED;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment