Skip to content

Commit

Permalink
Added comments to explain what code does. Fixed bug where time differ…
Browse files Browse the repository at this point in the history
…ences greater than 2^31 - 1 ms caused integer overflow.
  • Loading branch information
rkalz committed Apr 28, 2017
1 parent 29ccd72 commit 5f278c7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/net/rofael/uabparkingfinder/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ protected void onCreate(Bundle savedInstanceState) {
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

// Adds testlots to list of lots
lots.add(testLot1);
lots.add(testLot2);
lots.add(testLot3);

// Initializes table holding list of lots
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lotList);
final GridView list = (GridView) findViewById(R.id.parking_list);
list.setNumColumns(2);
Expand All @@ -59,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) {
adapter.notifyDataSetChanged();
accessNewLotMenu(list);


// Refreses list of lots, adding of testLot4 is for testing purposes
final SwipeRefreshLayout mainSwipe = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_main);

mainSwipe.setOnRefreshListener(
Expand All @@ -77,6 +79,7 @@ public void onRefresh() {
);
}

// When a lot is pressed, changes to that lot's menu
public void accessNewLotMenu(GridView list)
{
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Expand Down
37 changes: 23 additions & 14 deletions app/src/main/java/net/rofael/uabparkingfinder/ParkingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,46 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking);

// Receives name of parking lot from selection in main menu
lot = (Parking) getIntent().getParcelableExtra("Parking");
TextView setParkingName = (TextView) findViewById(R.id.parking_name);
TextView setParkingStatus = (TextView) findViewById(R.id.parking_status);

// Sets text based on the selection
setParkingName.setText(lot.toString());
setParkingStatus.setText(lot.viewStatus());

// Sets up the drop down box for report selection
final Spinner dropDownBox = (Spinner) findViewById(R.id.status_selection);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.reports_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropDownBox.setAdapter(adapter);

ArrayAdapter<CharSequence> dropDownBoxAdapter = ArrayAdapter.createFromResource(this, R.array.reports_array, android.R.layout.simple_spinner_item);
dropDownBoxAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropDownBox.setAdapter(dropDownBoxAdapter);
dropDownBox.setOnItemSelectedListener(this);
drop = dropDownBox;

final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, reportData);
final GridView list = (GridView) findViewById(R.id.recent_reports_table);
gridList = list;
stringListAdapter = adapter2;
list.setNumColumns(2);
list.setAdapter(adapter2);
// Sets up the table that stores the report information
final ArrayAdapter<String> listOfReportsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, reportData);
final GridView listOfReports = (GridView) findViewById(R.id.recent_reports_table);
gridList = listOfReports;
stringListAdapter = listOfReportsAdapter;
listOfReports.setNumColumns(2);
listOfReports.setAdapter(listOfReportsAdapter);
reportData.add("Time");
reportData.add("Report");
for (int i = 0; i < 20; i++)
{
reportData.add("");
}

SharedPreferences sharedPrefs = getPreferences(Context.MODE_PRIVATE);
/*SharedPreferences sharedPrefs = getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor edit = sharedPrefs.edit();
String commitName;
String commitName;*/

// Initializes connection to Google Firebase and checks for reports from server
mDatabase = FirebaseDatabase.getInstance().getReference();
checkFirebase();

// Initializes the Send button
Button confirmClick = (Button) findViewById(R.id.send_staus);
confirmClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
Expand All @@ -89,8 +94,8 @@ public void onClick(View v)
}
});

// Code for the swipe refresh
final SwipeRefreshLayout listSwipe = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_report_list);

listSwipe.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
Expand All @@ -111,7 +116,7 @@ else if (reports.size() >= 10)
reportData.set(i, reports.get((i / 2) - 1).readableLastReportTime());
}

adapter2.notifyDataSetChanged();
listOfReportsAdapter.notifyDataSetChanged();
listSwipe.setRefreshing(false);
}
listSwipe.setRefreshing(false);
Expand Down Expand Up @@ -167,6 +172,7 @@ public void onNothingSelected(AdapterView<?> parent) {

}

// Creates a new report and sends it to Firebase
public void addToList() {
reportType = drop.getSelectedItemPosition() - 1;
if (reportType > -1 && reportType < 3) {
Expand All @@ -179,6 +185,7 @@ public void addToList() {

}

// Checks reports from Google Firebase. Downloads them if we don't have them.
public void checkFirebase()
{

Expand All @@ -187,6 +194,7 @@ public void checkFirebase()
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot reportSnapshot: dataSnapshot.getChildren())
{
// Adds a report from Firebase to the list if it hasn't been downloaded
long reportTime = (long) reportSnapshot.child("reportTime").getValue();
long reportStatus = (long) reportSnapshot.child("status").getValue();
int reportStat = Integer.parseInt(Long.toString(reportStatus));
Expand All @@ -197,6 +205,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
}
}

// Populates the text list of reports based on the report list
Collections.sort(reports, new ReportComparator());
if (reports.size() > 0)
{
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/net/rofael/uabparkingfinder/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,29 @@ else if (status == 1)
public String readableLastReportTime()
{
long timeDif = System.currentTimeMillis() - getReportTime();
int seconds = ((int) timeDif / 1000);
int minutes = (seconds / 60);
int hours = (minutes / 60);
int days = hours / 24;
long seconds = (timeDif / 1000);
long minutes = (seconds / 60);
long hours = (minutes / 60);
long days = hours / 24;

if (days > 0)
{
hours = hours % 24;
return Integer.toString(days) + " days and " + Integer.toString(hours) + " hours ago";
return Long.toString(days) + " days and " + Long.toString(hours) + " hours ago";
}
else if (hours > 0)
{
minutes = minutes % 60;
return Integer.toString(hours) + " hours and " + Integer.toString(minutes) + " minutes ago";
return Long.toString(hours) + " hours and " + Long.toString(minutes) + " minutes ago";
}
else if (minutes > 0)
{
seconds = seconds % 60;
return Integer.toString(minutes) + " minutes and " + Integer.toString(seconds) + " seconds ago";
return Long.toString(minutes) + " minutes and " + Long.toString(seconds) + " seconds ago";
}
else
{
return Integer.toString(seconds) + " seconds ago";
return Long.toString(seconds) + " seconds ago";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Created by Rofael on 2/24/2017.
*/

// Sorts reports in descending order (newest before oldest)
public class ReportComparator implements Comparator
{
public int compare(Object o1, Object o2)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.google.gms:google-services:3.0.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Thu Apr 27 12:46:37 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 comments on commit 5f278c7

Please sign in to comment.