From 3df0484e6a38e4db2586eccec595a7664c1f55b6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 20 Jan 2013 16:07:38 -0800 Subject: [PATCH] get maintenance mode message into an alert -- and create a new common superclass for Service subclasses to reuse some code. --- .../org/eehouse/android/xw4/BTService.java | 26 +------- .../org/eehouse/android/xw4/DlgDelegate.java | 5 ++ .../org/eehouse/android/xw4/MultiService.java | 2 + .../org/eehouse/android/xw4/RelayService.java | 13 +--- .../org/eehouse/android/xw4/SMSService.java | 27 +-------- .../org/eehouse/android/xw4/XWActivity.java | 6 +- .../android/xw4/XWExpandableListActivity.java | 16 +++++ .../eehouse/android/xw4/XWListActivity.java | 6 +- .../org/eehouse/android/xw4/XWService.java | 60 +++++++++++++++++++ 9 files changed, 92 insertions(+), 69 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/XWService.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java index 4d6f8fd6d..f73959b5b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java @@ -29,7 +29,6 @@ import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.os.IBinder; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.OutputStream; @@ -49,7 +48,7 @@ import junit.framework.Assert; import org.eehouse.android.xw4.MultiService.MultiEvent; import org.eehouse.android.xw4.jni.CommsAddrRec; -public class BTService extends Service { +public class BTService extends XWService { private static final long RESEND_TIMEOUT = 5; // seconds private static final int MAX_SEND_FAIL = 3; @@ -77,8 +76,6 @@ public class BTService extends Service { private static final String NTO_STR = "TOT"; private static final String NHE_STR = "HER"; - private static MultiService s_srcMgr = null; - private enum BTCmd { PING, PONG, @@ -147,16 +144,6 @@ public class BTService extends Service { } } - public static void setListener( MultiService.MultiEventListener li ) - { - if ( XWApp.BTSUPPORTED ) { - if ( null == s_srcMgr ) { - s_srcMgr = new MultiService(); - } - s_srcMgr.setListener( li ); - } - } - public static void radioChanged( Context context, boolean cameOn ) { Intent intent = getIntentTo( context, RADIO ); @@ -316,12 +303,6 @@ public class BTService extends Service { return result; } - @Override - public IBinder onBind( Intent intent ) - { - return null; - } - private class BTListenerThread extends Thread { private BluetoothServerSocket m_serverSocket; @@ -852,11 +833,6 @@ public class BTService extends Service { sendResult( MultiEvent.SCAN_DONE, (Object)(names()) ); } - private void sendResult( MultiEvent event, Object ... args ) - { - s_srcMgr.sendResult( event, args ); - } - private void listLocalBTGames( boolean force ) { if ( null == s_devGames ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index 3c3dfa467..a7152518f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -272,6 +272,11 @@ public class DlgDelegate { (String)args[0] ); asToast = false; break; + case RELAY_ALERT: + msg = (String)args[0]; + asToast = false; + break; + default: DbgUtils.logf( "eventOccurred: unhandled event %s", event.toString() ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java index 5d2a776eb..17c123d73 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java @@ -62,6 +62,8 @@ public class MultiService { , SMS_SEND_OK , SMS_SEND_FAILED , SMS_SEND_FAILED_NORADIO + + , RELAY_ALERT }; public interface MultiEventListener { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index b5ed85fcc..2e92d7c9d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -23,7 +23,6 @@ package org.eehouse.android.xw4; import android.app.Service; import android.content.Context; import android.content.Intent; -import android.os.IBinder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; @@ -41,8 +40,9 @@ import junit.framework.Assert; import org.eehouse.android.xw4.jni.GameSummary; import org.eehouse.android.xw4.jni.UtilCtxt; +import org.eehouse.android.xw4.MultiService.MultiEvent; -public class RelayService extends Service { +public class RelayService extends XWService { private static final int MAX_SEND = 1024; private static final int MAX_BUF = MAX_SEND - 2; @@ -86,12 +86,6 @@ public class RelayService extends Service { startFetchThreadIf(); } - @Override - public IBinder onBind( Intent intent ) - { - return null; - } - @Override public int onStartCommand( Intent intent, int flags, int startId ) { @@ -276,8 +270,7 @@ public class RelayService extends Service { short len = dis.readShort(); byte[] tmp = new byte[len]; dis.read( tmp ); - String msg = new String( tmp ); - DbgUtils.logf( "got message: %s", msg ); + sendResult( MultiEvent.RELAY_ALERT, new String( tmp ) ); break; default: DbgUtils.logf( "RelayService: Unhandled cmd: %d", cmd ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java index 1a78e9f47..1c8671843 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java @@ -32,7 +32,6 @@ import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; -import android.os.IBinder; import android.preference.PreferenceManager; import android.telephony.SmsManager; import android.telephony.SmsMessage; @@ -52,7 +51,7 @@ import org.eehouse.android.xw4.jni.CommsAddrRec; import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; import org.eehouse.android.xw4.jni.XwJNI; -public class SMSService extends Service { +public class SMSService extends XWService { private static final String INSTALL_URL = "http://eehouse.org/_/a.py/a "; private static final int MAX_SMS_LEN = 140; // ??? differs by network @@ -76,7 +75,6 @@ public class SMSService extends Service { private static final String PHONE = "PHONE"; private static Boolean s_showToasts = null; - private static MultiService s_srcMgr = null; // All messages are base64-encoded byte arrays. The first byte is // always one of these. What follows depends. @@ -189,16 +187,6 @@ public class SMSService extends Service { return result; } - public static void setListener( MultiService.MultiEventListener li ) - { - if ( XWApp.SMSSUPPORTED ) { - if ( null == s_srcMgr ) { - s_srcMgr = new MultiService(); - } - s_srcMgr.setListener( li ); - } - } - private static Intent getIntentTo( Context context, int cmd ) { if ( null == s_showToasts ) { @@ -289,12 +277,6 @@ public class SMSService extends Service { return result; } // onStartCommand - @Override - public IBinder onBind( Intent intent ) - { - return null; - } - private void inviteRemote( String phone, int gameID, String gameName, int lang, String dict, int nPlayersT, int nPlayersH ) @@ -647,13 +629,6 @@ public class SMSService extends Service { } } - private void sendResult( MultiEvent event, Object ... args ) - { - if ( null != s_srcMgr ) { - s_srcMgr.sendResult( event, args ); - } - } - private void registerReceivers() { registerReceiver( new BroadcastReceiver() { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java index 5b0141838..e6da5a830 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java @@ -54,8 +54,7 @@ public class XWActivity extends Activity protected void onResume() { DbgUtils.logf( "%s.onResume(this=%H)", getClass().getName(), this ); - BTService.setListener( this ); - SMSService.setListener( this ); + XWService.setListener( this ); super.onResume(); } @@ -63,8 +62,7 @@ public class XWActivity extends Activity protected void onPause() { DbgUtils.logf( "%s.onPause(this=%H)", getClass().getName(), this ); - BTService.setListener( null ); - SMSService.setListener( null ); + XWService.setListener( null ); super.onPause(); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWExpandableListActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWExpandableListActivity.java index f33ce61cc..f047589bd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWExpandableListActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWExpandableListActivity.java @@ -41,6 +41,22 @@ public class XWExpandableListActivity extends ExpandableListActivity m_delegate = new DlgDelegate( this, this, savedInstanceState ); } + @Override + protected void onResume() + { + DbgUtils.logf( "%s.onResume(this=%H)", getClass().getName(), this ); + XWService.setListener( this ); + super.onResume(); + } + + @Override + protected void onPause() + { + DbgUtils.logf( "%s.onPause(this=%H)", getClass().getName(), this ); + XWService.setListener( null ); + super.onPause(); + } + @Override protected void onSaveInstanceState( Bundle outState ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java index 3a042fe68..fcd96af06 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java @@ -51,8 +51,7 @@ public class XWListActivity extends ListActivity protected void onResume() { DbgUtils.logf( "%s.onResume(this=%H)", getClass().getName(), this ); - BTService.setListener( this ); - SMSService.setListener( this ); + XWService.setListener( this ); super.onResume(); } @@ -60,8 +59,7 @@ public class XWListActivity extends ListActivity protected void onPause() { DbgUtils.logf( "%s.onPause(this=%H)", getClass().getName(), this ); - BTService.setListener( null ); - SMSService.setListener( null ); + XWService.setListener( null ); super.onPause(); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWService.java new file mode 100644 index 000000000..2f93a94e7 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWService.java @@ -0,0 +1,60 @@ +/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */ +/* + * Copyright 2010 - 2012 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.Service; +import android.content.Intent; +import android.os.IBinder; + +import org.eehouse.android.xw4.MultiService.MultiEvent; + + +public class XWService extends Service { + + protected static MultiService s_srcMgr = null; + + @Override + public IBinder onBind( Intent intent ) + { + return null; + } + + public final static void setListener( MultiService.MultiEventListener li ) + { + if ( null == s_srcMgr ) { + DbgUtils.logf( "setListener: registering %s", li.getClass().getName() ); + s_srcMgr = new MultiService(); + } + s_srcMgr.setListener( li ); + } + + protected void sendResult( MultiEvent event, Object ... args ) + { + if ( null != s_srcMgr ) { + s_srcMgr.sendResult( event, args ); + } else { + DbgUtils.logf( "sendResult: dropping event" ); + } + } + + + +}