package cn.ucvl.zhaodisplay; import android.content.*; import android.location.Location; import android.os.*; /** Runs production service control flow, with synthetic fixes and no network/GPS permission. */ final class LocationCycleChecks { static class Probe extends LocationService { int listens=0,finishes=0;boolean online=true; Probe(Context c){attachBaseContext(c);onCreate();} void notification(String s){} void bounded(){deadline=SystemClock.elapsedRealtime()+90000;} void listen(){listens++;capturing=true;captureStarted=System.currentTimeMillis();} boolean connected(){return online;} void finishWork(){finishes++;detach();} void dispose(){destroyed=true;detach();ui.removeCallbacksAndMessages(null);worker.shutdownNow();busy=false;} } static void check(boolean value,String message){if(!value)throw new AssertionError(message);} static void run(Context c,Runnable done){ SharedPreferences p=c.getSharedPreferences("location",0);long now=System.currentTimeMillis(); p.edit().putBoolean("active",true).putString("sharingMode","scheduled").putString("sessionToken","synthetic-token").putString("sessionNonce","test") .putLong("collectionStarted",now-10000).putLong("expires",now+86400000).putInt("intervalSeconds",7200).putInt("sampleIntervalSeconds",300) .putLong("nextReportAt",now+7200000).putBoolean("collectHistory",true).putString("collectionId","fake-session").apply(); Probe failure=new Probe(c);failure.restore();failure.deadline=SystemClock.elapsedRealtime()+90000; p.edit().putLong("nextReportAt",0).putLong("lastLiveAt",0).apply();DisplayApi.offline=true;failure.cycle(); check(p.getBoolean("reportPending",false),"HTTP failure preserves overdue report for retry");DisplayApi.offline=false;failure.dispose(); p.edit().putBoolean("reportPending",false).putLong("nextReportAt",now+7200000).apply(); Probe receipt=new Probe(c);receipt.restore();receipt.deadline=SystemClock.elapsedRealtime()+90000;receipt.cycle(); check(p.getLong("nextReportAt",0)==now+7200000,"receipt retry never advances configured upload cadence");receipt.dispose(); Probe service=new Probe(c);service.onStartCommand(new Intent("SAMPLE").putExtra("session","test"),0,1); check(service.listens==1&&service.capturing,"scheduled wake starts one sample"); Location fix=new Location("fixture");fix.setLatitude(0);fix.setLongitude(0);fix.setAccuracy(12);fix.setTime(System.currentTimeMillis());fix.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); service.onLocationChanged(fix);check(!service.capturing,"first fix releases listener immediately"); new Handler().postDelayed(()->{ check(service.finishes>0,"scheduled cycle completes");check(p.getBoolean("active",false),"logical sharing survives sample completion"); check(p.getLong("nextSampleAt",0)>System.currentTimeMillis(),"next sample scheduled"); check(p.getLong("nextReportAt",0)==now+7200000,"sampling does not advance upload cadence"); check(service.queue.remembered!=null&&service.queue.appended==1,"fix and history persisted once"); service.dispose(); Probe sync=new Probe(c);sync.onStartCommand(new Intent("SYNC"),0,2); new Handler().postDelayed(()->{ check(sync.listens==0,"manual sync never starts GPS");check(sync.finishes>0,"sync cycle completes"); sync.dispose(); Probe offline=new Probe(c);offline.online=false;offline.onStartCommand(new Intent("SYNC"),0,3); new Handler().postDelayed(()->{ check(offline.finishes>0&&!offline.capturing,"offline sync sleeps instead of permanent service");check(p.getBoolean("reportPending",false),"offline work is retained");offline.dispose(); Probe stop=new Probe(c);stop.online=false;stop.onStartCommand(new Intent("STOP"),0,4); new Handler().postDelayed(()->{ check(!p.getBoolean("active",true)&&p.getBoolean("stopPending",false),"offline stop prevents new sampling immediately"); check(stop.finishes>0&&!stop.capturing,"offline stop service terminates");stop.dispose(); p.edit().putBoolean("active",true).putBoolean("stopPending",false).putString("sessionNonce","new").apply(); Probe realtime=new Probe(c);realtime.active=true;realtime.realtime=true;realtime.capturing=true;realtime.captureStarted=System.currentTimeMillis();realtime.collectionStarted=now-10000; fix.setTime(System.currentTimeMillis());fix.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());realtime.onLocationChanged(fix); check(realtime.capturing,"explicit realtime retains listener"); new Handler().postDelayed(()->{realtime.dispose();p.edit().putBoolean("active",false).putBoolean("reportPending",false).apply();done.run();},150); },200); },200); },200); },250); } }