introduce AbstractScannerActivity, zxing.ScannerActivity now extends it, refs #9991
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright 2017 .
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.dinect.checker;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.dinect.net.ApiClient;
|
||||
|
||||
/**
|
||||
* Created by anonymous on 04.08.17.
|
||||
*/
|
||||
|
||||
public abstract class AbstractScannerActivity extends AppCompatActivity {
|
||||
|
||||
public static final String ERROR_INFO = "ERROR_INFO";
|
||||
|
||||
static final String TAG = "C.ScannerActivity";
|
||||
|
||||
protected ApiClient apiClient;
|
||||
protected FindUserThread findUserThread;
|
||||
|
||||
LogoutDialogFragment logoutDialog;
|
||||
|
||||
boolean isCameraAvailable() {
|
||||
Log.d(TAG, "isCamerAvailable");
|
||||
PackageManager pm = getPackageManager();
|
||||
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
|
||||
}
|
||||
|
||||
void cancelRequest() {
|
||||
Log.d(TAG, "cancelRequest");
|
||||
final Intent response = new Intent();
|
||||
response.putExtra(ERROR_INFO, "Camera unavailable");
|
||||
setResult(RESULT_CANCELED, response);
|
||||
}
|
||||
|
||||
protected final boolean init(final int layoutID) {
|
||||
Log.d(TAG, "init");
|
||||
if (!isCameraAvailable()) {
|
||||
// Cancel request if there is no rear-facing camera.
|
||||
cancelRequest();
|
||||
return false;
|
||||
}
|
||||
// Hide the window title.
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(layoutID);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected final void setupToolbar(final int toolbarId, final @NonNull String title) {
|
||||
Log.d(TAG, "setupToolbar");
|
||||
final Toolbar toolbar = (Toolbar) findViewById(toolbarId);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
final ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
if (actionBar != null) {
|
||||
actionBar.setTitle(title);
|
||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected final void setupScanner(final View view, final int targetFrameId) {
|
||||
Log.d(TAG, "setupScanner");
|
||||
final FrameLayout root = (FrameLayout) findViewById(targetFrameId);
|
||||
root.addView(view, 0);
|
||||
}
|
||||
|
||||
protected final void setupApiClient(final Intent intent) {
|
||||
Log.d(TAG, "setupApiClient");
|
||||
final String url = intent.getStringExtra(MainActivity.PREF_API_URL);
|
||||
final String appToken = intent.getStringExtra(MainActivity.PREF_APP_TOKEN);
|
||||
final String token = intent.getStringExtra(MainActivity.PREF_POS_TOKEN);
|
||||
|
||||
Log.d(TAG, "initializing setupScanner activity with url "
|
||||
+ url + ", appToken " + appToken + ", token " + token);
|
||||
|
||||
apiClient = new ApiClient(url, appToken, token);
|
||||
}
|
||||
|
||||
protected final void networkCallback(final @NonNull Pair<String, String> result) {
|
||||
|
||||
if (null != result.first) {
|
||||
Log.d(TAG, "user found, finish activity with result");
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("user", result.second);
|
||||
intent.putExtra("card", result.first);
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
findUserThread.close();
|
||||
findUserThread = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.logout) {
|
||||
logoutDialog = new AbstractScannerActivity.LogoutDialogFragment();
|
||||
logoutDialog.show(getFragmentManager(), "logout");
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.faq) {
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("item", "faq");
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
void dismissDialog() {
|
||||
if (logoutDialog != null) {
|
||||
logoutDialog.dismiss();
|
||||
logoutDialog = null;
|
||||
}
|
||||
}
|
||||
|
||||
void logout() {
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("item", "logout");
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
public static class LogoutDialogFragment extends DialogFragment {
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
final LayoutInflater inflater = getActivity().getLayoutInflater();
|
||||
final View content = inflater.inflate(R.layout.f_logout_dialog, null);
|
||||
builder.setView(content);
|
||||
final View positiveButton = content.findViewById(R.id.positiveButton);
|
||||
final View negativeButton = content.findViewById(R.id.negativeButton);
|
||||
|
||||
negativeButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
((AbstractScannerActivity) getActivity()).dismissDialog();
|
||||
}
|
||||
});
|
||||
|
||||
positiveButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
((AbstractScannerActivity) getActivity()).logout();
|
||||
}
|
||||
});
|
||||
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,13 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.dinect.checker.zxing;
|
||||
package com.dinect.checker;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.dinect.checker.zxing.*;
|
||||
import com.dinect.net.ApiClient;
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,7 @@ public final class FindUserThread extends Thread {
|
||||
|
||||
private static final String TAG = "C.FindUserThread";
|
||||
|
||||
private ScannerActivity activity;
|
||||
private com.dinect.checker.zxing.ScannerActivity activity;
|
||||
private final ApiClient client;
|
||||
private final String card;
|
||||
|
||||
@@ -38,7 +39,7 @@ public final class FindUserThread extends Thread {
|
||||
* @param client ApiClient instance
|
||||
* @param card card number
|
||||
*/
|
||||
FindUserThread(final @NonNull ScannerActivity activity, final @NonNull ApiClient client, final @NonNull String card) {
|
||||
public FindUserThread(final @NonNull com.dinect.checker.zxing.ScannerActivity activity, final @NonNull ApiClient client, final @NonNull String card) {
|
||||
this.activity = activity;
|
||||
this.client = client;
|
||||
this.card = card;
|
||||
@@ -23,9 +23,9 @@ public class MainActivity extends FlutterActivity {
|
||||
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";
|
||||
public static final String PREF_API_URL = "prefs_api_token";
|
||||
public static final String PREF_APP_TOKEN = "pres_app_token";
|
||||
public static final String PREF_POS_TOKEN = "pref_pos_token";
|
||||
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";
|
||||
|
||||
private MethodChannel mChannel;
|
||||
private SharedPreferences mPreferences;
|
||||
|
||||
@@ -15,30 +15,13 @@
|
||||
*/
|
||||
package com.dinect.checker.zxing;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.dinect.checker.MainActivity;
|
||||
import com.dinect.checker.AbstractScannerActivity;
|
||||
import com.dinect.checker.FindUserThread;
|
||||
import com.dinect.checker.R;
|
||||
import com.dinect.net.ApiClient;
|
||||
import com.google.zxing.Result;
|
||||
|
||||
import me.dm7.barcodescanner.zxing.ZXingScannerView;
|
||||
@@ -46,88 +29,21 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView;
|
||||
/**
|
||||
* Created by anonymous
|
||||
*/
|
||||
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
|
||||
|
||||
public static final String ERROR_INFO = "ERROR_INFO";
|
||||
|
||||
final static String TAG = "C.ScannerActivity";
|
||||
public class ScannerActivity extends AbstractScannerActivity
|
||||
implements ZXingScannerView.ResultHandler {
|
||||
|
||||
private ZXingScannerView scannerView;
|
||||
private ApiClient apiClient;
|
||||
private FindUserThread findUserThread;
|
||||
private LogoutDialogFragment logoutDialog;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
|
||||
if (!isCameraAvailable()) {
|
||||
// Cancel request if there is no rear-facing camera.
|
||||
cancelRequest();
|
||||
if (!init(R.layout.activity_zxing_scanner)) {
|
||||
return;
|
||||
}
|
||||
final String url = getIntent().getStringExtra(MainActivity.PREF_API_URL);
|
||||
final String appToken = getIntent().getStringExtra(MainActivity.PREF_APP_TOKEN);
|
||||
final String token = getIntent().getStringExtra(MainActivity.PREF_POS_TOKEN);
|
||||
|
||||
Log.d(TAG, "initializing scanner activity with url "
|
||||
+ url + ", appToken " + appToken + ", token " + token);
|
||||
|
||||
apiClient = new ApiClient(url, appToken, token);
|
||||
scannerView = new ZXingScannerView(this);
|
||||
|
||||
// Hide the window title.
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(R.layout.activity_zxing_scanner);
|
||||
setupToolbar();
|
||||
final FrameLayout root = (FrameLayout) findViewById(R.id.zxingRoot);
|
||||
root.addView(scannerView, 0);
|
||||
}
|
||||
|
||||
private boolean isCameraAvailable() {
|
||||
PackageManager pm = getPackageManager();
|
||||
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
|
||||
}
|
||||
|
||||
private void cancelRequest() {
|
||||
final Intent response = new Intent();
|
||||
response.putExtra(ERROR_INFO, "Camera unavailable");
|
||||
setResult(RESULT_CANCELED, response);
|
||||
finish();
|
||||
}
|
||||
|
||||
private void setupToolbar() {
|
||||
final Toolbar toolbar = (Toolbar) findViewById(R.id.zxingToolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
final ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
if (actionBar != null) {
|
||||
actionBar.setTitle(getString(R.string.scanner_title));
|
||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.logout) {
|
||||
logoutDialog = new ScannerActivity.LogoutDialogFragment();
|
||||
logoutDialog.show(getFragmentManager(), "logout");
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.faq) {
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("item", "faq");
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
setupToolbar(R.id.zxingToolbar, getString(R.string.scanner_title));
|
||||
setupScanner(scannerView, R.id.zxingRoot);
|
||||
setupApiClient(getIntent());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -165,61 +81,4 @@ public class ScannerActivity extends AppCompatActivity implements ZXingScannerVi
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
protected void networkCallback(final @NonNull Pair<String, String> result) {
|
||||
|
||||
if (null != result.first) {
|
||||
Log.d(TAG, "user found, finish activity with result");
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("user", result.second);
|
||||
intent.putExtra("card", result.first);
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
findUserThread.close();
|
||||
findUserThread = null;
|
||||
}
|
||||
|
||||
void dismissDialog() {
|
||||
if (logoutDialog != null) {
|
||||
logoutDialog.dismiss();
|
||||
logoutDialog = null;
|
||||
}
|
||||
}
|
||||
|
||||
void logout() {
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra("item", "logout");
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
public static class LogoutDialogFragment extends DialogFragment {
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
final LayoutInflater inflater = getActivity().getLayoutInflater();
|
||||
final View content = inflater.inflate(R.layout.f_logout_dialog, null);
|
||||
builder.setView(content);
|
||||
final View positiveButton = content.findViewById(R.id.positiveButton);
|
||||
final View negativeButton = content.findViewById(R.id.negativeButton);
|
||||
|
||||
negativeButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
((ScannerActivity) getActivity()).dismissDialog();
|
||||
}
|
||||
});
|
||||
|
||||
positiveButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
((ScannerActivity) getActivity()).logout();
|
||||
}
|
||||
});
|
||||
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user