更新陌生人检测Android端app代码

master
Logical 4 years ago
parent 10cd25385a
commit 2036b565f1

@ -113,7 +113,22 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".stranger_detect.NewListener"
android:icon="@mipmap/icon"
android:theme="@style/Theme.AppCompat">
<intent-filter>
<action android:name="com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.NewListener" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".stranger_detect.StrangerDialog"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.FallDialog" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MasterChooser" />
<activity android:name=".NfcLauncherActivity"
android:label="NFC tag launcher">
@ -169,6 +184,7 @@
</intent-filter>
</activity>
<activity
android:name=".fall_dectect.FallDetectedActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">

@ -61,8 +61,8 @@ public class FallDetectedActivity extends AppCompatActivity implements ViewAnima
drawerLayout.closeDrawers();
}
});
setActionBar();
createMenuList();
setActionBar();//设置活动bar
createMenuList();//设置菜单
viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);
Button buttonStartDetect=(Button)findViewById(R.id.StartFallDetect);

@ -0,0 +1,114 @@
package com.github.rosjava.android_remocons.rocon_remocon.stranger_detect;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.github.rosjava.android_remocons.common_tools.apps.RosAppActivity;
import com.github.rosjava.android_remocons.rocon_remocon.R;
import com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.FallDialog;
import com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.subscriber;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMainExecutor;
import java.io.IOException;
/*本类实现了检测陌生人事件的发生*/
public class NewListener extends RosAppActivity
{
private subscriber sc;
boolean alertConfirm;
public NewListener()
{
super("listener", "listener");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
setDefaultMasterName(getString(R.string.default_robot));
setDashboardResource(R.id.top_bar);
setMainWindowResource(R.layout.listener);
super.onCreate(savedInstanceState);
}
@Override
protected void init(NodeMainExecutor nodeMainExecutor)
{
//super.init(nodeMainExecutor);
try {
//建立socket连接将机器人端与安卓端联系在一起实现信息的沟通
Thread.sleep(1000);
java.net.Socket socket = new java.net.Socket(getMasterUri().getHost(), getMasterUri().getPort());
java.net.InetAddress local_network_address = socket.getLocalAddress();
socket.close();
//声明一个新的subscriber对象
sc = new subscriber();
//配置网络
NodeConfiguration nodeConfiguration =
NodeConfiguration.newPublic(local_network_address.getHostAddress(), getMasterUri());
//处理sc的内容
nodeMainExecutor.execute(sc, nodeConfiguration);
Log.i("listener", "I heard msg from ubuntu123456 : \"" + sc.getAlertText() + "\"");
alertConfirm = true;
while(alertConfirm) {
//sc.getAlertText();
Log.i("listener", "I heard msg from ubuntu : \"" + sc.getAlertText() + "\"");
if (sc.getAlertText() != null) {
Log.e("listener", "收到了");
Intent intent = new Intent(NewListener.this,FallDialog.class);
startActivity(intent);
alertConfirm = false;
}
}
} catch(InterruptedException e) {
// Thread interruption
} catch (IOException e) {
// Socket problem
}
}
private DialogInterface.OnClickListener click1=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0,int arg1)
{
android.os.Process.killProcess(android.os.Process.myPid());
}
};
private DialogInterface.OnClickListener click2=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0,int arg1)
{
arg0.cancel();
}
};
//重构父类函数
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0,0,0,R.string.stop_app);
return super.onCreateOptionsMenu(menu);
}
//重构父类函数
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case 0:
finish();
break;
}
return true;
}
}

@ -14,6 +14,8 @@ import android.view.View;
import com.github.rosjava.android_remocons.rocon_remocon.R;
import com.github.rosjava.android_remocons.rocon_remocon.Remocon;
import com.github.rosjava.android_remocons.rocon_remocon.WelcomActivity;
import com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.FallDetectedActivity;
import com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.FallDialog;
public class StrangerDialog extends AppCompatActivity {
/*本类实现检测到陌生人后弹出提示对话框的功能*/
@ -29,4 +31,26 @@ public class StrangerDialog extends AppCompatActivity {
alertdialog1.show();
//setContentView(R.layout.fall_alert);
}
private DialogInterface.OnClickListener click1=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0,int arg1)
{
//响应点击返回陌生人检测的主界面
Intent intent = new Intent(StrangerDialog.this, StrangerlDetectedActivity.class);
startActivity(intent);
//android.os.Process.killProcess(android.os.Process.myPid());
}
};
private DialogInterface.OnClickListener click2=new DialogInterface.OnClickListener()
{
//响应点击返回继续监视的界面
@Override
public void onClick(DialogInterface arg0,int arg1)
{
Intent intent = new Intent(StrangerDialog.this, NewListener.class);
startActivity(intent);
arg0.cancel();
}
};
}

