1 package com.atol.drivers.examples.paycardexample;
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.os.AsyncTask;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.LinearLayout;
12 import android.widget.TextView;
20 public class PaycardExampleActivity
extends Activity {
21 private static final String PREFERENCES =
"PREFERENCES";
22 private static final String FPTR_PREF =
"FPTR_PREF";
23 private static final String PAYCARD_PREF =
"PAYCARD_PREF";
24 private static final int REQUEST_SHOW_FPTR_SETTINGS = 1;
25 private static final int REQUEST_SHOW_PAYCARD_SETTINGS = 2;
27 private SharedPreferences preferences;
28 private LinearLayout buttonsLayout;
31 protected void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
34 preferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
36 setContentView(R.layout.activity_main);
37 buttonsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
38 Button fptrSettingsButton = (Button) findViewById(R.id.fptr_settings_button);
39 Button paycardSettingsButton = (Button) findViewById(R.id.paycard_settings_button);
40 Button paymentButton = (Button) findViewById(R.id.payment_button);
41 Button reportButton = (Button) findViewById(R.id.report_button);
42 log = (TextView) findViewById(R.id.log);
44 paycardSettingsButton.setOnClickListener(
new View.OnClickListener() {
46 public void onClick(View v) {
47 Intent intent =
new Intent(PaycardExampleActivity.this,
com.
atol.
drivers.paycard.settings.SettingsActivity.class);
48 String settings = getPaycardSettings();
49 if (settings == null) {
50 settings = getPaycardDefaultSettings();
52 intent.putExtra(
com.
atol.
drivers.paycard.settings.SettingsActivity.DEVICE_SETTINGS, settings);
53 startActivityForResult(intent, REQUEST_SHOW_PAYCARD_SETTINGS);
57 fptrSettingsButton.setOnClickListener(
new View.OnClickListener() {
59 public void onClick(View v) {
60 Intent intent =
new Intent(PaycardExampleActivity.this,
com.
atol.
drivers.
fptr.settings.SettingsActivity.class);
61 String settings = getFptrSettings();
62 if (settings == null) {
63 settings = getFptrDefaultSettings();
65 intent.putExtra(
com.
atol.
drivers.
fptr.settings.SettingsActivity.DEVICE_SETTINGS, settings);
66 startActivityForResult(intent, REQUEST_SHOW_FPTR_SETTINGS);
70 paymentButton.setOnClickListener(
new View.OnClickListener() {
71 AsyncTask<Void, String, Void> task;
74 public void onClick(View v) {
76 task =
new AsyncTask<Void, String, Void>() {
78 private IPaycard paycard;
80 private void checkError(IFptr i)
throws DriverException {
81 int rc = i.get_ResultCode();
83 String rd = i.get_ResultDescription(), bpd = null;
85 bpd = i.get_BadParamDescription();
88 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
90 throw new DriverException(String.format(
"[%d] %s", rc, rd));
95 private void checkError(IPaycard i)
throws DriverException {
96 int rc = i.get_ResultCode();
98 String rd = i.get_ResultDescription(), bpd = null;
100 bpd = i.get_BadParamDescription();
103 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
105 throw new DriverException(String.format(
"[%d] %s", rc, rd));
111 protected void onPreExecute() {
112 buttonsLayout.setVisibility(View.GONE);
116 protected void onPostExecute(Void result) {
117 buttonsLayout.setVisibility(View.VISIBLE);
121 protected void onProgressUpdate(String... values) {
122 if (values == null || values.length == 0) {
129 protected Void doInBackground(Void... params) {
131 paycard =
new Paycard();
134 fptr.create(getApplication());
135 paycard.create(getApplication());
137 publishProgress(
"Загрузка настроек...");
138 if (fptr.put_DeviceSettings(getFptrSettings()) < 0) {
141 if (paycard.put_DeviceSettings(getPaycardSettings()) < 0) {
144 paycard.put_PinPadDevice(fptr.get_PinPadDevice());
145 paycard.put_ModemDevice(fptr.get_ModemDevice());
147 publishProgress(
"Установка соединения...");
148 if (fptr.put_DeviceEnabled(
true) < 0) {
151 if (paycard.put_DeviceEnabled(
true) < 0) {
154 publishProgress(
"OK");
156 publishProgress(
"Проверка связи с ККМ...");
157 if (fptr.GetStatus() < 0) {
160 publishProgress(
"OK");
162 publishProgress(
"Продажа...");
163 if (paycard.put_AuthorizationType(IPaycard.AUTHORIZATION_READER_PIN) < 0) {
166 if (paycard.put_OperationType(IPaycard.OPERATION_SUB) < 0) {
169 if (paycard.put_Sum(10.0) < 0) {
172 if (paycard.put_CharLineLength(fptr.get_CharLineLength()) < 0) {
175 if (paycard.PrepareAuthorization() < 0) {
178 if (paycard.Authorization() < 0) {
181 publishProgress(
"OK");
183 publishProgress(
"Печать слипа...");
184 if (fptr.put_Caption(paycard.get_Text()) < 0) {
187 if (fptr.put_TextWrap(IFptr.WRAP_LINE) < 0) {
190 if (fptr.put_Alignment(IFptr.ALIGNMENT_LEFT) < 0) {
193 if (fptr.PrintString() < 0) {
196 if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0) {
199 if (fptr.SetMode() < 0) {
202 if (fptr.PrintFooter() < 0) {
205 publishProgress(
"OK");
207 }
catch (Exception e) {
208 publishProgress(e.toString());
220 reportButton.setOnClickListener(
new View.OnClickListener() {
221 AsyncTask<Void, String, Void> task;
224 public void onClick(View v) {
226 task =
new AsyncTask<Void, String, Void>() {
228 private IPaycard paycard;
230 private void checkError(IFptr i)
throws DriverException {
231 int rc = i.get_ResultCode();
233 String rd = i.get_ResultDescription(), bpd = null;
235 bpd = i.get_BadParamDescription();
238 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
240 throw new DriverException(String.format(
"[%d] %s", rc, rd));
245 private void checkError(IPaycard i)
throws DriverException {
246 int rc = i.get_ResultCode();
248 String rd = i.get_ResultDescription(), bpd = null;
250 bpd = i.get_BadParamDescription();
253 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
255 throw new DriverException(String.format(
"[%d] %s", rc, rd));
261 protected void onPreExecute() {
262 buttonsLayout.setVisibility(View.GONE);
266 protected void onPostExecute(Void result) {
267 buttonsLayout.setVisibility(View.VISIBLE);
271 protected void onProgressUpdate(String... values) {
272 if (values == null || values.length == 0) {
279 protected Void doInBackground(Void... params) {
281 paycard =
new Paycard();
284 fptr.create(getApplication());
285 paycard.create(getApplication());
287 publishProgress(
"Загрузка настроек...");
288 if (fptr.put_DeviceSettings(getFptrSettings()) < 0) {
291 if (paycard.put_DeviceSettings(getPaycardSettings()) < 0) {
294 paycard.put_PinPadDevice(fptr.get_PinPadDevice());
295 paycard.put_ModemDevice(fptr.get_ModemDevice());
297 publishProgress(
"Установка соединения...");
298 if (fptr.put_DeviceEnabled(
true) < 0) {
301 if (paycard.put_DeviceEnabled(
true) < 0) {
304 publishProgress(
"OK");
306 publishProgress(
"Проверка связи с ККМ...");
307 if (fptr.GetStatus() < 0) {
310 publishProgress(
"OK");
312 publishProgress(
"Сверка итогов...");
313 if (paycard.put_ReportType(IPaycard.REPORT_CLOSE_SESSION) < 0) {
316 if (paycard.put_CharLineLength(fptr.get_CharLineLength()) < 0) {
319 if (paycard.BeginReport() < 0) {
322 if (paycard.EndReport() < 0) {
325 publishProgress(
"OK");
327 publishProgress(
"Печать слипа...");
328 if (fptr.put_Caption(paycard.get_Text()) < 0) {
331 if (fptr.put_TextWrap(IFptr.WRAP_LINE) < 0) {
334 if (fptr.put_Alignment(IFptr.ALIGNMENT_LEFT) < 0) {
337 if (fptr.PrintString() < 0) {
340 if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0) {
343 if (fptr.SetMode() < 0) {
346 if (fptr.PrintFooter() < 0) {
349 publishProgress(
"OK");
351 }
catch (Exception e) {
352 publishProgress(e.toString());
366 protected void onActivityResult(
int requestCode,
int resultCode, Intent data) {
367 switch (requestCode) {
368 case REQUEST_SHOW_FPTR_SETTINGS:
369 if (data != null && data.getExtras() != null) {
370 String settings = data.getExtras().getString(
com.
atol.
drivers.
fptr.settings.SettingsActivity.DEVICE_SETTINGS);
371 setFptrSettings(settings);
374 case REQUEST_SHOW_PAYCARD_SETTINGS:
375 if (data != null && data.getExtras() != null) {
376 String settings = data.getExtras().getString(
com.
atol.
drivers.paycard.settings.SettingsActivity.DEVICE_SETTINGS);
377 setPaycardSettings(settings);
383 private String getFptrSettings() {
384 return preferences.getString(FPTR_PREF, getFptrDefaultSettings());
387 private void setFptrSettings(String settings) {
388 SharedPreferences.Editor editor = preferences.edit();
389 editor.putString(FPTR_PREF, settings);
393 private String getFptrDefaultSettings() {
394 IFptr fprint =
new Fptr();
396 String settings = fprint.get_DeviceSettings();
401 private String getPaycardSettings() {
402 return preferences.getString(PAYCARD_PREF, getPaycardDefaultSettings());
405 private void setPaycardSettings(String settings) {
406 SharedPreferences.Editor editor = preferences.edit();
407 editor.putString(PAYCARD_PREF, settings);
411 private String getPaycardDefaultSettings() {
412 IPaycard paycard =
new Paycard();
413 paycard.create(
this);
414 String settings = paycard.get_DeviceSettings();
419 private void logAdd(String
string) {
420 String value = (String) log.getText();
421 if (value.length() > 2096) {
422 value = value.substring(0, 2096);
424 log.setText(
string +
"\n" + value);
427 private static class DriverException
extends Exception {
428 public DriverException(String msg) {