Files
checker/android/app/src/main/java/com/dinect/checker/MainActivity.java

224 lines
8.7 KiB
Java

package com.dinect.checker;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import com.dinect.checker.zbar.CameraActivity;
import com.dinect.checker.zxing.ScannerActivity;
import java.util.Locale;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
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";
private static final String PREF_POS_ID = "pref_pos_id";
static final String PREF_API_URL = "prefs_api_token";
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
initLocale(this);
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 "getLocale":
result.success(BuildConfig.locale);
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;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == START_SCANNER_REQUEST_CODE && resultCode == RESULT_CANCELED) {
finish();
} else if (requestCode == START_SCANNER_REQUEST_CODE && resultCode == RESULT_OK) {
if (data != null) {
String user = data.getExtras().getString("user", null);
if (user != null) {
String card = data.getExtras().getString("card", null);
ArrayList<String> args = new ArrayList<>(2);
args.add(user);
args.add(card);
mChannel.invokeMethod("purchase", args);
} else {
String menuItem = data.getExtras().getString("item", null);
Log.d(TAG, menuItem);
if (menuItem != null) {
mChannel.invokeMethod(menuItem, null);
}
}
}
}
}
public static void initLocale(Context context) {
Resources res = context.getResources();
Configuration configuration = new Configuration(res.getConfiguration());
switch (BuildConfig.locale) {
case "en":
configuration.locale = new Locale("en");
Locale.setDefault(configuration.locale);
res.updateConfiguration(configuration, res.getDisplayMetrics());
break;
case "ru":
configuration.locale = new Locale("ru");
Locale.setDefault(configuration.locale);
res.updateConfiguration(configuration, res.getDisplayMetrics());
break;
case "ua":
configuration.locale = new Locale("ua");
Locale.setDefault(configuration.locale);
res.updateConfiguration(configuration, res.getDisplayMetrics());
break;
}
}
public void getLocale() {
}
public void handleItemClick() {
}
public void getDocID() {
}
public void removeKeys() {
}
public void startScanner() {
}
public void getInstanceID() {
}
public void saveToken() {
}
public void getToken() {
}
public void getPosID() {
}
public void saveMerchantID() {
}
public void getMerchantID() {
}
public void isOnline() {
}
}