@ -0,0 +1,245 @@
package com.github.rosjava.android_remocons.rocon_remocon.stranger_detect;
/**
* Created by lcp on 2021/4/19.
*/
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import com.github.rosjava.android_remocons.rocon_remocon.R;
import com.github.rosjava.android_remocons.rocon_remocon.fall_dectect.fragment.ContentFragment;
import java.util.ArrayList;
import java.util.List;
import io.codetail.animation.SupportAnimator;
import io.codetail.animation.ViewAnimationUtils;
import yalantis.com.sidemenu.interfaces.Resourceble;
import yalantis.com.sidemenu.interfaces.ScreenShotable;
import yalantis.com.sidemenu.model.SlideMenuItem;
import yalantis.com.sidemenu.util.ViewAnimator;
public class StrangerlDetectedActivity extends AppCompatActivity implements ViewAnimator.ViewAnimatorListener {
private DrawerLayout drawerLayout; //抽屉布局
private ActionBarDrawerToggle drawerToggle;
private List<SlideMenuItem> list = new ArrayList<>();
private ContentFragment contentFragment;
private ViewAnimator viewAnimator;
private int res = R.drawable.fall_pic2;//设置图片
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stranger_detect);//启动摔倒检测的界面
contentFragment = ContentFragment.newInstance(R.drawable.fall_pic2);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, contentFragment)
.commit();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setScrimColor(Color.TRANSPARENT);
linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
}
});
setActionBar();
createMenuList();
viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);
Button buttonStartDetect=(Button)findViewById(R.id.StartFallDetect);
buttonStartDetect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alertdialogbuilder=new AlertDialog.Builder(StrangerlDetectedActivity.this);
alertdialogbuilder.setMessage("您确定开启摔倒检测吗?");
alertdialogbuilder.setPositiveButton("我确定", click1);
alertdialogbuilder.setNegativeButton("再考虑一下", click2);
AlertDialog alertdialog1=alertdialogbuilder.create();
alertdialog1.show();
}
});
Button buttonStopDetect=(Button)findViewById(R.id.CloseFallDetect);
buttonStopDetect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alertdialogbuilder=new AlertDialog.Builder(StrangerlDetectedActivity.this);
alertdialogbuilder.setMessage("您确定关闭摔倒检测吗?");
alertdialogbuilder.setPositiveButton("我确定", click2);
alertdialogbuilder.setNegativeButton("再考虑一下", click2);
AlertDialog alertdialog1=alertdialogbuilder.create();
alertdialog1.show();
}
});
}
private DialogInterface.OnClickListener click1=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0,int arg1)
{
Intent intent = new Intent(StrangerlDetectedActivity.this, NewListener.class);
startActivity(intent);
}
};
private DialogInterface.OnClickListener click2=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0,int arg1)
{
arg0.cancel();
}
};
private void createMenuList() {//设置侧边菜单栏的布局和图片
SlideMenuItem menuItem0 = new SlideMenuItem(ContentFragment.CLOSE, R.drawable.icn_close);
list.add(menuItem0);
SlideMenuItem menuItem = new SlideMenuItem(ContentFragment.BUILDING, R.drawable.icn_1);
list.add(menuItem);
SlideMenuItem menuItem2 = new SlideMenuItem(ContentFragment.BOOK, R.drawable.icn_2);
list.add(menuItem2);
SlideMenuItem menuItem3 = new SlideMenuItem(ContentFragment.PAINT, R.drawable.icn_3);
list.add(menuItem3);
SlideMenuItem menuItem4 = new SlideMenuItem(ContentFragment.CASE, R.drawable.icn_4);
list.add(menuItem4);
SlideMenuItem menuItem5 = new SlideMenuItem(ContentFragment.SHOP, R.drawable.icn_5);
list.add(menuItem5);
SlideMenuItem menuItem6 = new SlideMenuItem(ContentFragment.PARTY, R.drawable.icn_6);
list.add(menuItem6);
SlideMenuItem menuItem7 = new SlideMenuItem(ContentFragment.MOVIE, R.drawable.icn_7);
list.add(menuItem7);
}
private void setActionBar() {//设置活动Bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
toolbar, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
linearLayout.removeAllViews();
linearLayout.invalidate();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset > 0.6 && linearLayout.getChildCount() == 0)
viewAnimator.showMenuContent();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
//配置改变的响应
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
//选择设置的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//菜单选择操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
// wyj
case android.R.id.home:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private ScreenShotable replaceFragment(ScreenShotable screenShotable, int topPosition) {
this.res = this.res == R.drawable.fall_pic2 ? R.drawable.fall_pic: R.drawable.fall_pic2;
View view = findViewById(R.id.content_frame);
int finalRadius = Math.max(view.getWidth(), view.getHeight());
SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius);
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);
findViewById(R.id.content_overlay).setBackgroundDrawable(new BitmapDrawable(getResources(), screenShotable.getBitmap()));
animator.start();
ContentFragment contentFragment = ContentFragment.newInstance(this.res);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, contentFragment).commit();
return contentFragment;
}
@Override
public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) {
switch (slideMenuItem.getName()) {
case ContentFragment.CLOSE:
return screenShotable;
default:
return replaceFragment(screenShotable, position);
}
}
@Override
public void disableHomeButton() {
getSupportActionBar().setHomeButtonEnabled(true);
}
@Override
public void enableHomeButton() {
getSupportActionBar().setHomeButtonEnabled(false);
drawerLayout.closeDrawers();
}
@Override
public void addViewToContainer(View view) {
linearLayout.addView(view);
}
}

