From b667738b921076e6bdcd73f12e638b27edf717de Mon Sep 17 00:00:00 2001 From: Daniyaal Khan Date: Fri, 24 May 2019 17:00:33 +0530 Subject: [PATCH] Completed ChartActivity.java. Base features complete. TODO: AddChart.java Require name to be filled Allow Deletion and Updating of airports and charts --- app/src/main/AndroidManifest.xml | 3 +- .../com/daniyaalkhan/imgmap/AddPoints.java | 35 +++++++++++- .../daniyaalkhan/imgmap/ChartActivity.java | 53 +++++++------------ .../java/com/daniyaalkhan/imgmap/Charts.java | 3 ++ .../com/daniyaalkhan/imgmap/DBHandler.java | 28 ++++++++++ .../main/res/layout/activity_add_points.xml | 5 +- 6 files changed, 89 insertions(+), 38 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a13b9b8..5d4b780 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,7 +24,8 @@ - + + \ No newline at end of file diff --git a/app/src/main/java/com/daniyaalkhan/imgmap/AddPoints.java b/app/src/main/java/com/daniyaalkhan/imgmap/AddPoints.java index 5f95115..559e75e 100644 --- a/app/src/main/java/com/daniyaalkhan/imgmap/AddPoints.java +++ b/app/src/main/java/com/daniyaalkhan/imgmap/AddPoints.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Point; import android.graphics.PointF; import android.net.Uri; @@ -23,6 +24,7 @@ import org.w3c.dom.Text; import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.net.URISyntaxException; import java.util.Arrays; @@ -129,6 +131,7 @@ public void onClick(View v) { String name=extras.getString("name"); Uri uri = Uri.parse(extras.getString("uri")); + Log.i("URIx", uri.getPath()); PointF geocoords1, geocoords2, pixelcoords1, pixelcoords2; @@ -140,11 +143,39 @@ public void onClick(View v) { geocoords2 = new PointF(Float.valueOf(geoCoordsString[0]), Float.valueOf(geoCoordsString[1])); pixelcoords2 = new PointF(Float.valueOf(pixelCoordsString[0]), Float.valueOf(pixelCoordsString[1])); + //Get Scaling + int imageViewHeight=chartView.getHeight(), imageViewWidth=chartView.getWidth(); + int imageResx, imageResy; + + //Get Image dimensions + BitmapFactory.Options dimensions = new BitmapFactory.Options(); + dimensions.inJustDecodeBounds = true; + Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); + try{ + InputStream ims = getContentResolver().openInputStream(uri); + mBitmap = BitmapFactory.decodeStream(ims); + }catch (Exception e){ + Toast.makeText(context,"Couldn't add chart", Toast.LENGTH_SHORT).show(); + Intent goHome = new Intent(getApplicationContext(), MainActivity.class); + startActivity(goHome); + } + imageResx = mBitmap.getWidth(); + imageResy = mBitmap.getHeight(); + + //Set Scaled values + PointF scaledPixelcoords1, scaledPixelcoords2; + scaledPixelcoords1 = new PointF( + (pixelcoords1.x/imageViewWidth)*imageResx, (pixelcoords1.y/imageViewHeight)*imageResy + ); + scaledPixelcoords2 = new PointF( + (pixelcoords2.x/imageViewWidth)*imageResx, (pixelcoords2.y/imageViewHeight)*imageResy + ); + //Send Received values to DB Log.d("Reached", "yup"); DBHandler db = new DBHandler(context); - boolean addToDB = db.addChart(icao, Uri.parse(extras.getString("uri")), - geocoords1, geocoords2, pixelcoords1, pixelcoords2, name, context); + boolean addToDB = db.addChart(icao, uri, + geocoords1, geocoords2, scaledPixelcoords1, scaledPixelcoords2, name, context); if(!addToDB){ Toast failed = Toast.makeText(context, "Failed to add to database", Toast.LENGTH_SHORT); diff --git a/app/src/main/java/com/daniyaalkhan/imgmap/ChartActivity.java b/app/src/main/java/com/daniyaalkhan/imgmap/ChartActivity.java index 9aa8db8..af31a04 100644 --- a/app/src/main/java/com/daniyaalkhan/imgmap/ChartActivity.java +++ b/app/src/main/java/com/daniyaalkhan/imgmap/ChartActivity.java @@ -7,10 +7,10 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.BitmapFactory; -import android.graphics.PointF; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; +import android.net.Uri; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v4.content.PermissionChecker; @@ -33,11 +33,11 @@ public class ChartActivity extends Activity implements LocationListener{ private ImgMap imgMap; private ImageView locationPin; - - - private int chartPicture; private ImageView chartView; + private int imageWidth, imageHeight; + private double[][] cords1, cords2; + String[] appPermissions = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION @@ -49,41 +49,16 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chart_activity); - double[][] cords1, cords2; - - Bundle chartName = getIntent().getExtras(); - Log.i("chartName", chartName.getString("icao")); - - if(chartName.getString("icao").equals("VAAU")){ - this.chartPicture = R.drawable.mozilla_page_011; - - //Cords for VAAU testing: - cords1 = new double[][]{{20.16666667, 75.16666667}, {264, 192}}; - cords2 = new double[][]{{19.66666667, 75.66666667}, {1054, 960}}; - }else{ - this.chartPicture = R.drawable.vidp_sid; - - //Cords for VIDP testing: - cords1 = new double[][]{{28.75000000,75.88222222}, {197, 225}}; - cords2 = new double[][]{{27.83333333,77.71666667}, {700, 1100}}; - } - + Bundle extras = getIntent().getExtras(); this.chartView = findViewById(R.id.chartView); - this.chartView.setImageResource(this.chartPicture); this.locationPin = findViewById(R.id.locationPin); this.textView = findViewById(R.id.textView); - //Get image dimensions - BitmapFactory.Options dimensions = new BitmapFactory.Options(); - dimensions.inJustDecodeBounds = true; - BitmapFactory.decodeResource(getResources(), chartPicture, dimensions); - int imageHeight = dimensions.outHeight; - int imageWidth = dimensions.outWidth; - + setChartData(extras.getInt("id")); imgMap = new ImgMap(cords1, cords2); - imgMap.setScaleRes(imageWidth, imageHeight); + imgMap.setScaleRes(this.imageWidth, this.imageHeight); /* LOCATION CODE BELOW THIS POINT */ locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); @@ -97,7 +72,19 @@ protected void onCreate(Bundle savedInstanceState) { } - private PointF pointf = new PointF(); + private void setChartData(int id){ + + DBHandler db = new DBHandler(this); + + Chart chart = db.getChart(id); + + this.chartView.setImageBitmap(chart.chartBitmap); + this.cords1 = chart.coords1; + this.cords2 = chart.coords2; + this.imageHeight = chart.imageHeight; + this.imageWidth = chart.imageWidth; + + } @Override public void onLocationChanged(Location location){ diff --git a/app/src/main/java/com/daniyaalkhan/imgmap/Charts.java b/app/src/main/java/com/daniyaalkhan/imgmap/Charts.java index 09ec0d9..5784a09 100644 --- a/app/src/main/java/com/daniyaalkhan/imgmap/Charts.java +++ b/app/src/main/java/com/daniyaalkhan/imgmap/Charts.java @@ -77,6 +77,9 @@ private void populateListView(){ public void onItemClick(AdapterView parent, View view, int position, long id) { int chartId = chartsList.get(position).id; Log.d("ChartId", String.valueOf(chartId)); + Intent openChart = new Intent(getApplicationContext(), ChartActivity.class); + openChart.putExtra("id", chartId); + startActivity(openChart); } }); diff --git a/app/src/main/java/com/daniyaalkhan/imgmap/DBHandler.java b/app/src/main/java/com/daniyaalkhan/imgmap/DBHandler.java index 7320d7b..93396f5 100644 --- a/app/src/main/java/com/daniyaalkhan/imgmap/DBHandler.java +++ b/app/src/main/java/com/daniyaalkhan/imgmap/DBHandler.java @@ -111,6 +111,7 @@ public boolean addChart(String icao, Uri uri, PointF geoCoords1, PointF geoCoord return false; } + //Convert Image to ByteArray ByteArrayOutputStream stream = new ByteArrayOutputStream(); chartBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] byteArray = stream.toByteArray(); @@ -162,4 +163,31 @@ public List getChartsList(String icao){ return chartsList; } + + public Chart getChart(int id){ + + SQLiteDatabase db = getReadableDatabase(); + + String query = "SELECT * FROM charts WHERE id=?"; + Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(id)}); + + cursor.moveToFirst(); + + Chart chart = new Chart(); + + chart.id = id; + chart.name = cursor.getString(cursor.getColumnIndex("name")); + chart.coords1 = new double[][]{ + {cursor.getDouble(cursor.getColumnIndex("cords1x")), cursor.getDouble(cursor.getColumnIndex("cords1y"))}, + {cursor.getDouble(cursor.getColumnIndex("pixel1y")), cursor.getDouble(cursor.getColumnIndex("pixel1x"))} + }; + chart.coords2 = new double[][]{ + {cursor.getDouble(cursor.getColumnIndex("cords2x")), cursor.getDouble(cursor.getColumnIndex("cords2y"))}, + {cursor.getDouble(cursor.getColumnIndex("pixel2y")), cursor.getDouble(cursor.getColumnIndex("pixel2x"))} + }; + chart.setBitmap(cursor.getBlob(cursor.getColumnIndex("chart"))); + + return chart; + + } } diff --git a/app/src/main/res/layout/activity_add_points.xml b/app/src/main/res/layout/activity_add_points.xml index 2c4825c..58dc2da 100644 --- a/app/src/main/res/layout/activity_add_points.xml +++ b/app/src/main/res/layout/activity_add_points.xml @@ -18,8 +18,9 @@