limit toasts rate, refs #9991
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.Intent;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
@@ -39,6 +40,8 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
import com.dinect.net.ApiClient;
|
import com.dinect.net.ApiClient;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
|
||||||
@@ -62,11 +65,12 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
public static final String ERROR_INFO = "ERROR_INFO";
|
public static final String ERROR_INFO = "ERROR_INFO";
|
||||||
|
|
||||||
|
|
||||||
|
protected AppCompatActivity ctx;
|
||||||
protected ApiClient apiClient;
|
protected ApiClient apiClient;
|
||||||
protected NetworkThread networkThread;
|
protected NetworkThread networkThread;
|
||||||
private DecrementCounterThread counterThread;
|
protected DecrementCounterThread counterThread;
|
||||||
|
protected LogoutDialogFragment logoutDialog;
|
||||||
LogoutDialogFragment logoutDialog;
|
protected NotificationThread notificationThread;
|
||||||
|
|
||||||
boolean isCameraAvailable() {
|
boolean isCameraAvailable() {
|
||||||
Log.d(TAG, "isCameraAvailable");
|
Log.d(TAG, "isCameraAvailable");
|
||||||
@@ -79,6 +83,7 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
final Intent response = new Intent();
|
final Intent response = new Intent();
|
||||||
response.putExtra(ERROR_INFO, message);
|
response.putExtra(ERROR_INFO, message);
|
||||||
setResult(RESULT_CANCELED, response);
|
setResult(RESULT_CANCELED, response);
|
||||||
|
notificationThread = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,10 +100,12 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
cancelRequest("Camera unavailable");
|
cancelRequest("Camera unavailable");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
ctx = this;
|
||||||
|
notificationThread = new NotificationThread(this);
|
||||||
|
notificationThread.start();
|
||||||
// Hide the window title.
|
// Hide the window title.
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(layoutID);
|
setContentView(layoutID);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,21 +149,22 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
toastMessage = CLICK_MESSAGES[2];
|
toastMessage = CLICK_MESSAGES[2];
|
||||||
break;
|
break;
|
||||||
case 23:
|
case 23:
|
||||||
toastMessage = CLICK_MESSAGES[3];
|
toastMessage = null;
|
||||||
counterThread.pause();
|
counterThread.pause();
|
||||||
counter.set(0);
|
Toast.makeText(ctx, CLICK_MESSAGES[3], Toast.LENGTH_SHORT).show();
|
||||||
switchScanner();
|
switchScanner();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Log.d(TAG, "toolbar clicked " + counter.get() + " times");
|
Log.d(TAG, "toolbar clicked " + counter.get() + " times");
|
||||||
|
|
||||||
if (null != toastMessage) {
|
if (null != toastMessage) {
|
||||||
Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT).show();
|
notificationThread.addMessage(toastMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class DecrementCounterThread extends Thread {
|
private class DecrementCounterThread extends Thread {
|
||||||
final long delay;
|
final long delay;
|
||||||
final AtomicInteger counter;
|
final AtomicInteger counter;
|
||||||
@@ -255,7 +263,7 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
*/
|
*/
|
||||||
public void handleBarcode(final @NonNull String card) {
|
public void handleBarcode(final @NonNull String card) {
|
||||||
Log.d(TAG, "handleBarcode");
|
Log.d(TAG, "handleBarcode");
|
||||||
Toast.makeText(this, card, Toast.LENGTH_SHORT).show();
|
notificationThread.addMessage(card);
|
||||||
networkThread = new NetworkThread(this, apiClient);
|
networkThread = new NetworkThread(this, apiClient);
|
||||||
networkThread.card(card).start();
|
networkThread.card(card).start();
|
||||||
}
|
}
|
||||||
@@ -268,11 +276,12 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
intent.putExtra("user", result.second);
|
intent.putExtra("user", result.second);
|
||||||
intent.putExtra("card", result.first);
|
intent.putExtra("card", result.first);
|
||||||
setResult(RESULT_OK, intent);
|
setResult(RESULT_OK, intent);
|
||||||
networkThread.close();
|
networkThread.cancel();
|
||||||
|
notificationThread.cancel();
|
||||||
finish();
|
finish();
|
||||||
} else {
|
} else {
|
||||||
if (null != result.second) {
|
if (null != result.second) {
|
||||||
Toast.makeText(this, result.second, Toast.LENGTH_SHORT).show();
|
notificationThread.addMessage(result.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,4 +350,54 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
|
|||||||
return builder.create();
|
return builder.create();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NotificationThread extends Thread {
|
||||||
|
|
||||||
|
private final static int MAX_NOTIFICATION_MESSAGES = 2;
|
||||||
|
|
||||||
|
private boolean run = true;
|
||||||
|
private final Queue<String> queue;
|
||||||
|
private final AbstractScannerActivity ctx;
|
||||||
|
|
||||||
|
public NotificationThread(final @NonNull AbstractScannerActivity ctx) {
|
||||||
|
this.queue = new ArrayBlockingQueue<>(MAX_NOTIFICATION_MESSAGES);
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (run) {
|
||||||
|
if (null != queue.peek()) {
|
||||||
|
Log.d(TAG, "null != queue.peek()");
|
||||||
|
final String message = queue.poll();
|
||||||
|
Log.d(TAG, "message: " + message);
|
||||||
|
if (null != message) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(300);
|
||||||
|
} catch (final InterruptedException ie) {
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMessage(final @NonNull String message) {
|
||||||
|
if (queue.size() == MAX_NOTIFICATION_MESSAGES) {
|
||||||
|
Log.d(TAG, "Discard message: " + queue.poll());
|
||||||
|
}
|
||||||
|
queue.add(message);
|
||||||
|
Log.d(TAG, "Add message: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public final class NetworkThread extends Thread {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void cancel() {
|
||||||
activity = null;
|
activity = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.dinect.checker.zxing;
|
package com.dinect.checker.zxing;
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import com.dinect.checker.AbstractScannerActivity;
|
import com.dinect.checker.AbstractScannerActivity;
|
||||||
@@ -33,6 +31,8 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView;
|
|||||||
public class ScannerActivity extends AbstractScannerActivity
|
public class ScannerActivity extends AbstractScannerActivity
|
||||||
implements ZXingScannerView.ResultHandler {
|
implements ZXingScannerView.ResultHandler {
|
||||||
|
|
||||||
|
private static final int SCAN_INTERVAL_PERIOD = 2000;
|
||||||
|
|
||||||
private ZXingScannerView scannerView;
|
private ZXingScannerView scannerView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -84,6 +84,6 @@ public class ScannerActivity extends AbstractScannerActivity
|
|||||||
public void run() {
|
public void run() {
|
||||||
scannerView.resumeCameraPreview(ScannerActivity.this);
|
scannerView.resumeCameraPreview(ScannerActivity.this);
|
||||||
}
|
}
|
||||||
}, 500);
|
}, SCAN_INTERVAL_PERIOD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user