Драйверы торгового оборудования v.9
PaycardExampleActivity.java
1 package com.atol.drivers.examples.paycardexample;
2 
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;
13 
14 import com.atol.drivers.fptr.Fptr;
15 import com.atol.drivers.fptr.IFptr;
16 import com.atol.drivers.paycard.IPaycard;
17 import com.atol.drivers.paycard.Paycard;
18 
19 
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;
26  private TextView log;
27  private SharedPreferences preferences;
28  private LinearLayout buttonsLayout;
29 
30  @Override
31  protected void onCreate(Bundle savedInstanceState) {
32  super.onCreate(savedInstanceState);
33 
34  preferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
35 
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);
43 
44  paycardSettingsButton.setOnClickListener(new View.OnClickListener() {
45  @Override
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();
51  }
52  intent.putExtra(com.atol.drivers.paycard.settings.SettingsActivity.DEVICE_SETTINGS, settings);
53  startActivityForResult(intent, REQUEST_SHOW_PAYCARD_SETTINGS);
54  }
55  });
56 
57  fptrSettingsButton.setOnClickListener(new View.OnClickListener() {
58  @Override
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();
64  }
65  intent.putExtra(com.atol.drivers.fptr.settings.SettingsActivity.DEVICE_SETTINGS, settings);
66  startActivityForResult(intent, REQUEST_SHOW_FPTR_SETTINGS);
67  }
68  });
69 
70  paymentButton.setOnClickListener(new View.OnClickListener() {
71  AsyncTask<Void, String, Void> task;
72 
73  @Override
74  public void onClick(View v) {
75  log.setText("");
76  task = new AsyncTask<Void, String, Void>() {
77  private IFptr fptr;
78  private IPaycard paycard;
79 
80  private void checkError(IFptr i) throws DriverException {
81  int rc = i.get_ResultCode();
82  if (rc < 0) {
83  String rd = i.get_ResultDescription(), bpd = null;
84  if (rc == -6) {
85  bpd = i.get_BadParamDescription();
86  }
87  if (bpd != null) {
88  throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
89  } else {
90  throw new DriverException(String.format("[%d] %s", rc, rd));
91  }
92  }
93  }
94 
95  private void checkError(IPaycard i) throws DriverException {
96  int rc = i.get_ResultCode();
97  if (rc < 0) {
98  String rd = i.get_ResultDescription(), bpd = null;
99  if (rc == -6) {
100  bpd = i.get_BadParamDescription();
101  }
102  if (bpd != null) {
103  throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
104  } else {
105  throw new DriverException(String.format("[%d] %s", rc, rd));
106  }
107  }
108  }
109 
110  @Override
111  protected void onPreExecute() {
112  buttonsLayout.setVisibility(View.GONE);
113  }
114 
115  @Override
116  protected void onPostExecute(Void result) {
117  buttonsLayout.setVisibility(View.VISIBLE);
118  }
119 
120  @Override
121  protected void onProgressUpdate(String... values) {
122  if (values == null || values.length == 0) {
123  return;
124  }
125  logAdd(values[0]);
126  }
127 
128  @Override
129  protected Void doInBackground(Void... params) {
130  fptr = new Fptr();
131  paycard = new Paycard();
132 
133  try {
134  fptr.create(getApplication());
135  paycard.create(getApplication());
136 
137  publishProgress("Загрузка настроек...");
138  if (fptr.put_DeviceSettings(getFptrSettings()) < 0) {
139  checkError(fptr);
140  }
141  if (paycard.put_DeviceSettings(getPaycardSettings()) < 0) {
142  checkError(paycard);
143  }
144  paycard.put_PinPadDevice(fptr.get_PinPadDevice());
145  paycard.put_ModemDevice(fptr.get_ModemDevice());
146 
147  publishProgress("Установка соединения...");
148  if (fptr.put_DeviceEnabled(true) < 0) {
149  checkError(fptr);
150  }
151  if (paycard.put_DeviceEnabled(true) < 0) {
152  checkError(paycard);
153  }
154  publishProgress("OK");
155 
156  publishProgress("Проверка связи с ККМ...");
157  if (fptr.GetStatus() < 0) {
158  checkError(fptr);
159  }
160  publishProgress("OK");
161 
162  publishProgress("Продажа...");
163  if (paycard.put_AuthorizationType(IPaycard.AUTHORIZATION_READER_PIN) < 0) {
164  checkError(paycard);
165  }
166  if (paycard.put_OperationType(IPaycard.OPERATION_SUB) < 0) {
167  checkError(paycard);
168  }
169  if (paycard.put_Sum(10.0) < 0) {
170  checkError(paycard);
171  }
172  if (paycard.put_CharLineLength(fptr.get_CharLineLength()) < 0) {
173  checkError(paycard);
174  }
175  if (paycard.PrepareAuthorization() < 0) {
176  checkError(paycard);
177  }
178  if (paycard.Authorization() < 0) {
179  checkError(paycard);
180  }
181  publishProgress("OK");
182 
183  publishProgress("Печать слипа...");
184  if (fptr.put_Caption(paycard.get_Text()) < 0) {
185  checkError(fptr);
186  }
187  if (fptr.put_TextWrap(IFptr.WRAP_LINE) < 0) {
188  checkError(fptr);
189  }
190  if (fptr.put_Alignment(IFptr.ALIGNMENT_LEFT) < 0) {
191  checkError(fptr);
192  }
193  if (fptr.PrintString() < 0) {
194  checkError(fptr);
195  }
196  if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0) {
197  checkError(fptr);
198  }
199  if (fptr.SetMode() < 0) {
200  checkError(fptr);
201  }
202  if (fptr.PrintFooter() < 0) {
203  checkError(fptr);
204  }
205  publishProgress("OK");
206 
207  } catch (Exception e) {
208  publishProgress(e.toString());
209  } finally {
210  fptr.destroy();
211  paycard.destroy();
212  }
213  return null;
214  }
215  };
216  task.execute();
217  }
218  });
219 
220  reportButton.setOnClickListener(new View.OnClickListener() {
221  AsyncTask<Void, String, Void> task;
222 
223  @Override
224  public void onClick(View v) {
225  log.setText("");
226  task = new AsyncTask<Void, String, Void>() {
227  private IFptr fptr;
228  private IPaycard paycard;
229 
230  private void checkError(IFptr i) throws DriverException {
231  int rc = i.get_ResultCode();
232  if (rc < 0) {
233  String rd = i.get_ResultDescription(), bpd = null;
234  if (rc == -6) {
235  bpd = i.get_BadParamDescription();
236  }
237  if (bpd != null) {
238  throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
239  } else {
240  throw new DriverException(String.format("[%d] %s", rc, rd));
241  }
242  }
243  }
244 
245  private void checkError(IPaycard i) throws DriverException {
246  int rc = i.get_ResultCode();
247  if (rc < 0) {
248  String rd = i.get_ResultDescription(), bpd = null;
249  if (rc == -6) {
250  bpd = i.get_BadParamDescription();
251  }
252  if (bpd != null) {
253  throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
254  } else {
255  throw new DriverException(String.format("[%d] %s", rc, rd));
256  }
257  }
258  }
259 
260  @Override
261  protected void onPreExecute() {
262  buttonsLayout.setVisibility(View.GONE);
263  }
264 
265  @Override
266  protected void onPostExecute(Void result) {
267  buttonsLayout.setVisibility(View.VISIBLE);
268  }
269 
270  @Override
271  protected void onProgressUpdate(String... values) {
272  if (values == null || values.length == 0) {
273  return;
274  }
275  logAdd(values[0]);
276  }
277 
278  @Override
279  protected Void doInBackground(Void... params) {
280  fptr = new Fptr();
281  paycard = new Paycard();
282 
283  try {
284  fptr.create(getApplication());
285  paycard.create(getApplication());
286 
287  publishProgress("Загрузка настроек...");
288  if (fptr.put_DeviceSettings(getFptrSettings()) < 0) {
289  checkError(fptr);
290  }
291  if (paycard.put_DeviceSettings(getPaycardSettings()) < 0) {
292  checkError(paycard);
293  }
294  paycard.put_PinPadDevice(fptr.get_PinPadDevice());
295  paycard.put_ModemDevice(fptr.get_ModemDevice());
296 
297  publishProgress("Установка соединения...");
298  if (fptr.put_DeviceEnabled(true) < 0) {
299  checkError(fptr);
300  }
301  if (paycard.put_DeviceEnabled(true) < 0) {
302  checkError(paycard);
303  }
304  publishProgress("OK");
305 
306  publishProgress("Проверка связи с ККМ...");
307  if (fptr.GetStatus() < 0) {
308  checkError(fptr);
309  }
310  publishProgress("OK");
311 
312  publishProgress("Сверка итогов...");
313  if (paycard.put_ReportType(IPaycard.REPORT_CLOSE_SESSION) < 0) {
314  checkError(paycard);
315  }
316  if (paycard.put_CharLineLength(fptr.get_CharLineLength()) < 0) {
317  checkError(paycard);
318  }
319  if (paycard.BeginReport() < 0) {
320  checkError(paycard);
321  }
322  if (paycard.EndReport() < 0) {
323  checkError(paycard);
324  }
325  publishProgress("OK");
326 
327  publishProgress("Печать слипа...");
328  if (fptr.put_Caption(paycard.get_Text()) < 0) {
329  checkError(fptr);
330  }
331  if (fptr.put_TextWrap(IFptr.WRAP_LINE) < 0) {
332  checkError(fptr);
333  }
334  if (fptr.put_Alignment(IFptr.ALIGNMENT_LEFT) < 0) {
335  checkError(fptr);
336  }
337  if (fptr.PrintString() < 0) {
338  checkError(fptr);
339  }
340  if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0) {
341  checkError(fptr);
342  }
343  if (fptr.SetMode() < 0) {
344  checkError(fptr);
345  }
346  if (fptr.PrintFooter() < 0) {
347  checkError(fptr);
348  }
349  publishProgress("OK");
350 
351  } catch (Exception e) {
352  publishProgress(e.toString());
353  } finally {
354  fptr.destroy();
355  paycard.destroy();
356  }
357  return null;
358  }
359  };
360  task.execute();
361  }
362  });
363  }
364 
365  @Override
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);
372  }
373  break;
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);
378  }
379  break;
380  }
381  }
382 
383  private String getFptrSettings() {
384  return preferences.getString(FPTR_PREF, getFptrDefaultSettings());
385  }
386 
387  private void setFptrSettings(String settings) {
388  SharedPreferences.Editor editor = preferences.edit();
389  editor.putString(FPTR_PREF, settings);
390  editor.apply();
391  }
392 
393  private String getFptrDefaultSettings() {
394  IFptr fprint = new Fptr();
395  fprint.create(this);
396  String settings = fprint.get_DeviceSettings();
397  fprint.destroy();
398  return settings;
399  }
400 
401  private String getPaycardSettings() {
402  return preferences.getString(PAYCARD_PREF, getPaycardDefaultSettings());
403  }
404 
405  private void setPaycardSettings(String settings) {
406  SharedPreferences.Editor editor = preferences.edit();
407  editor.putString(PAYCARD_PREF, settings);
408  editor.apply();
409  }
410 
411  private String getPaycardDefaultSettings() {
412  IPaycard paycard = new Paycard();
413  paycard.create(this);
414  String settings = paycard.get_DeviceSettings();
415  paycard.destroy();
416  return settings;
417  }
418 
419  private void logAdd(String string) {
420  String value = (String) log.getText();
421  if (value.length() > 2096) {
422  value = value.substring(0, 2096);
423  }
424  log.setText(string + "\n" + value);
425  }
426 
427  private static class DriverException extends Exception {
428  public DriverException(String msg) {
429  super(msg);
430  }
431  }
432 }