Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.psn.asset;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.util.HashMap;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONObject;
- import org.json.JSONArray;
- import org.json.JSONException;
- import android.os.Bundle;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.net.Uri;
- import android.widget.EditText;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.ToggleButton;
- import android.widget.Spinner;
- import android.widget.Toast;
- import android.widget.ImageView;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.location.Location;
- import android.location.LocationManager;
- import android.location.LocationListener;
- import android.provider.MediaStore;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.database.Cursor;
- /**
- * PSN Mobile Asset
- * Copyright (c) 2013
- * @author Raditya Kurnianto
- */
- public class Asset extends Activity {
- String sn, mn, loc, cond, notes, pht, toast, selected, type, image_name, encoded_image;
- int code;
- int[] imageArray = {R.id.image, R.id.image2, R.id.image3};
- String[] encoded_imageArray = {};
- Vector<String> vector;
- Button save, browse;
- ToggleButton toggle_loc;
- Spinner condition;
- AutoCompleteTextView material_name;
- EditText serial_number, location, note, photo;
- ProgressDialog pDialog;
- ConnectivityManager connMgr;
- NetworkInfo networkInfo;
- LocationManager locManager;
- LocationListener locListener;
- File file;
- Intent intent;
- Uri selected_image;
- Cursor cursor;
- ImageView image, image2, image3;
- ByteArrayOutputStream baos;
- Bitmap imageUpload, new_image;
- ArrayList<HashMap<String, String>> material;
- List<String> list = null;
- private static final String url = "http://192.168.7.124/psn_asset_oracle/insert_asset.php";
- private static final String url_photo = "http://192.168.7.124/psn_asset_oracle/base.php";
- private static final String url_material = "http://192.168.7.124/psn_asset_oracle/getAllLocation.php";
- private static final String TAG_CODE = "code";
- private static final String TAG_MATERIAL_ARRAY = "material_array";
- private static final String TAG_MAT_NAME = "MAT_NAME";
- private static int RESULT_LOAD_IMAGE = 1;
- JSONObject json_query, json_photo;
- JSONParser jsonParser = new JSONParser();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_asset);
- save = (Button)findViewById(R.id.save);
- browse = (Button)findViewById(R.id.browse);
- serial_number = (EditText)findViewById(R.id.serial_number);
- material_name = (AutoCompleteTextView)findViewById(R.id.material_name);
- location = (EditText)findViewById(R.id.location);
- condition = (Spinner)findViewById(R.id.condition);
- note = (EditText)findViewById(R.id.note);
- photo = (EditText)findViewById(R.id.photo);
- image = (ImageView)findViewById(R.id.image);
- image2 = (ImageView)findViewById(R.id.image2);
- image3 = (ImageView)findViewById(R.id.image3);
- locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locListener = new GetLocation();
- locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locListener);
- /*executing new background task the application is first launch*/
- new fetch_material().execute();
- /*array adapter for spinner condition*/
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.condition, android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- condition.setAdapter(adapter);
- /*detect changes listener on spinner*/
- condition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
- {
- @Override
- public void onItemSelected(AdapterView<?> parent, View v, int pos, long id)
- {
- Object item = parent.getItemAtPosition(pos);
- selected = item.toString();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent)
- {
- /*do nothing*/
- }
- });
- /*button to save*/
- save.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- sn = serial_number.getText().toString();
- mn = material_name.getText().toString();
- loc = location.getText().toString();
- cond = selected;
- notes = note.getText().toString();
- pht = photo.getText().toString();
- /*check internet connectivity*/
- connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- networkInfo = connMgr.getActiveNetworkInfo();
- if(networkInfo != null && networkInfo.isConnected())
- {
- new addAsset().execute();
- }
- else
- {
- Toast.makeText(getApplicationContext(), "Failed! Internet Connection is needed.", Toast.LENGTH_SHORT).show();
- }
- }
- });
- /*this button will be remove*/
- browse.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, RESULT_LOAD_IMAGE);
- }
- });
- }
- /*set image view and decode to byte array stream*/
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
- {
- selected_image = data.getData();
- String[] filePath= {MediaStore.Images.Media.DATA};
- cursor = getContentResolver().query(selected_image, filePath, null, null, null);
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePath[0]);
- String picturePath = cursor.getString(columnIndex);
- cursor.close();
- /*naming image to ex : psn_asset_112344215.jpg*/
- image_name = "psn_asset_" + serial_number.getText().toString() + ".jpg";
- photo.setText(image_name);
- /*get width and height to rescaled*/
- Bitmap original_image = BitmapFactory.decodeFile(picturePath);
- int width = original_image.getWidth();
- int height = original_image.getHeight();
- int newWidth = width / 3;
- int newHeight = height / 3;
- original_image.recycle();
- Log.i("Before Scale", "Width = " + width + ", Height = " + height);
- Log.i("After Scale", "Width = " + newWidth + ", Height = " + newHeight);
- /*scaling image*/
- BitmapFactory.Options image_size = new BitmapFactory.Options();
- image_size.inSampleSize = 4;
- imageUpload = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(picturePath, image_size), newWidth, newHeight, false);
- if(image.getDrawable() == null)
- {
- image.setImageBitmap(imageUpload);
- }
- else if(image2.getDrawable() == null)
- {
- image2.setImageBitmap(imageUpload);
- }
- else if(image3.getDrawable() == null)
- {
- image3.setImageBitmap(imageUpload);
- }
- }
- }
- public String[] encodeImage()
- {
- /*convert to byte array output stream*/
- vector = new Vector<String>();
- baos = new ByteArrayOutputStream();
- for(int i = 1; i < imageArray.length; i++)
- {
- imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- byte[] image_data = baos.toByteArray();
- vector.add(Base64.encodeBytes(image_data));
- }
- for(int i = 0; i < encoded_imageArray.length ; i++){
- Log.i("Encoded Image", encoded_imageArray[i]);}
- return vector.toArray(encoded_imageArray);
- }
- /*detect selected toggle state
- public void onToggleClick(View view)
- {
- boolean on = ((ToggleButton)view).isChecked();
- if(on)
- {
- locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locListener = new GetLocation();
- locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locListener);
- Toast.makeText(getApplicationContext(), "Automatic location turning on.", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(getApplicationContext(), "Warning. You're not turning on automatic location. Toggle the location buttoon ON to turn on.", Toast.LENGTH_LONG).show();
- }
- }*/
- /*async task to insert new aseet record*/
- class addAsset extends AsyncTask<String, String, String>
- {
- @Override
- protected void onPreExecute()
- {
- pDialog = new ProgressDialog(Asset.this);
- pDialog.setMessage("Inserting..");
- pDialog.setCancelable(false);
- pDialog.setIndeterminate(false);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... args)
- {
- Log.i("Do In Background", "Background Task Started");
- try
- {
- locManager.removeUpdates(locListener);
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("SERIAL_NUMBER", sn));
- params.add(new BasicNameValuePair("MATERIAL_NAME", mn));
- params.add(new BasicNameValuePair("LOCATION", loc));
- params.add(new BasicNameValuePair("CONDITION", cond));
- params.add(new BasicNameValuePair("NOTE", notes));
- params.add(new BasicNameValuePair("PHOTO", pht));
- json_query = jsonParser.makeHttpRequest(url, "POST", params);/*send all field to database*/
- encodeImage();
- //ArrayList<NameValuePair> params_p = new ArrayList<NameValuePair>();
- //params_p.add(new BasicNameValuePair("image_name", image_name));/*title*/
- //params_p.add(new BasicNameValuePair("image", encoded_imageArray[0]));
- //params_p.add(new BasicNameValuePair("image", encoded_imageArray[1]));
- //params_p.add(new BasicNameValuePair("image", encoded_imageArray[2]));/*image*/
- //json_photo = jsonParser.makeHttpRequest(url_photo, "POST",params_p);/*upload image to server as binary*/
- Log.d("Create Response", json_query.toString());
- //code = json_photo.getInt(TAG_CODE);
- if(code == 1)
- {
- toast = "Success!";
- }
- else if(code == 10)
- {
- toast = "No parameter";
- }
- else if(code == 11)
- {
- toast = "Can't insert query";
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- Log.i("Do In Background", "Background Task Finished");
- return toast;
- }
- @Override
- protected void onPostExecute(String message)
- {
- message = toast;
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- pDialog.dismiss();
- /*clear all field*/
- serial_number.setText("");
- material_name.setText("");
- note.setText("");
- photo.setText("");
- image.setImageDrawable(getResources().getDrawable(R.drawable.default_preview));
- }
- }
- /*get current location based on gps*/
- public class GetLocation implements LocationListener
- {
- @Override
- public void onLocationChanged(Location loc)
- {
- location.setText(String.valueOf(loc.getLatitude()) + ", " + String.valueOf(loc.getLongitude()));
- }
- @Override
- public void onProviderDisabled(String provider)
- {
- Toast.makeText(getApplicationContext(), "Please Enable your GPS and WiFi to improve your location accuracy", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onProviderEnabled(String provider)
- {
- Toast.makeText(getApplicationContext(), "GPS Enabled\n To improve your location accuracy please enabled Wifi", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras)
- {
- }
- }
- /*fetching material name from database. this class execute on every first launch*/
- class fetch_material extends AsyncTask<String, String, String>
- {
- @Override
- protected void onPreExecute()
- {
- pDialog = new ProgressDialog(Asset.this);
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(false);
- pDialog.setMessage("Fetching material name. Please wait..");
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... args)
- {
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- JSONObject a = jsonParser.makeHttpRequest(url_material, "GET", params);
- Log.i("Fetching Material Name", a.toString());
- try
- {
- int code = a.getInt(TAG_CODE);
- if(code == 1)
- {
- JSONArray material_array = a.getJSONArray(TAG_MATERIAL_ARRAY);
- final String[] sFinalResult = new String[material_array.length()];
- for (int i = 0;i < material_array.length();i++)
- {
- a = material_array.getJSONObject(i);
- sFinalResult[i] = a.getString(TAG_MAT_NAME);
- }
- list = new ArrayList<String>();
- for(int i = 0; i < sFinalResult.length;i++)
- {
- list.add(sFinalResult[i]);
- }
- toast = "Success";
- }
- else if(code == 11)
- {
- toast = "Failed";
- }
- }
- catch(JSONException ex)
- {
- ex.printStackTrace();
- }
- return toast;
- }
- @Override
- protected void onPostExecute(String message)
- {
- pDialog.dismiss();
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(Asset.this, android.R.layout.simple_list_item_1, list);
- material_name.setAdapter(adapter);
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.asset, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment