diff --git a/pit/src/main/AndroidManifest.xml b/pit/src/main/AndroidManifest.xml index a98e9db9..7a289317 100644 --- a/pit/src/main/AndroidManifest.xml +++ b/pit/src/main/AndroidManifest.xml @@ -13,13 +13,17 @@ + android:theme="@style/AppTheme"> - + + diff --git a/pit/src/main/ic_launcher-web.png b/pit/src/main/ic_launcher-web.png new file mode 100644 index 00000000..b5ba53d6 Binary files /dev/null and b/pit/src/main/ic_launcher-web.png differ diff --git a/pit/src/main/java/com/mvrt/pit/LocalDataActivity.java b/pit/src/main/java/com/mvrt/pit/LocalDataActivity.java new file mode 100644 index 00000000..09a4c97a --- /dev/null +++ b/pit/src/main/java/com/mvrt/pit/LocalDataActivity.java @@ -0,0 +1,237 @@ +package com.mvrt.pit; + +import android.os.Bundle; +import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import com.firebase.client.Firebase; +import com.mvrt.mvrtlib.util.Constants; +import com.mvrt.mvrtlib.util.JSONUtils; +import com.mvrt.mvrtlib.util.MatchInfo; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FilenameFilter; +import java.io.IOException; + +public class LocalDataActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, ItemSelectListener { + + RecyclerView fileListRecycler; + LocalDataAdapter localDataAdapter; + SwipeRefreshLayout swipeRefreshLayout; + + Firebase firebase; + + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + initFirebase(); + + setContentView(R.layout.activity_localdata); + + setTitle("Local Data"); + Toolbar t = (Toolbar)findViewById(R.id.pitscout_toolbar); + setSupportActionBar(t); + + fileListRecycler = (RecyclerView)findViewById(R.id.localdata_listrecycler); + initRecycler(); + swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.localdata_swiperefresh); + swipeRefreshLayout.setOnRefreshListener(this); + } + + private void initFirebase(){ + Firebase.setAndroidContext(getApplication()); + firebase = new Firebase("http://teamdata.firebaseio.com/pit"); + } + + private void initRecycler(){ + fileListRecycler.setLayoutManager(new LinearLayoutManager(this)); + String[] filenames = getFilesDir().list(new JSONFilter()); + localDataAdapter = new LocalDataAdapter(filenames); + localDataAdapter.setSelectListener(this); + fileListRecycler.setAdapter(localDataAdapter); + } + + public void syncData(){ + String[] filenames = localDataAdapter.getFilenames(); + for(String file:filenames){ + readFile(file); + } + this.onRefresh(); + } + + private void readFile(String filename){ + try { + FileInputStream fis = openFileInput(filename); + + int size = fis.available(); + byte[] buffer = new byte[size]; + fis.read(buffer); + fis.close(); + + JSONObject scoutData = new JSONObject(new String(buffer)); + + uploadData(scoutData, filename); + + }catch(FileNotFoundException e){ + Toast.makeText(this, "File " + filename + " not found", Toast.LENGTH_SHORT).show(); + Log.e("MVRT", "File " + filename + " not found"); + }catch(IOException e){ + Toast.makeText(this, "File Read IOException", Toast.LENGTH_SHORT).show(); + Log.e("MVRT", "File Read IOException"); + }catch(JSONException e){ + Toast.makeText(this, "File Read JSONException", Toast.LENGTH_SHORT).show(); + Log.e("MVRT", "File Read JSONException"); + } + } + + private void uploadData(JSONObject scoutData, String filename){ + try{ + firebase.push().setValue(JSONUtils.jsonToMap(scoutData)); + deleteFile(filename); + Log.d("MVRT", "Deleted file " + filename); + }catch(JSONException e){ + Toast.makeText(this, "Upload JSONException", Toast.LENGTH_SHORT).show(); + Log.e("MVRT", "Upload JSONException"); + } + } + + @Override + public void onRefresh() { + String[] filenames = getFilesDir().list(new JSONFilter()); + localDataAdapter.setFilenames(filenames); + swipeRefreshLayout.setRefreshing(false); + } + + @Override + public void itemSelected(String filename) { + Toast.makeText(this, filename + " selected", Toast.LENGTH_SHORT); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu){ + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu_localdata, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item){ + switch(item.getItemId()){ + case R.id.action_syncdata: + syncData(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + static class JSONFilter implements FilenameFilter{ + + @Override + public boolean accept(File dir, String filename) { + Log.d("MVRT", "validating filename " + filename); + return filename.matches("pitscout-\\d+.json"); + } + } + + static class LocalDataAdapter extends RecyclerView.Adapter{ + + String[] filenames; + ItemSelectListener selectListener; + + public LocalDataAdapter(String[] files){ + filenames = files; + } + + public String[] getFilenames(){ + return filenames; + } + + public void setSelectListener(ItemSelectListener listener){ + selectListener = listener; + } + + public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ + + private TextView filenameView; + private ItemSelectListener selectListener; + + public ViewHolder(View itemView, ItemSelectListener selectListener) { + super(itemView); + this.selectListener = selectListener; + itemView.setOnClickListener(this); + itemView.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + Log.d("MVRT", ""); + return false; + } + }); + filenameView = (TextView)itemView.findViewById(R.id.filelistitem_filename); + } + + public String getFilename(){ + return filenameView.getText().toString(); + } + + public void setFilename(String fn){ + filenameView.setText(fn); + } + + @Override + public void onClick(View v) { + if(selectListener != null)selectListener.itemSelected(getFilename()); + } + + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filelist, parent, false); + ViewHolder vh = new ViewHolder(v, selectListener); + return vh; + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + holder.setFilename(filenames[position]); + } + + public void clear(){ + setFilenames(new String[0]); + } + + public void setFilenames(String[] filenames){ + this.filenames = filenames; + notifyDataSetChanged(); + } + + @Override + public int getItemCount() { + return filenames.length; + } + + } + +} + +interface ItemSelectListener { + void itemSelected(String filename); +} \ No newline at end of file diff --git a/pit/src/main/java/com/mvrt/pit/PitScout.java b/pit/src/main/java/com/mvrt/pit/PitScout.java index 31f91bd6..00483f65 100644 --- a/pit/src/main/java/com/mvrt/pit/PitScout.java +++ b/pit/src/main/java/com/mvrt/pit/PitScout.java @@ -1,6 +1,9 @@ package com.mvrt.pit; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; +import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; @@ -15,10 +18,14 @@ import com.firebase.client.Firebase; import com.mvrt.mvrtlib.util.JSONUtils; +import com.mvrt.mvrtlib.util.MatchInfo; +import com.mvrt.mvrtlib.util.Snacker; import org.json.JSONException; import org.json.JSONObject; +import java.io.FileOutputStream; + public class PitScout extends AppCompatActivity implements View.OnClickListener { EditText team; @@ -59,6 +66,9 @@ public class PitScout extends AppCompatActivity implements View.OnClickListener @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + Firebase.setAndroidContext(getApplication()); + setContentView(R.layout.activity_pit_scout); initUI(); } @@ -139,8 +149,9 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); - if (id == R.id.action_settings) { - return true; + if (id == R.id.action_localdata) { + Intent i = new Intent(this, LocalDataActivity.class); + startActivity(i); } return super.onOptionsItemSelected(item); @@ -159,13 +170,11 @@ public void onClick (View v) { } private void pushData(){ - Firebase.setAndroidContext(getApplication()); - Firebase firebase = new Firebase("http://teamdata.firebaseio.com/pit"); - Log.d("MVRT", "Pushing Data"); - firebase.child("test").setValue("Hello!"); + int teamNo = Integer.parseInt(team.getText().toString()); + JSONObject obj = new JSONObject(); try{ - obj.put("Team", team.getText().toString()); + obj.put("Team",teamNo); obj.put("Low Bar", lowBar.isChecked()); obj.put("Rock Wall", rockWall.isChecked()); @@ -184,8 +193,8 @@ private void pushData(){ obj.put("High Goal", highGoal.isChecked()); obj.put("Low Goal", lowGoal.isChecked()); - obj.put("Number of Motors", numMotors.getText().toString()); - obj.put("Number of Wheels", numWheels.getText().toString()); + obj.put("Number of Motors", Integer.parseInt(numMotors.getText().toString())); + obj.put("Number of Wheels", Integer.parseInt(numWheels.getText().toString())); obj.put("Type of Wheels", typeWheels.getText().toString()); obj.put("Auton Reach", autonReach.isChecked()); @@ -193,15 +202,15 @@ private void pushData(){ obj.put("Auton Low", autonLowShot.isChecked()); obj.put("Auton High", autonHighShot.isChecked()); - obj.put("Driver Regionals", driverExp.getText().toString()); - obj.put("Weight", weight.getText().toString()); + obj.put("Driver Regionals", Integer.parseInt(driverExp.getText().toString())); + obj.put("Weight", Integer.parseInt(weight.getText().toString())); obj.put("Strategy", strategy.getText().toString()); } catch(Exception e){ Log.e("MVRT", "JSON Error"); } try { - firebase.push().setValue(JSONUtils.jsonToMap(obj)); + writeToFile(obj, teamNo); } catch (JSONException e) { Log.e("MVRT", "JSON Error"); @@ -221,4 +230,17 @@ private void clearData() { sallyport.setChecked(false); } + + public String writeToFile(JSONObject data, int team) throws JSONException { + String filename = "pitscout-" + team + ".json"; + try { + FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE); + fos.write(data.toString().getBytes()); + Snacker.snack("Written to file: " + filename, this, Snackbar.LENGTH_SHORT); + } catch (Exception e) { + e.printStackTrace(); + } + return filename; + } + } diff --git a/pit/src/main/res/drawable-hdpi/ic_menu_upload.png b/pit/src/main/res/drawable-hdpi/ic_menu_upload.png new file mode 100644 index 00000000..535cddfa Binary files /dev/null and b/pit/src/main/res/drawable-hdpi/ic_menu_upload.png differ diff --git a/pit/src/main/res/drawable-mdpi/ic_menu_upload.png b/pit/src/main/res/drawable-mdpi/ic_menu_upload.png new file mode 100644 index 00000000..20b93673 Binary files /dev/null and b/pit/src/main/res/drawable-mdpi/ic_menu_upload.png differ diff --git a/pit/src/main/res/drawable-xhdpi/ic_menu_upload.png b/pit/src/main/res/drawable-xhdpi/ic_menu_upload.png new file mode 100644 index 00000000..411d9c08 Binary files /dev/null and b/pit/src/main/res/drawable-xhdpi/ic_menu_upload.png differ diff --git a/pit/src/main/res/drawable-xxhdpi/ic_menu_upload.png b/pit/src/main/res/drawable-xxhdpi/ic_menu_upload.png new file mode 100644 index 00000000..729b8246 Binary files /dev/null and b/pit/src/main/res/drawable-xxhdpi/ic_menu_upload.png differ diff --git a/pit/src/main/res/layout/activity_localdata.xml b/pit/src/main/res/layout/activity_localdata.xml new file mode 100644 index 00000000..fcbf475b --- /dev/null +++ b/pit/src/main/res/layout/activity_localdata.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pit/src/main/res/layout/activity_pit_scout.xml b/pit/src/main/res/layout/activity_pit_scout.xml index d5007959..935ba0d1 100644 --- a/pit/src/main/res/layout/activity_pit_scout.xml +++ b/pit/src/main/res/layout/activity_pit_scout.xml @@ -27,23 +27,19 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - - + + android:orientation="horizontal">