From d004a44054726f801042a9c093a8cdee04732fcd Mon Sep 17 00:00:00 2001 From: Pascal Date: Sat, 16 Feb 2019 11:49:58 +0100 Subject: [PATCH] AndroidX Studio3.4 API19-28 SpeedGauge --- .travis.yml | 4 +- app/build.gradle | 23 +- app/libs/README.md | 4 +- app/src/main/AndroidManifest.xml | 6 +- .../app/BalancingRobotFullSizeActivity.java | 91 ++++---- .../app/BluetoothProtocol.java | 109 ++++----- .../app/DeviceListActivity.java | 55 ++--- .../app/GraphFragment.java | 118 ++++------ .../app/InfoFragment.java | 30 ++- .../app/MapFragment.java | 54 ++--- .../app/PIDFragment.java | 217 +++++++----------- .../app/ViewPagerAdapter.java | 6 +- .../activity_balancing_robot_full_size.xml | 10 +- app/src/main/res/layout/device_list.xml | 4 +- app/src/main/res/layout/info.xml | 2 +- app/src/main/res/layout/pid.xml | 2 +- app/src/main/res/values/strings.xml | 2 + app/src/main/res/values/styles.xml | 4 +- build.gradle | 4 +- gradle.properties | 2 + gradle/wrapper/gradle-wrapper.properties | 4 +- 21 files changed, 309 insertions(+), 442 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5af89b2..20246a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,10 +20,10 @@ android: - tools # The BuildTools version used by your project - - build-tools-25.0.3 + - build-tools-28.0.3 # The SDK version used to compile your project - - android-25 + - android-28 # Additional components - extra-android-m2repository diff --git a/app/build.gradle b/app/build.gradle index a02ecf9..c649a77 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion '25.0.3' + compileSdkVersion 28 + buildToolsVersion '28.0.3' defaultConfig { applicationId 'com.lauszus.balancingrobotfullsizeandroid.app' - minSdkVersion 14 - targetSdkVersion 25 + minSdkVersion 19 + targetSdkVersion 28 versionCode 2 versionName '1.0.1' } @@ -27,6 +27,10 @@ android { lintOptions { abortOnError false } + compileOptions { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' + } } repositories { @@ -36,11 +40,14 @@ repositories { } dependencies { - compile 'com.android.support:design:25.3.1' - compile 'com.google.android.gms:play-services-maps:11.0.4' - compile 'com.jjoe64:graphview:3.1.4' + implementation 'com.google.android.material:material:1.0.0' + implementation 'com.google.android.gms:play-services-maps:16.1.0' + // TODO Upgrade to 4. + implementation 'com.jjoe64:graphview:3.1.4' // TODO: Use Maven repo instead - compile(name: 'SpeedometerView-1.0.1', ext: 'aar') + implementation 'com.cardiomood.android:android-widgets:0.1.1' + //implementation 'com.cardiomood.android.speedometer:speedometer:1.0.1' + //implementation (name: 'SpeedometerView-1.0.1', ext: 'aar') } // These values are all set in my global gradle.properties file diff --git a/app/libs/README.md b/app/libs/README.md index 3cd9fbb..1a51c8c 100644 --- a/app/libs/README.md +++ b/app/libs/README.md @@ -1,4 +1,6 @@ Libraries used that are not located at Maven Central are located here. ### SpeedometerView -* Source: \ No newline at end of file +* Source: + +PP voir compile 'com.cardiomood.android:android-widgets:0.1.1' \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index da7e185..823a477 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -26,7 +27,8 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" - android:theme="@style/AppTheme" > + android:theme="@style/AppTheme" + tools:ignore="GoogleAppIndexingWarning"> @@ -36,7 +38,7 @@ android:name=".BalancingRobotFullSizeActivity" android:label="@string/app_name" android:launchMode="singleTask" - android:theme="@style/AppTheme.NoActionBar" > + android:theme="@style/AppTheme.NoActionBar" android:screenOrientation="portrait" > diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BalancingRobotFullSizeActivity.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BalancingRobotFullSizeActivity.java index 452faf1..9095927 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BalancingRobotFullSizeActivity.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BalancingRobotFullSizeActivity.java @@ -1,4 +1,4 @@ -/************************************************************************************* +/* ************************************************************************************ * Copyright (C) 2014 Kristian Sloth Lauszus. All rights reserved. * * This software may be distributed and modified under the terms of the GNU @@ -19,29 +19,23 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.annotation.TargetApi; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothManager; -import android.content.Context; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.view.ViewPager; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.view.inputmethod.InputMethodManager; -import android.widget.Toast; - -import java.lang.ref.WeakReference; +import android.annotation.*; +import android.bluetooth.*; +import android.content.*; +import android.os.*; +import android.util.*; +import android.view.*; +import android.view.inputmethod.*; +import android.widget.*; + +import androidx.appcompat.app.*; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.*; +import androidx.viewpager.widget.*; + +import com.google.android.material.tabs.*; + +import java.lang.ref.*; public class BalancingRobotFullSizeActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener { private static final String TAG = "BalancingRobotFullSize"; @@ -178,10 +172,8 @@ public void onStart() { @Override public void onBackPressed() { if (mChatService != null) { - new Handler().postDelayed(new Runnable() { - public void run() { - mChatService.stop(); // Stop the Bluetooth chat services if the user exits the app - } + new Handler().postDelayed(() -> { + mChatService.stop(); // Stop the Bluetooth chat services if the user exits the app }, 1000); // Wait 1 second before closing the connection, this is needed as onPause() will send stop messages before closing } finish(); // Exits the app @@ -305,6 +297,7 @@ public boolean checkTab(int tab) { return (currentTabSelected == tab); } + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (D) Log.d(TAG, "onActivityResult " + resultCode); @@ -381,36 +374,30 @@ public void handleMessage(Message msg) { if (mBalancingRobotFullSizeActivity.mChatService == null) return; Handler mHandler = new Handler(); - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity = mActivity.get(); - if (mBalancingRobotFullSizeActivity != null) { - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.getPID(); - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.getTarget(); - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.getTurning(); - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.getKalman(); - } + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity13 = mActivity.get(); + if (mBalancingRobotFullSizeActivity13 != null) { + mBalancingRobotFullSizeActivity13.mChatService.mBluetoothProtocol.getPID(); + mBalancingRobotFullSizeActivity13.mChatService.mBluetoothProtocol.getTarget(); + mBalancingRobotFullSizeActivity13.mChatService.mBluetoothProtocol.getTurning(); + mBalancingRobotFullSizeActivity13.mChatService.mBluetoothProtocol.getKalman(); } }, 1000); // Wait 1 second before sending the message if (mBalancingRobotFullSizeActivity.checkTab(ViewPagerAdapter.INFO_FRAGMENT)) { - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity = mActivity.get(); - if (mBalancingRobotFullSizeActivity != null) - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.startInfo(); // Request info - } + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity12 = mActivity.get(); + if (mBalancingRobotFullSizeActivity12 != null) + mBalancingRobotFullSizeActivity12.mChatService.mBluetoothProtocol.startInfo(); // Request info }, 2000); // Wait 2 seconds before sending the message } else if (mBalancingRobotFullSizeActivity.checkTab(ViewPagerAdapter.GRAPH_FRAGMENT)) { - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity = mActivity.get(); - if (mBalancingRobotFullSizeActivity != null) { - if (GraphFragment.mToggleButton.isChecked()) - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.startImu(); // Request data - else - mBalancingRobotFullSizeActivity.mChatService.mBluetoothProtocol.stopImu(); // Stop sending data - } + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity mBalancingRobotFullSizeActivity1 = mActivity.get(); + if (mBalancingRobotFullSizeActivity1 != null) { + if (GraphFragment.mToggleButton.isChecked()) + mBalancingRobotFullSizeActivity1.mChatService.mBluetoothProtocol.startImu(); // Request data + else + mBalancingRobotFullSizeActivity1.mChatService.mBluetoothProtocol.stopImu(); // Stop sending data } }, 2000); // Wait 2 seconds before sending the message } diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BluetoothProtocol.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BluetoothProtocol.java index f0b37a3..b07ba0e 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BluetoothProtocol.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/BluetoothProtocol.java @@ -60,7 +60,7 @@ public BluetoothProtocol(BluetoothChatService mChatService, Handler handler) { } - private void sendCommand(byte output[]) { + private void sendCommand(byte[] output) { mChatService.write(commandHeader); mChatService.write(output); mChatService.write(getChecksum(output)); @@ -76,25 +76,17 @@ public void setPID(int Kp, int Ki, int Kd) { if (D) Log.i(TAG, "setPID " + Kp + " " + Ki + " " + Kd); - byte output[] = { - SET_PID, // Cmd - 6, // Length - (byte)(Kp & 0xFF), - (byte)(Kp >> 8), - (byte)(Ki & 0xFF), - (byte)(Ki >> 8), - (byte)(Kd & 0xFF), - (byte)(Kd >> 8), - }; + byte[] output = {SET_PID, // Cmd + 6, // Length + (byte) (Kp & 0xFF), (byte) (Kp >> 8), (byte) (Ki & 0xFF), (byte) (Ki >> 8), (byte) (Kd & 0xFF), (byte) (Kd >> 8),}; sendCommand(output); // Set PID values } /** Use this to request PID values. */ public void getPID() { - byte output[] = { - GET_PID, // Cmd - 0, // Length - }; + byte[] output = {GET_PID, // Cmd + 0, // Length + }; sendCommand(output); // Send output } @@ -106,37 +98,30 @@ public void setTarget(int targetAngle) { if (D) Log.i(TAG, "setTarget: " + targetAngle); - byte output[] = { - SET_TARGET, // Cmd - 2, // Length - (byte)(targetAngle & 0xFF), - (byte)(targetAngle >> 8), - }; + byte[] output = {SET_TARGET, // Cmd + 2, // Length + (byte) (targetAngle & 0xFF), (byte) (targetAngle >> 8),}; sendCommand(output); // Set PID values } public void getTarget() { - byte output[] = { - GET_TARGET, // Cmd - 0, // Length - }; + byte[] output = {GET_TARGET, // Cmd + 0, // Length + }; sendCommand(output); // Send output } public void setTurning(byte turningValue) { - byte output[] = { - SET_TURNING, // Cmd - 1, // Length - turningValue, - }; + byte[] output = {SET_TURNING, // Cmd + 1, // Length + turningValue,}; sendCommand(output); // Set PID values } public void getTurning() { - byte output[] = { - GET_TURNING, // Cmd - 0, // Length - }; + byte[] output = {GET_TURNING, // Cmd + 0, // Length + }; sendCommand(output); // Send output } @@ -149,56 +134,44 @@ public void getTurning() { */ public void setKalman(int Qangle, int Qbias, int Rmeasure) { - byte output[] = { - SET_KALMAN, // Cmd - 6, // Length - (byte)(Qangle & 0xFF), - (byte)(Qangle >> 8), - (byte)(Qbias & 0xFF), - (byte)(Qbias >> 8), - (byte)(Rmeasure & 0xFF), - (byte)(Rmeasure >> 8), - }; + byte[] output = {SET_KALMAN, // Cmd + 6, // Length + (byte) (Qangle & 0xFF), (byte) (Qangle >> 8), (byte) (Qbias & 0xFF), (byte) (Qbias >> 8), (byte) (Rmeasure & 0xFF), (byte) (Rmeasure >> 8),}; sendCommand(output); // Set PID values } public void getKalman() { - byte output[] = { - GET_KALMAN, // Cmd - 0, // Length - }; + byte[] output = {GET_KALMAN, // Cmd + 0, // Length + }; sendCommand(output); // Send output } public void startInfo() { - byte output[] = { - START_INFO, // Cmd - 0, // Length - }; + byte[] output = {START_INFO, // Cmd + 0, // Length + }; sendCommand(output); // Send output } public void stopInfo() { - byte output[] = { - STOP_INFO, // Cmd - 0, // Length - }; + byte[] output = {STOP_INFO, // Cmd + 0, // Length + }; sendCommand(output); // Send output } public void startImu() { - byte output[] = { - START_IMU, // Cmd - 0, // Length - }; + byte[] output = {START_IMU, // Cmd + 0, // Length + }; sendCommand(output); // Send output } public void stopImu() { - byte output[] = { - STOP_IMU, // Cmd - 0, // Length - }; + byte[] output = {STOP_IMU, // Cmd + 0, // Length + }; sendCommand(output); // Send output } /* @@ -213,7 +186,7 @@ private byte[] concat(byte[] A, byte[] B) { // Source: http://stackoverflow.com/ */ private byte[] buffer = new byte[1024]; - public void parseData(byte msg[], int offset, int length) { + public void parseData(byte[] msg, int offset, int length) { System.arraycopy(msg, offset, buffer, 0, length); String readMessage = new String(buffer, 0, length); @@ -244,7 +217,7 @@ public void parseData(byte msg[], int offset, int length) { return; } - int input[] = new int[msgLength]; + int[] input = new int[msgLength]; int i; for (i = 0; i < msgLength; i++) input[i] = data[i + responseHeader.length() + 2]; @@ -358,14 +331,14 @@ public void parseData(byte msg[], int offset, int length) { } // TODO: Combine these two - private byte getChecksum(byte data[]) { + private byte getChecksum(byte[] data) { byte checksum = 0; for (byte val : data) checksum ^= val; return checksum; } - private int getChecksum(int data[]) { + private int getChecksum(int[] data) { int checksum = 0; for (int val : data) checksum ^= val; diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/DeviceListActivity.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/DeviceListActivity.java index b309892..5d41882 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/DeviceListActivity.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/DeviceListActivity.java @@ -16,29 +16,18 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.annotation.TargetApi; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Build; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.ListView; -import android.widget.ProgressBar; -import android.widget.TextView; - -import java.util.Set; +import android.bluetooth.*; +import android.content.*; +import android.os.*; +import android.util.*; +import android.view.*; +import android.widget.*; +import android.widget.AdapterView.*; + +import androidx.appcompat.app.*; +import androidx.appcompat.widget.Toolbar; + +import java.util.*; /** * This Activity appears as a dialog. It lists any paired devices and devices @@ -62,7 +51,6 @@ public class DeviceListActivity extends AppCompatActivity { private ProgressBar mProgressBar; @Override - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.device_list); @@ -79,14 +67,12 @@ protected void onCreate(Bundle savedInstanceState) { // Initialize the button to perform device discovery Button scanButton = (Button) findViewById(R.id.button_scan); - scanButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - if (!BalancingRobotFullSizeActivity.isEmulator()) - doDiscovery(); - else - mProgressBar.setVisibility(View.VISIBLE); // Just show the progress bar in the emulator - v.setVisibility(View.GONE); - } + scanButton.setOnClickListener(v -> { + if (!BalancingRobotFullSizeActivity.isEmulator()) + doDiscovery(); + else + mProgressBar.setVisibility(View.VISIBLE); // Just show the progress bar in the emulator + v.setVisibility(View.GONE); }); // Initialize array adapters. One for already paired devices and @@ -113,10 +99,7 @@ public void onClick(View v) { this.registerReceiver(mReceiver, filter); // Get the local Bluetooth adapter - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) - mBtAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); - else - mBtAdapter = BluetoothAdapter.getDefaultAdapter(); + mBtAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); // Get a set of currently paired devices Set pairedDevices = null; diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/GraphFragment.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/GraphFragment.java index ed2e855..327bcbc 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/GraphFragment.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/GraphFragment.java @@ -19,26 +19,17 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.graphics.Color; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.ToggleButton; - -import com.jjoe64.graphview.GraphView.GraphViewData; -import com.jjoe64.graphview.GraphView.LegendAlign; -import com.jjoe64.graphview.GraphViewSeries; -import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle; -import com.jjoe64.graphview.GraphViewStyle; -import com.jjoe64.graphview.LineGraphView; +import android.graphics.*; +import android.os.*; +import android.util.*; +import android.view.*; +import android.widget.*; + +import androidx.fragment.app.*; + +import com.jjoe64.graphview.GraphView.*; +import com.jjoe64.graphview.*; +import com.jjoe64.graphview.GraphViewSeries.*; // TODO: Remove static public class GraphFragment extends Fragment { @@ -122,53 +113,41 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa layout.addView(graphView); mCheckBox1 = (CheckBox) v.findViewById(R.id.checkBox1); - mCheckBox1.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (((CheckBox) v).isChecked()) - graphView.addSeries(accSeries); - else - graphView.removeSeries(accSeries); - } + mCheckBox1.setOnClickListener(v15 -> { + if (((CheckBox) v15).isChecked()) + graphView.addSeries(accSeries); + else + graphView.removeSeries(accSeries); }); mCheckBox2 = (CheckBox) v.findViewById(R.id.checkBox2); - mCheckBox2.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (((CheckBox) v).isChecked()) - graphView.addSeries(gyroSeries); - else - graphView.removeSeries(gyroSeries); - } + mCheckBox2.setOnClickListener(v14 -> { + if (((CheckBox) v14).isChecked()) + graphView.addSeries(gyroSeries); + else + graphView.removeSeries(gyroSeries); }); mCheckBox3 = (CheckBox) v.findViewById(R.id.checkBox3); - mCheckBox3.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (((CheckBox) v).isChecked()) - graphView.addSeries(kalmanSeries); - else - graphView.removeSeries(kalmanSeries); - } + mCheckBox3.setOnClickListener(v13 -> { + if (((CheckBox) v13).isChecked()) + graphView.addSeries(kalmanSeries); + else + graphView.removeSeries(kalmanSeries); }); mToggleButton = (ToggleButton) v.findViewById(R.id.toggleButton); - mToggleButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (((ToggleButton) v).isChecked()) - mToggleButton.setText("Stop"); - else - mToggleButton.setText("Start"); - - BalancingRobotFullSizeActivity activity = ((BalancingRobotFullSizeActivity) getActivity()); - if (activity != null && activity.mChatService != null) { - if (activity.mChatService.getState() == BluetoothChatService.STATE_CONNECTED && activity.checkTab(ViewPagerAdapter.GRAPH_FRAGMENT)) { - if (((ToggleButton) v).isChecked()) - activity.mChatService.mBluetoothProtocol.startImu(); // Request data - else - activity.mChatService.mBluetoothProtocol.stopImu(); // Stop sending data - } + mToggleButton.setOnClickListener(v12 -> { + if (((ToggleButton) v12).isChecked()) + mToggleButton.setText("Stop"); + else + mToggleButton.setText("Start"); + + BalancingRobotFullSizeActivity activity = ((BalancingRobotFullSizeActivity) getActivity()); + if (activity != null && activity.mChatService != null) { + if (activity.mChatService.getState() == BluetoothChatService.STATE_CONNECTED && activity.checkTab(ViewPagerAdapter.GRAPH_FRAGMENT)) { + if (((ToggleButton) v12).isChecked()) + activity.mChatService.mBluetoothProtocol.startImu(); // Request data + else + activity.mChatService.mBluetoothProtocol.stopImu(); // Stop sending data } } }); @@ -177,18 +156,15 @@ public void onClick(View v) { mQbias = (EditText) v.findViewById(R.id.editText2); mRmeasure = (EditText) v.findViewById(R.id.editText3); Button mButton = (Button) v.findViewById(R.id.updateButton); - mButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - BalancingRobotFullSizeActivity activity = ((BalancingRobotFullSizeActivity) getActivity()); - if (activity == null || activity.mChatService == null) { - if (D) - Log.e(TAG, "mChatService == null"); - return; - } - if (!mQangle.getText().toString().isEmpty() && !mQbias.getText().toString().isEmpty() && !mRmeasure.getText().toString().isEmpty()) - activity.mChatService.mBluetoothProtocol.setKalman((int) (Float.parseFloat(mQangle.getText().toString()) * 10000.0f), (int) (Float.parseFloat(mQbias.getText().toString()) * 10000.0f), (int) (Float.parseFloat(mRmeasure.getText().toString()) * 10000.0f)); + mButton.setOnClickListener(v1 -> { + BalancingRobotFullSizeActivity activity = ((BalancingRobotFullSizeActivity) getActivity()); + if (activity == null || activity.mChatService == null) { + if (D) + Log.e(TAG, "mChatService == null"); + return; } + if (!mQangle.getText().toString().isEmpty() && !mQbias.getText().toString().isEmpty() && !mRmeasure.getText().toString().isEmpty()) + activity.mChatService.mBluetoothProtocol.setKalman((int) (Float.parseFloat(mQangle.getText().toString()) * 10000.0f), (int) (Float.parseFloat(mQbias.getText().toString()) * 10000.0f), (int) (Float.parseFloat(mRmeasure.getText().toString()) * 10000.0f)); }); return v; diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/InfoFragment.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/InfoFragment.java index 5872767..32486d9 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/InfoFragment.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/InfoFragment.java @@ -19,20 +19,21 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.graphics.Color; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; +import android.graphics.*; +import android.os.*; +import android.view.*; +import android.widget.*; -import com.cardiomood.android.speedometer.SpeedometerView; +import androidx.fragment.app.*; -import java.util.Locale; +import com.cardiomood.android.controls.gauge.*; + +import java.util.*; + +//import com.cardiomood.android.speedometer.SpeedometerView; public class InfoFragment extends Fragment { - private SpeedometerView mSpeed; + private SpeedometerGauge/*View*/ mSpeed; TextView mCurrentDraw, mTurning, mBatteryLevel, mRunTime; @@ -44,19 +45,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa throw new RuntimeException(); // Customize SpeedometerView - mSpeed = (SpeedometerView) v.findViewById(R.id.speedometer); + mSpeed = (SpeedometerGauge/*View*/) v.findViewById(R.id.speedometer); mCurrentDraw = (TextView) v.findViewById(R.id.current); mTurning = (TextView) v.findViewById(R.id.turning); mBatteryLevel = (TextView) v.findViewById(R.id.battery); mRunTime = (TextView) v.findViewById(R.id.runTime); // Add label converter - mSpeed.setLabelConverter(new SpeedometerView.LabelConverter() { - @Override - public String getLabelFor(double progress, double maxProgress) { - return String.valueOf((int) Math.round(progress)); - } - }); + mSpeed.setLabelConverter((progress, maxProgress) -> String.valueOf((int) Math.round(progress))); // Configure value range and ticks mSpeed.setMaxSpeed(100); diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/MapFragment.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/MapFragment.java index 8409d3b..5cb6062 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/MapFragment.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/MapFragment.java @@ -19,24 +19,18 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.Manifest; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.location.Criteria; -import android.location.Location; -import android.location.LocationManager; -import android.support.annotation.NonNull; -import android.support.v4.app.ActivityCompat; -import android.widget.Toast; +import android.*; +import android.content.*; +import android.content.pm.*; +import android.location.*; +import android.widget.*; -import com.google.android.gms.maps.CameraUpdateFactory; -import com.google.android.gms.maps.GoogleMap; -import com.google.android.gms.maps.OnMapReadyCallback; -import com.google.android.gms.maps.SupportMapFragment; -import com.google.android.gms.maps.model.LatLng; +import androidx.annotation.*; +import androidx.appcompat.app.*; +import androidx.core.app.*; + +import com.google.android.gms.maps.*; +import com.google.android.gms.maps.model.*; public class MapFragment extends SupportMapFragment implements OnMapReadyCallback { private GoogleMap mMap; @@ -52,22 +46,14 @@ public void onResume() { new AlertDialog.Builder(getActivity()) .setMessage("Your GPS seems to be disabled, do you want to enable it?") .setCancelable(false) - .setPositiveButton("Yes", - new DialogInterface.OnClickListener() { - public void onClick(final DialogInterface dialog, final int id) { - startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); - } - }) - .setNegativeButton("No", - new DialogInterface.OnClickListener() { - public void onClick(final DialogInterface dialog, final int id) { - dialog.cancel(); - if (!BalancingRobotFullSizeActivity.isEmulator()) { - Toast.makeText(getActivity(), "GPS must be on in order to use this application!", Toast.LENGTH_LONG).show(); - getActivity().finish(); - } - } - }) + .setPositiveButton("Yes", (dialog, id) -> startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))) + .setNegativeButton("No", (dialog, id) -> { + dialog.cancel(); + if (!BalancingRobotFullSizeActivity.isEmulator()) { + Toast.makeText(getActivity(), "GPS must be on in order to use this application!", Toast.LENGTH_LONG).show(); + getActivity().finish(); + } + }) .create().show(); } else setUpMapIfNeeded(); @@ -109,7 +95,7 @@ private void setUpMapIfNeeded() { } @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case GPS_PERMISSIONS_REQUEST: { // If request is cancelled, the result arrays are empty. diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/PIDFragment.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/PIDFragment.java index 6aedb99..7b43637 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/PIDFragment.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/PIDFragment.java @@ -20,19 +20,14 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.os.Bundle; -import android.os.Handler; -import android.support.v4.app.Fragment; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.SeekBar; -import android.widget.TextView; - -import java.util.Locale; +import android.os.*; +import android.util.*; +import android.view.*; +import android.widget.*; + +import androidx.fragment.app.*; + +import java.util.*; public class PIDFragment extends Fragment { private static final String TAG = "PIDFragment"; @@ -131,79 +126,63 @@ public void onStopTrackingTouch(SeekBar seekBar) { mTurningSeekBar.setProgress(mTurningSeekBar.getMax() / 2); // Call this after the OnSeekBarChangeListener is created mButton = (Button) v.findViewById(R.id.button); - mButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity == null || activity.mChatService == null) { - if (D) - Log.e(TAG, "mChatService == null"); - return; + mButton.setOnClickListener(v111 -> { + BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); + if (activity == null || activity.mChatService == null) { + if (D) + Log.e(TAG, "mChatService == null"); + return; + } + if (activity.mChatService.getState() == BluetoothChatService.STATE_CONNECTED) { + if (mKpSeekBarValue.getText() != null && mKiSeekBarValue.getText() != null && mKdSeekBarValue.getText() != null && (!mKpSeekBarValue.getText().equals(oldKpValue) || !mKiSeekBarValue.getText().equals(oldKiValue) || !mKdSeekBarValue.getText().equals(oldKdValue))) { + oldKpValue = mKpSeekBarValue.getText(); + oldKiValue = mKiSeekBarValue.getText(); + oldKdValue = mKdSeekBarValue.getText(); + mHandler.post(() -> { + BalancingRobotFullSizeActivity activity16 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity16 != null) + activity16.mChatService.mBluetoothProtocol.setPID((int) (Float.parseFloat(mKpSeekBarValue.getText().toString()) * 100.0f), (int) (Float.parseFloat(mKiSeekBarValue.getText().toString()) * 100.0f), (int) (Float.parseFloat(mKdSeekBarValue.getText().toString()) * 100.0f)); + }); // Wait before sending the message + counter += 25; + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity activity15 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity15 != null) activity15.mChatService.mBluetoothProtocol.getPID(); + }, counter); // Wait before sending the message + counter += 25; + } + + if (mTargetAngleSeekBarValue.getText() != null && !mTargetAngleSeekBarValue.getText().equals(oldTargetAngleValue)) { + oldTargetAngleValue = mTargetAngleSeekBarValue.getText(); + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity activity14 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity14 != null) + activity14.mChatService.mBluetoothProtocol.setTarget((int) (Float.parseFloat(mTargetAngleSeekBarValue.getText().toString()) * 100.0f)); // The SeekBar can't handle negative numbers, do this to convert it + }, counter); // Wait before sending the message + counter += 25; + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity activity13 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity13 != null) + activity13.mChatService.mBluetoothProtocol.getTarget(); + }, counter); // Wait before sending the message + counter += 25; } - if (activity.mChatService.getState() == BluetoothChatService.STATE_CONNECTED) { - if (mKpSeekBarValue.getText() != null && mKiSeekBarValue.getText() != null && mKdSeekBarValue.getText() != null && (!mKpSeekBarValue.getText().equals(oldKpValue) || !mKiSeekBarValue.getText().equals(oldKiValue) || !mKdSeekBarValue.getText().equals(oldKdValue))) { - oldKpValue = mKpSeekBarValue.getText(); - oldKiValue = mKiSeekBarValue.getText(); - oldKdValue = mKdSeekBarValue.getText(); - mHandler.post(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.setPID((int) (Float.parseFloat(mKpSeekBarValue.getText().toString()) * 100.0f), (int) (Float.parseFloat(mKiSeekBarValue.getText().toString()) * 100.0f), (int) (Float.parseFloat(mKdSeekBarValue.getText().toString()) * 100.0f)); - } - }); // Wait before sending the message - counter += 25; - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.getPID(); - } - }, counter); // Wait before sending the message - counter += 25; - } - - if (mTargetAngleSeekBarValue.getText() != null && !mTargetAngleSeekBarValue.getText().equals(oldTargetAngleValue)) { - oldTargetAngleValue = mTargetAngleSeekBarValue.getText(); - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.setTarget((int) (Float.parseFloat(mTargetAngleSeekBarValue.getText().toString()) * 100.0f) ); // The SeekBar can't handle negative numbers, do this to convert it - } - }, counter); // Wait before sending the message - counter += 25; - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.getTarget(); - } - }, counter); // Wait before sending the message - counter += 25; - } - - if (mTurningSeekBarValue.getText() != null && !mTurningSeekBarValue.getText().equals(oldTurningValue)) { - oldTurningValue = mTurningSeekBarValue.getText(); - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.setTurning(Byte.parseByte(mTurningSeekBarValue.getText().toString())); - } - }, counter); // Wait before sending the message - counter += 25; - mHandler.postDelayed(new Runnable() { - public void run() { - BalancingRobotFullSizeActivity activity = (BalancingRobotFullSizeActivity) getActivity(); - if (activity != null) - activity.mChatService.mBluetoothProtocol.getTurning(); - } - }, counter); // Wait before sending the message - counter += 25; - } - counter = 0; // Reset counter + + if (mTurningSeekBarValue.getText() != null && !mTurningSeekBarValue.getText().equals(oldTurningValue)) { + oldTurningValue = mTurningSeekBarValue.getText(); + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity activity12 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity12 != null) + activity12.mChatService.mBluetoothProtocol.setTurning(Byte.parseByte(mTurningSeekBarValue.getText().toString())); + }, counter); // Wait before sending the message + counter += 25; + mHandler.postDelayed(() -> { + BalancingRobotFullSizeActivity activity1 = (BalancingRobotFullSizeActivity) getActivity(); + if (activity1 != null) + activity1.mChatService.mBluetoothProtocol.getTurning(); + }, counter); // Wait before sending the message + counter += 25; } + counter = 0; // Reset counter } }); @@ -223,69 +202,39 @@ public void run() { Button mTurningUpArrow = (Button) v.findViewById(R.id.TurningUpArrow); Button mTurningDownArrow = (Button) v.findViewById(R.id.TurningDownArrow); - mKpUpArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKpSeekBar.setProgress(round10(mKpSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 - } + mKpUpArrow.setOnClickListener(v110 -> { + mKpSeekBar.setProgress(round10(mKpSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 }); - mKpDownArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKpSeekBar.setProgress(round10(mKpSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 - } + mKpDownArrow.setOnClickListener(v19 -> { + mKpSeekBar.setProgress(round10(mKpSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 }); - mKiUpArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKiSeekBar.setProgress(round10(mKiSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 - } + mKiUpArrow.setOnClickListener(v18 -> { + mKiSeekBar.setProgress(round10(mKiSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 }); - mKiDownArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKiSeekBar.setProgress(round10(mKiSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 - } + mKiDownArrow.setOnClickListener(v17 -> { + mKiSeekBar.setProgress(round10(mKiSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 }); - mKdUpArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKdSeekBar.setProgress(round10(mKdSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 - } + mKdUpArrow.setOnClickListener(v16 -> { + mKdSeekBar.setProgress(round10(mKdSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 }); - mKdDownArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mKdSeekBar.setProgress(round10(mKdSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 - } + mKdDownArrow.setOnClickListener(v15 -> { + mKdSeekBar.setProgress(round10(mKdSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 }); - mTargetAngleUpArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mTargetAngleSeekBar.setProgress(round10(mTargetAngleSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 - } + mTargetAngleUpArrow.setOnClickListener(v14 -> { + mTargetAngleSeekBar.setProgress(round10(mTargetAngleSeekBar.getProgress() + 10)); // Increase with 0.1 and round to nearest multiple of 10 }); - mTargetAngleDownArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mTargetAngleSeekBar.setProgress(round10(mTargetAngleSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 - } + mTargetAngleDownArrow.setOnClickListener(v13 -> { + mTargetAngleSeekBar.setProgress(round10(mTargetAngleSeekBar.getProgress() - 10)); // Decrease with 0.1 and round to nearest multiple of 10 }); - mTurningUpArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mTurningSeekBar.setProgress(mTurningSeekBar.getProgress() + 1); // Increase with 1 - } + mTurningUpArrow.setOnClickListener(v12 -> { + mTurningSeekBar.setProgress(mTurningSeekBar.getProgress() + 1); // Increase with 1 }); - mTurningDownArrow.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - mTurningSeekBar.setProgress(mTurningSeekBar.getProgress() - 1); // Decrease with 1 - } + mTurningDownArrow.setOnClickListener(v1 -> { + mTurningSeekBar.setProgress(mTurningSeekBar.getProgress() - 1); // Decrease with 1 }); updateButton(); diff --git a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/ViewPagerAdapter.java b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/ViewPagerAdapter.java index 9b34c8c..2809e32 100644 --- a/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/ViewPagerAdapter.java +++ b/app/src/main/java/com/lauszus/balancingrobotfullsizeandroid/app/ViewPagerAdapter.java @@ -19,9 +19,9 @@ package com.lauszus.balancingrobotfullsizeandroid.app; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; public class ViewPagerAdapter extends FragmentPagerAdapter { public static final int INFO_FRAGMENT = 0; diff --git a/app/src/main/res/layout/activity_balancing_robot_full_size.xml b/app/src/main/res/layout/activity_balancing_robot_full_size.xml index f6c3932..f95d137 100644 --- a/app/src/main/res/layout/activity_balancing_robot_full_size.xml +++ b/app/src/main/res/layout/activity_balancing_robot_full_size.xml @@ -7,12 +7,12 @@ android:orientation="vertical" tools:context=".BalancingRobotFullSizeActivity"> - - - - + - diff --git a/app/src/main/res/layout/device_list.xml b/app/src/main/res/layout/device_list.xml index 2c00305..cc930d5 100644 --- a/app/src/main/res/layout/device_list.xml +++ b/app/src/main/res/layout/device_list.xml @@ -7,7 +7,7 @@ android:paddingBottom="@dimen/margin" android:background="@drawable/dialog_background_inset"> - - + - + android:layout_weight="0" android:text="@string/Ki" /> Rmeasure Update + + Replace with your own diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index cbb61c8..0af3734 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -8,8 +8,8 @@