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.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.app.DialogFragment; import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@@ -37,19 +39,32 @@ import android.widget.Toast;
import com.dinect.net.ApiClient; 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 { 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 SCAN_MODES = "SCAN_MODES";
public static final String ERROR_INFO = "ERROR_INFO"; public static final String ERROR_INFO = "ERROR_INFO";
static final String TAG = "C.ScannerActivity";
protected ApiClient apiClient; protected ApiClient apiClient;
protected NetworkThread networkThread; protected NetworkThread networkThread;
private DecrementCounterThread counterThread;
LogoutDialogFragment logoutDialog; LogoutDialogFragment logoutDialog;
@@ -59,10 +74,10 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
} }
void cancelRequest() { void cancelRequest(final @NonNull String message) {
Log.d(TAG, "cancelRequest"); Log.d(TAG, "cancelRequest: " + message);
final Intent response = new Intent(); final Intent response = new Intent();
response.putExtra(ERROR_INFO, "Camera unavailable"); response.putExtra(ERROR_INFO, message);
setResult(RESULT_CANCELED, response); setResult(RESULT_CANCELED, response);
} }
@@ -77,12 +92,13 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
Log.d(TAG, "init"); Log.d(TAG, "init");
if (!isCameraAvailable()) { if (!isCameraAvailable()) {
// Cancel request if there is no rear-facing camera. // Cancel request if there is no rear-facing camera.
cancelRequest(); cancelRequest("Camera unavailable");
return false; return false;
} }
// 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;
} }
@@ -100,8 +116,110 @@ public abstract class AbstractScannerActivity extends AppCompatActivity {
actionBar.setTitle(title); actionBar.setTitle(title);
actionBar.setDisplayHomeAsUpEnabled(false); 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 * Adds scanner view to target frame layout
*/ */

View File

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