Skip to content

Commit 0d0b35e

Browse files
committed
Version 0.8:
* Updated the Assets to Final Dataset * Created Discover Mode * Created App Icon * (The code is a horrible mess, please don't look)
1 parent 3417fff commit 0d0b35e

26 files changed

+468
-29
lines changed

app/build.gradle

Lines changed: 1 addition & 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', '"AIza..."'
26+
it.buildConfigField 'String', 'API_KEY', '"AIzaSyCbt2d9aEVwJylEQKw-Kuv7sbdt83oYVys"'
2727
}
2828
}
2929
compileOptions {

app/src/main/assets/recyclable.csv

Lines changed: 358 additions & 0 deletions
Large diffs are not rendered by default.
22.1 KB
Loading

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,33 @@
55
import androidx.recyclerview.widget.RecyclerView;
66

77
import android.content.Context;
8+
import android.content.Intent;
89
import android.graphics.Bitmap;
910
import android.os.Bundle;
11+
import android.util.Log;
1012
import android.view.View;
1113
import android.view.ViewGroup;
1214
import android.widget.ImageView;
1315
import android.widget.Toast;
1416

17+
import java.io.BufferedReader;
18+
import java.io.IOException;
19+
import java.io.InputStreamReader;
1520
import java.lang.ref.WeakReference;
1621
import java.util.ArrayList;
1722
import java.util.LinkedList;
1823
import java.util.List;
24+
import java.util.Scanner;
25+
26+
import static google.sc21.sortify.MainActivity.DATASET_FILENAME;
1927

2028
public class ExploreActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
2129

2230
private static MyRecyclerViewAdapter adapter;
2331
private static RecyclerView exploreList;
2432
private static ImageView junkPhoto;
2533
private static Context context;
34+
private static List<Junk> referenceDataSet;
2635

2736

2837
@Override
@@ -35,12 +44,19 @@ protected void onCreate(Bundle savedInstanceState) {
3544

3645
List<Junk> template = new ArrayList<>();
3746
List<String> empty = new LinkedList<String>();
38-
template.add(new Junk("Please Wait...",empty,""));
47+
template.add(new Junk("Please Wait...",empty,"",""));
3948

4049
exploreList.setLayoutManager(new LinearLayoutManager(this));
4150
adapter = new MyRecyclerViewAdapter(this, template);
4251
adapter.setClickListener(this);
4352
exploreList.setAdapter(adapter);
53+
54+
Intent intent = getIntent();
55+
String mode = intent.getStringExtra("mode");
56+
if (mode.equals("discover")) {
57+
junkPhoto.setVisibility(View.GONE);
58+
loadData(importDataset());
59+
}
4460
}
4561

4662

@@ -60,4 +76,26 @@ public static void setImage(Bitmap image) {
6076
public void onItemClick(View view, int position) {
6177
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
6278
}
79+
80+
81+
//region PLEASE DO NOT LOOK IN HERE (terrible practise, I know)
82+
private List<Junk> importDataset() {
83+
List<Junk> referenceData = new ArrayList<>();
84+
85+
try (InputStreamReader is = new InputStreamReader(getAssets().open(DATASET_FILENAME))){
86+
BufferedReader reader = new BufferedReader(is);
87+
reader.readLine();
88+
String line;
89+
while ((line = reader.readLine()) != null) {
90+
try (Scanner column = new Scanner(line)) {
91+
column.useDelimiter(",");
92+
referenceData.add(Junk.newItemFromCSV(column));
93+
}
94+
}
95+
} catch (IOException e) {
96+
}
97+
98+
return referenceData;
99+
}
100+
//endregion
63101
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package google.sc21.sortify;
22

3+
import java.io.Serializable;
34
import java.util.LinkedList;
45
import java.util.List;
56
import java.util.Scanner;
67

78
public class Junk {
89
private String name;
910
private List<String> aliases = new LinkedList<String>();
10-
private String binColour;
11+
private String guidance;
12+
private String link;
1113

12-
public Junk(String junkName, List<String> junkAliases, String junkBinColour) {
14+
public Junk(String junkName, List<String> junkAliases, String junkGuidance, String junkURL) {
1315
name = junkName;
1416
aliases = junkAliases;
15-
binColour = junkBinColour;
17+
guidance = junkGuidance;
18+
link = junkURL;
1619
}
1720
private Junk() {}
1821

1922
public static Junk newItemFromCSV(Scanner scanner) {
2023
Junk item = new Junk();
2124
item.name = scanner.next();
25+
item.link = scanner.next();
2226
String temp = scanner.next().toLowerCase();
2327
if (temp != null) {
2428
item.aliases.add(temp);
@@ -31,7 +35,18 @@ public static Junk newItemFromCSV(Scanner scanner) {
3135
item.aliases.set((item.aliases.size() - 1),item.aliases.get(item.aliases.size() - 1).substring(0, item.aliases.get(item.aliases.size() - 1).length()-1));
3236
}
3337
}
34-
item.binColour = scanner.next();
38+
temp = scanner.next().toLowerCase();
39+
if (temp != null) {
40+
item.guidance = temp;
41+
if (item.guidance != null && item.guidance.contains("\"")) {
42+
item.guidance = item.guidance.substring(1);
43+
item.guidance = item.guidance + scanner.next();
44+
while (!(item.guidance.contains("\""))) {
45+
item.guidance = item.guidance + scanner.next();
46+
}
47+
item.guidance = item.guidance.substring(0, (item.guidance.length()-1));
48+
}
49+
}
3550
return item;
3651
}
3752

@@ -45,7 +60,7 @@ public boolean matches(String label) {
4560

4661
public String returnName() {return name;}
4762
public List<String> returnAliases() {return aliases;}
48-
public String returnInfo() {return binColour;
63+
public String returnInfo() {return guidance;
4964
}
5065

5166
}

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
* --------------------------------------------------
1515
* VERSION HISTORY:
1616
*
17+
* Version 0.8:
18+
* Updated the Assets to Final Dataset
19+
* Created Discover Mode
20+
* Created App Icon
21+
* (The code is a horrible mess, please don't look)
22+
* - - - - - - - - - - - - - - - - - - - - - - - - -
1723
* Version 0.7:
1824
* Implemented a List View for Displaying Results
1925
* Improved Matching function to Create a List of
@@ -64,6 +70,7 @@
6470
import android.graphics.Bitmap;
6571
import android.os.AsyncTask;
6672
import android.os.Bundle;
73+
import android.os.Parcelable;
6774
import android.provider.MediaStore;
6875
import android.util.Log;
6976
import android.widget.Button;
@@ -90,8 +97,10 @@
9097
import java.io.ByteArrayOutputStream;
9198
import java.io.IOException;
9299
import java.io.InputStreamReader;
100+
import java.io.Serializable;
93101
import java.lang.ref.WeakReference;
94102
import java.util.ArrayList;
103+
import java.util.LinkedList;
95104
import java.util.List;
96105
import java.util.Locale;
97106
import java.util.Scanner;
@@ -104,8 +113,8 @@ public class MainActivity extends AppCompatActivity {
104113
//region Declare Class Objects
105114
//GUI Objects:
106115
private Button cameraButton;
116+
private Button discoverButton;
107117
private TextView testLabel;
108-
private ImageView imageBox;
109118
private static Bitmap junkPhoto;
110119
private static List<Junk> referenceDataSet;
111120
//Constants/Parameters:
@@ -116,7 +125,7 @@ public class MainActivity extends AppCompatActivity {
116125
private static final String TAG = MainActivity.class.getSimpleName();
117126
private static final String ANDROID_CERT_HEADER = "X-Android-Cert";
118127
private static final String ANDROID_PACKAGE_HEADER = "X-Android-Package";
119-
private static final String DATASET_FILENAME = "test_dataset.csv";
128+
public static final String DATASET_FILENAME = "recyclable.csv";
120129
//endregion
121130

122131

@@ -190,6 +199,7 @@ private void callCloudVision(final Bitmap bitmap) {
190199
Log.d(TAG, "failed to make API request because of other IOException " + e.getMessage());
191200
}
192201
Intent discoverActivity = new Intent(this, ExploreActivity.class);
202+
discoverActivity.putExtra("mode","camera");
193203
startActivity(discoverActivity);
194204
}
195205
//endregion
@@ -226,10 +236,10 @@ protected void onPostExecute(List<EntityAnnotation> result) {
226236
if (activity != null && !activity.isFinishing()) {
227237
TextView imageDetail = activity.findViewById(R.id.helloLabel);
228238
imageDetail.setText(analyseResponse(result));
239+
ExploreActivity.setImage(junkPhoto);
229240

230241
//ExploreActivity.loadData(referenceDataSet);
231242
ExploreActivity.loadData(matchData(result));
232-
ExploreActivity.setImage(junkPhoto);
233243

234244
}
235245
}
@@ -318,7 +328,7 @@ private static String analyseResponse(List<EntityAnnotation> labels) {
318328
int item = 0;
319329
while (!matchFound && item < referenceDataSet.size()) {
320330
if (referenceDataSet.get(item).matches(label.getDescription())) {
321-
message.append(", Bin: " + referenceDataSet.get(item).returnInfo());
331+
message.append(", Matched to: " + referenceDataSet.get(item).returnName());
322332
matchFound = true;
323333
}
324334
item++;
@@ -351,6 +361,10 @@ private static List<Junk> matchData(List<EntityAnnotation> labels) {
351361
item++;
352362
}
353363
}
364+
if (matchedData == null) {
365+
List<String> empty = new LinkedList<String>();
366+
matchedData.add(new Junk("No Matches Found!",empty,"",""));
367+
}
354368
}
355369
return matchedData;
356370
}
@@ -360,6 +374,8 @@ private static List<Junk> matchData(List<EntityAnnotation> labels) {
360374
//region CSV File Import Function
361375
private List<Junk> importDataset() {
362376
List<Junk> referenceData = new ArrayList<>();
377+
long logTime = java.lang.System.currentTimeMillis();
378+
int logItems = 0;
363379

364380
try (InputStreamReader is = new InputStreamReader(getAssets().open(DATASET_FILENAME))){
365381
BufferedReader reader = new BufferedReader(is);
@@ -370,10 +386,13 @@ private List<Junk> importDataset() {
370386
column.useDelimiter(",");
371387
referenceData.add(Junk.newItemFromCSV(column));
372388
}
389+
logItems++;
373390
}
374391
} catch (IOException e) {
375392
Log.e(TAG, "File Not Found: " + e);
376393
}
394+
395+
testLabel.setText("Done. Fetched "+logItems+" items in "+(java.lang.System.currentTimeMillis()-logTime)+"ms!");
377396
return referenceData;
378397
}
379398
//endregion
@@ -387,18 +406,25 @@ protected void onCreate(Bundle savedInstanceState) {
387406
super.onCreate(savedInstanceState);
388407
setContentView(R.layout.activity_main);
389408
cameraButton = findViewById(R.id.cameraButton);
409+
discoverButton = findViewById(R.id.discoverButton);
390410
testLabel = findViewById(R.id.helloLabel);
391-
imageBox = findViewById(R.id.photoView);
392411

393412
// Import Data-Set
394413
referenceDataSet = importDataset();
395414

396415
// Camera Button Pressed
397416
cameraButton.setOnClickListener(v -> {
398-
testLabel.setText("Open Camera");
417+
testLabel.setText("Opening Camera...");
399418
dispatchTakePictureIntent();
400419

401420
});
421+
422+
discoverButton.setOnClickListener(v -> {
423+
testLabel.setText("Opening Discover...");
424+
Intent discoverActivity = new Intent(this, ExploreActivity.class);
425+
discoverActivity.putExtra("mode","discover");
426+
startActivity(discoverActivity);
427+
});
402428
}
403429
//endregion
404430
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {
4343
String aliasTemp = junkAlias.toString().substring(1);
4444
aliasTemp = aliasTemp.substring(0, aliasTemp.length()-1);
4545
holder.aliasField.setText(aliasTemp);
46-
if (junkInstruction != null) {
47-
holder.descField.setText("Please dispose of me in the " + junkInstruction + " bin");
48-
}
46+
holder.descField.setText(junkInstruction);
4947
}
5048

5149
// total number of rows

app/src/main/res/layout/activity_main.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@
2525
android:id="@+id/cameraButton"
2626
android:layout_width="wrap_content"
2727
android:layout_height="wrap_content"
28-
android:layout_marginBottom="25dp"
2928
android:backgroundTint="@color/sortify_dark_green"
3029
android:text="Open Camera"
3130
app:layout_constraintBottom_toBottomOf="parent"
31+
app:layout_constraintTop_toBottomOf="@id/discoverButton"
3232
app:layout_constraintLeft_toLeftOf="parent"
3333
app:layout_constraintRight_toRightOf="parent" />
3434

35-
<ImageView
36-
android:id="@+id/photoView"
37-
android:layout_width="0dp"
38-
android:layout_height="0dp"
39-
android:layout_marginTop="25dp"
40-
android:layout_marginBottom="25dp"
35+
<Button
36+
android:id="@+id/discoverButton"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:backgroundTint="@color/sortify_dark_green"
40+
android:text="Discover"
41+
app:layout_constraintTop_toTopOf="parent"
4142
app:layout_constraintBottom_toTopOf="@id/cameraButton"
4243
app:layout_constraintLeft_toLeftOf="parent"
43-
app:layout_constraintRight_toRightOf="parent"
44-
app:layout_constraintTop_toBottomOf="@id/helloLabel" />
44+
app:layout_constraintRight_toRightOf="parent" />
4545

4646
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3-
<background android:drawable="@drawable/ic_launcher_background" />
4-
<foreground android:drawable="@drawable/ic_launcher_foreground" />
3+
<background android:drawable="@color/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
55
</adaptive-icon>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3-
<background android:drawable="@drawable/ic_launcher_background" />
4-
<foreground android:drawable="@drawable/ic_launcher_foreground" />
3+
<background android:drawable="@color/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
55
</adaptive-icon>

0 commit comments

Comments
 (0)