@ -0,0 +1,135 @@
package com.github.rosjava.android_remocons.rocon_remocon.stranger_detect;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.github.rosjava.android_remocons.common_tools.apps.RosAppActivity;
import com.github.rosjava.android_remocons.rocon_remocon.R;
import org.ros.android.view.RosTextView;
import org.ros.node.ConnectedNode;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMainExecutor;
import java.io.IOException;
import std_msgs.String;
public class listener extends RosAppActivity
{
private Toast lastToast;
private ConnectedNode node;
private RosTextView<String> rosTextView;
//private subscriber sb;
public listener()
{
super("listener", "listener");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
setDefaultMasterName(getString(R.string.default_robot));
setDashboardResource(R.id.top_bar);
setMainWindowResource(R.layout.listener);
super.onCreate(savedInstanceState);
}
@Override
protected void init(NodeMainExecutor nodeMainExecutor)
{
//String chatterTopic = remaps.get(getString(R.string.chatter_topic));
super.init(nodeMainExecutor);
/*rosTextView = (RosTextView<std_msgs.String>) findViewById(R.id.text);
rosTextView.setTopicName(getMasterNameSpace().resolve(chatterTopic).toString());
rosTextView.setMessageType(std_msgs.String._TYPE);
rosTextView.setMessageToStringCallable(new MessageCallable<String, std_msgs.String>() {
@Override
public java.lang.String call(std_msgs.String message) {
Log.e("Listener", "received a message [" + message.getData() + "]");
return message.getData();
}
});*/
try {
// Really horrible hack till I work out exactly the root cause and fix for
// https://github.com/rosjava/android_remocons/issues/47
Thread.sleep(1000);
java.net.Socket socket = new java.net.Socket(getMasterUri().getHost(), getMasterUri().getPort());
java.net.InetAddress local_network_address = socket.getLocalAddress();
socket.close();
Log.i("123", "I heard msg from ubuntu : \" \"");
// sb = new subscriber();
NodeConfiguration nodeConfiguration =
NodeConfiguration.newPublic(local_network_address.getHostAddress(), getMasterUri());
//nodeMainExecutor.execute(sb, nodeConfiguration);
/*boolean flag = true;
while(flag){
if(sb.getAlertText()==null) {
/*new AlertDialog.Builder(listener.this)
.setTitle("确认")
.setMessage("确定吗?")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.show();
flag = false;
Intent intent = new Intent(listener.this, NewListener.class);
startActivity(intent);
}
}*/
} catch(InterruptedException e) {
// Thread interruption
} catch (IOException e) {
// Socket problem
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0,0,0,R.string.stop_app);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case 0:
finish();
break;
}
return true;
}
// /**
// * Call Toast on UI thread.
// * @param message Message to show on toast.
// */
// private void showToast(final String message)
// {
// runOnUiThread(new Runnable()
// {
// @Override
// public void run() {
// if (lastToast != null)
// lastToast.cancel();
//
// lastToast = Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG);
// lastToast.show();
// }
// });
// }
}

@ -0,0 +1,72 @@
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.codetail.widget.RevealFrameLayout
android:id="@+id/container_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/content_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/CloseFallDetect"
android:layout_width="205dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:background="@color/cardview_light_background"
android:text="关闭陌生人检测" />
<Button
android:id="@+id/StartFallDetect"
android:layout_width="205dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:background="@color/cardview_light_background"
android:text="开启陌生人检测" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</io.codetail.widget.RevealFrameLayout>
<ScrollView
android:id="@+id/scrollView"
android:scrollbarThumbVertical="@android:color/transparent"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom">
<LinearLayout
android:id="@+id/left_drawer"
android:orientation="vertical"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:background="@android:color/transparent">
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>

@ -338,19 +338,19 @@ int main(int argc, char **argv) {
printf("the v is %f\n",fall_v);
if(fall_v > 200){
printf("the user fall!\n");
ros::Rate loop_rate(10);
ros::Rate loop_rate(10);
ros::Publisher pub = nh.advertise<std_msgs::String>("chatter", 1000);
while(ros::ok() && count < 4){
std_msgs::String msg;
std::stringstream ss;
ss << "用户摔倒了! ";
msg.data = ss.str();
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
count ++;
}
}
while(ros::ok() && count < 4){
std_msgs::String msg;
std::stringstream ss;
ss << "用户摔倒了! ";
msg.data = ss.str();
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
count ++;
}
}
}
}

Loading…
Cancel
Save