Skip to content

Commit 3417fff

Browse files
committed
Version 0.7:
* Implemented a List View for Displaying Results * Improved Matching function to Create a List of Only Items that Both the Camera and Database saw
1 parent c8522ea commit 3417fff

File tree

8 files changed

+270
-14
lines changed

8 files changed

+270
-14
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ android {
2323
}
2424
buildTypes.each {
2525
// INSERT THE GOOGLE CLOUD API KEY BELOW (Instead of "AIza..."):
26-
it.buildConfigField 'String', 'API_KEY', '"AIzaSyB2fSbpVV3msdZ5M3ZITdZdmNYNCE8PRu4"'
26+
it.buildConfigField 'String', 'API_KEY', '"AIza..."'
2727
}
2828
}
2929
compileOptions {
@@ -53,6 +53,7 @@ dependencies {
5353
compile 'com.google.api-client:google-api-client-android:1.23.0' exclude module: 'httpclient'
5454
compile 'com.google.http-client:google-http-client-gson:1.23.0' exclude module: 'httpclient'
5555
implementation 'com.opencsv:opencsv:4.6'
56+
implementation 'com.android.support:recyclerview-v7:28.0.0'
5657

5758

5859
testImplementation 'junit:junit:4.+'

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--Sortify App, Created 13/03/2021 -->
3-
<!--Made by Paul & Udit -->
4-
<!--for Google's 2021 Solution Challenge-->
52
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
63
package="google.sc21.sortify">
4+
<!-- This Ensures that Only Devices with a Camera can Download our App -->
5+
<uses-feature
6+
android:name="android.hardware.camera"
7+
android:required="true" />
8+
9+
<uses-permission android:name="android.permission.INTERNET" />
710

811
<application
912
android:allowBackup="true"
@@ -12,6 +15,7 @@
1215
android:roundIcon="@mipmap/ic_launcher_round"
1316
android:supportsRtl="true"
1417
android:theme="@style/Theme.Sortify">
18+
<activity android:name=".ExploreActivity"></activity>
1519
<activity android:name=".MainActivity">
1620
<intent-filter>
1721
<action android:name="android.intent.action.MAIN" />
@@ -21,9 +25,4 @@
2125
</activity>
2226
</application>
2327

24-
<!--This Ensures that Only Devices with a Camera can Download our App-->
25-
<uses-feature android:name="android.hardware.camera"
26-
android:required="true" />
27-
<uses-permission android:name="android.permission.INTERNET" />
28-
2928
</manifest>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package google.sc21.sortify;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.recyclerview.widget.LinearLayoutManager;
5+
import androidx.recyclerview.widget.RecyclerView;
6+
7+
import android.content.Context;
8+
import android.graphics.Bitmap;
9+
import android.os.Bundle;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.ImageView;
13+
import android.widget.Toast;
14+
15+
import java.lang.ref.WeakReference;
16+
import java.util.ArrayList;
17+
import java.util.LinkedList;
18+
import java.util.List;
19+
20+
public class ExploreActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
21+
22+
private static MyRecyclerViewAdapter adapter;
23+
private static RecyclerView exploreList;
24+
private static ImageView junkPhoto;
25+
private static Context context;
26+
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
setContentView(R.layout.activity_explore);
32+
junkPhoto = findViewById(R.id.junkView);
33+
exploreList = findViewById(R.id.junkListView);
34+
ExploreActivity.context = getApplicationContext();
35+
36+
List<Junk> template = new ArrayList<>();
37+
List<String> empty = new LinkedList<String>();
38+
template.add(new Junk("Please Wait...",empty,""));
39+
40+
exploreList.setLayoutManager(new LinearLayoutManager(this));
41+
adapter = new MyRecyclerViewAdapter(this, template);
42+
adapter.setClickListener(this);
43+
exploreList.setAdapter(adapter);
44+
}
45+
46+
47+
public static void loadData(List<Junk> data) {
48+
exploreList.setLayoutManager(new LinearLayoutManager(ExploreActivity.context));
49+
adapter = new MyRecyclerViewAdapter(ExploreActivity.context, data);
50+
exploreList.setAdapter(adapter);
51+
}
52+
53+
54+
public static void setImage(Bitmap image) {
55+
junkPhoto.setImageBitmap(image);
56+
}
57+
58+
59+
@Override
60+
public void onItemClick(View view, int position) {
61+
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
62+
}
63+
}

