Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

デモ用にダンス開始のトリガーのボタンを追加した

  • Loading branch information...
commit 15f571fb53928852108164b116b2aa7006f47a8c 1 parent fea59fa
@cattaka authored
View
6 DroidRobo01/res/layout/monitor_twitter.xml
@@ -3,6 +3,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
+ <Button
+ android:id="@+id/DanceButton"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Dance"
+ />
<ListView
android:id="@+id/TweetListView"
android:layout_width="match_parent"
View
97 DroidRobo01/src/net/cattaka/droidrobo01/MonitorTwitterActivity.java
@@ -1,3 +1,4 @@
+
package net.cattaka.droidrobo01;
import java.text.DateFormat;
@@ -10,7 +11,6 @@
import net.cattaka.droidrobo01.robo.RoboPauseInfo;
import net.cattaka.droidrobo01.service.AdkService;
import net.cattaka.droidrobo01.service.IAdkService;
-
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
@@ -31,23 +31,31 @@
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;
+import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
-public class MonitorTwitterActivity extends Activity {
+public class MonitorTwitterActivity extends Activity implements View.OnClickListener {
private static final int EVENT_ADD_MESSAGE = 1;
+
private static final int EVENT_DRIVE = 2;
-
+
private MonitorTwitterActivity me = this;
+
private IAdkService mAdkService;
+
private ListView mTweetListView;
+
private TwitterStream mTwitterStream;
+
private ArmSetting mSetting;
+
private Queue<RoboPauseInfo> mPauseQueue = new LinkedList<RoboPauseInfo>();
-
- private RoboPauseInfo ROBO_PAUSE_STOP = new RoboPauseInfo(false, MotorState.NONE, 500, 0.25f, 0.25f);
-
+
+ private RoboPauseInfo ROBO_PAUSE_STOP = new RoboPauseInfo(false, MotorState.NONE, 500, 0.25f,
+ 0.25f);
+
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
@@ -71,7 +79,7 @@ public void handleMessage(android.os.Message msg) {
if (msg.what == EVENT_ADD_MESSAGE) {
if (mTweetListView.getAdapter() instanceof ArrayAdapter<?>) {
if (msg.obj instanceof String) {
- addMessage((String) msg.obj);
+ addMessage((String)msg.obj);
}
}
} else if (msg.what == EVENT_DRIVE) {
@@ -79,16 +87,18 @@ public void handleMessage(android.os.Message msg) {
}
};
};
-
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.monitor_twitter);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);;
-
- mTweetListView = (ListView) findViewById(R.id.TweetListView);
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+
+ findViewById(R.id.DanceButton).setOnClickListener(this);
+
+ mTweetListView = (ListView)findViewById(R.id.TweetListView);
}
-
+
@Override
protected void onResume() {
super.onResume();
@@ -102,29 +112,37 @@ protected void onResume() {
mSetting = new ArmSetting();
mSetting.loadPreference(pref);
}
-
+
startMonitor();
startDriveRobo();
}
-
+
@Override
protected void onPause() {
super.onPause();
stopMonitor();
stopDriveRobo();
}
-
+
+ @Override
+ public void onClick(View v) {
+ if (v.getId() == R.id.DanceButton) {
+ addMessageAsync(null);
+ }
+ }
+
private void startMonitor() {
stopMonitor();
-
+
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthAccessToken(mSetting.getAccessToken());
builder.setOAuthAccessTokenSecret(mSetting.getAccessTokenSecret());
builder.setOAuthConsumerKey(Constants.getTwConsumerKey(this));
builder.setOAuthConsumerSecret(Constants.getTwConsumerSecret(this));
mTwitterStream = new TwitterStreamFactory(builder.build()).getInstance();
-
- final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
+
+ final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
+ android.R.layout.simple_list_item_1);
mTweetListView.setAdapter(adapter);
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
@@ -140,7 +158,8 @@ public void onStatus(Status status) {
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
- addMessageAsync("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
+ addMessageAsync("Got a status deletion notice id:"
+ + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
@@ -148,7 +167,8 @@ public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
}
public void onScrubGeo(long userId, long upToStatusId) {
- addMessageAsync("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
+ addMessageAsync("Got scrub_geo event userId:" + userId + " upToStatusId:"
+ + upToStatusId);
}
public void onException(Exception ex) {
@@ -156,7 +176,7 @@ public void onException(Exception ex) {
}
};
mTwitterStream.addListener(listener);
-
+
List<Long> follow = new ArrayList<Long>();
List<String> track = new ArrayList<String>();
{
@@ -169,7 +189,7 @@ public void onException(Exception ex) {
track.add(str);
}
}
-
+
long[] followArray = new long[follow.size()];
for (int i = 0; i < follow.size(); i++) {
followArray[i] = follow.get(i);
@@ -177,32 +197,36 @@ public void onException(Exception ex) {
String[] trackArray = track.toArray(new String[track.size()]);
mTwitterStream.filter(new FilterQuery(0, followArray, trackArray));
-
- //mTwitterStream.sample();
+
+ // mTwitterStream.sample();
}
+
private void addMessage(String str) {
@SuppressWarnings("unchecked")
- ArrayAdapter<String> adapter = (ArrayAdapter<String> )mTweetListView.getAdapter();
+ ArrayAdapter<String> adapter = (ArrayAdapter<String>)mTweetListView.getAdapter();
adapter.insert(str, 0);
if (adapter.getCount() >= 20) {
while (adapter.getCount() > 10) {
adapter.remove(adapter.getItem(10));
}
}
-
+
adapter.notifyDataSetChanged();
}
+
private void addMessageAsync(String str) {
- Message msg = new Message();
- msg.what = EVENT_ADD_MESSAGE;
- msg.obj = str;
- mHandler.sendMessage(msg);
+ if (str != null) {
+ Message msg = new Message();
+ msg.what = EVENT_ADD_MESSAGE;
+ msg.obj = str;
+ mHandler.sendMessage(msg);
+ }
if (mPauseQueue.size() == 0) {
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 500, 0.25f, 0.25f));
mPauseQueue.add(new RoboPauseInfo(false, MotorState.NONE, 500, 0.25f, 0.25f));
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 500, 0.25f, 0.25f));
mPauseQueue.add(new RoboPauseInfo(false, MotorState.NONE, 500, 0.25f, 0.25f));
-
+
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 1000, 0.75f, 0.75f));
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 500, 1.00f, 0.50f));
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 500, 0.50f, 1.00f));
@@ -214,19 +238,19 @@ private void addMessageAsync(String str) {
mPauseQueue.add(new RoboPauseInfo(true, MotorState.TURN_LEFT, 200, 0.75f, 0.75f));
mPauseQueue.add(new RoboPauseInfo(true, MotorState.TURN_RIGHT, 200, 0.75f, 0.75f));
-
+
mPauseQueue.add(new RoboPauseInfo(true, MotorState.NONE, 1000, 1.0f, 1.0f));
-
+
}
}
-
+
private void stopMonitor() {
if (mTwitterStream != null) {
mTwitterStream.shutdown();
mTwitterStream = null;
}
}
-
+
private void startDriveRobo() {
RoboPauseInfo rpInfo = mPauseQueue.poll();
if (rpInfo == null) {
@@ -238,12 +262,13 @@ private void startDriveRobo() {
mAdkService.sendCommand((byte)0x03, (byte)(0x0), rpInfo.toUpperBytes());
mAdkService.sendCommand((byte)0x03, (byte)(0x5), rpInfo.toLowerBytes());
} catch (RemoteException e) {
- Log.e("Debug", e.getMessage(),e);
+ Log.e("Debug", e.getMessage(), e);
}
}
}
mHandler.sendEmptyMessageDelayed(EVENT_DRIVE, rpInfo.getDucation());
}
+
private void stopDriveRobo() {
mHandler.removeMessages(EVENT_DRIVE);
}
Please sign in to comment.
Something went wrong with that request. Please try again.