diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser new file mode 100644 index 0000000..5a3fb77 Binary files /dev/null and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 3963879..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,11 +5,12 @@ diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 0bc8b7f..c865666 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 + compileSdkVersion 27 defaultConfig { applicationId "in.ac.siesgst.npl.remotecontrolbot" - minSdkVersion 19 - targetSdkVersion 26 + minSdkVersion 21 + targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -16,15 +16,19 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + buildToolsVersion '27.0.3' } dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.android.support.constraint:constraint-layout:1.0.2' + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.1' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' - compile 'io.github.controlwear:virtualjoystick:1.8.0' - compile 'com.android.support:design:26.1.0' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'io.github.controlwear:virtualjoystick:1.8.0' + implementation 'com.android.support:design:27.1.1' + implementation 'com.github.bumptech.glide:glide:4.8.0' + annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' + implementation 'com.google.code.gson:gson:2.8.5' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index de1c847..efc9e35 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,17 +8,21 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" - android:roundIcon="@mipmap/ic_launcher_round" + android:roundIcon="@drawable/app_icon" android:supportsRtl="true" android:theme="@style/AppTheme"> - + + + + + - - \ No newline at end of file diff --git a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/Event.java b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/Event.java new file mode 100644 index 0000000..e86c508 --- /dev/null +++ b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/Event.java @@ -0,0 +1,55 @@ +package in.ac.siesgst.npl.remotecontrolbot; + +public class Event { + private String name; + private int backgroundDrawable; + private int buttonDrawable; + private int joyDrawable; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public int getBackgroundDrawable() + { + return backgroundDrawable; + } + + public void setBackgroundDrawable(int backgroundDrawable) + { + this.backgroundDrawable = backgroundDrawable; + } + + public int getButtonDrawable() + { + return buttonDrawable; + } + + public void setButtonDrawable(int buttonDrawable) + { + this.buttonDrawable = buttonDrawable; + } + + public int getJoyDrawable() + { + return joyDrawable; + } + + public void setJoyDrawable(int joyDrawable) + { + this.joyDrawable = joyDrawable; + } + + public Event(String name, int backgroundDrawable, int buttonDrawable, int joyDrawable) { + this.name = name; + this.backgroundDrawable = backgroundDrawable; + this.buttonDrawable = buttonDrawable; + this.joyDrawable = joyDrawable; + } +} diff --git a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/JoystickActivity.java b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/JoystickActivity.java new file mode 100644 index 0000000..1516969 --- /dev/null +++ b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/JoystickActivity.java @@ -0,0 +1,323 @@ +package in.ac.siesgst.npl.remotecontrolbot; + +import android.annotation.SuppressLint; +import android.content.Intent; +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.os.Bundle; +import android.support.constraint.ConstraintLayout; +import android.support.constraint.ConstraintSet; +import android.support.design.widget.TextInputEditText; +import android.support.transition.TransitionManager; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.RadioButton; +import android.widget.RadioGroup; +import android.widget.TextView; +import android.widget.Toast; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketException; + +import io.github.controlwear.virtual.joystick.android.JoystickView; + +public class JoystickActivity extends AppCompatActivity { + + private String curr = ""; + + // UI Elements in Joystick Activity + private JoystickView joystickView; + private ImageView secondaryAction; + private TextView eventTitle; + private ConstraintLayout rootView; + private TextView editConnection; + + private SharedPreferenceManager sharedPreferenceManager; + + // Swap Controls + private boolean isJoystickToRight = true; + private ImageView swapControlButton; + + private int buttonMargin; + + // AlertDialog + private View dialogView; + private TextInputEditText ipAddressText; + private Button submitButton; + private TextView playerActionTitle; + private RadioGroup playerActionGroup; + private RadioButton attackRadio; + private RadioButton defenseRadio; + private AlertDialog connectivityDialog; + + // Swaps the JoystickView and Secondary Action Button + private void swapControls() { + ConstraintSet rootConstraintSet = new ConstraintSet(); + rootConstraintSet.clone(rootView); + + rootConstraintSet.clear(R.id.joystick_view, ConstraintSet.END); + rootConstraintSet.clear(R.id.joystick_view, ConstraintSet.START); + + rootConstraintSet.clear(R.id.button_view, ConstraintSet.START); + rootConstraintSet.clear(R.id.button_view, ConstraintSet.END); + + rootConstraintSet.setMargin(R.id.button_view, ConstraintSet.START, buttonMargin); + rootConstraintSet.setMargin(R.id.button_view, ConstraintSet.END, buttonMargin); + + if (isJoystickToRight) { + rootConstraintSet.connect(R.id.joystick_view, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START); + rootConstraintSet.connect(R.id.button_view, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END); + } else { + rootConstraintSet.connect(R.id.joystick_view, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END); + rootConstraintSet.connect(R.id.button_view, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START); + } + + TransitionManager.beginDelayedTransition(rootView); + + rootConstraintSet.applyTo(rootView); + + isJoystickToRight = !isJoystickToRight; + } + + private Event event; + + + @SuppressLint("ClickableViewAccessibility") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_joystick); + + event = new Gson().fromJson(getIntent().getStringExtra("event"), new TypeToken(){}.getType()); + + eventTitle = findViewById(R.id.joystick_event_title); + eventTitle.setText(event.getName()); + + rootView = findViewById(R.id.joystick_root_view); + rootView.setBackgroundResource(event.getBackgroundDrawable()); + + joystickView = findViewById(R.id.joystick_view); + joystickView.setBackgroundResource(event.getJoyDrawable()); + + secondaryAction = findViewById(R.id.button_view); + secondaryAction.setImageResource(event.getButtonDrawable()); + + secondaryAction.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + if(motionEvent.getAction() == MotionEvent.ACTION_UP) { + new Thread(new UDPSender(12)).start(); + secondaryAction.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#80000000"))); + } + else if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) { + new Thread(new UDPSender(11)).start(); + secondaryAction.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#80FFFFFF"))); + } + return true; + } + }); + + buttonMargin = ((ViewGroup.MarginLayoutParams) secondaryAction.getLayoutParams()).bottomMargin; + + editConnection = findViewById(R.id.edit_connection); + + swapControlButton = findViewById(R.id.controls_swap_button); + + swapControlButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + swapControls(); + } + }); + + sharedPreferenceManager = new SharedPreferenceManager(JoystickActivity.this); + + dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_connectivity, null, false); + + // IP Addr InputView + ipAddressText = dialogView.findViewById(R.id.ip_address_textview); + + // Player Strategy Stuff + playerActionTitle = dialogView.findViewById(R.id.player_selection_title); + playerActionGroup = dialogView.findViewById(R.id.player_selection_group); + defenseRadio = dialogView.findViewById(R.id.strategy_defense); + attackRadio = dialogView.findViewById(R.id.strategy_attack); + + // Submit button + submitButton = dialogView.findViewById(R.id.submit_button); + + connectivityDialog = new AlertDialog.Builder(this) + .setView(dialogView) + .setCancelable(false) + .create(); + + if (sharedPreferenceManager.isIpThere()) { + ipAddressText.setText(sharedPreferenceManager.getIp()); + } + + submitButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String ipAddress = ipAddressText.getText().toString(); + + if (ipAddress.isEmpty()) { + ipAddressText.setError("Please Enter a Valid IP"); + return; + } + + if (!event.getName().equals(MainActivity.ROBO_WARS) + && !attackRadio.isChecked() + && !defenseRadio.isChecked()) { + Toast.makeText(JoystickActivity.this, "Please select a player strategy", Toast.LENGTH_SHORT).show(); + return; + } + + sharedPreferenceManager.createIpSession(ipAddress); + + secondaryAction.setVisibility(attackRadio.isChecked() ? View.VISIBLE : View.GONE); + + connectivityDialog.dismiss(); + } + }); + + connectivityDialog.show(); + + editConnection.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (!connectivityDialog.isShowing()) { + connectivityDialog.show(); + } + } + }); + + switch(event.getName()) { + case MainActivity.ROBO_SOCCER: + playerActionGroup.setVisibility(View.VISIBLE); + playerActionTitle.setVisibility(View.VISIBLE); + secondaryAction.setVisibility(View.VISIBLE); + break; + case MainActivity.ROBO_WARS: + playerActionGroup.setVisibility(View.GONE); + playerActionTitle.setVisibility(View.GONE); + secondaryAction.setVisibility(View.GONE); + break; + case MainActivity.SHELL_SHOCK: + playerActionGroup.setVisibility(View.VISIBLE); + playerActionTitle.setVisibility(View.VISIBLE); + secondaryAction.setVisibility(View.VISIBLE); + break; + } + + if (sharedPreferenceManager.isFirstRun()) { + sharedPreferenceManager.firstRun(); + sharedPreferenceManager.defaultControls(); + Log.d("First", "Run"); + } + + joystickView.setOnMoveListener(new JoystickView.OnMoveListener() { + @Override + public void onMove(int angle, int strength) { + + if (((angle <= 30 && angle >= 0) || (angle >= 331 && angle <= 359)) && strength >= 85 && !curr.equals("rr")) { +// status.setText("Hard Right"); + new Thread(new UDPSender(sharedPreferenceManager.getRight())).start(); + curr = "rr"; + } else if (angle >= 31 && angle <= 60 && strength >= 85 && !curr.equals("rf")) { +// status.setText("Soft Right F"); + new Thread(new UDPSender(sharedPreferenceManager.getSoftRightF())).start(); + curr = "rf"; + } else if (angle >= 61 && angle <= 120 && strength >= 85 && !curr.equals("ff")) { +// status.setText("Forward"); + new Thread(new UDPSender(sharedPreferenceManager.getForward())).start(); + curr = "ff"; + } else if (angle >= 121 && angle <= 150 && strength >= 85 && !curr.equals("lf")) { +// status.setText("Soft Left F"); + new Thread(new UDPSender(sharedPreferenceManager.getSoftLeftF())).start(); + curr = "lf"; + } else if (angle >= 151 && angle <= 210 && strength >= 85 && !curr.equals("ll")) { +// status.setText("Hard Left"); + new Thread(new UDPSender(sharedPreferenceManager.getLeft())).start(); + curr = "ll"; + } else if (angle >= 211 && angle <= 240 && strength >= 85 && !curr.equals("lb")) { +// status.setText("Soft Left B"); + new Thread(new UDPSender(sharedPreferenceManager.getSoftLeftB())).start(); + curr = "lb"; + } else if (angle >= 241 && angle <= 300 && strength >= 85 && !curr.equals("bb")) { +// status.setText("Backward"); + new Thread(new UDPSender(sharedPreferenceManager.getBackward())).start(); + curr = "bb"; + } else if (angle >= 301 && angle <= 330 && strength >= 85 && !curr.equals("rb")) { +// status.setText("Soft Right B"); + new Thread(new UDPSender(sharedPreferenceManager.getSoftRightB())).start(); + curr = "rb"; + } else if (strength <= 85 && !curr.equals("reset")) { +// status.setText(""); + new Thread(new UDPSender(sharedPreferenceManager.getMotorReset())).start(); + curr = "reset"; + } + } + }); + } + + @Override + protected void onStop() { + + sharedPreferenceManager.closeIpSession(); + super.onStop(); + } + + @Override + public void onBackPressed() { + finish(); + } + + class UDPSender implements Runnable { + + int message; + + public UDPSender(int message) { + this.message = message; + + } + + @Override + public void run() { + try { + DatagramSocket udpsocket = new DatagramSocket(1111); + udpsocket.setReuseAddress(true); + InetAddress serverAddress = InetAddress.getByName(sharedPreferenceManager.getIp()); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + PrintStream pout = new PrintStream(byteArrayOutputStream); + pout.print(message); + byte[] buf = byteArrayOutputStream.toByteArray(); + DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddress, 1111); + udpsocket.send(packet); + udpsocket.close(); + } catch (SocketException e) { + Log.e("UDP", "Socket Error" + e); + } catch (IOException e) { + Log.e("UDP Send", "IO Error" + e); + } + } + } + +} diff --git a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/MainActivity.java b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/MainActivity.java index a961920..ec6cfdf 100644 --- a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/MainActivity.java +++ b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/MainActivity.java @@ -1,203 +1,117 @@ package in.ac.siesgst.npl.remotecontrolbot; + +import android.content.Context; import android.content.Intent; -import android.support.v7.app.AppCompatActivity; +import android.content.res.Resources; +import android.graphics.Color; import android.os.Bundle; -import android.support.v7.widget.Toolbar; -import android.util.Log; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.DisplayMetrics; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import android.widget.Button; -import android.widget.EditText; +import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.TextView; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.SocketException; - -import io.github.controlwear.virtual.joystick.android.JoystickView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.bumptech.glide.request.RequestOptions; +import com.google.gson.Gson; public class MainActivity extends AppCompatActivity { - - String ipAddress; - EditText ip; - Button submitButton; - String curr = ""; - TextView status; - JoystickView joystickLeft; - Toolbar toolbar; - - SharedPreferenceManager sharedPreferenceManager; - + public static final String ROBO_SOCCER = "Robo Soccer"; + public static final String SHELL_SHOCK = "Shell Shock"; + public static final String ROBO_WARS = "Robo Wars"; + private SharedPreferenceManager sharedPreferenceManager; + public static final Event[] events = new Event[]{ + new Event(ROBO_SOCCER, R.drawable.robosoccer, R.drawable.kick, R.drawable.robosoccer_joy), + new Event(SHELL_SHOCK, R.drawable.shellshock, R.drawable.bullet, R.drawable.shellshock_joy), + new Event(ROBO_WARS, R.drawable.robowars, R.drawable.bullet, R.drawable.robowars_joy) + }; + private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - - sharedPreferenceManager = new SharedPreferenceManager(MainActivity.this); - - toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - getSupportActionBar().setTitle(""); - - joystickLeft = (JoystickView) findViewById(R.id.joystickView_left); - status = findViewById(R.id.currentStatus); - - ip = findViewById(R.id.ip); - submitButton = findViewById(R.id.submitButton); + recyclerView = findViewById(R.id.robo_events_list); + recyclerView.setAdapter(new EventAdapter()); + recyclerView.setHasFixedSize(true); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + sharedPreferenceManager = new SharedPreferenceManager(this); + getWindow().setStatusBarColor(Color.BLACK); + } - if(sharedPreferenceManager.isFirstRun()) { - sharedPreferenceManager.firstRun(); - sharedPreferenceManager.defaultControls(); - Log.d("First","Run"); + class EventAdapter extends RecyclerView.Adapter { + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new ViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.event_card, parent, false)); } - if (sharedPreferenceManager.isIpThere()) { - submitButton.setVisibility(View.GONE); - ip.setVisibility(View.GONE); - - ip.setEnabled(false); - submitButton.setEnabled(false); - - status.setVisibility(View.VISIBLE); - joystickLeft.setVisibility(View.VISIBLE); + @Override + public void onBindViewHolder(ViewHolder holder, final int position) { + holder.eventName.setText(events[position].getName()); + Glide.with(MainActivity.this) + .load(events[position].getBackgroundDrawable()) + .into(holder.eventBg); + holder.bindViewHolder(); } - submitButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - ipAddress = ip.getText().toString(); - submitButton.setVisibility(View.GONE); - ip.setVisibility(View.GONE); - - sharedPreferenceManager.createIpSession(ipAddress); - ip.setEnabled(false); - submitButton.setEnabled(false); + @Override + public int getItemCount() { + return events.length; + } + class ViewHolder extends RecyclerView.ViewHolder { - status.setVisibility(View.VISIBLE); - joystickLeft.setVisibility(View.VISIBLE); + TextView eventName; + ImageView eventBg; + public ViewHolder(View itemView) { + super(itemView); + eventName = itemView.findViewById(R.id.event_title); + eventBg = itemView.findViewById(R.id.event_bg); } - }); - joystickLeft.setOnMoveListener(new JoystickView.OnMoveListener() { - @Override - public void onMove(int angle, int strength) { - - if(((angle<=30 && angle>=0)||(angle>=331 && angle<=359)) && strength>=85 && !curr.equals("rr")) { - status.setText("Hard Right"); - new Thread(new UDPSender(sharedPreferenceManager.getRight())).start(); - curr = "rr"; - } - else if(angle>=31 && angle<=60 && strength>=85 && !curr.equals("rf")) { - status.setText("Soft Right F"); - new Thread(new UDPSender(sharedPreferenceManager.getSoftRightF())).start(); - curr = "rf"; - } - else if(angle>=61 && angle<=120 && strength>=85 && !curr.equals("ff")) { - status.setText("Forward"); - new Thread(new UDPSender(sharedPreferenceManager.getForward())).start(); - curr = "ff"; - } - else if(angle>=121 && angle<=150 && strength>=85 && !curr.equals("lf")) { - status.setText("Soft Left F"); - new Thread(new UDPSender(sharedPreferenceManager.getSoftLeftF())).start(); - curr = "lf"; - } - else if(angle>=151 && angle<=210 && strength>=85 && !curr.equals("ll")) { - status.setText("Hard Left"); - new Thread(new UDPSender(sharedPreferenceManager.getLeft())).start(); - curr = "ll"; - } - else if(angle>=211 && angle<=240 && strength>=85 && !curr.equals("lb")) { - status.setText("Soft Left B"); - new Thread(new UDPSender(sharedPreferenceManager.getSoftLeftB())).start(); - curr = "lb"; - } - else if(angle>=241 && angle<=300 && strength>=85 && !curr.equals("bb")) { - status.setText("Backward"); - new Thread(new UDPSender(sharedPreferenceManager.getBackward())).start(); - curr = "bb"; - } - else if(angle>=301 && angle<=330 && strength>=85 && !curr.equals("rb")) { - status.setText("Soft Right B"); - new Thread(new UDPSender(sharedPreferenceManager.getSoftRightB())).start(); - curr = "rb"; - } - else if(strength<=85 && !curr.equals("reset")) { - status.setText(""); - new Thread(new UDPSender(sharedPreferenceManager.getMotorReset())).start(); - curr = "reset"; - } - } - }); - } - class UDPSender implements Runnable{ - - int message; - public UDPSender(int message) { - this.message = message; - - } - - @Override - public void run() { - try{ - DatagramSocket udpsocket = new DatagramSocket(1111); - udpsocket.setReuseAddress(true); - InetAddress serverAddress = InetAddress.getByName(sharedPreferenceManager.getIp()); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - PrintStream pout = new PrintStream(byteArrayOutputStream); - pout.print(message); - byte[] buf = byteArrayOutputStream.toByteArray(); - DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddress, 1111); - udpsocket.send(packet); - udpsocket.close(); - } catch(SocketException e){ - Log.e("UDP", "Socket Error"+e); - } catch(IOException e){ - Log.e("UDP Send", "IO Error"+e); + void bindViewHolder() { + itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent joyIntent = new Intent(MainActivity.this, JoystickActivity.class); + joyIntent.putExtra("event", new Gson().toJson(events[getAdapterPosition()])); + startActivity(joyIntent); + } + }); } } } + + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater mMenuInflater = getMenuInflater(); - mMenuInflater.inflate(R.menu.mymenu,menu); + mMenuInflater.inflate(R.menu.mymenu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { - if(item.getItemId() == R.id.settings) { - Intent i = new Intent(MainActivity.this,SettingsActivity.class); + if (item.getItemId() == R.id.settings) { + Intent i = new Intent(MainActivity.this, SettingsActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - i.putExtra("ip_set",sharedPreferenceManager.isIpThere()); - i.putExtra("ip",sharedPreferenceManager.getIp()); + i.putExtra("ip_set", sharedPreferenceManager.isIpThere()); + i.putExtra("ip", sharedPreferenceManager.getIp()); startActivity(i); } return super.onOptionsItemSelected(item); } - @Override - protected void onStop() { - - sharedPreferenceManager.closeIpSession(); - super.onStop(); - } - - @Override - public void onBackPressed() { - finish(); - } } diff --git a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SettingsActivity.java b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SettingsActivity.java index b3e2fca..6379898 100644 --- a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SettingsActivity.java +++ b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SettingsActivity.java @@ -1,8 +1,8 @@ package in.ac.siesgst.npl.remotecontrolbot; import android.content.Intent; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; @@ -16,7 +16,7 @@ public class SettingsActivity extends AppCompatActivity { Toolbar toolbar; Button submitBtn; - EditText rr,fr,ff,fl,ll,bl,bb,br,resetMotor; + EditText rr, fr, ff, fl, ll, bl, bb, br, resetMotor; SharedPreferenceManager sharedPreferenceManager; @Override @@ -50,8 +50,8 @@ protected void onCreate(Bundle savedInstanceState) { resetMotor.setText(String.valueOf(sharedPreferenceManager.getMotorReset())); - if(getIntent().getBooleanExtra("ip_set",false)) { - Log.d("IP",getIntent().getStringExtra("ip")); + if (getIntent().getBooleanExtra("ip_set", false)) { + Log.d("IP", getIntent().getStringExtra("ip")); sharedPreferenceManager.createIpSession(getIntent().getStringExtra("ip")); } @@ -61,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) { public void onClick(View v) { sharedPreferenceManager.changeAllControls(Integer.parseInt(rr.getText().toString()), Integer.parseInt(fr.getText().toString()), Integer.parseInt(ff.getText().toString()), Integer.parseInt(fl.getText().toString()), Integer.parseInt(ll.getText().toString()), Integer.parseInt(bl.getText().toString()), Integer.parseInt(bb.getText().toString()), Integer.parseInt(br.getText().toString()), Integer.parseInt(resetMotor.getText().toString())); - Intent i = new Intent(SettingsActivity.this,MainActivity.class); + Intent i = new Intent(SettingsActivity.this, JoystickActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); finish(); } @@ -77,14 +77,14 @@ public void onClick(View v) { @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater mMenuInflater = getMenuInflater(); - mMenuInflater.inflate(R.menu.menu_settings,menu); + mMenuInflater.inflate(R.menu.menu_settings, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { - if(item.getItemId() == R.id.homeIcon) { - Intent i = new Intent(SettingsActivity.this,MainActivity.class); + if (item.getItemId() == R.id.homeIcon) { + Intent i = new Intent(SettingsActivity.this, JoystickActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); finish(); diff --git a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SharedPreferenceManager.java b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SharedPreferenceManager.java index 6e2590c..4389e59 100644 --- a/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SharedPreferenceManager.java +++ b/app/src/main/java/in/ac/siesgst/npl/remotecontrolbot/SharedPreferenceManager.java @@ -8,88 +8,106 @@ */ public class SharedPreferenceManager { - SharedPreferences sharedPreferences; - SharedPreferences.Editor editor; - Context context; public static final String PREF_NAME = "ROBO_SOCCER"; public static final int PRIVATE_MODE = 0; public static final String LOG_TAG = SharedPreferenceManager.class.getSimpleName(); + SharedPreferences sharedPreferences; + SharedPreferences.Editor editor; + Context context; + public SharedPreferenceManager(Context context) { this.context = context; - sharedPreferences = context.getSharedPreferences(PREF_NAME,PRIVATE_MODE); + sharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = sharedPreferences.edit(); } + public void createIpSession(String ipAddr) { - editor.putString("IP",ipAddr); - editor.putBoolean("IP_SET",true); + editor.putString("IP", ipAddr); + editor.putBoolean("IP_SET", true); editor.apply(); } + public void closeIpSession() { - editor.putBoolean("IP_SET",false); + editor.putBoolean("IP_SET", false); editor.apply(); } + public void defaultControls() { - editor.putInt("rr",1); - editor.putInt("fr",2); - editor.putInt("ff",3); - editor.putInt("fl",4); - editor.putInt("ll",5); - editor.putInt("bl",6); - editor.putInt("bb",7); - editor.putInt("br",8); - editor.putInt("resetmotor",9); + editor.putInt("rr", 1); + editor.putInt("fr", 2); + editor.putInt("ff", 3); + editor.putInt("fl", 4); + editor.putInt("ll", 5); + editor.putInt("bl", 6); + editor.putInt("bb", 7); + editor.putInt("br", 8); + editor.putInt("resetmotor", 9); editor.apply(); } + public void firstRun() { - editor.putBoolean("firstrun",false); + editor.putBoolean("firstrun", false); editor.apply(); } + public boolean isFirstRun() { - return sharedPreferences.getBoolean("firstrun",true); + return sharedPreferences.getBoolean("firstrun", true); } + public String getIp() { - return sharedPreferences.getString("IP","IP_ADDRESS"); - } - public void changeAllControls(int rr,int fr,int ff, int fl, int ll, int bl, int bb, int br, int resetmotor) { - editor.putInt("rr",rr); - editor.putInt("fr",fr); - editor.putInt("ff",ff); - editor.putInt("fl",fl); - editor.putInt("ll",ll); - editor.putInt("bl",bl); - editor.putInt("bb",bb); - editor.putInt("br",br); - editor.putInt("resetmotor",resetmotor); + return sharedPreferences.getString("IP", "IP_ADDRESS"); + } + + public void changeAllControls(int rr, int fr, int ff, int fl, int ll, int bl, int bb, int br, int resetmotor) { + editor.putInt("rr", rr); + editor.putInt("fr", fr); + editor.putInt("ff", ff); + editor.putInt("fl", fl); + editor.putInt("ll", ll); + editor.putInt("bl", bl); + editor.putInt("bb", bb); + editor.putInt("br", br); + editor.putInt("resetmotor", resetmotor); editor.apply(); } + boolean isIpThere() { - return sharedPreferences.getBoolean("IP_SET",false); + return sharedPreferences.getBoolean("IP_SET", false); } + int getForward() { - return sharedPreferences.getInt("ff",0); + return sharedPreferences.getInt("ff", 0); } + int getBackward() { - return sharedPreferences.getInt("bb",0); + return sharedPreferences.getInt("bb", 0); } + int getLeft() { - return sharedPreferences.getInt("ll",0); + return sharedPreferences.getInt("ll", 0); } + int getRight() { - return sharedPreferences.getInt("rr",0); + return sharedPreferences.getInt("rr", 0); } + int getSoftLeftB() { - return sharedPreferences.getInt("bl",0); + return sharedPreferences.getInt("bl", 0); } + int getSoftRightB() { - return sharedPreferences.getInt("br",0); + return sharedPreferences.getInt("br", 0); } + int getSoftLeftF() { - return sharedPreferences.getInt("fl",0); + return sharedPreferences.getInt("fl", 0); } + int getSoftRightF() { - return sharedPreferences.getInt("fr",0); + return sharedPreferences.getInt("fr", 0); } + int getMotorReset() { - return sharedPreferences.getInt("resetmotor",0); + return sharedPreferences.getInt("resetmotor", 0); } } diff --git a/app/src/main/res/drawable-v24/ic_home_black_24dp.xml b/app/src/main/res/drawable-v24/ic_home_black_24dp.xml index 5fb51c1..6b449dc 100644 --- a/app/src/main/res/drawable-v24/ic_home_black_24dp.xml +++ b/app/src/main/res/drawable-v24/ic_home_black_24dp.xml @@ -1,9 +1,9 @@ + android:width="24dp" + android:height="24dp" + android:viewportHeight="24.0" + android:viewportWidth="24.0"> + android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" /> diff --git a/app/src/main/res/drawable-v24/ic_settings_black_24dp.xml b/app/src/main/res/drawable-v24/ic_settings_black_24dp.xml index b40b68f..94d131a 100644 --- a/app/src/main/res/drawable-v24/ic_settings_black_24dp.xml +++ b/app/src/main/res/drawable-v24/ic_settings_black_24dp.xml @@ -1,9 +1,9 @@ + android:width="24dp" + android:height="24dp" + android:viewportHeight="24.0" + android:viewportWidth="24.0"> + android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z" /> diff --git a/app/src/main/res/drawable/app_icon.png b/app/src/main/res/drawable/app_icon.png new file mode 100644 index 0000000..ec3f21f Binary files /dev/null and b/app/src/main/res/drawable/app_icon.png differ diff --git a/app/src/main/res/drawable/bullet.png b/app/src/main/res/drawable/bullet.png new file mode 100644 index 0000000..a2723ad Binary files /dev/null and b/app/src/main/res/drawable/bullet.png differ diff --git a/app/src/main/res/drawable/circle.xml b/app/src/main/res/drawable/circle.xml new file mode 100644 index 0000000..6db94e4 --- /dev/null +++ b/app/src/main/res/drawable/circle.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/fire_button.xml b/app/src/main/res/drawable/fire_button.xml new file mode 100644 index 0000000..1c281ec --- /dev/null +++ b/app/src/main/res/drawable/fire_button.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_edit_black_24dp.xml b/app/src/main/res/drawable/ic_edit_black_24dp.xml new file mode 100644 index 0000000..5d69ed6 --- /dev/null +++ b/app/src/main/res/drawable/ic_edit_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_swap_horiz.xml b/app/src/main/res/drawable/ic_swap_horiz.xml new file mode 100644 index 0000000..52ecc59 --- /dev/null +++ b/app/src/main/res/drawable/ic_swap_horiz.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/kick.png b/app/src/main/res/drawable/kick.png new file mode 100644 index 0000000..704d30b Binary files /dev/null and b/app/src/main/res/drawable/kick.png differ diff --git a/app/src/main/res/drawable/rekt.xml b/app/src/main/res/drawable/rekt.xml new file mode 100644 index 0000000..1c281ec --- /dev/null +++ b/app/src/main/res/drawable/rekt.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/robosoccer.jpg b/app/src/main/res/drawable/robosoccer.jpg new file mode 100644 index 0000000..5941ca5 Binary files /dev/null and b/app/src/main/res/drawable/robosoccer.jpg differ diff --git a/app/src/main/res/drawable/robosoccer_joy.png b/app/src/main/res/drawable/robosoccer_joy.png new file mode 100644 index 0000000..476c311 Binary files /dev/null and b/app/src/main/res/drawable/robosoccer_joy.png differ diff --git a/app/src/main/res/drawable/robowars.png b/app/src/main/res/drawable/robowars.png new file mode 100644 index 0000000..dfb95e5 Binary files /dev/null and b/app/src/main/res/drawable/robowars.png differ diff --git a/app/src/main/res/drawable/robowars_joy.png b/app/src/main/res/drawable/robowars_joy.png new file mode 100644 index 0000000..0e72b95 Binary files /dev/null and b/app/src/main/res/drawable/robowars_joy.png differ diff --git a/app/src/main/res/drawable/shellshock.png b/app/src/main/res/drawable/shellshock.png new file mode 100644 index 0000000..637d082 Binary files /dev/null and b/app/src/main/res/drawable/shellshock.png differ diff --git a/app/src/main/res/drawable/shellshock_joy.png b/app/src/main/res/drawable/shellshock_joy.png new file mode 100644 index 0000000..2547d44 Binary files /dev/null and b/app/src/main/res/drawable/shellshock_joy.png differ diff --git a/app/src/main/res/layout/activity_joystick.xml b/app/src/main/res/layout/activity_joystick.xml new file mode 100644 index 0000000..4dabf66 --- /dev/null +++ b/app/src/main/res/layout/activity_joystick.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 79ee63b..589f0b2 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,54 +1,20 @@ - + tools:context=".MainActivity"> - + - + app:layout_constraintTop_toBottomOf="@id/main_toolbar" + tools:listitem="@layout/event_card" /> -