My app is a QR Code scanner that when reading a QR from a device places a request to the server through a JSON.
In DeviceInfo.class
activity is where I show all the information I want regarding the scaned QRCode. I now have to create a history that shows me the last 20 scanned devices.
My difficulty is to create a history. I want to save the device IDs after scanning. In History.class
activity I have a costum adapter where I want to show the latest scanning devices.
I have no idea how to go about saving the ID's internally and when I start the application, I make a request to the server to receive the 20 ID's and then return only the designacao
field to the costum adapter each item has the updated name of each device.
PS: I do not have access to server PHP drivers
Home.class
public class Home extends ActionBarActivity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Código responsável por remover a actionBar desta actividade
//android.support.v7.app.ActionBar actionBar = getSupportActionBar();
//actionBar.hide();
}
//onClickMethod responsável por iniciar o QRScanner
//atribuído ao botão scan_bt
public void scanQR(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
//catch que chama o método showDialog quando o user nao tem o scanner
//e questiona-o se prentede fazer download
showDialog(Home.this, "No Scanner Found", "Download a scanner?", "Yes", "No").show();
}
}
public void onBtn(View v){
int id = v.getId();
switch (id){
case R.id.historyButton:
Intent intent = new Intent(this, History.class);
startActivityForResult(intent, 5);
break;
}
}
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:\" + \"com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
String[] resultado = contents.split("/");
String id = resultado[resultado.length - 1];
Toast toast = Toast.makeText(this, "ID: " + id + " Content: " + contents + " Format: " + format, Toast.LENGTH_LONG);
toast.show();
Intent myIntent = new Intent(this, DeviceInfo.class);
myIntent.putExtra("ID_DISPOSITIVO", id);
Log.d("MYLOG", "ID: " + id);
startActivity(myIntent);
}
}
}
}
DeviceInfo.class
public class DeviceInfo extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String lol = getIntent().getStringExtra("ID_DISPOSITIVO");
String url = "https://cc.streamline.pt/devices/view_json/" + lol;
setTitle("My new title");
Log.d("MYLOG", "lolID= " + lol + " e url= " + url);
sslCertificates(url);
final LinearLayout interfaceLayout = (LinearLayout) findViewById(R.id.interfaceLayout);
Button interfaceButton = (Button)findViewById(R.id.interfaceButton);
interfaceButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interfaceLayout.getVisibility() == View.VISIBLE) {
interfaceLayout.setVisibility(View.GONE);
} else {
interfaceLayout.setVisibility(View.VISIBLE);
}
}
}
);
}
//Classe responsável por "instalar" todos os certificados
//para poder passar o de insegurança da streamline
//e criação do jsonRequest e jsonObject
public void sslCertificates(String s) {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
//No need to implement.
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
//No need to implement.
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println(e);
}
final TextView deviceTextView;
final TextView soTextView;
final TextView colabTextView;
final TextView localTextView;
final TextView facturaTextView;
final TextView calendarioTextView;
final TextView obsTextView;
deviceTextView = (TextView) findViewById(R.id.deviceTextView);
soTextView = (TextView) findViewById(R.id.soTextView);
colabTextView = (TextView) findViewById(R.id.colabTextView);
localTextView = (TextView) findViewById(R.id.localTextView);
calendarioTextView = (TextView) findViewById(R.id.calendarioTextView);
obsTextView = (TextView) findViewById(R.id.obsTextView);
facturaTextView = (TextView) findViewById(R.id.facturaTextView);
final TextView designacaoInterface;
designacaoInterface = (TextView) findViewById(R.id.designacaoInterface);
final TextView tomadaInterface;
tomadaInterface = (TextView) findViewById(R.id.tomadaInterface);
final TextView ipInterface;
ipInterface = (TextView) findViewById(R.id.ipInterface);
final TextView macInterface;
macInterface = (TextView) findViewById(R.id.macInterface);
//Criação do objecto que vai fazer e receber o request de json
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, s, null,
new Response.Listener<JSONObject>() {
@Override
//Caso haja resposta do server
public void onResponse(JSONObject response) {
try {
Log.d("MYTAG", "Estou no try");
JSONObject desig = response.getJSONObject("Device");
String d_desig = desig.getString("designacao");
setTitle(d_desig);
//Criação do objecto que vai conter a informação da tabela device
JSONObject tipoMaquina = response.getJSONObject("TipoMaquina");
String t_designacao = tipoMaquina.getString("designacao");
deviceTextView.setText(t_designacao);
JSONObject sistemaOperativo = response.getJSONObject("Device");
String sO = sistemaOperativo.getString("so");
String sOv = sistemaOperativo.getString("so_versao");
soTextView.setText(sO + " " + sOv);
String dataEntrada = sistemaOperativo.getString("data_entrada");
calendarioTextView.setText(dataEntrada);
String obs = sistemaOperativo.getString("obs");
obsTextView.setText(obs);
JSONObject colab = response.getJSONObject("Colaboradore");
String colaborador = colab.getString("nome_preferido");
colabTextView.setText(colaborador);
JSONObject local = response.getJSONObject("Local");
String localS = local.getString("designacao");
localTextView.setText(localS);
JSONObject factura = response.getJSONObject("Factura");
String facturaS = factura.getString("designacao_full");
facturaTextView.setText(facturaS);
//AQUI ESTOU A LIDAR COM UM ARRAY, TENHO DE CRIAR UM OBJECTO
//PARA METER O ARRAY E DEPOIS IR AO OBJECTO bUSCAR ESSE ARRAY
JSONArray networkArray = response.getJSONArray("NetworkInterface");
for (int i=0; i<networkArray.length(); i++) {
JSONObject inter = networkArray.getJSONObject(i);
String designacao = inter.getString("designacao");
designacaoInterface.setText(designacao);
String tomada = inter.getString("tomada_bastidor");
tomadaInterface.setText(tomada);
String ip = inter.getString("ip");
ipInterface.setText(ip);
String mac = inter.getString("mac");
macInterface.setText(mac);
}
String jsonResponse = "";
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
Log.d("MYLOG", "response= " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// mTextView.setText(error.toString());
}
}
);
Log.d("LOL", "Saltei ate aqui");
VolleyApplication.getInstance().getRequestQueue().add(request);
}
}
History.class
public class History extends ActionBarActivity{
public
ListView historyList;
ArrayList<JSONBox> Data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Data = new ArrayList<JSONBox>();
historyList = (ListView) findViewById(R.id.historyList);
historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
JSONBox j = new JSONBox("designacao");
Data.add(j);
JSONBox j1 = new JSONBox("designacao 2");
Data.add(j1);
ListAdapter adapter = new ListAdapter(this, R.layout.listitem_history, Data);
historyList.setAdapter(adapter);
}