mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-01 19:57:11 +01:00
implement peers-info menuitem (Debug only)
This commit is contained in:
parent
6781f1e69f
commit
fac2eca5b2
5 changed files with 170 additions and 2 deletions
|
@ -417,6 +417,20 @@ public class BoardDelegate extends DelegateBase
|
|||
.create();
|
||||
break;
|
||||
|
||||
case MQTT_PEERS: {
|
||||
final PeerStatusView psv = (PeerStatusView)
|
||||
inflate( R.layout.peers_status );
|
||||
CommsAddrRec selfAddr = XwJNI.comms_getSelfAddr( m_jniGamePtr );
|
||||
|
||||
psv.configure( m_gi.gameID, selfAddr.mqtt_devID );
|
||||
dialog = ab
|
||||
.setTitle( R.string.menu_about_peers )
|
||||
.setView( psv )
|
||||
.setPositiveButton( android.R.string.ok, null )
|
||||
.create();
|
||||
}
|
||||
break;
|
||||
|
||||
case ASK_DUP_PAUSE: {
|
||||
final boolean isPause = (Boolean)params[0];
|
||||
final ConfirmPauseView pauseView =
|
||||
|
@ -1505,13 +1519,14 @@ public class BoardDelegate extends DelegateBase
|
|||
@Override
|
||||
public void onStatusClicked()
|
||||
{
|
||||
if ( BuildConfig.NON_RELEASE ) {
|
||||
if ( BuildConfig.NON_RELEASE || XWPrefs.getDebugEnabled( m_activity ) ) {
|
||||
View view = findViewById( R.id.netstatus_view );
|
||||
PopupMenu popup = new PopupMenu( m_activity, view );
|
||||
popup.getMenuInflater().inflate( R.menu.netstat, popup.getMenu() );
|
||||
|
||||
if ( ! m_connTypes.contains(CommsConnType.COMMS_CONN_MQTT) ) {
|
||||
popup.getMenu().removeItem( R.id.netstat_menu_traffic );
|
||||
popup.getMenu().removeItem( R.id.netstat_peers );
|
||||
}
|
||||
|
||||
popup.setOnMenuItemClickListener( new PopupMenu
|
||||
|
@ -1530,7 +1545,7 @@ public class BoardDelegate extends DelegateBase
|
|||
NetUtils.gameURLToClip( m_activity, m_gi.gameID );
|
||||
break;
|
||||
case R.id.netstat_peers:
|
||||
Utils.notImpl( m_activity );
|
||||
showDialogFragment( DlgID.MQTT_PEERS );
|
||||
break;
|
||||
default:
|
||||
handled = false;
|
||||
|
|
|
@ -72,6 +72,7 @@ public enum DlgID {
|
|||
BACKUP_LOADSTORE,
|
||||
GET_DEVID,
|
||||
SET_MQTTID,
|
||||
MQTT_PEERS,
|
||||
;
|
||||
|
||||
private boolean m_addToStack;
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
/* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */
|
||||
/*
|
||||
* Copyright 2024 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
package org.eehouse.android.xw4;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.eehouse.android.xw4.jni.CommsAddrRec;
|
||||
import org.eehouse.android.xw4.jni.XwJNI;
|
||||
import org.eehouse.android.xw4.loc.LocUtils;
|
||||
|
||||
public class PeerStatusView extends LinearLayout {
|
||||
private static final String TAG = PeerStatusView.class.getSimpleName();
|
||||
|
||||
private Context mContext;
|
||||
private boolean mFinished;
|
||||
private int mGameID;
|
||||
private String mDevID;
|
||||
|
||||
public PeerStatusView( Context cx, AttributeSet as )
|
||||
{
|
||||
super( cx, as );
|
||||
mContext = cx;
|
||||
}
|
||||
|
||||
public void configure( int gameID, String devID )
|
||||
{
|
||||
mGameID = gameID;
|
||||
mDevID = devID;
|
||||
startThreadOnce();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFinishInflate()
|
||||
{
|
||||
mFinished = true;
|
||||
startThreadOnce();
|
||||
}
|
||||
|
||||
private void startThreadOnce()
|
||||
{
|
||||
if ( mFinished && null != mDevID ) {
|
||||
new Thread( new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetchAndDisplay();
|
||||
}
|
||||
} ).start();
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchAndDisplay()
|
||||
{
|
||||
String userStr = null;
|
||||
JSONObject params = new JSONObject();
|
||||
try {
|
||||
params.put( "gid16", String.format("%X", mGameID) );
|
||||
params.put( "devid", mDevID );
|
||||
|
||||
HttpURLConnection conn = NetUtils
|
||||
.makeHttpMQTTConn( mContext, "peers" );
|
||||
final String resStr = NetUtils.runConn( conn, params, true );
|
||||
Log.d( TAG, "runConn(ack) => %s", resStr );
|
||||
|
||||
JSONObject result = new JSONObject( resStr );
|
||||
JSONArray results = result.optJSONArray( "results" );
|
||||
if ( null != results ) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
for ( int ii = 0; ii < results.length(); ++ii ) {
|
||||
JSONObject line = results.getJSONObject(ii);
|
||||
String mqttID = line.getString("devid");
|
||||
String age = line.getString( "age" );
|
||||
String name = XwJNI.kplr_nameForMqttDev( mqttID );
|
||||
if ( null == name ) {
|
||||
name = mqttID;
|
||||
}
|
||||
lines.add( String.format( "%s: %s", name, age ) );
|
||||
}
|
||||
userStr = TextUtils.join( "\n", lines );
|
||||
}
|
||||
} catch ( JSONException je ) {
|
||||
Log.ex( TAG, je );
|
||||
} catch ( NullPointerException npe ) {
|
||||
Log.ex( TAG, npe );
|
||||
}
|
||||
|
||||
Activity activity = DelegateBase.getHasLooper();
|
||||
if ( null != activity ) {
|
||||
final String finalUserStr = userStr != null
|
||||
? userStr : LocUtils.getString(mContext, R.string.no_peers_info);
|
||||
activity.runOnUiThread( new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
TextView tv = (TextView)findViewById( R.id.status );
|
||||
tv.setText( finalUserStr );
|
||||
}
|
||||
} );
|
||||
} else {
|
||||
Log.d( TAG, "no activity found" );
|
||||
}
|
||||
}
|
||||
}
|
18
xwords4/android/app/src/main/res/layout/peers_status.xml
Normal file
18
xwords4/android/app/src/main/res/layout/peers_status.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<org.eehouse.android.xw4.PeerStatusView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="8dp"
|
||||
>
|
||||
|
||||
<TextView android:id="@+id/status"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:text="@string/peers_info_pending"
|
||||
/>
|
||||
|
||||
</org.eehouse.android.xw4.PeerStatusView>
|
|
@ -5,4 +5,7 @@
|
|||
|
||||
<string name="dup_allscores_fmt">All scores: %1$s</string>
|
||||
|
||||
<string name="peers_info_pending">Querying server…</string>
|
||||
<string name="no_peers_info">Peers information not available</string>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Add table
Reference in a new issue