private double connectionType=0;
)NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (!activeNetwork.isConnectedOrConnecting()) return;
connectionType = (activeNetwork.getType()); // EDGE = 0; WiFi = 1
private long tapInterval = 5000; private byte tapTrigger = 0; private double tapAssemblyAverage = 5000;private long nextTap = 0; private long lastTap = 0;
) public synchronized void setNextTap(long nextTap) { this.lastTap=this.nextTap; this.nextTap = nextTap; if (nextTap > lastTap) { tapInterval = nextTap - lastTap; lastTap = nextTap; } if (tapInterval > TAP_INTERVAL_UPPER_THRESHOLD) tapInterval = TAP_INTERVAL_UPPER_THRESHOLD; if (tapInterval < 100) tapInterval = 100; tapAssemblyAverage = (tapAssemblyAverage * tapTrigger + tapInterval) / (++tapTrigger); }
which is called from the place where the user switches items. TAP_INTERVAL_UPPER_THRESHOLD
is a constant, in my case equal to 10,000 ms.tapTrigger
reset when updating.private long delayInterval = 500; private long finishDelay = 0; private long startDelay = 0;
) public synchronized void setStartDelay(long start) { this.startDelay = start; } public synchronized void setFinishDelay(long finish) { this.finishDelay = finish; if (finishDelay > startDelay) { delayInterval = finishDelay - startDelay; } }
Here the last delay is important, not the average value of the delays.private long freeRam = 5000000;
) freeRam = Runtime.getRuntime().freeMemory();
double[][] RW = new double[2][5];
For normalization, a series of constants and formulas are used. private static final int TAP_EQUALIZER = 1000; private static final int DELAY_EQUALIZER = 1000; private static final int RAM_EQUALIZER = 1000000; private static final int TAP_INTERVAL_UPPER_THRESHOLD = 10000; private static final int DELAY_INTERVAL_UPPER_THRESHOLD = 5000; private static final int NORMAL_TAP_INTERVAL = 9; private static final int TAP_I = 0; private static final int DELAY_I = 1; private static final int CONNECTION = 2; private static final int RAM = 3; private static final int QUEUE = 4;
private synchronized void normalization() { if (delayInterval > DELAY_INTERVAL_UPPER_THRESHOLD) delayInterval = DELAY_INTERVAL_UPPER_THRESHOLD; if (delayInterval < 0) delayInterval = 0; if (connectionType > 1 || connectionType < 0) connectionType = 0.5; connectionType = (connectionType - 2) * 2; if (freeRam < 100000 || freeRam > 10000000) freeRam = 5000000; } private void cast() { RW[0][TAP_I] = (NORMAL_TAP_INTERVAL - (tapAssemblyAverage / TAP_EQUALIZER)); RW[0][DELAY_I] = (double) (delayInterval / DELAY_EQUALIZER); RW[0][CONNECTION] = connectionType; RW[0][RAM] = (double) (freeRam / RAM_EQUALIZER); RW[0][QUEUE] = (double) (count); }
private void setInitialWeights() { RW[1][TAP_I] = 0.5; RW[1][DELAY_I] = 1; RW[1][CONNECTION] = -1; RW[1][RAM] = 0.1; RW[1][QUEUE] = -0.1; }
int SLEEP_COMPARISON_THRESHOLD = 2
, then the interval decreases, otherwise it increases. private static final int SLEEP_COMPARISON_THRESHOLD = 2; private static final int SLEEP_ADDITION_INC_STEP = 100; private static final int SLEEP_ADDITION_DEC_STEP = -500; private long sleepInterval = 500;
private int activation() { double value = 0; for (int i = 0; i < 5; i++) value += RW[0][i] * RW[1][i]; sleepInterval += ((Math.abs(count - value) > SLEEP_COMPARISON_THRESHOLD || sleepInterval > 10000)) ? SLEEP_ADDITION_DEC_STEP : SLEEP_ADDITION_INC_STEP; if (sleepInterval < 500) sleepInterval = 500; Log.d("QUEUE", "sleep: " + String.valueOf(sleepInterval)); if (value < 1) value = 1; Log.d("QUEUE", "queue: " + String.valueOf(value)); return (int) Math.ceil(value); }
public class IntellijQueue extends Thread { private static final int TAP_I = 0; private static final int DELAY_I = 1; private static final int CONNECTION = 2; private static final int RAM = 3; private static final int QUEUE = 4; private static final int TAP_EQUALIZER = 1000; private static final int DELAY_EQUALIZER = 1000; private static final int RAM_EQUALIZER = 1000000; private static final int SLEEP_COMPARISON_THRESHOLD = 2; private static final int SLEEP_ADDITION_INC_STEP = 100; private static final int SLEEP_ADDITION_DEC_STEP = -500; private static final int TAP_INTERVAL_UPPER_THRESHOLD = 10000; private static final int DELAY_INTERVAL_UPPER_THRESHOLD = 5000; private static final int NORMAL_TAP_INTERVAL = 9; public volatile int count = 3; private long finishDelay = 0; private long startDelay = 0; private long nextTap = 0; private long lastTap = 0; private long sleepInterval = 500; private double connectionType = 0; private long tapInterval = 5000; private long delayInterval = 500; private long freeRam = 5000000; private double RW[][]; private byte tapTrigger = 0; private double tapAssemblyAverage = 5000; private NetworkInfo activeNetwork; public IntellijQueue(Context context) { this.setPriority(MIN_PRIORITY); this.setDaemon(true); activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); RW = new double[2][5]; setInitialWeights(); lastTap = System.currentTimeMillis(); } private void setInitialWeights() { RW[1][TAP_I] = 0.5; RW[1][DELAY_I] = 1; RW[1][CONNECTION] = -1; RW[1][RAM] = 0.1; RW[1][QUEUE] = -0.1; } private void cast() { RW[0][TAP_I] = (NORMAL_TAP_INTERVAL - (tapAssemblyAverage / TAP_EQUALIZER)); RW[0][DELAY_I] = (double) (delayInterval / DELAY_EQUALIZER); RW[0][CONNECTION] = connectionType; RW[0][RAM] = (double) (freeRam / RAM_EQUALIZER); RW[0][QUEUE] = (double) (count); } private int activation() { double value = 0; for (int i = 0; i < 5; i++) value += RW[0][i] * RW[1][i]; sleepInterval += ((Math.abs(count - value) > SLEEP_COMPARISON_THRESHOLD || sleepInterval > 10000)) ? SLEEP_ADDITION_DEC_STEP : SLEEP_ADDITION_INC_STEP; if (sleepInterval < 500) sleepInterval = 500; Log.d("QUEUE", "sleep: " + String.valueOf(sleepInterval)); if (value < 1) value = 1; Log.d("QUEUE", "queue: " + String.valueOf(value)); return (int) Math.ceil(value); } private synchronized void updateValues() { if (!activeNetwork.isConnectedOrConnecting()) return; connectionType = (activeNetwork.getType()); // EDGE = 0; WiFi = 1 tapTrigger=0; freeRam = Runtime.getRuntime().freeMemory(); Log.d("QUEUE", "tap interval: " + String.valueOf(tapInterval)); Log.d("QUEUE", "delay interval: " + String.valueOf(delayInterval)); Log.d("QUEUE", "free RAM: " + String.valueOf(freeRam)); normalization(); cast(); } private synchronized void normalization() { if (delayInterval > DELAY_INTERVAL_UPPER_THRESHOLD) delayInterval = DELAY_INTERVAL_UPPER_THRESHOLD; if (delayInterval < 0) delayInterval = 0; if (connectionType > 1 || connectionType < 0) connectionType = 0.5; connectionType = (connectionType - 2) * 2; if (freeRam < 100000 || freeRam > 10000000) freeRam = 5000000; } @Override public void run() { try { while (true) { updateValues(); count = activation(); sleep(sleepInterval); } } catch (InterruptedException e) { } } public synchronized void setStartDelay(long start) { this.startDelay = start; Log.d("QUEUE", "S Ok."); } public synchronized void setFinishDelay(long finish) { this.finishDelay = finish; Log.d("QUEUE", "F Ok."); if (finishDelay > startDelay) { delayInterval = finishDelay - startDelay; } } public synchronized void setNextTap(long nextTap) { this.lastTap=this.nextTap; this.nextTap = nextTap; if (nextTap > lastTap) { tapInterval = nextTap - lastTap; lastTap = nextTap; } if (tapInterval > TAP_INTERVAL_UPPER_THRESHOLD) tapInterval = TAP_INTERVAL_UPPER_THRESHOLD; if (tapInterval < 100) tapInterval = 100; tapAssemblyAverage = (tapAssemblyAverage * tapTrigger + tapInterval) / (++tapTrigger); } }
Source: https://habr.com/ru/post/171173/
All Articles