app/src/main/java/google/sc21/sortify/Junk.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class Junk {
99
private List<String> aliases = new LinkedList<String>();
1010
private String binColour;
1111

12+
public Junk(String junkName, List<String> junkAliases, String junkBinColour) {
13+
name = junkName;
14+
aliases = junkAliases;
15+
binColour = junkBinColour;
16+
}
1217
private Junk() {}
1318

1419
public static Junk newItemFromCSV(Scanner scanner) {
@@ -19,7 +24,7 @@ public static Junk newItemFromCSV(Scanner scanner) {
1924
item.aliases.add(temp);
2025
if (item.aliases != null && item.aliases.get(0).contains("\"")) {
2126
item.aliases.set(0,item.aliases.get(0).substring(1));
22-
item.aliases.add(scanner.next());
27+
item.aliases.add(scanner.next().toLowerCase());
2328
while (!(item.aliases.get(item.aliases.size() - 1).contains("\""))) {
2429
item.aliases.add(scanner.next().toLowerCase());
2530
}
@@ -38,8 +43,9 @@ public boolean matches(String label) {
3843
}
3944
}
4045

41-
public String returnInfo() {
42-
return binColour;
46+
public String returnName() {return name;}
47+
public List<String> returnAliases() {return aliases;}
48+
public String returnInfo() {return binColour;
4349
}
4450

4551
}

app/src/main/java/google/sc21/sortify/MainActivity.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* --------------------------------------------------
1515
* VERSION HISTORY:
1616
*
17+
* Version 0.7:
18+
* Implemented a List View for Displaying Results
19+
* Improved Matching function to Create a List of
20+
* Only Items that Both the Camera and Database saw
21+
* - - - - - - - - - - - - - - - - - - - - - - - - -
1722
* Version 0.6:
1823
* Implemented CSV Dataset Handling.
1924
* Implemented Scanned Label to Data Matching for
@@ -101,7 +106,7 @@ public class MainActivity extends AppCompatActivity {
101106
private Button cameraButton;
102107
private TextView testLabel;
103108
private ImageView imageBox;
104-
private Bitmap junkPhoto;
109+
private static Bitmap junkPhoto;
105110
private static List<Junk> referenceDataSet;
106111
//Constants/Parameters:
107112
private static final int REQUEST_IMAGE_CAPTURE = 1;
@@ -145,7 +150,6 @@ public void uploadImage(Bitmap bitmapImage) {
145150
if (bitmapImage != null) {
146151
Bitmap bitmap = scaleBitmapDown(bitmapImage, MAX_DIMENSION);
147152
callCloudVision(bitmap);
148-
imageBox.setImageBitmap(bitmap);
149153
} else {
150154
Log.d(TAG, "Image picker gave us a null image.");
151155
Toast.makeText(this, R.string.image_picker_error, Toast.LENGTH_LONG).show();
@@ -185,6 +189,8 @@ private void callCloudVision(final Bitmap bitmap) {
185189
} catch (IOException e) {
186190
Log.d(TAG, "failed to make API request because of other IOException " + e.getMessage());
187191
}
192+
Intent discoverActivity = new Intent(this, ExploreActivity.class);
193+
startActivity(discoverActivity);
188194
}
189195
//endregion
190196

@@ -220,6 +226,11 @@ protected void onPostExecute(List<EntityAnnotation> result) {
220226
if (activity != null && !activity.isFinishing()) {
221227
TextView imageDetail = activity.findViewById(R.id.helloLabel);
222228
imageDetail.setText(analyseResponse(result));
229+
230+
//ExploreActivity.loadData(referenceDataSet);
231+
ExploreActivity.loadData(matchData(result));
232+
ExploreActivity.setImage(junkPhoto);
233+
223234
}
224235
}
225236
}
@@ -322,6 +333,30 @@ private static String analyseResponse(List<EntityAnnotation> labels) {
322333
//endregion
323334

324335

336+
//region Relevant Info Combining Function
337+
private static List<Junk> matchData(List<EntityAnnotation> labels) {
338+
List<Junk> matchedData = new ArrayList<>();
339+
340+
if (labels != null) {
341+
for (EntityAnnotation label : labels) {
342+
boolean matchFound = false;
343+
int item = 0;
344+
while (!matchFound && item < referenceDataSet.size()) {
345+
if (referenceDataSet.get(item).matches(label.getDescription())) {
346+
if (!matchedData.contains(referenceDataSet.get(item))) {
347+
matchedData.add(referenceDataSet.get(item));
348+
matchFound = true;
349+
}
350+
}
351+
item++;
352+
}
353+
}
354+
}
355+
return matchedData;
356+
}
357+
//endregion
358+
359+
325360
//region CSV File Import Function
326361
private List<Junk> importDataset() {
327362
List<Junk> referenceData = new ArrayList<>();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Sample code from Stack Overflow:
3+
* https://stackoverflow.com/a/40584425
4+
*/
5+
6+
package google.sc21.sortify;
7+
8+
import android.content.Context;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.TextView;
13+
14+
import androidx.recyclerview.widget.RecyclerView;
15+
16+
import java.util.List;
17+
18+
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
19+
private List<Junk> mData;
20+
private LayoutInflater mInflater;
21+
private ItemClickListener mClickListener;
22+
23+
//Data is Passed into the Constructor
24+
MyRecyclerViewAdapter(Context context, List<Junk> data) {
25+
this.mInflater = LayoutInflater.from(context);
26+
this.mData = data;
27+
}
28+
29+
//Inflates the row layout from xml when needed
30+
@Override
31+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
32+
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
33+
return new ViewHolder(view);
34+
}
35+
36+
//Binds the data to the TextView in each row
37+
@Override
38+
public void onBindViewHolder(ViewHolder holder, int position) {
39+
String junkName = mData.get(position).returnName();
40+
List<String> junkAlias = mData.get(position).returnAliases();
41+
String junkInstruction = mData.get(position).returnInfo();
42+
holder.nameField.setText(junkName);
43+
String aliasTemp = junkAlias.toString().substring(1);
44+
aliasTemp = aliasTemp.substring(0, aliasTemp.length()-1);
45+
holder.aliasField.setText(aliasTemp);
46+
if (junkInstruction != null) {
47+
holder.descField.setText("Please dispose of me in the " + junkInstruction + " bin");
48+
}
49+
}
50+
51+
// total number of rows
52+
@Override
53+
public int getItemCount() {
54+
return mData.size();
55+
}
56+
57+
58+
//Stores and recycles views as they are scrolled off screen
59+
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
60+
TextView nameField;
61+
TextView aliasField;
62+
TextView descField;
63+
ViewHolder(View itemView) {
64+
super(itemView);
65+
nameField = itemView.findViewById(R.id.junkName);
66+
aliasField = itemView.findViewById(R.id.junkAlias);
67+
descField = itemView.findViewById(R.id.junkInstruction);
68+
itemView.setOnClickListener(this);
69+
}
70+
71+
@Override
72+
public void onClick(View view) {
73+
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
74+
}
75+
}
76+
77+
// convenience method for getting data at click position
78+
String getItem(int id) {
79+
return mData.get(id).returnName();
80+
}
81+
82+
// allows clicks events to be caught
83+
void setClickListener(ItemClickListener itemClickListener) {
84+
this.mClickListener = itemClickListener;
85+
}
86+
87+
// parent activity will implement this method to respond to click events
88+
public interface ItemClickListener {
89+
void onItemClick(View view, int position);
90+
}
91+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:background="@color/sortify_dark_green"
8+
tools:context=".ExploreActivity">
9+
10+
<ImageView
11+
android:id="@+id/junkView"
12+
android:layout_width="100pt"
13+
android:layout_height="100pt"
14+
app:layout_constraintLeft_toLeftOf="parent"
15+
app:layout_constraintRight_toRightOf="parent"
16+
app:layout_constraintTop_toTopOf="parent" />
17+
18+
<androidx.recyclerview.widget.RecyclerView
19+
android:id="@+id/junkListView"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
app:layout_constraintBottom_toBottomOf="parent"
23+
app:layout_constraintLeft_toLeftOf="parent"
24+
app:layout_constraintRight_toRightOf="parent"
25+
app:layout_constraintTop_toBottomOf="@id/junkView"
26+
app:layout_constraintVertical_bias="0.0" />
27+
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:background="@color/sortify_green"
7+
android:orientation="vertical"
8+
android:padding="10dp">
9+
10+
<TextView
11+
android:id="@+id/junkName"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="Test Name"
15+
android:textColor="@color/black"
16+
android:textSize="30sp" />
17+
18+
<TextView
19+
android:id="@+id/junkAlias"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:text="Prototype Name"
23+
android:textColor="@color/sortify_dark_green"
24+
android:textSize="20sp" />
25+
26+
<TextView
27+
android:id="@+id/junkInstruction"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="Please Dispose of Responsibly"
31+
android:textColor="@color/white"
32+
android:textSize="15sp" />
33+
34+
</LinearLayout>

0 commit comments

Comments
 (0)