nonlin-lin-chaos-order-etc-etal
2 years ago
8 changed files with 220 additions and 36 deletions
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
package org.purplei2p.i2pd; |
||||
|
||||
import android.app.job.JobParameters; |
||||
import android.app.job.JobService; |
||||
import android.os.Build; |
||||
import android.util.Log; |
||||
|
||||
import androidx.annotation.RequiresApi; |
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) |
||||
public class MyJobService extends JobService { |
||||
private static final String TAG = "i2pdJobSrvc"; |
||||
|
||||
private final DaemonWrapper.StateUpdateListener listener = new DaemonWrapper.StateUpdateListener() { |
||||
@Override |
||||
public void daemonStateUpdate(DaemonWrapper.State oldValue, DaemonWrapper.State newValue) { |
||||
if(!newValue.needsToBeAlive()) { |
||||
/*synchronized (MyJobService.this) { |
||||
MyJobService.this.notifyAll(); //wakeup
|
||||
}*/ |
||||
|
||||
removeDaemonStateChangeListener(); |
||||
jobFinished(null, false); |
||||
} |
||||
} |
||||
}; |
||||
@Override |
||||
public boolean onStartJob(JobParameters params) { |
||||
Log.d(TAG,"onStartJob entered"); |
||||
I2PDActivity.getDaemon().addStateChangeListener(listener); |
||||
|
||||
/* |
||||
"So, the system may kill the process at any time to reclaim memory, and in doing so, |
||||
it terminates the spawned thread running in the process. The solution to this problem is |
||||
typically to schedule a JobService from the BroadcastReceiver, so the system knows |
||||
that there is still active work being done in the process." |
||||
|
||||
Source for the quote: https://developer.android.com/guide/components/activities/process-lifecycle
|
||||
*/ |
||||
/* |
||||
while(I2PDActivity.getDaemon().getState().needsToBeAlive()){ |
||||
synchronized (MyJobService.this) { |
||||
try { |
||||
MyJobService.this.wait(); // occurs in main GUI thread when not commented out
|
||||
} catch (InterruptedException e) { |
||||
Log.e(TAG,"onStartJob interrupted"); |
||||
} |
||||
} |
||||
} |
||||
*/ |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean onStopJob(JobParameters params) { |
||||
Log.d(TAG,"onStopJob entered"); |
||||
removeDaemonStateChangeListener(); |
||||
return true; |
||||
} |
||||
|
||||
private void removeDaemonStateChangeListener() { |
||||
I2PDActivity.getDaemon().removeStateChangeListener(listener); |
||||
} |
||||
} |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
package org.purplei2p.i2pd.receivers; |
||||
|
||||
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.util.Log; |
||||
|
||||
import org.purplei2p.i2pd.I2PDActivity; |
||||
import org.purplei2p.i2pd.MyJobService; |
||||
|
||||
public class MakeMeAJobReceiver extends BroadcastReceiver { |
||||
private static final String TAG = "i2pdMMJR"; |
||||
public static final String MAKEMEAJOB_ACTION = "org.purplei2p.i2pd.MAKEMEAJOB_ACTION"; |
||||
private static final int JOB_ID = 0; |
||||
|
||||
@Override |
||||
public void onReceive(Context context, Intent intent) { |
||||
Log.d(TAG, "MakeMeAJobReceiver.onReceive entered: actionString=='" + intent.getAction() + "'"); |
||||
if(!MAKEMEAJOB_ACTION.equals(intent.getAction())){ |
||||
Log.d(TAG, "MakeMeAJobReceiver exiting, got unknown action"); |
||||
return; |
||||
} |
||||
scheduleJob(context); |
||||
Log.d(TAG, "MakeMeAJobReceiver: MyJobService job scheduled"); |
||||
} |
||||
|
||||
private static void scheduleJob(Context context) { |
||||
if(!I2PDActivity.isJobServiceApiAvailable()) { |
||||
Log.d(TAG, "MakeMeAJobReceiver JobService api not available, need OS API LEVEL >= 21'"); |
||||
return; |
||||
} |
||||
JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
||||
ComponentName serviceComponent = new ComponentName(context, MyJobService.class); |
||||
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceComponent); |
||||
|
||||
// a workaround for "You're trying to build a job
|
||||
// with no constraints, this is not allowed." exception
|
||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); |
||||
builder.setPersisted(true); |
||||
|
||||
// ping it every minute, otherwise on my physical device API >= 21 it stops after 10 minutes
|
||||
//builder.setPeriodic(60*1000);
|
||||
|
||||
jobScheduler.schedule(builder.build()); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue