implement secret taps (on ScannerAcvitivty toolbar) for scanner backend change, refs #9991

This commit is contained in:
anonymouzz
2017-08-04 16:06:28 +07:00
parent d0345ec5d5
commit 10d16a36d2
2 changed files with 200 additions and 64 deletions

View File

@@ -18,7 +18,9 @@ package com.dinect.checker;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
@@ -37,19 +39,32 @@ import android.widget.Toast;
import com.dinect.net.ApiClient;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by anonymous on 04.08.17.
* Created by anonymous
*/
public abstract class AbstractScannerActivity extends AppCompatActivity {
private final static String TAG = "Checker.ScannerActivity";
private static final String[] CLICK_MESSAGES = {
"Там ничего нет.",
"Зачем ты это делаешь?",
"Перестань!",
"Ну и зачем?..",
};
private final AtomicInteger counter = new AtomicInteger(0);
public static final String SCAN_MODES = "SCAN_MODES";
public static final String ERROR_INFO = "ERROR_INFO";
static final String TAG = "C.ScannerActivity";
protected ApiClient apiClient;
protected NetworkThread networkThread;
private DecrementCounterThread counterThread;
LogoutDialogFragment logoutDialog;
@@ -59,10 +74,10 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
void cancelRequest() {
Log.d(TAG, "cancelRequest");
void cancelRequest(final @NonNull String message) {
Log.d(TAG, "cancelRequest: " + message);
final Intent response = new Intent();
response.putExtra(ERROR_INFO, "Camera unavailable");
response.putExtra(ERROR_INFO, message);
setResult(RESULT_CANCELED, response);
}
@@ -77,12 +92,13 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
Log.d(TAG, "init");
if (!isCameraAvailable()) {
// Cancel request if there is no rear-facing camera.
cancelRequest();
cancelRequest("Camera unavailable");
return false;
}
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutID);
return true;
}
@@ -100,8 +116,110 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
actionBar.setTitle(title);
actionBar.setDisplayHomeAsUpEnabled(false);
}
setupSecretClickHandler(toolbar);
}
private void setupSecretClickHandler(final @NonNull Toolbar toolbar) {
// Configure increment handler
counterThread = new DecrementCounterThread(counter, 700L);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String toastMessage = null;
switch (counter.incrementAndGet()) {
case 1:
if (false == counterThread.isRunning()) {
counterThread.start();
}
break;
case 5:
toastMessage = CLICK_MESSAGES[0];
break;
case 11:
toastMessage = CLICK_MESSAGES[1];
break;
case 17:
toastMessage = CLICK_MESSAGES[2];
break;
case 23:
toastMessage = CLICK_MESSAGES[3];
counterThread.pause();
counter.set(0);
switchScanner();
break;
}
Log.d(TAG, "toolbar clicked " + counter.get() + " times");
if (null != toastMessage) {
Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT).show();
}
}
});
}
private class DecrementCounterThread extends Thread {
final long delay;
final AtomicInteger counter;
private boolean running = false;
private int previous = 0;
public DecrementCounterThread(final AtomicInteger counter, final long delay) {
// Configure decrement handler
Log.d(TAG, "setupSecretClickHandler: DecrementCounterThread()");
this.counter = counter;
this.delay = delay;
}
@Override
public void run() {
running = true;
while (running) {
try {
Thread.sleep(delay);
if (counter.get() > 0) {
if (previous > counter.get()) {
previous = counter.decrementAndGet();
Log.d(TAG, "decrement counter, now " + counter.get());
} else {
previous = counter.get();
}
}
} catch (final IllegalArgumentException | InterruptedException e) {
Log.d(TAG, "disable counter decrease Thread", e);
}
}
}
public boolean isRunning() {
return running;
}
public void pause() {
Log.d(TAG, "pause decrementer");
running = false;
}
}
private void switchScanner() {
final SharedPreferences prefs = getSharedPreferences("MainActivity", Context.MODE_PRIVATE);
int idx = prefs.getInt(MainActivity.SCANNER_BACKEND_KEY, 0);
Log.d(TAG, "current scanner backend " + idx + ", " + MainActivity.SCANNER_BACKEND[idx].toString());
if (idx >= MainActivity.SCANNER_BACKEND.length - 1) {
idx = 0;
} else {
idx++;
}
Log.d(TAG, "switch to scanner backend " + idx + ", " + MainActivity.SCANNER_BACKEND[idx].toString());
prefs.edit().putInt(MainActivity.SCANNER_BACKEND_KEY, idx).apply();
final Intent response = new Intent();
response.putExtra("item", "restartScanner");
cancelRequest("Scanner backend changed");
finish();
}
/**
* Adds scanner view to target frame layout
*/

View File

@@ -8,6 +8,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import com.dinect.checker.zbar.CameraActivity;
import com.dinect.checker.zxing.ScannerActivity;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
@@ -15,12 +16,16 @@ import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.util.Map;
import java.util.ArrayList;
import java.lang.System;
import java.util.Set;
public class MainActivity extends FlutterActivity {
static final String TAG = "Checker.MainActivity";
private static final int START_SCANNER_REQUEST_CODE = 2017;
private static final String PREF_POS_MERCHANT_ID = "pref_pos_merchant_id";
private static final String PREF_DOC_ID = "pref_doc_id";
@@ -29,6 +34,12 @@ public class MainActivity extends FlutterActivity {
static final String PREF_APP_TOKEN = "pres_app_token";
static final String PREF_POS_TOKEN = "pref_pos_token";
static final Class[] SCANNER_BACKEND = {
ScannerActivity.class,
CameraActivity.class,
};
static final String SCANNER_BACKEND_KEY = "scanner_backend_idx";
private MethodChannel mChannel;
private SharedPreferences mPreferences;
@@ -39,66 +50,73 @@ public class MainActivity extends FlutterActivity {
mPreferences = getPreferences(Context.MODE_PRIVATE);
Log.d(TAG, "application prefs:");
for(final Map.Entry<String, ?> kv: mPreferences.getAll().entrySet()){
Log.d(TAG, " key = " + kv.getKey() + ", value = " + kv.getValue().toString());
}
mChannel = new MethodChannel(getFlutterView(), "com.dinect.checker/instance_id");
mChannel.setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "saveToken":
Map tokenArguments = call.arguments();
mPreferences.edit().putString(PREF_POS_TOKEN, (String) tokenArguments.get("token")).apply();
break;
case "getToken":
result.success(mPreferences.getString(PREF_POS_TOKEN, null));
break;
case "saveMerchantID":
Map merchantIDArguments = call.arguments();
mPreferences.edit().putString(PREF_POS_MERCHANT_ID, (String) merchantIDArguments.get("merchantID")).apply();
break;
case "getMerchantID":
result.success(mPreferences.getString(PREF_POS_MERCHANT_ID, null));
break;
case "startScanner":
Map arguments = call.arguments();
Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
cameraIntent.putExtra(PREF_API_URL, (String) arguments.get("url"));
cameraIntent.putExtra(PREF_APP_TOKEN, (String) arguments.get("appToken"));
cameraIntent.putExtra(PREF_POS_TOKEN, (String) arguments.get("token"));
startActivityForResult(cameraIntent, START_SCANNER_REQUEST_CODE);
break;
case "removeKeys":
mPreferences.edit().remove(PREF_POS_TOKEN).apply();
mPreferences.edit().remove(PREF_POS_MERCHANT_ID).apply();
mPreferences.edit().remove(PREF_DOC_ID).apply();
mPreferences.edit().remove(PREF_POS_ID).apply();
result.success(null);
break;
case "getDocID":
int docId = mPreferences.getInt(PREF_DOC_ID, 0) + 1;
mPreferences.edit().putInt(PREF_DOC_ID, docId).apply();
result.success(String.valueOf(docId));
break;
case "isOnline":
boolean online = Utils.isOnline(MainActivity.this);
if (!online) {
Toast.makeText(MainActivity.this, "Проверьте интернет соединение", Toast.LENGTH_SHORT).show();
}
result.success(online);
break;
case "getPosID":
String posId = mPreferences.getString(PREF_POS_ID, null);
if (posId == null) {
posId = String.valueOf(System.currentTimeMillis());
}
mPreferences.edit().putString(PREF_POS_ID, posId).apply();
result.success(posId);
break;
default:
result.notImplemented();
break;
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "saveToken":
Map tokenArguments = call.arguments();
mPreferences.edit().putString(PREF_POS_TOKEN, (String) tokenArguments.get("token")).apply();
break;
case "getToken":
result.success(mPreferences.getString(PREF_POS_TOKEN, null));
break;
case "saveMerchantID":
Map merchantIDArguments = call.arguments();
mPreferences.edit().putString(PREF_POS_MERCHANT_ID, (String) merchantIDArguments.get("merchantID")).apply();
break;
case "getMerchantID":
result.success(mPreferences.getString(PREF_POS_MERCHANT_ID, null));
break;
case "startScanner":
final Map arguments = call.arguments();
final int idx = mPreferences.getInt(SCANNER_BACKEND_KEY, 0);
Log.d(TAG, "use " + SCANNER_BACKEND[idx].toString() + " backend, with idx = " + idx);
Intent cameraIntent = new Intent(MainActivity.this, SCANNER_BACKEND[idx]);
cameraIntent.putExtra(PREF_API_URL, (String) arguments.get("url"));
cameraIntent.putExtra(PREF_APP_TOKEN, (String) arguments.get("appToken"));
cameraIntent.putExtra(PREF_POS_TOKEN, (String) arguments.get("token"));
startActivityForResult(cameraIntent, START_SCANNER_REQUEST_CODE);
break;
case "removeKeys":
mPreferences.edit().remove(PREF_POS_TOKEN).apply();
mPreferences.edit().remove(PREF_POS_MERCHANT_ID).apply();
mPreferences.edit().remove(PREF_DOC_ID).apply();
mPreferences.edit().remove(PREF_POS_ID).apply();
result.success(null);
break;
case "getDocID":
int docId = mPreferences.getInt(PREF_DOC_ID, 0) + 1;
mPreferences.edit().putInt(PREF_DOC_ID, docId).apply();
result.success(String.valueOf(docId));
break;
case "isOnline":
boolean online = Utils.isOnline(MainActivity.this);
if (!online) {
Toast.makeText(MainActivity.this, "Проверьте интернет соединение", Toast.LENGTH_SHORT).show();
}
result.success(online);
break;
case "getPosID":
String posId = mPreferences.getString(PREF_POS_ID, null);
if (posId == null) {
posId = String.valueOf(System.currentTimeMillis());
}
mPreferences.edit().putString(PREF_POS_ID, posId).apply();
result.success(posId);
break;
default:
result.notImplemented();
break;
}
}
}
});
}
@@ -117,7 +135,7 @@ public class MainActivity extends FlutterActivity {
mChannel.invokeMethod("purchase", args);
} else {
String menuItem = data.getExtras().getString("item", null);
Log.d("item", menuItem);
Log.d(TAG, menuItem);
if (menuItem != null) {
mChannel.invokeMethod(menuItem, null);
}